NoticeController.php 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  1. <?php
  2. namespace hd\controllers;
  3. use biz\shop\classes\ShopClass;
  4. use biz\notice\classes\NoticeClass;
  5. use bizGhs\custom\classes\CustomClass;
  6. use bizGhs\custom\classes\CustomRechargeClass;
  7. use bizGhs\order\classes\OrderClearClass;
  8. use bizGhs\order\classes\RefundOrderClass;
  9. use bizHd\cg\classes\CgRefundClass;
  10. use bizHd\ghs\classes\GhsClass;
  11. use bizHd\ljh\classes\LjhApplyClass;
  12. use bizHd\purchase\classes\PurchaseClass;
  13. use bizHd\purchase\classes\PurchaseClearClass;
  14. use bizHd\wx\classes\WxOpenClass;
  15. use common\components\dict;
  16. use common\components\noticeUtil;
  17. use common\components\payUtil;
  18. use common\components\sms;
  19. use Yii;
  20. use common\components\util;
  21. use biz\wx\classes\WxMessageClass;
  22. use bizGhs\order\classes\OrderClass;
  23. use biz\shop\classes\ShopExtClass;
  24. use common\components\lakala\Lakala;
  25. class NoticeController extends PublicController
  26. {
  27. public $categoryList;
  28. //收银台的回调通知 ssh 20231229
  29. public function actionCashPayCallback()
  30. {
  31. header("Content-type: text/html; charset=utf-8");
  32. Yii::$app->params['ptStyle'] = dict::getDict('ptStyle', 'hd');
  33. $postStr = file_get_contents('php://input');//接收微信服务器返回的xml消息体
  34. if (empty($postStr)) {
  35. util::end();
  36. }
  37. $headers = Yii::$app->getRequest()->getHeaders();
  38. $Authorization = $headers->get('Authorization');
  39. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  40. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  41. $params = [
  42. 'appid' => 'OP00002119',
  43. 'serial_no' => '018b08cfddbd',
  44. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  45. 'lklCertificatePath' => $lklCertificatePath,
  46. ];
  47. $laResource = new Lakala($params);
  48. $return = $laResource->signatureVerification($Authorization, $postStr);
  49. $postData = json_decode($postStr, true);
  50. $orderSn = $postData['out_order_no'] ?? '';
  51. $orderTradeInfo = $postData['order_trade_info'] ?? [];
  52. $payMode = $orderTradeInfo['pay_mode'] ?? null;
  53. if (empty($payMode)) {
  54. noticeUtil::push('收银台支付回调失败了,没有找到钱包类型,请注意,orderSn:' . $orderSn, '15280215347');
  55. util::end();
  56. }
  57. $trade_amount = $orderTradeInfo['trade_amount'] ?? 0;
  58. $totalFee = $trade_amount / 100;
  59. if ($totalFee <= 0) {
  60. noticeUtil::push('收银台支付回调验证失败了,金额错误,请注意,orderSn:' . $orderSn, '15280215347');
  61. util::end();
  62. }
  63. $transactionId = $orderTradeInfo['trade_no'] ?? 0;
  64. if ($return == false) {
  65. noticeUtil::push('收银台支付回调验证失败了,请注意,orderSn:' . $orderSn, '15280215347');
  66. util::end();
  67. }
  68. $orderStatus = $postData['order_status'];
  69. $tradeType = $orderTradeInfo['trade_type'] ?? '';
  70. if ($tradeType != 'PAY') {
  71. noticeUtil::push('收银台支付回调验证失败了,不是消费的回调,orderSn:' . $orderSn, '15280215347');
  72. util::end();
  73. }
  74. //接收流水类型、代金劵
  75. $remarkString = $orderTradeInfo['trade_remark'] ?? '';
  76. $remarkData = json_decode($remarkString, true);
  77. //流水类型
  78. $capitalType = isset($remarkData['capitalType']) ? $remarkData['capitalType'] : null;
  79. if (isset($capitalType) == false) {
  80. noticeUtil::push('支付回调失败了,没有找到capitalType,orderSn:' . $orderSn . ' remark:' . $remarkString, '15280215347');
  81. util::end();
  82. }
  83. $connection = Yii::$app->db;
  84. $transaction = $connection->beginTransaction();
  85. try {
  86. if ($orderStatus == 2) {
  87. $payWayType = null;
  88. if ($payMode == 'ALIPAY') {
  89. $payWayType = dict::getDict('payWay', 'alipay');
  90. }
  91. if ($payMode == 'WECHAT') {
  92. $payWayType = dict::getDict('wxPay', 'alipay');
  93. }
  94. if (isset($payWayType) == false) {
  95. noticeUtil::push('收银台支付回调验证失败了,没有找到支付方式,orderSn:' . $orderSn, '15280215347');
  96. util::end();
  97. }
  98. $attach = '';
  99. payUtil::thirdPay($payWayType, $capitalType, $orderSn, $totalFee, $attach, $transactionId);
  100. $transaction->commit();
  101. }
  102. //请不要修改
  103. echo "SUCCESS";
  104. //解决重复通知
  105. $cacheKey = 'pay_success_notice_' . $orderSn;
  106. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  107. if (!empty($has)) {
  108. Yii::$app->end();
  109. }
  110. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 864000, 'has']);
  111. //客户向供货商充值然后进行清账,关键词 custom_recharge_ghs_clear_action
  112. if ($capitalType == dict::getDict('capitalType', 'customRechargeToGhs', 'id')) {
  113. $customRecharge = CustomRechargeClass::getByCondition(['orderSn' => $orderSn], true);
  114. $customId = $customRecharge->customId ?? 0;
  115. $ghsId = $customRecharge->ghsId ?? 0;
  116. $ghs = GhsClass::getLockById($ghsId);
  117. $ghsShopId = $ghs->shopId ?? 0;
  118. $custom = CustomClass::getLockById($customId);
  119. $shop = ShopClass::getById($ghsShopId, true);
  120. OrderClearClass::useBalanceClear($custom, $ghs, $shop, null);
  121. //充值销账通知
  122. WxMessageClass::customRechargeClearInform($shop, $customRecharge);
  123. }
  124. //零售采购结账通知
  125. if ($capitalType == dict::getDict('capitalType', 'xhPurchase', 'id')) {
  126. $cg = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  127. $saleId = $cg->saleId ?? 0;
  128. $order = OrderClass::getById($saleId, true);
  129. //不是预订单,并且采购人没有开批发店,供货商不是源花汇、小明鲜花、淄博花超、云漫梦金鹏,则订单直接完成掉。是预订单,则确认发货时直接完成掉。有多个地方要修改,请搜索关键词zjFinish
  130. if (!empty($cg) && !empty($order)) {
  131. if (isset($cg->book) && $cg->book == 0) {
  132. $ghsShopId = $order->shopId ?? 0;
  133. if (getenv('YII_ENV') == 'production') {
  134. $specialList = [10649, 8042, 16454, 21319,33668];
  135. } else {
  136. $specialList = [36523];
  137. }
  138. if (!in_array($ghsShopId, $specialList)) {
  139. $cgShopId = $cg->shopId ?? 0;
  140. $cgShop = ShopClass::getById($cgShopId, true);
  141. if (!empty($cgShop)) {
  142. if (isset($cgShop->pfShopId) && $cgShop->pfShopId == 0) {
  143. PurchaseClass::confirmTake($cg);
  144. }
  145. }
  146. }
  147. }
  148. }
  149. if (isset($cg->localOrder) && $cg->localOrder == 1) {
  150. if ($cg->transType != 5 && $cg->transType != 4) {
  151. //昆明发货,客户算了一次打包费和运费,第二次就不再算了
  152. $customId = $cg->customId ?? 0;
  153. $current = date("Y_m_d");
  154. $key = 'ghs_custom_today_has_get_pack_cost_' . $current . '_' . $customId;
  155. Yii::$app->redis->executeCommand('SETEX', [$key, 86400, '1']);
  156. }
  157. } else {
  158. if (isset($cg->sendType) && $cg->sendType == 4) {
  159. //标记当天发快递方式已收一次包装费,岭南批发市场用的功能
  160. $customId = $cg->customId ?? 0;
  161. $current = date("Y_m_d");
  162. $key = 'ghs_custom_today_has_get_pack_cost_' . $current . '_' . $customId;
  163. Yii::$app->redis->executeCommand('SETEX', [$key, 86400, '1']);
  164. }
  165. }
  166. if (!empty($cg) && $cg->payStatus == 1) {
  167. //微信通知
  168. if (!empty($order)) {
  169. //订单生成时唤起在线打印
  170. if (isset($order->needPrint) && $order->needPrint == dict::getDict('needPrint', 'need')) {
  171. //有几个地方要同步去修改
  172. if (getenv('YII_ENV') == 'production') {
  173. //源花汇不自动打开小票
  174. $mallOrderNoPrint = [10536];
  175. } else {
  176. $mallOrderNoPrint = [];
  177. }
  178. if (in_array($order->mainId, $mallOrderNoPrint)) {
  179. } else {
  180. OrderClass::onlinePrint($order);
  181. $order->printNum += 1;
  182. $order->save();
  183. if (getenv('YII_ENV') == 'production') {
  184. //福州我要花要打二次小票
  185. $needTwoPrint = [7538];
  186. } else {
  187. $needTwoPrint = [];
  188. }
  189. if (in_array($order->mainId, $needTwoPrint)) {
  190. OrderClass::onlinePrint($order);
  191. $order->printNum += 1;
  192. $order->save();
  193. }
  194. }
  195. }
  196. $cgShopId = $cg->shopId ?? 0;
  197. $cgShop = ShopClass::getById($cgShopId, true);
  198. if (!empty($cgShop)) {
  199. \bizHd\notice\classes\NoticeClass::cgSuccessMiniNotice($cgShop, $cg);
  200. }
  201. //如果是门店用手机开单,让客户扫码付款的,则也要语音提示
  202. if (isset($order->shopAdminId) && $order->shopAdminId > 0) {
  203. ShopExtClass::ghsGatheringReport($order);
  204. }
  205. $shopId = $order->shopId ?? 0;
  206. $shop = ShopClass::getById($shopId, true);
  207. if (!empty($shop)) {
  208. //花店采购供货商端微信收到通知
  209. //WxMessageClass::ghsHasNewOrderInform($shop, $order);
  210. $noticeText = json_encode(['orderId' => $saleId]);
  211. $noticeKey = "hdCgNoticeGhs";
  212. Yii::$app->redis->executeCommand('LPUSH', [$noticeKey, $noticeText]);
  213. }
  214. //花店采购供货商端APP收到通知
  215. //NoticeClass::ghsNewOrderNotice($order);
  216. }
  217. }
  218. }
  219. } catch (\Exception $e) {
  220. $transaction->rollBack();
  221. Yii::error("失败原因:" . $e->getMessage());
  222. }
  223. }
  224. //拉卡拉回调通知 ssh 20231009
  225. public function actionPayCallback()
  226. {
  227. $postStr = file_get_contents('php://input');//接收微信服务器返回的xml消息体
  228. if (empty($postStr)) {
  229. util::end();
  230. }
  231. $headers = Yii::$app->getRequest()->getHeaders();
  232. $Authorization = $headers->get('Authorization');
  233. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  234. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  235. $params = [
  236. 'appid' => 'OP00002119',
  237. 'serial_no' => '018b08cfddbd',
  238. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  239. 'lklCertificatePath' => $lklCertificatePath,
  240. ];
  241. $laResource = new Lakala($params);
  242. $return = $laResource->signatureVerification($Authorization, $postStr);
  243. $postData = json_decode($postStr, true);
  244. $orderSn = $postData['out_trade_no'] ?? '';
  245. $total_amount = $postData['total_amount'] ?? 0;
  246. $account_type = $postData['account_type'] ?? '';
  247. $settle_merchant_no = $postData['settle_merchant_no']??'';
  248. if (empty($account_type)) {
  249. noticeUtil::push('支付回调失败了,没有找到钱包类型,请注意,orderSn:' . $orderSn, '15280215347');
  250. util::end();
  251. }
  252. $totalFee = $total_amount / 100;
  253. $transactionId = $postData['trade_no'] ?? 0;
  254. if ($return == false) {
  255. noticeUtil::push('支付回调验证失败了,请注意,orderSn:' . $orderSn, '15280215347');
  256. util::end();
  257. }
  258. //接收流水类型、代金劵
  259. $remarkString = $postData['remark'] ?? '';
  260. $remarkData = json_decode($remarkString, true);
  261. //流水类型
  262. $capitalType = isset($remarkData['capitalType']) ? $remarkData['capitalType'] : null;
  263. if (isset($capitalType) == false) {
  264. noticeUtil::push('支付回调失败了,没有找到capitalType,orderSn:' . $orderSn . ' remark:' . $remarkString, '15280215347');
  265. util::end();
  266. }
  267. $connection = Yii::$app->db;
  268. $transaction = $connection->beginTransaction();
  269. try {
  270. $currentPayWay = null;
  271. if ($account_type == 'WECHAT') {
  272. $currentPayWay = dict::getDict('payWay', 'wxPay');
  273. } elseif ($account_type == 'ALIPAY') {
  274. $currentPayWay = dict::getDict('payWay', 'alipay');
  275. } else {
  276. noticeUtil::push('支付回调失败了,没有找到支付类型,orderSn:' . $orderSn, '15280215347');
  277. util::end();
  278. }
  279. //支付成功后续流程
  280. $attach = 'settle_merchant_no='.$settle_merchant_no;
  281. payUtil::thirdPay($currentPayWay, $capitalType, $orderSn, $totalFee, $attach, $transactionId);
  282. $transaction->commit();
  283. echo "SUCCESS";
  284. //解决重复通知
  285. $cacheKey = 'pay_success_notice_' . $orderSn;
  286. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  287. if (!empty($has)) {
  288. Yii::$app->end();
  289. }
  290. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 864000, 'has']);
  291. //客户向供货商充值然后进行清账,关键词 custom_recharge_ghs_clear_action
  292. if ($capitalType == dict::getDict('capitalType', 'customRechargeToGhs', 'id')) {
  293. $customRecharge = CustomRechargeClass::getByCondition(['orderSn' => $orderSn], true);
  294. $customId = $customRecharge->customId ?? 0;
  295. $ghsId = $customRecharge->ghsId ?? 0;
  296. $ghs = GhsClass::getLockById($ghsId);
  297. $ghsShopId = $ghs->shopId ?? 0;
  298. $custom = CustomClass::getLockById($customId);
  299. $shop = ShopClass::getById($ghsShopId, true);
  300. OrderClearClass::useBalanceClear($custom, $ghs, $shop, null);
  301. //充值销账通知
  302. WxMessageClass::customRechargeClearInform($shop, $customRecharge);
  303. }
  304. //零售线上结账通知
  305. if ($capitalType == dict::getDict('capitalType', 'hdPurchaseClear', 'id')) {
  306. $clearInfo = PurchaseClearClass::getByCondition(['orderSn' => $orderSn], true);
  307. $customId = $clearInfo->customId ?? 0;
  308. $ghsShopId = $clearInfo->ghsShopId ?? 0;
  309. $ghsShop = ShopClass::getById($ghsShopId, true);
  310. $custom = CustomClass::getById($customId, true);
  311. if (!empty($custom) && !empty($ghsShop)) {
  312. //WxMessageClass::clearInform($ghsShop, $clearInfo, $custom);
  313. WxMessageClass::customHasClearToGhsInform($ghsShop, $clearInfo, $custom);
  314. }
  315. }
  316. //零售采购成功后续流程
  317. if ($capitalType == dict::getDict('capitalType', 'xhPurchase', 'id')) {
  318. $cg = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  319. $saleId = $cg->saleId ?? 0;
  320. $order = OrderClass::getById($saleId, true);
  321. //不是预订单,并且采购人没有开批发店,供货商不是源花汇、小明鲜花、淄博花超、云漫梦金鹏、镜中花卉,则订单直接完成掉。是预订单,则确认发货时直接完成掉。有多个地方要修改,请搜索关键词zjFinish
  322. if (!empty($cg) && !empty($order)) {
  323. if (isset($cg->book) && $cg->book == 0) {
  324. $ghsShopId = $order->shopId ?? 0;
  325. if (getenv('YII_ENV') == 'production') {
  326. $specialList = [10649, 8042, 16454, 21319,33668];
  327. } else {
  328. $specialList = [36523];
  329. }
  330. if (!in_array($ghsShopId, $specialList)) {
  331. $cgShopId = $cg->shopId ?? 0;
  332. $cgShop = ShopClass::getById($cgShopId, true);
  333. if (!empty($cgShop)) {
  334. if (isset($cgShop->pfShopId) && $cgShop->pfShopId == 0) {
  335. PurchaseClass::confirmTake($cg);
  336. }
  337. }
  338. }
  339. }
  340. }
  341. //更新最后下单时间
  342. $customId = $order->customId ?? 0;
  343. $custom = CustomClass::getById($customId, true);
  344. $date = date("Y-m-d H:i:s");
  345. if (!empty($custom)) {
  346. $custom->recentExpend = $date;
  347. $custom->save();
  348. }
  349. $ghsId = $order->ghsId ?? 0;
  350. $ghs = GhsClass::getById($ghsId, true);
  351. if (!empty($ghs)) {
  352. $ghs->recentExpend = $date;
  353. $ghs->save();
  354. }
  355. if (isset($cg->localOrder) && $cg->localOrder == 1) {
  356. if ($cg->transType != 5 && $cg->transType != 4) {
  357. //昆明发货,客户算了一次打包费和运费,第二次就不再算了
  358. $customId = $cg->customId ?? 0;
  359. $current = date("Y_m_d");
  360. $key = 'ghs_custom_today_has_get_pack_cost_' . $current . '_' . $customId;
  361. Yii::$app->redis->executeCommand('SETEX', [$key, 86400, '1']);
  362. }
  363. } else {
  364. if (isset($cg->sendType) && $cg->sendType == 4) {
  365. //标记当天发快递方式已收一次包装费,岭南批发市场用的功能
  366. $customId = $cg->customId ?? 0;
  367. $current = date("Y_m_d");
  368. $key = 'ghs_custom_today_has_get_pack_cost_' . $current . '_' . $customId;
  369. Yii::$app->redis->executeCommand('SETEX', [$key, 86400, '1']);
  370. }
  371. }
  372. if (!empty($cg) && $cg->payStatus == 1) {
  373. //微信通知
  374. if (!empty($order)) {
  375. //订单生成时唤起在线打印
  376. if (isset($order->needPrint) && $order->needPrint == dict::getDict('needPrint', 'need')) {
  377. //有几个地方要同步去修改
  378. if (getenv('YII_ENV') == 'production') {
  379. //源花汇不自动打开小票
  380. $mallOrderNoPrint = [10536];
  381. } else {
  382. $mallOrderNoPrint = [];
  383. }
  384. if (in_array($order->mainId, $mallOrderNoPrint)) {
  385. } else {
  386. $shopId = $order->shopId ?? 0;
  387. $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  388. OrderClass::onlinePrint($order);
  389. if (isset($ext->printSn) && !empty($ext->printSn)) {
  390. $order->printNum += 1;
  391. $order->save();
  392. }
  393. if (getenv('YII_ENV') == 'production') {
  394. //福州我要花要打二次小票
  395. $needTwoPrint = [7538];
  396. } else {
  397. $needTwoPrint = [];
  398. }
  399. if (in_array($order->mainId, $needTwoPrint)) {
  400. OrderClass::onlinePrint($order);
  401. if (isset($ext->printSn) && !empty($ext->printSn)) {
  402. $order->printNum += 1;
  403. $order->save();
  404. }
  405. }
  406. }
  407. }
  408. $cgShopId = $cg->shopId ?? 0;
  409. $cgShop = ShopClass::getById($cgShopId, true);
  410. if (!empty($cgShop)) {
  411. \bizHd\notice\classes\NoticeClass::cgSuccessMiniNotice($cgShop, $cg);
  412. }
  413. //如果是门店用手机开单,让客户扫码付款的,则也要语音提示
  414. if (isset($order->shopAdminId) && $order->shopAdminId > 0) {
  415. ShopExtClass::ghsGatheringReport($order);
  416. }
  417. $shopId = $order->shopId ?? 0;
  418. $shop = ShopClass::getById($shopId, true);
  419. if (!empty($shop)) {
  420. //花店采购供货商端微信收到通知
  421. //WxMessageClass::ghsHasNewOrderInform($shop, $order);
  422. $noticeText = json_encode(['orderId' => $saleId]);
  423. $noticeKey = "hdCgNoticeGhs";
  424. Yii::$app->redis->executeCommand('LPUSH', [$noticeKey, $noticeText]);
  425. }
  426. //花店采购供货商端APP收到通知
  427. //NoticeClass::ghsNewOrderNotice($order);
  428. }
  429. }
  430. }
  431. } catch (\Exception $e) {
  432. $transaction->rollBack();
  433. Yii::error("失败原因:" . $e->getMessage());
  434. }
  435. }
  436. //微信支付异步回调
  437. public function actionWxCallback()
  438. {
  439. $postStr = file_get_contents('php://input');//接收微信服务器返回的xml消息体
  440. if (empty($postStr)) {
  441. util::end();
  442. }
  443. $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  444. if ($postObj->return_code != 'SUCCESS') {
  445. Yii::warning('return_code error:' . $postObj->return_code);
  446. util::end();
  447. }
  448. $orderSn = $postObj->out_trade_no;
  449. $total_fee = $postObj->total_fee;
  450. $totalFee = $total_fee / 100;
  451. $attach = $postObj->attach;
  452. $transactionId = $postObj->transaction_id ?? '';
  453. //接收流水类型、代金劵
  454. parse_str($attach);
  455. //流水类型
  456. $capitalType = isset($capitalType) ? $capitalType : null;
  457. if (isset($capitalType) == false) {
  458. Yii::warning('capitalType empty,orderSn:' . $orderSn);
  459. util::end();
  460. }
  461. $connection = Yii::$app->db;
  462. $transaction = $connection->beginTransaction();
  463. try {
  464. $wxPay = dict::getDict('payWay', 'wxPay');
  465. //支付成功后续流程
  466. payUtil::thirdPay($wxPay, $capitalType, $orderSn, $totalFee, $attach, $transactionId);
  467. $transaction->commit();
  468. echo '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
  469. //解决重复通知
  470. $cacheKey = 'pay_success_notice_' . $orderSn;
  471. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  472. if (!empty($has)) {
  473. Yii::$app->end();
  474. }
  475. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 864000, 'has']);
  476. //零售采购结账通知
  477. if ($capitalType == dict::getDict('capitalType', 'xhPurchase', 'id')) {
  478. $cg = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  479. if (isset($cg->localOrder) && $cg->localOrder == 1) {
  480. if ($cg->transType != 5 && $cg->transType != 4) {
  481. //昆明发货,客户算了一次打包费和运费,第二次就不再算了
  482. $customId = $cg->customId ?? 0;
  483. $current = date("Y_m_d");
  484. $key = 'ghs_custom_today_has_get_pack_cost_' . $current . '_' . $customId;
  485. Yii::$app->redis->executeCommand('SETEX', [$key, 86400, '1']);
  486. }
  487. } else {
  488. if (isset($cg->sendType) && $cg->sendType == 4) {
  489. //标记当天发快递方式已收一次包装费,岭南批发市场用的功能
  490. $customId = $cg->customId ?? 0;
  491. $current = date("Y_m_d");
  492. $key = 'ghs_custom_today_has_get_pack_cost_' . $current . '_' . $customId;
  493. Yii::$app->redis->executeCommand('SETEX', [$key, 86400, '1']);
  494. }
  495. }
  496. if (!empty($cg) && $cg->payStatus == 1) {
  497. //微信通知
  498. $saleId = $cg->saleId ?? 0;
  499. $order = OrderClass::getById($saleId, true);
  500. if (!empty($order)) {
  501. //订单生成时唤起在线打印
  502. if (isset($order->needPrint) && $order->needPrint == dict::getDict('needPrint', 'need')) {
  503. //有几个地方要同步去修改
  504. if (getenv('YII_ENV') == 'production') {
  505. //源花汇不自动打开小票
  506. $mallOrderNoPrint = [10536];
  507. } else {
  508. $mallOrderNoPrint = [];
  509. }
  510. if (in_array($order->mainId, $mallOrderNoPrint)) {
  511. } else {
  512. OrderClass::onlinePrint($order);
  513. $order->printNum += 1;
  514. $order->save();
  515. }
  516. }
  517. $cgShopId = $cg->shopId ?? 0;
  518. $cgShop = ShopClass::getById($cgShopId, true);
  519. if (!empty($cgShop)) {
  520. \bizHd\notice\classes\NoticeClass::cgSuccessMiniNotice($cgShop, $cg);
  521. }
  522. //如果是门店用手机开单,让客户扫码付款的,则也要语音提示
  523. if (isset($order->shopAdminId) && $order->shopAdminId > 0) {
  524. ShopExtClass::ghsGatheringReport($order);
  525. }
  526. $shopId = $order->shopId ?? 0;
  527. $shop = ShopClass::getById($shopId, true);
  528. if (!empty($shop)) {
  529. //花店采购供货商端微信收到通知
  530. //WxMessageClass::ghsHasNewOrderInform($shop, $order);
  531. $noticeText = json_encode(['orderId' => $saleId]);
  532. $noticeKey = "hdCgNoticeGhs";
  533. Yii::$app->redis->executeCommand('LPUSH', [$noticeKey, $noticeText]);
  534. }
  535. //花店采购供货商端APP收到通知
  536. //NoticeClass::ghsNewOrderNotice($order);
  537. }
  538. }
  539. }
  540. } catch (\Exception $e) {
  541. $transaction->rollBack();
  542. Yii::error("失败原因:" . $e->getMessage());
  543. }
  544. }
  545. //微信退款结果通知
  546. public function actionWxRefundCallback()
  547. {
  548. $postStr = file_get_contents('php://input');//接收微信服务器返回的xml消息体
  549. if (empty($postStr)) {
  550. util::end();
  551. }
  552. $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  553. if ($postObj->return_code != 'SUCCESS') {
  554. $msg = $postObj->return_msg ?? '';
  555. $text = '微信退款结果通知,报错了,报错内容:' . $msg;
  556. Yii::warning($text);
  557. noticeUtil::push($text, '15280215347');
  558. util::end();
  559. }
  560. //解密req_info
  561. $wx = WxOpenClass::getWxInfo();
  562. $req_info = $postObj->req_info ?? '';
  563. $key = $wx['wxPayKey'] ?? '';
  564. $xml = openssl_decrypt(base64_decode($req_info), 'AES-256-ECB', MD5($key), OPENSSL_RAW_DATA, '');
  565. $req_info_obj = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
  566. $refund_status = $req_info_obj->refund_status ?? '';
  567. $orderSn = $req_info_obj->out_trade_no ?? '';
  568. $out_refund_no = $req_info_obj->out_refund_no ?? '';
  569. //订单总金额
  570. $total_fee = $req_info_obj->total_fee ?? 0.00;
  571. //申请退款金额
  572. //$refund_fee = $req_info_obj->refund_fee ?? 0.00;
  573. //退款金额
  574. $settlement_refund_fee = $req_info_obj->settlement_refund_fee ?? 0.00;
  575. $new_total_fee = bcdiv($total_fee, 100, 2);
  576. $new_total_fee = floatval($new_total_fee);
  577. $new_settlement_refund_fee = bcdiv($settlement_refund_fee, 100, 2);
  578. $new_settlement_refund_fee = floatval($new_settlement_refund_fee);
  579. $connection = Yii::$app->db;
  580. $transaction = $connection->beginTransaction();
  581. $text = '';
  582. try {
  583. if ($refund_status == 'SUCCESS') {
  584. $cgRefund = CgRefundClass::getByCondition(['orderSn' => $out_refund_no], true);
  585. if (!empty($cgRefund)) {
  586. $cgRefund->status = CgRefundClass::STATUS_COMPLETE;
  587. $cgRefund->save();
  588. $saleRefundId = $cgRefund->saleRefundId ?? 0;
  589. $saleRefund = RefundOrderClass::getById($saleRefundId, true);
  590. if (!empty($saleRefund)) {
  591. $saleRefund->status = RefundOrderClass::STATUS_COMPLETE;
  592. $saleRefund->save();
  593. }
  594. }
  595. //$text = "【退款成功】退款单号:{$orderSn} {$out_refund_no} 订单金额:{$new_total_fee} 退款金额:{$new_settlement_refund_fee} ";
  596. } elseif ($refund_status == 'REFUNDCLOSE') {
  597. $text = "【退款关闭】退款单号:{$orderSn} {$out_refund_no} 订单金额:{$new_total_fee} 退款金额:{$new_settlement_refund_fee} ";
  598. } else {
  599. $text = "【退款失败】退款单号:{$orderSn} {$out_refund_no} 订单金额:{$new_total_fee} 退款金额:{$new_settlement_refund_fee} ";
  600. }
  601. $transaction->commit();
  602. if (!empty($text)) {
  603. noticeUtil::push($text, '15280215347');
  604. }
  605. echo '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
  606. } catch (\Exception $e) {
  607. $transaction->rollBack();
  608. Yii::error("失败原因:" . $e->getMessage());
  609. }
  610. }
  611. //采购后支付宝异步通知
  612. public function actionCgAliCallback()
  613. {
  614. header("Content-type: text/html; charset=utf-8");
  615. $aliWap = Yii::getAlias("@vendor/alipayWap");
  616. //商户订单号
  617. $orderSn = $_POST['out_trade_no'];
  618. // $aliPId = '2017041906821571';
  619. // $aliKey = 'MIIEpAIBAAKCAQEAvbeu6o90L+0AilI+mbhuJJH0lUv7hWQCEC+XUc+cWlC9ulKC3cSVScVLJHpUc09aj6v/iD1p9iVfgMxjeADNVKiQC2RJyRshAjzyTq+2bTcODp619iV2y4qW8UM65VcP7Ide3P0uT5xC2ReW0HpkdHZgm4F+P7mh1VhGt9qOP3hjvoraAHhtGACvsGvy8H69HhSAG+CEmFt7f2tKNWJ8xEUvkfhDvys93PfTk4xMWj048hJE2r38+2mWI/Il5dFQHWXXTVl2x9yi7uhEEW42a+7s9Q3HgqEvZiQ2NI1FTeEAPLFBApiEsNLY/smaszwqqfWGAyTMvtQ6T4K96GcTLQIDAQABAoIBAHwYTj33n9RJfnT73x7F2KXrIsUVcmyKQh88QgqtdmRNNA1QM3HESLJ8bu5pZhwW5/HaW8dOBKWRRKsHBnlUbPrXV4FcFDeLm0fPfd+iZ/2AaZ1+ix962f3BpYIiq7+f9zaMRazfnw9L8x31pByyMktLs12EkoQ0dHsMxxUzzKAOi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F8qt3hmTSXuME5bT4vghckSGUTp4CNzsT90Vfjk4wci+bpAqNnpUASDuqZLCzebTuOhxYtGVBqYzhp/CxcC/Mag3ISjUG6jRqyUi15R1e2ruq74sohLGnb4m3nnwRVwnNVAaUcVfV1J0Nb0/9pusrj2t6FCyCkFeWbRDzKhplPQejaET2p5yplwSUpd1gqF+3srIn3erW5uAR6F0Z0NoCMzlo5TEXKWY2XMq97ubueE7Cgp8/c3dh5dkscAT2CCWwRq23aKozp+XMwIDAQAB';
  624. $config = [
  625. //应用ID,您的APP_ID
  626. 'app_id' => $aliPId,
  627. //商户私钥,您的原始格式RSA私钥
  628. 'merchant_private_key' => $aliKey,
  629. //异步通知地址
  630. 'notify_url' => Yii::$app->params['hdHost'] . "/notice/ali-async",
  631. //同步跳转
  632. 'return_url' => "",
  633. //编码格式
  634. 'charset' => "UTF-8",
  635. //签名方式
  636. 'sign_type' => "RSA2",
  637. //支付宝网关
  638. 'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
  639. //支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
  640. 'alipay_public_key' => $aliPublicKey,
  641. ];
  642. require_once($aliWap . '/wappay/service/AlipayTradeService.php');
  643. require_once($aliWap . '/wappay/buildermodel/AlipayTradeQueryContentBuilder.php');//查询接口
  644. $arr = $_POST;
  645. $aliService = new \AlipayTradeService($config);
  646. $aliService->writeLog(var_export($_POST, true));
  647. $result = $aliService->check($arr);
  648. Yii::info('check:' . $result);
  649. if ($result == false) {
  650. //请不要修改
  651. echo "fail";
  652. util::end();
  653. }
  654. //验证成功后续
  655. $tradeNo = $_POST['trade_no'] ?? '';//支付宝交易号
  656. $tradeStatus = $_POST['trade_status'] ?? '';//交易状态
  657. $totalFee = $_POST['total_amount'] ?? '';
  658. $appId = $_POST['app_id'] ?? '';
  659. $attach = $_POST['passback_params'] ?? '';
  660. $attach = urldecode($attach);
  661. //接收流水类型、代金劵
  662. parse_str($attach);
  663. $capitalType = dict::getDict('capitalType', 'xhPurchase', 'id');
  664. $RequestBuilder = new \AlipayTradeQueryContentBuilder();
  665. $RequestBuilder->setTradeNo($tradeNo);
  666. //$queryResult = $aliService->Query($RequestBuilder);
  667. //$aliId = $queryResult->buyer_user_id;
  668. //买家的支付宝帐号,不完整,带*号
  669. //$aliAccount = $queryResult->buyer_logon_id;
  670. if ($config['app_id'] != $appId) {
  671. Yii::warning('app_id disagree, orderSn:' . $orderSn . ' config app_id:' . $config['app_id'] . ' appId:' . $appId);
  672. util::end();
  673. }
  674. $connection = Yii::$app->db;
  675. $transaction = $connection->beginTransaction();
  676. try {
  677. if ($tradeStatus == 'TRADE_FINISHED' || $tradeStatus == 'TRADE_SUCCESS') {
  678. $aliPay = dict::getDict('payWay', 'alipay');
  679. //支付成功后续流程
  680. payUtil::thirdPay($aliPay, $capitalType, $orderSn, $totalFee, $attach, $tradeNo);
  681. $transaction->commit();
  682. //请不要修改
  683. echo "success";
  684. //解决重复通知
  685. $cacheKey = 'pay_success_notice_' . $orderSn;
  686. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  687. if (!empty($has)) {
  688. Yii::$app->end();
  689. }
  690. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 864000, 'has']);
  691. //零售采购通知
  692. if ($capitalType == dict::getDict('capitalType', 'xhPurchase', 'id')) {
  693. $cg = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  694. if (!empty($cg) && $cg->payStatus == 1) {
  695. if (isset($cg->localOrder) && $cg->localOrder == 1) {
  696. if ($cg->transType != 5 && $cg->transType != 4) {
  697. //昆明发货,客户算了一次打包费和运费,第二次就不再算了
  698. $customId = $cg->customId ?? 0;
  699. $current = date("Y_m_d");
  700. $key = 'ghs_custom_today_has_get_pack_cost_' . $current . '_' . $customId;
  701. Yii::$app->redis->executeCommand('SETEX', [$key, 86400, '1']);
  702. }
  703. } else {
  704. if (isset($cg->sendType) && $cg->sendType == 4) {
  705. //标记当天发快递方式已收一次包装费,岭南批发市场用的功能
  706. $customId = $cg->customId ?? 0;
  707. $current = date("Y_m_d");
  708. $key = 'ghs_custom_today_has_get_pack_cost_' . $current . '_' . $customId;
  709. Yii::$app->redis->executeCommand('SETEX', [$key, 86400, '1']);
  710. }
  711. }
  712. //微信通知
  713. $saleId = $cg->saleId ?? 0;
  714. $order = OrderClass::getById($saleId, true);
  715. if (!empty($order)) {
  716. //订单生成时唤起在线打印
  717. if (isset($order->needPrint) && $order->needPrint == dict::getDict('needPrint', 'need')) {
  718. //有几个地方要同步去修改
  719. if (getenv('YII_ENV') == 'production') {
  720. //源花汇不自动打开小票
  721. $mallOrderNoPrint = [10536];
  722. } else {
  723. $mallOrderNoPrint = [];
  724. }
  725. if (in_array($order->mainId, $mallOrderNoPrint)) {
  726. } else {
  727. OrderClass::onlinePrint($order);
  728. $order->printNum += 1;
  729. $order->save();
  730. }
  731. }
  732. //如果是门店用手机开单,让客户扫码付款的,则也要语音提示
  733. if (isset($order->shopAdminId) && $order->shopAdminId > 0) {
  734. ShopExtClass::ghsGatheringReport($order);
  735. }
  736. $cgShopId = $cg->shopId ?? 0;
  737. $cgShop = ShopClass::getById($cgShopId, true);
  738. if (!empty($cgShop)) {
  739. \bizHd\notice\classes\NoticeClass::cgSuccessMiniNotice($cgShop, $cg);
  740. }
  741. $shopId = $order->shopId ?? 0;
  742. $shop = ShopClass::getById($shopId, true);
  743. if (!empty($shop)) {
  744. //花店采购供货商端微信收到通知
  745. //WxMessageClass::ghsHasNewOrderInform($shop, $order);
  746. $noticeText = json_encode(['orderId' => $saleId]);
  747. $noticeKey = "hdCgNoticeGhs";
  748. Yii::$app->redis->executeCommand('LPUSH', [$noticeKey, $noticeText]);
  749. }
  750. //花店采购供货商端APP收到通知
  751. //NoticeClass::ghsNewOrderNotice($order);
  752. }
  753. }
  754. }
  755. }
  756. } catch (\Exception $e) {
  757. $transaction->rollBack();
  758. Yii::error("失败原因:" . $e->getMessage());
  759. }
  760. }
  761. //支付宝异步通知
  762. public function actionAliCallback()
  763. {
  764. header("Content-type: text/html; charset=utf-8");
  765. $aliWap = Yii::getAlias("@vendor/alipayWap");
  766. //商户订单号
  767. $orderSn = $_POST['out_trade_no'];
  768. // $aliPId = '2017041906821571';
  769. // $aliKey = 'MIIEpAIBAAKCAQEAvbeu6o90L+0AilI+mbhuJJH0lUv7hWQCEC+XUc+cWlC9ulKC3cSVScVLJHpUc09aj6v/iD1p9iVfgMxjeADNVKiQC2RJyRshAjzyTq+2bTcODp619iV2y4qW8UM65VcP7Ide3P0uT5xC2ReW0HpkdHZgm4F+P7mh1VhGt9qOP3hjvoraAHhtGACvsGvy8H69HhSAG+CEmFt7f2tKNWJ8xEUvkfhDvys93PfTk4xMWj048hJE2r38+2mWI/Il5dFQHWXXTVl2x9yi7uhEEW42a+7s9Q3HgqEvZiQ2NI1FTeEAPLFBApiEsNLY/smaszwqqfWGAyTMvtQ6T4K96GcTLQIDAQABAoIBAHwYTj33n9RJfnT73x7F2KXrIsUVcmyKQh88QgqtdmRNNA1QM3HESLJ8bu5pZhwW5/HaW8dOBKWRRKsHBnlUbPrXV4FcFDeLm0fPfd+iZ/2AaZ1+ix962f3BpYIiq7+f9zaMRazfnw9L8x31pByyMktLs12EkoQ0dHsMxxUzzKAOiiPnvCn8Pv+Jb2qej8GXgCjefutCVNMWGsdwJtMVXEbpdmMa48Wvw+0In5ZS/UG39YAreFzgDqI9jWHE1ZhkGmB1+8hKpeIlEeVGAeYVveUgssMORdA/YY8SWHk26KSIFXBrmdKjAcRJ8Q7ZHUmzIqAV+IMV+RD4J5mZHeJgdAECgYEA8J+Hp1M6eYbT4CPLXa//yRP6Xo+qSzrKHaRB1LRAjYhvKpx/TQetYpNCJ5bWpa+Jp0T5R21pzuYaJkPuGlNEHA13b5T3r1l2ltomSEdaShvD5EZ9WtErYV5zpSv7FjjIGkbwa0H3A8Ydnvw5z4A9TZjjHLjC2GtVGJReqHGglMECgYEAydddA4NRPxmXvIjjmLb1Ft1qwHuyv3vvWhi50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QsF4KITRzqrAoGAZBNyYNgBguRaEd1Sxw2gPmOUfvqiUCo6F4MJ+8xN8idn4FnaofcH8ic613PQPqpB/yYaxeqgIEfnvGY9ILXCuvbePfYqFmMwzALWvZ+v7uVKa2M7HstWCrq7O+m/lQFN/ut8ZnUDcBn4WwwHa/PjCYietetO2gpDRmAdSqrWGH8CgYBkwp+r6pkJzW60kHSZIlEKAe7oJMwLNC2ZqQ4MwGwzfBaH+E34kCuR8ZqYzyAIVOa/Nwi5By1Zvi1KzBwJmUUTJ3o7WCOE96EzSrmOZlqXNCwO/36Vh6dshhhE/birIlXJSP0xdzpBQy2ksyli9eGy8cdJ2Y72Wo+TjSclRbKiPQKBgQD1C/CJRQbahz6B3Sc4NOiFUQioM30qDKbDibzUq95u5PtQOx69+/h68tA2nj7ILZOXoh6/KF6LM9WK0QctY0HR5Uy4Uz8YkY/Y0L/LExSkNdpYqbpBc8DdIsKYHoMLNHDwc03tpINRLrmq00sD0907oeuY8/2l6P7XOZSnCvE5Fw==';
  773. $aliPublicKey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgEb9zdQQDva63DhksZHPZCDrRe2xvZcTRy9mEiWs/pUhCG0QABkuePUyvqTAKINiCEv2QJmdF8qt3hmTSXuME5bT4vghckSGUTp4CNzsT90Vfjk4wci+bpAqNnpUASDuqZLCzebTuOhxYtGVBqYzhp/CxcC/Mag3ISjUG6jRqyUi15R1e2ruq74sohLGnb4m3nnwRVwnNVAaUcVfV1J0Nb0/9pusrj2t6FCyCkFeWbRDzKhplPQejaET2p5yplwSUpd1gqF+3srIn3erW5uAR6F0Z0NoCMzlo5TEXKWY2XMq97ubueE7Cgp8/c3dh5dkscAT2CCWwRq23aKozp+XMwIDAQAB';
  774. $config = [
  775. //应用ID,您的APP_ID
  776. 'app_id' => $aliPId,
  777. //商户私钥,您的原始格式RSA私钥
  778. 'merchant_private_key' => $aliKey,
  779. //异步通知地址
  780. 'notify_url' => Yii::$app->params['hdHost'] . "/notice/ali-async",
  781. //同步跳转
  782. 'return_url' => "",
  783. //编码格式
  784. 'charset' => "UTF-8",
  785. //签名方式
  786. 'sign_type' => "RSA2",
  787. //支付宝网关
  788. 'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
  789. //支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
  790. 'alipay_public_key' => $aliPublicKey,
  791. ];
  792. require_once($aliWap . '/wappay/service/AlipayTradeService.php');
  793. require_once($aliWap . '/wappay/buildermodel/AlipayTradeQueryContentBuilder.php');//查询接口
  794. $arr = $_POST;
  795. $aliService = new \AlipayTradeService($config);
  796. $aliService->writeLog(var_export($_POST, true));
  797. $result = $aliService->check($arr);
  798. Yii::info('check:' . $result);
  799. if ($result == false) {
  800. //请不要修改
  801. echo "fail";
  802. util::end();
  803. }
  804. //验证成功后续
  805. $tradeNo = $_POST['trade_no'] ?? '';//支付宝交易号
  806. $tradeStatus = $_POST['trade_status'] ?? '';//交易状态
  807. $totalFee = $_POST['total_amount'] ?? '';
  808. $appId = $_POST['app_id'] ?? '';
  809. $attach = $_POST['passback_params'] ?? '';
  810. $attach = urldecode($attach);
  811. //接收流水类型、代金劵
  812. parse_str($attach);
  813. //流水类型
  814. $capitalType = isset($capitalType) ? $capitalType : null;
  815. $RequestBuilder = new \AlipayTradeQueryContentBuilder();
  816. $RequestBuilder->setTradeNo($tradeNo);
  817. //$queryResult = $aliService->Query($RequestBuilder);
  818. //$aliId = $queryResult->buyer_user_id;
  819. //买家的支付宝帐号,不完整,带*号
  820. //$aliAccount = $queryResult->buyer_logon_id;
  821. if (isset($capitalType) == false) {
  822. Yii::warning('capitalType empty, orderSn:' . $orderSn);
  823. util::end();
  824. }
  825. if ($config['app_id'] != $appId) {
  826. Yii::warning('app_id disagree, orderSn:' . $orderSn . ' config app_id:' . $config['app_id'] . ' appId:' . $appId);
  827. util::end();
  828. }
  829. $connection = Yii::$app->db;
  830. $transaction = $connection->beginTransaction();
  831. try {
  832. if ($tradeStatus == 'TRADE_FINISHED' || $tradeStatus == 'TRADE_SUCCESS') {
  833. $aliPay = dict::getDict('payWay', 'alipay');
  834. //支付成功后续流程
  835. payUtil::thirdPay($aliPay, $capitalType, $orderSn, $totalFee, $attach, $tradeNo);
  836. $transaction->commit();
  837. }
  838. //请不要修改
  839. echo "success";
  840. } catch (\Exception $e) {
  841. $transaction->rollBack();
  842. Yii::error("失败原因:" . $e->getMessage());
  843. }
  844. }
  845. }