NoticeController.php 56 KB

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