Alipay.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\utils\Alipay;
  8. use app\models\Goods;
  9. use app\models\Option;
  10. use app\constants\OptionSetting;
  11. use app\models\OrderDetail;
  12. use app\models\OrderUnion;
  13. use app\models\Store;
  14. use app\models\User;
  15. use app\utils\CurlHelper;
  16. use app\utils\OrderNo;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Yansongda\Pay\Exceptions\GatewayException;
  19. use Yansongda\Pay\Exceptions\InvalidConfigException;
  20. use Yansongda\Pay\Exceptions\InvalidSignException;
  21. use Yansongda\Pay\Pay;
  22. use Yansongda\Supports\Collection;
  23. use yii\base\BaseObject;
  24. use yii\helpers\Json;
  25. /**
  26. * Class Alipay 证书公钥模式
  27. * 支付宝支付相关操作
  28. * @package app\utils
  29. */
  30. class Alipay
  31. {
  32. const FLAG_REFUND = 'refund';
  33. const FLAG_NORMAL = 'wap';
  34. const FLAG_TRANSFER = 'transfer';
  35. private static $validFlag = [
  36. self::FLAG_NORMAL,
  37. self::FLAG_REFUND,
  38. self::FLAG_TRANSFER
  39. ];
  40. /**
  41. * @var \Yansongda\Pay\Gateways\Alipay
  42. */
  43. public static $alipay;
  44. public static $alipay_conf;
  45. public $mini_id;
  46. public $store_id;
  47. /**
  48. * @var string
  49. * 回调url
  50. */
  51. private static $notify_url = 'alipay/notify';
  52. public static $h5_url = 'order/order/order';
  53. /**
  54. * @param $return_url
  55. * @param $level
  56. * 获取支付宝实例
  57. */
  58. private function __construct($return_url = 'www.baidu.com', $type = 0, $store_id = 0)
  59. {
  60. $this->mini_id = get_mini_id();
  61. $this->store_id = $store_id = $store_id ?: get_store_id();
  62. if (\Yii::$app->prod_is_dandianpu() && !Store::hasIncoming($store_id) && self_mini() === false) {
  63. $alipay_config = Json::decode(Option::get('one_store_alipay_config', 0, 'saas', '')['value']);
  64. } else {
  65. $config_cache = \Yii::$app->cache->get('alipay_config_cache_' . $store_id);
  66. if ($config_cache) {
  67. $alipay_config = Json::decode($config_cache);
  68. } else {
  69. $alipay_config = Json::decode(Option::get(Option::OPTOPN_KEY, $store_id, 'alipay')['value']);
  70. }
  71. }
  72. $config = [
  73. 'app_id' => $alipay_config['app_id'],
  74. 'notify_url' => pay_notify_url(self::$notify_url),
  75. 'return_url' => $return_url,
  76. 'private_key' => $alipay_config['app_private_key'],
  77. 'log' => [ // optional
  78. 'file' => \Yii::$app->basePath . '/runtime/logs/alipay.log',
  79. 'level' => 'debug', // 建议生产环境等级调整为 info,开发环境为 debug
  80. 'type' => 'single', // optional, 可选 daily.
  81. 'max_file' => 30, // optional, 当 type 为 daily 时有效,默认 30 天
  82. ],
  83. 'ali_public_key' => $alipay_config['alipay_public_key']
  84. // 'app_cert_public_key' => \Yii::$app->basePath. '/utils/Alipay/CRT/app_cert_public_key.crt', // 应用公钥证书路径
  85. // 'ali_public_key' => \Yii::$app->basePath. '/utils/Alipay/CRT/alipay_cert_public_key.crt', // 支付宝公钥证书路径
  86. // 'alipay_root_cert' => \Yii::$app->basePath. '/utils/Alipay/CRT/alipay_root_cert.crt', // 支付宝根证书路径
  87. // 'mode' => 'dev' // optional,设置此参数,将进入沙箱模式
  88. ];
  89. if ($type) {
  90. $key = Option::ALIPAY_CONFIG_CERT;
  91. if (\Yii::$app->prod_is_dandianpu() && is_h5()){
  92. $key = OptionSetting::ALIPAY_PAY_DANDIANPUH5;
  93. if(!Store::hasIncoming($store_id, 'alipay', is_h5())){
  94. $store_id = 0;
  95. }
  96. }
  97. $config_cache = Option::get($key, $store_id, 'alipay')['value'];
  98. $config_cache = json_decode($config_cache, true);
  99. if (!empty($config_cache)) {
  100. $config = [
  101. 'app_id' => $config_cache['app_id'],
  102. 'notify_url' => pay_notify_url(self::$notify_url),
  103. 'return_url' => $return_url,
  104. 'log' => [ // optional
  105. 'file' => \Yii::$app->basePath . '/runtime/logs/alipay.log',
  106. 'level' => 'debug', // 建议生产环境等级调整为 info,开发环境为 debug
  107. 'type' => 'single', // optional, 可选 daily.
  108. 'max_file' => 30, // optional, 当 type 为 daily 时有效,默认 30 天
  109. ],
  110. 'private_key' => $config_cache['app_private_key'],
  111. 'app_cert_public_key' => $this->saveTempImage($config_cache['app_public_cert']), // 应用公钥证书路径
  112. 'ali_public_key' => $this->saveTempImage($config_cache['alipay_public_cert']), // 支付宝公钥证书路径
  113. 'alipay_root_cert' => $this->saveTempImage($config_cache['alipay_root_cert']), // 支付宝根证书路径
  114. ];
  115. }
  116. }
  117. self::$alipay = Pay::alipay($config);
  118. self::$alipay_conf = $config;
  119. }
  120. /**
  121. * 静态方法调用construct方法
  122. * @param $return_url
  123. */
  124. public static function init($return_url = '', $type = 0, $store_id = 0) {
  125. return new self($return_url, $type, $store_id);
  126. }
  127. //获取网络图片到临时目录
  128. public function saveTempImage($url)
  129. {
  130. if (strpos($url, 'http') === false) {
  131. $url = 'http:' . trim($url);
  132. }
  133. if (!is_dir(\Yii::$app->runtimePath . '/crt')) {
  134. mkdir(\Yii::$app->runtimePath . '/crt');
  135. }
  136. $save_path = \Yii::$app->runtimePath . '/crt/' . md5($url) . '.crt';
  137. CurlHelper::download($url, $save_path);
  138. return $save_path;
  139. }
  140. /**
  141. * app支付
  142. * @param $order
  143. * @param User $user
  144. * @param $subject
  145. * @param $balance_price
  146. * @param $return_url
  147. * @return Response | false | string | array
  148. */
  149. public static function app($order, $subject, $user, $type = '', $total_pay_price = 0,$balance_price = 0, $return_url = '') {
  150. self::init($return_url, 1);
  151. $order1 = $order;
  152. if($type === OrderNo::ORDER_UNION){
  153. $order = [
  154. 'body' => '订单合并支付',
  155. 'out_trade_no' => OrderNo::getOrderNo(OrderNo::ORDER_UNION),
  156. 'total_amount' => $balance_price > 0 ? $total_pay_price - $balance_price : $total_pay_price,
  157. 'subject' => count($order1) . '笔订单合并支付',
  158. ];
  159. $order_union = new OrderUnion();
  160. $order_union->store_id = get_store_id();
  161. $order_union->user_id = get_user()->id;
  162. $order_union->order_no = $order['out_trade_no'];
  163. $order_union->price = $order['total_amount'];
  164. $order_union->is_pay = 0;
  165. $order_union->created_at = time();
  166. $order_union->is_delete = 0;
  167. $order_id_list = [];
  168. foreach ($order1 as $value) {
  169. $order_id_list[] = $value->id;
  170. }
  171. $order_union->order_id_list = json_encode($order_id_list);
  172. if (!$order_union->save()) {
  173. foreach ($order_union->errors as $error) {
  174. return [
  175. 'code' => 1,
  176. 'msg' => $error
  177. ];
  178. }
  179. }
  180. }else{
  181. /** 逻辑开始 **/
  182. $order = [
  183. 'body' => $subject,
  184. 'out_trade_no' => $order->order_no,
  185. 'total_amount' => $balance_price > 0 ? ($order->pay_price - $balance_price) : $order->pay_price,
  186. 'subject' => $subject
  187. ];
  188. }
  189. try {
  190. $res = self::$alipay->app($order)->getContent();
  191. } catch (\Exception $e) {
  192. return [
  193. 'code' => 1,
  194. 'msg' => $e->getMessage()
  195. ];
  196. }
  197. if($type === OrderNo::ORDER_UNION){
  198. foreach($order1 as $value){
  199. $value->order_union_id = $order_union->id;
  200. $value->save();
  201. }
  202. }
  203. return [
  204. 'code' => 0,
  205. 'msg' => 'success',
  206. 'data' => $res,
  207. 'res' => $res
  208. ];
  209. }
  210. /**
  211. * 订单查询
  212. * @param $order
  213. * @param $type 默认:wap: 普通订单,refund:退款订单,transfer: 转账订单
  214. * @return array | Collection
  215. */
  216. public static function find($order, $type = 'wap') {
  217. if (!in_array($type, self::$validFlag)) {
  218. return [
  219. 'code' => 1,
  220. 'msg' => 'Params are wrong'
  221. ];
  222. }
  223. self::init();
  224. /** 逻辑开始 **/
  225. $order = [
  226. 'out_trade_no' => $order->order_no,
  227. ];
  228. if ($type == self::FLAG_REFUND) {
  229. // 后端生成,唯一即可
  230. $order['out_request_no'] = $order->order_no;
  231. }
  232. try {
  233. $res = self::$alipay->find($order, $type);
  234. if ($res && $res->code === '10000' && $res->msg === 'Success') {
  235. return [
  236. 'code' => 0,
  237. 'msg' => 'success',
  238. 'data' => $res->toArray()
  239. ];
  240. }
  241. return [
  242. 'code' => $res->code,
  243. 'msg' => $res->msg,
  244. ];
  245. } catch (GatewayException $e) {
  246. return [
  247. 'code' => 1,
  248. 'msg' => $e->getMessage()
  249. ];
  250. } catch (InvalidSignException $e) {
  251. return [
  252. 'code' => 1,
  253. 'msg' => $e->getMessage()
  254. ];
  255. } catch (InvalidConfigException $e) {
  256. return [
  257. 'code' => 1,
  258. 'msg' => $e->getMessage()
  259. ];
  260. }
  261. }
  262. /**
  263. * wap支付
  264. * @param $order
  265. * @param $subject
  266. * @param $return_url
  267. * @return array
  268. */
  269. public static function wap($order, $subject, $return_url = '', $type = '', $total_pay_price = 0, $balance_price = 0, $payData = []) {
  270. if(!$return_url){
  271. $return_url = pay_return_url(self::$h5_url);
  272. }
  273. self::init($return_url, 1);
  274. $order1 = $order;
  275. if($type == OrderNo::ORDER_UNION){
  276. $order = [
  277. 'body' => '订单合并支付',
  278. 'out_trade_no' => OrderNo::getOrderNo(OrderNo::ORDER_UNION),
  279. 'total_amount' => $balance_price > 0 ? $total_pay_price - $balance_price : $total_pay_price,
  280. 'subject' => count($order1) . '笔订单合并支付',
  281. 'buyer_id' => get_user()->alipay_open_id
  282. ];
  283. $order_union = new OrderUnion();
  284. $order_union->store_id = get_store_id();
  285. $order_union->user_id = get_user()->id;
  286. $order_union->order_no = $order['out_trade_no'];
  287. $order_union->price = $order['total_amount'];
  288. $order_union->is_pay = 0;
  289. $order_union->created_at = time();
  290. $order_union->is_delete = 0;
  291. $order_id_list = [];
  292. foreach ($order1 as $value) {
  293. $order_id_list[] = $value->id;
  294. }
  295. $order_union->order_id_list = json_encode($order_id_list);
  296. if (!$order_union->save()) {
  297. foreach ($order_union->errors as $error) {
  298. return [
  299. 'code' => 1,
  300. 'msg' => $error
  301. ];
  302. }
  303. }
  304. }else{
  305. /** 逻辑开始 **/
  306. $order = [
  307. 'body' => $subject,
  308. 'out_trade_no' => $order->order_no,
  309. 'total_amount' => $order->pay_price,
  310. 'subject' => $subject,
  311. 'buyer_id' => get_user()->alipay_open_id
  312. ];
  313. }
  314. try {
  315. $res = self::$alipay->wap($order);
  316. if ($res->getStatusCode() == 40004 || $res->getStatusCode() == 'ACQ.CONTEXT_INCONSISTENT') {
  317. $order->order_no = OrderNo::getOrderNo(OrderNo::ORDER_MALL);
  318. $order->save();
  319. return self::wap($order, $subject, $return_url, $type, $total_pay_price, $balance_price, $payData);
  320. }
  321. if($type == OrderNo::ORDER_UNION){
  322. foreach($order1 as $value){
  323. $value->order_union_id = $order_union->id;
  324. $value->save();
  325. }
  326. }
  327. return [
  328. 'code' => 0,
  329. 'msg' => 'success',
  330. 'data' => $res->getContent()
  331. ];
  332. } catch (\Exception $e) {
  333. return [
  334. 'code' => 1,
  335. 'msg' => '支付失败'. $e->getMessage()
  336. ];
  337. }
  338. }
  339. /**
  340. * web支付
  341. * @param $order
  342. * @param $subject
  343. * @param $return_url
  344. * @return Response | false | string
  345. */
  346. public static function web($order, $subject, $return_url = '') {
  347. self::init($return_url);
  348. /** 逻辑开始 **/
  349. $order = [
  350. 'body' => $subject,
  351. 'out_trade_no' => $order->order_no,
  352. 'total_amount' => $order->pay_price,
  353. 'subject' => $subject,
  354. ];
  355. $res = self::$alipay->web($order)->send();
  356. return $res->getContent();
  357. }
  358. /**
  359. * 扫码支付
  360. * @param $order
  361. * @param $subject
  362. * @param $return_url
  363. * @return Collection | array
  364. */
  365. public static function scan($order, $subject, $return_url = '', $user_id = null) {
  366. self::init($return_url);
  367. /** 逻辑开始 **/
  368. $order = [
  369. 'body' => $subject,
  370. 'out_trade_no' => $order->order_no,
  371. 'total_amount' => $order->pay_price,
  372. 'subject' => $subject,
  373. 'buyer_id' => $user_id
  374. ];
  375. try {
  376. $res = self::$alipay->mini($order);
  377. if ($res && $res->code === '10000' && $res->msg === 'Success') {
  378. return [
  379. 'code' => 0,
  380. 'msg' => 'success',
  381. 'data' => $res->toArray()
  382. ];
  383. }
  384. return [
  385. 'code' => $res->code,
  386. 'msg' => $res->msg,
  387. ];
  388. } catch (GatewayException $e) {
  389. return [
  390. 'code' => 1,
  391. 'msg' => $e->getMessage()
  392. ];
  393. } catch (InvalidSignException $e) {
  394. return [
  395. 'code' => 1,
  396. 'msg' => $e->getMessage()
  397. ];
  398. } catch (InvalidConfigException $e) {
  399. return [
  400. 'code' => 1,
  401. 'msg' => $e->getMessage()
  402. ];
  403. }
  404. }
  405. /**
  406. * 扫码支付
  407. * @param $order
  408. * @param $subject 订单标题
  409. * @param $return_url
  410. * @return Collection | array
  411. */
  412. public static function scanNew($order, $subject, $return_url = '', $auth_code,$total_pay_price = 0,$balance_price = 0) {
  413. self::init($return_url);
  414. /** 逻辑开始 **/
  415. $order = [
  416. 'out_trade_no' => $order->order_no,//订单号
  417. 'total_amount' => $balance_price > 0 ? $total_pay_price - $balance_price : $total_pay_price,
  418. 'subject' => $subject,// 订单标题
  419. 'auth_code' => $auth_code,
  420. 'scene' => 'bar_code',
  421. ];
  422. try {
  423. $res = self::$alipay->pos($order);
  424. if ($res && $res->code === '10000' && $res->msg === 'Success') {
  425. return [
  426. 'code' => 0,
  427. 'msg' => 'success',
  428. 'data' => $res->toArray()
  429. ];
  430. }
  431. return [
  432. 'code' => $res->code,
  433. 'msg' => $res->msg,
  434. ];
  435. } catch (GatewayException $e) {
  436. return [
  437. 'code' => 1,
  438. 'msg' => $e->getMessage()
  439. ];
  440. } catch (InvalidSignException $e) {
  441. return [
  442. 'code' => 1,
  443. 'msg' => $e->getMessage()
  444. ];
  445. } catch (InvalidConfigException $e) {
  446. return [
  447. 'code' => 1,
  448. 'msg' => $e->getMessage()
  449. ];
  450. }
  451. }
  452. /**
  453. * 转账
  454. * @param $order
  455. * @param $mail
  456. * @param $return_url
  457. * @return Collection | array
  458. */
  459. public static function transfer($order, $mail, $return_url = '') {
  460. if (empty($mail)) {
  461. return [
  462. 'code' => 1,
  463. 'msg' => 'Params are wrong'
  464. ];
  465. }
  466. self::init($return_url, 1);
  467. /** 逻辑开始 **/
  468. $order = [
  469. 'out_biz_no' => $order->order_no,
  470. 'trans_amount' => floatval(sprintf("%.2f", $order->pay_price)),
  471. 'biz_scene' => 'DIRECT_TRANSFER',
  472. 'product_code' => 'TRANS_ACCOUNT_NO_PWD',
  473. 'order_title' => '佣金提现',
  474. 'payee_info' => [
  475. 'identity' => $mail,
  476. 'identity_type' => 'ALIPAY_LOGON_ID',
  477. 'name' => $order->name
  478. ],
  479. ];
  480. try {
  481. $res = self::$alipay->transfer($order);
  482. if ($res && $res->code === '10000' && $res->msg === 'Success') {
  483. return [
  484. 'code' => 0,
  485. 'msg' => 'success',
  486. 'data' => $res->toArray()
  487. ];
  488. }
  489. return [
  490. 'code' => $res->code,
  491. 'msg' => $res->msg,
  492. ];
  493. } catch (GatewayException $e) {
  494. return [
  495. 'code' => 1,
  496. 'msg' => $e->getMessage()
  497. ];
  498. } catch (InvalidSignException $e) {
  499. return [
  500. 'code' => 1,
  501. 'msg' => $e->getMessage()
  502. ];
  503. } catch (InvalidConfigException $e) {
  504. return [
  505. 'code' => 1,
  506. 'msg' => $e->getMessage()
  507. ];
  508. }
  509. }
  510. /**
  511. * 批量转账
  512. * @param $params
  513. * @return Collection | array
  514. */
  515. public static function batchTransfer($params) {
  516. self::init('');
  517. /** 逻辑开始 **/
  518. $order = [
  519. 'out_batch_no' => $params['out_batch_no'],
  520. 'product_code' => 'BATCH_API_TO_ACC_NO_PWD',
  521. 'biz_scene' => 'MESSAGE_BATCH_PAY',
  522. 'total_trans_amount' => $params['total_trans_amount'],
  523. 'total_count' => 1,
  524. 'trans_order_list' => [
  525. [
  526. 'out_biz_no' => $params['out_batch_no'],
  527. 'trans_amount' => $params['total_trans_amount'],
  528. 'payee_info' => [
  529. 'identity' => $params['account'],
  530. 'identity_type' => 'ALIPAY_LOGON_ID',
  531. 'name' => $params['name']
  532. ],
  533. 'remark' => $params['title']
  534. ],
  535. ],
  536. 'order_title' => $params['title'],
  537. 'remark' => '转账',
  538. ];
  539. try {
  540. $res = self::$alipay->batchtransfer($order);
  541. \Yii::error($res);
  542. if ($res && $res->code === '10000' && $res->msg === 'Success') {
  543. return [
  544. 'code' => 0,
  545. 'msg' => 'success',
  546. 'data' => $res->toArray()
  547. ];
  548. }
  549. return [
  550. 'code' => $res->code,
  551. 'msg' => $res->msg,
  552. ];
  553. } catch (GatewayException $e) {
  554. return [
  555. 'code' => 1,
  556. 'msg' => $e->getMessage()
  557. ];
  558. } catch (InvalidSignException $e) {
  559. return [
  560. 'code' => 1,
  561. 'msg' => $e->getMessage()
  562. ];
  563. } catch (InvalidConfigException $e) {
  564. return [
  565. 'code' => 1,
  566. 'msg' => $e->getMessage()
  567. ];
  568. }
  569. }
  570. /**
  571. * 退款
  572. * @param $order
  573. * @param $return_url
  574. * @param $refund_fee
  575. * @return Collection | array
  576. */
  577. public static function refund($order, $refund_fee, $return_url = '') {
  578. self::init($return_url, 1);
  579. /** 逻辑开始 **/
  580. $data = [
  581. 'out_trade_no' => $order->order_no,
  582. 'refund_amount' => $refund_fee,
  583. ];
  584. try {
  585. $res = self::$alipay->refund($data);
  586. if ($res && $res->code === '10000' && $res->msg === 'Success') {
  587. return [
  588. 'code' => 0,
  589. 'msg' => 'success',
  590. 'data' => $res->toArray()
  591. ];
  592. }
  593. return [
  594. 'code' => $res->code,
  595. 'msg' => $res->msg,
  596. ];
  597. } catch (GatewayException $e) {
  598. return [
  599. 'code' => 1,
  600. 'msg' => $e->getMessage()
  601. ];
  602. } catch (InvalidSignException $e) {
  603. return [
  604. 'code' => 1,
  605. 'msg' => $e->getMessage()
  606. ];
  607. } catch (InvalidConfigException $e) {
  608. return [
  609. 'code' => 1,
  610. 'msg' => $e->getMessage()
  611. ];
  612. }
  613. }
  614. /**
  615. * 小程序支付
  616. * @param $order
  617. * @param $subject
  618. * @param User $user
  619. * @param $return_url
  620. * @param $is_food
  621. * @return Collection | array
  622. */
  623. public static function mini($order, $subject, $user, $return_url = '', $type = '', $total_pay_price = 0, $balance_price = 0, $payData = []) {
  624. $self = self::init($return_url);
  625. if (!$order || ($type != OrderNo::ORDER_UNION && empty($subject)) || !$user) {
  626. return [
  627. 'code' => 1,
  628. 'msg' => '参数有误'
  629. ];
  630. }
  631. $order1 = $order;
  632. if ($type == OrderNo::ORDER_UNION) {
  633. $order = [
  634. 'body' => '订单合并支付',
  635. 'out_trade_no' => OrderNo::getOrderNo(OrderNo::ORDER_UNION),
  636. 'total_amount' => $balance_price > 0 ? $total_pay_price - $balance_price : $total_pay_price,
  637. 'subject' => count($order1) . '笔订单合并支付',
  638. 'buyer_id' => ($user->alipay_open_id ?? $user->ali_user_id)
  639. ];
  640. $order_union = new OrderUnion();
  641. $order_union->store_id = get_store_id();
  642. $order_union->user_id = get_user()->id;
  643. $order_union->order_no = $order['out_trade_no'];
  644. $order_union->price = $order['total_amount'];
  645. $order_union->is_pay = 0;
  646. $order_union->created_at = time();
  647. $order_union->is_delete = 0;
  648. $order_id_list = [];
  649. foreach ($order1 as $value) {
  650. $order_id_list[] = $value->id;
  651. }
  652. $order_union->order_id_list = json_encode($order_id_list);
  653. if (!$order_union->save()) {
  654. foreach ($order_union->errors as $error) {
  655. return [
  656. 'code' => 1,
  657. 'msg' => $error
  658. ];
  659. }
  660. }
  661. } else {
  662. try{
  663. $goods_detail = [];
  664. $details = OrderDetail::findAll(['order_id' => $order->id]);
  665. foreach ($details as $detail) {
  666. $goods = Goods::findOne($detail->goods_id);
  667. $goods_detail[] = [
  668. 'goods_id' => $goods->goods_no ,
  669. 'goods_name' => $detail->goods_name,
  670. 'quantity' => $detail->num,
  671. 'price' => $detail->total_price / $detail->num,
  672. ];
  673. }
  674. } catch (\Exception $e) {
  675. \Yii::error([__METHOD__, $order, $e]);
  676. }
  677. $order = [
  678. 'body' => $subject,
  679. 'out_trade_no' => $order->order_no,
  680. 'total_amount' => $balance_price > 0 ? ($order->pay_price - $balance_price) : $order->pay_price,
  681. 'subject' => $subject,
  682. 'buyer_id' => ($user->alipay_open_id ?? $user->ali_user_id)
  683. ];
  684. $goods_detail && $order['goods_detail'] = $goods_detail;
  685. }
  686. try {
  687. if($payData->ali_requireOrder && $payData->ali_sourceId){
  688. $form = new \app\modules\admin\models\AlipayThirdForm();
  689. $form->store_id = $self->store_id;
  690. $form->mini_id = $self->mini_id;
  691. $openOrder = $form->AlipayOpenMiniOrderCreate($order1, $order, $payData);
  692. if($openOrder['code'] != 0){
  693. return $openOrder;
  694. }
  695. $order['extend_params'] = [
  696. 'trade_component_order_id' => $openOrder['data'],
  697. ];
  698. foreach($openOrder['biz_content']['order_detail']['item_infos'] as $item){
  699. $order['goods_detail'][] = [
  700. 'goods_id' => $item['goods_id'],
  701. 'goods_name' => $item['goods_name'],
  702. 'quantity' => $item['item_cnt'],
  703. 'price' => $item['sale_price'],
  704. 'out_item_id' => $item['out_item_id'],
  705. 'out_sku_id' => $item['out_sku_id'],
  706. ];
  707. }
  708. }
  709. $order['product_code'] = 'JSAPI_PAY';
  710. $res = self::$alipay->mini($order);
  711. if ($res && $res->code === '10000' && $res->msg === 'Success') {
  712. return [
  713. 'code' => 0,
  714. 'msg' => 'success',
  715. 'data' => $res->toArray()
  716. ];
  717. }
  718. if ($type == OrderNo::ORDER_UNION) {
  719. foreach ($order1 as $value) {
  720. $value->order_union_id = $value->id;
  721. $value->save();
  722. }
  723. }
  724. return [
  725. 'code' => $res->code,
  726. 'msg' => $res->msg,
  727. ];
  728. } catch (GatewayException $e) {
  729. return [
  730. 'code' => 1,
  731. 'msg' => $e->getMessage()
  732. ];
  733. } catch (InvalidSignException $e) {
  734. return [
  735. 'code' => 1,
  736. 'msg' => $e->getMessage()
  737. ];
  738. } catch (InvalidConfigException $e) {
  739. return [
  740. 'code' => 1,
  741. 'msg' => $e->getMessage()
  742. ];
  743. }
  744. }
  745. }