NoticeController.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. <?php
  2. namespace mall\controllers;
  3. use biz\shop\classes\ShopClass;
  4. use biz\wx\classes\WxMessageClass;
  5. use bizHd\express\classes\HdDeliveryOrderClass;
  6. use bizHd\order\classes\OrderClass;
  7. use bizHd\order\classes\ScanPayClass;
  8. use bizHd\recharge\classes\RechargeClass;
  9. use bizHd\refund\classes\HdRefundClass;
  10. use bizHd\shop\classes\MainClass;
  11. use bizHd\work\classes\WorkClass;
  12. use bizHd\wx\classes\WxOpenClass;
  13. use common\components\noticeUtil;
  14. use common\components\payUtil;
  15. use Yii;
  16. use common\components\util;
  17. use common\components\dict;
  18. use biz\shop\classes\ShopExtClass;
  19. use common\components\lakala\Lakala;
  20. use bizHd\recharge\classes\RechargeShbClass;
  21. use bizHd\hb\classes\HbClass;
  22. use bizHd\hb\classes\HbManageClass;
  23. class NoticeController extends PublicController
  24. {
  25. //微信退款结果通知
  26. public function actionWxRefundCallback()
  27. {
  28. $postStr = file_get_contents('php://input');//接收微信服务器返回的xml消息体
  29. if (empty($postStr)) {
  30. util::end();
  31. }
  32. $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  33. if ($postObj->return_code != 'SUCCESS') {
  34. $msg = $postObj->return_msg ?? '';
  35. $text = '微信退款结果通知,报错了,报错内容:' . $msg;
  36. Yii::warning($text);
  37. noticeUtil::push($text, '15280215347');
  38. util::end();
  39. }
  40. //解密req_info
  41. $wx = WxOpenClass::getMallWxInfo();
  42. $req_info = $postObj->req_info ?? '';
  43. $key = $wx['wxPayKey'] ?? '';
  44. $xml = openssl_decrypt(base64_decode($req_info), 'AES-256-ECB', MD5($key), OPENSSL_RAW_DATA, '');
  45. $req_info_obj = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
  46. $refund_status = $req_info_obj->refund_status ?? '';
  47. $orderSn = $req_info_obj->out_trade_no ?? '';
  48. $out_refund_no = $req_info_obj->out_refund_no ?? '';
  49. //订单总金额
  50. $total_fee = $req_info_obj->total_fee ?? 0.00;
  51. //申请退款金额
  52. //$refund_fee = $req_info_obj->refund_fee ?? 0.00;
  53. //退款金额
  54. $settlement_refund_fee = $req_info_obj->settlement_refund_fee ?? 0.00;
  55. $new_total_fee = bcdiv($total_fee, 100, 2);
  56. $new_total_fee = floatval($new_total_fee);
  57. $new_settlement_refund_fee = bcdiv($settlement_refund_fee, 100, 2);
  58. $new_settlement_refund_fee = floatval($new_settlement_refund_fee);
  59. $connection = Yii::$app->db;
  60. $transaction = $connection->beginTransaction();
  61. $text = '';
  62. try {
  63. if ($refund_status == 'SUCCESS') {
  64. $hdRefund = HdRefundClass::getByCondition(['refundSn' => $out_refund_no], true);
  65. if (!empty($hdRefund)) {
  66. $hdRefund->status = HdRefundClass::STATUS_COMPLETE;
  67. $hdRefund->save();
  68. }
  69. //$text = "【退款成功】退款单号:{$orderSn} {$out_refund_no} 订单金额:{$new_total_fee} 退款金额:{$new_settlement_refund_fee} ";
  70. } elseif ($refund_status == 'REFUNDCLOSE') {
  71. $text = "【退款关闭】退款单号:{$orderSn} {$out_refund_no} 订单金额:{$new_total_fee} 退款金额:{$new_settlement_refund_fee} ";
  72. } else {
  73. $text = "【退款失败】退款单号:{$orderSn} {$out_refund_no} 订单金额:{$new_total_fee} 退款金额:{$new_settlement_refund_fee} ";
  74. }
  75. $transaction->commit();
  76. echo '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
  77. if (!empty($text)) {
  78. noticeUtil::push($text, '15280215347');
  79. }
  80. } catch (\Exception $e) {
  81. $transaction->rollBack();
  82. Yii::error("失败原因:" . $e->getMessage());
  83. }
  84. }
  85. //支付回调 ssh 20231011
  86. public function actionPayCallback()
  87. {
  88. Yii::$app->params['ptStyle'] = dict::getDict('ptStyle', 'mall');
  89. $postStr = file_get_contents('php://input');//接收微信服务器返回的xml消息体
  90. if (empty($postStr)) {
  91. util::end();
  92. }
  93. $headers = Yii::$app->getRequest()->getHeaders();
  94. $Authorization = $headers->get('Authorization');
  95. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  96. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  97. $params = [
  98. 'appid' => 'OP00002119',
  99. 'serial_no' => '018b08cfddbd',
  100. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  101. 'lklCertificatePath' => $lklCertificatePath,
  102. ];
  103. $laResource = new Lakala($params);
  104. $return = $laResource->signatureVerification($Authorization, $postStr);
  105. $postData = json_decode($postStr, true);
  106. $orderSn = $postData['out_trade_no'] ?? '';
  107. $total_amount = $postData['total_amount'] ?? 0;
  108. $account_type = $postData['account_type'] ?? '';
  109. if (empty($account_type)) {
  110. noticeUtil::push('支付回调失败了,没有找到钱包类型,请注意,orderSn:' . $orderSn, '15280215347');
  111. util::end();
  112. }
  113. $totalFee = $total_amount / 100;
  114. $transactionId = $postData['trade_no'] ?? 0;
  115. if ($return == false) {
  116. noticeUtil::push('支付回调验证失败了,请注意,orderSn:' . $orderSn, '15280215347');
  117. util::end();
  118. }
  119. //接收流水类型、代金劵
  120. $remarkString = $postData['remark'] ?? '';
  121. $remarkData = json_decode($remarkString, true);
  122. //流水类型
  123. $capitalType = isset($remarkData['capitalType']) ? $remarkData['capitalType'] : null;
  124. if (isset($capitalType) == false) {
  125. noticeUtil::push('支付回调失败了,没有找到capitalType,orderSn:' . $orderSn . ' remark:' . $remarkString, '15280215347');
  126. util::end();
  127. }
  128. $connection = Yii::$app->db;
  129. $transaction = $connection->beginTransaction();
  130. try {
  131. $currentPayWay = null;
  132. if ($account_type == 'WECHAT') {
  133. $currentPayWay = dict::getDict('payWay', 'wxPay');
  134. } elseif ($account_type == 'ALIPAY') {
  135. $currentPayWay = dict::getDict('payWay', 'alipay');
  136. } else {
  137. noticeUtil::push('支付回调失败了,没有找到支付类型,orderSn:' . $orderSn, '15280215347');
  138. util::end();
  139. }
  140. //支付成功后续流程
  141. $attach = '';
  142. payUtil::thirdPay($currentPayWay, $capitalType, $orderSn, $totalFee, $attach, $transactionId);
  143. $transaction->commit();
  144. echo "SUCCESS";
  145. if ($capitalType == dict::getDict('capitalType', 'xhOrder', 'id')) {
  146. //解决重复通知,重要
  147. $cacheKey = 'mall_order_pay_' . $orderSn;
  148. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  149. if (empty($has)) {
  150. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 864000, 'has']);
  151. //打印小票和语音播报
  152. $order = OrderClass::getByCondition(['orderSn' => $orderSn], true);
  153. if (!empty($order)) {
  154. OrderClass::onlinePrint($order);
  155. //付款成功了,通知发跑腿功能,关键词 hd_pay_after_call_pt,多处要同步修改
  156. HdDeliveryOrderClass::payAfter($order);
  157. $shopId = $order->shopId ?? 0;
  158. $shop = ShopClass::getById($shopId, true);
  159. //制作任务
  160. $shopExt = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  161. if ($shopExt->needWork == 1 && $order->hasWork != 1) {
  162. $mainId = $shop->mainId ?? 0;
  163. $main = MainClass::getById($mainId, true);
  164. $orderId = $order->id ?? 0;
  165. WorkClass::needWork($order, $main);
  166. $has = WorkClass::getByCondition(['orderId' => $orderId], true);
  167. if (!empty($has)) {
  168. $order->hasWork = 1;
  169. $order->save();
  170. }
  171. ShopExtClass::newWorkRemind($shopExt, $order);
  172. }
  173. //来自商城的新订单微信通知
  174. WxMessageClass::hdNewOrderInform($shop, $order);
  175. }
  176. }
  177. }
  178. if ($capitalType == dict::getDict('capitalType', 'scanPay', 'id')) {
  179. //解决重复通知,重要
  180. $myKey = 'mall_order_scan_pay_' . $orderSn;
  181. $has = Yii::$app->redis->executeCommand('GET', [$myKey]);
  182. if (empty($has)) {
  183. Yii::$app->redis->executeCommand('SETEX', [$myKey, 864000, 'has']);
  184. $scan = ScanPayClass::getByCondition(['orderSn' => $orderSn], true);
  185. //微信通知
  186. $shopId = $scan->shopId ?? 0;
  187. $shop = ShopClass::getById($shopId, true);
  188. WxMessageClass::hdScanPayInform($shop, $scan);
  189. ShopExtClass::hdScanPayReport($scan);
  190. }
  191. }
  192. if ($capitalType == dict::getDict('capitalType', 'xhRecharge', 'id')) {
  193. //解决重复通知,重要
  194. $myKey = 'mall_rehcarge_' . $orderSn;
  195. $has = Yii::$app->redis->executeCommand('GET', [$myKey]);
  196. if (empty($has)) {
  197. Yii::$app->redis->executeCommand('SETEX', [$myKey, 864000, 'has']);
  198. $recharge = RechargeClass::getByCondition(['orderSn' => $orderSn], true);
  199. ShopExtClass::skRechargeReport($recharge);
  200. //充值送红包 -- 有多处,请搜索:充值送红包处理
  201. $shopId = $recharge->shopId;
  202. $amount = $recharge->amount;
  203. $maxHbRule = RechargeShbClass::getByCondition(['shopId' => $shopId, 'amount<=' => $amount], false, 'allHbAmount DESC');
  204. if (!empty($maxHbRule)) {
  205. $currentTime = time();
  206. $rulesJson = $maxHbRule['rules'] ?? '';
  207. $rulesArr = json_decode($rulesJson, true);
  208. if (is_array($rulesArr) && !empty($rulesArr)) {
  209. foreach ($rulesArr as $rule) {
  210. $hbAmount = $rule['hbAmount'];
  211. $hbNum = $rule['hbNum'];
  212. $miniCost = $rule['miniCost'];
  213. $duration = $rule['duration'];
  214. $effectiveType = $rule['effectiveType'] ?? 2;
  215. $effectiveDate = $rule['effectiveDate'] ?? '';
  216. $effectiveDays = $rule['effectiveDays'] ?? 0;
  217. if ($effectiveType == 1 && !empty($effectiveDate) && $effectiveDate != '0000-00-00') {
  218. $beginTime = strtotime($effectiveDate);
  219. if (!$beginTime) {
  220. $beginTime = $currentTime;
  221. }
  222. } else {
  223. $beginTime = $currentTime + 86400 * $effectiveDays;
  224. }
  225. //给用户生成红包
  226. $data = [
  227. 'mainId' => $recharge->mainId,
  228. 'shopId' => $shopId,
  229. 'userId' => $recharge->userId,
  230. 'customId' => $recharge->customId,
  231. 'amount' => $hbAmount,
  232. 'minConsume' => $miniCost,
  233. 'beginTime' => $beginTime,
  234. 'endTime' => $duration == 0 ? 4102416000 : ($beginTime + 86400 * $duration),
  235. 'willTime' => $duration == 0 ? 4102416000 : ($beginTime + 86400 * $duration),
  236. 'duration' => $duration,
  237. 'getType' => 2, //取得方式 0新人领取 1门店发放 2充值赠送
  238. 'remark' => '充值赠送红包',
  239. ];
  240. $manageData = [
  241. 'getType' => 2,
  242. 'getTargetId' => 0,
  243. 'createStaffId' => 0,
  244. 'createStaffName' => '',
  245. 'remark' => $data['remark'] ?? '',
  246. ];
  247. for ($i = 0; $i < $hbNum; $i++) {
  248. $hb = HbClass::add($data);
  249. $manageData['hbId'] = $hb['id'];
  250. HbManageClass::add($manageData);
  251. }
  252. }
  253. }
  254. }
  255. }
  256. }
  257. } catch (\Exception $e) {
  258. $transaction->rollBack();
  259. Yii::error("失败原因:" . $e->getMessage());
  260. }
  261. }
  262. //微信支付异步回调
  263. public function actionWxCallback()
  264. {
  265. Yii::$app->params['ptStyle'] = dict::getDict('ptStyle', 'mall');
  266. $postStr = file_get_contents('php://input');//接收微信服务器返回的xml消息体
  267. if (empty($postStr)) {
  268. util::end();
  269. }
  270. $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  271. if ($postObj->return_code != 'SUCCESS') {
  272. Yii::warning('return_code error:' . $postObj->return_code);
  273. util::end();
  274. }
  275. $orderSn = $postObj->out_trade_no;
  276. $total_fee = $postObj->total_fee;
  277. $totalFee = $total_fee / 100;
  278. $attach = $postObj->attach;
  279. $transactionId = $postObj->transaction_id ?? '';
  280. //接收流水类型、代金劵
  281. parse_str($attach);
  282. //流水类型
  283. $capitalType = $capitalType ?? null;
  284. if (!isset($capitalType)) {
  285. Yii::warning('capitalType empty,orderSn:' . $orderSn);
  286. util::end();
  287. }
  288. $connection = Yii::$app->db;
  289. $transaction = $connection->beginTransaction();
  290. try {
  291. $wxPay = dict::getDict('payWay', 'wxPay');
  292. //支付成功后续流程
  293. payUtil::thirdPay($wxPay, $capitalType, $orderSn, $totalFee, $attach, $transactionId);
  294. $transaction->commit();
  295. echo '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
  296. //解决重复通知
  297. $cacheKey = 'mall_order_pay_' . $orderSn;
  298. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  299. if (empty($has)) {
  300. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 864000, 'has']);
  301. //打印小票和语音播报
  302. $order = OrderClass::getByCondition(['orderSn' => $orderSn], true);
  303. ShopExtClass::hdGatheringReport($order);
  304. OrderClass::onlinePrint($order);
  305. $shopId = $order->shopId ?? 0;
  306. $shop = ShopClass::getById($shopId, true);
  307. if (in_array($order->fromType, [dict::getDict("fromType", "shop"), dict::getDict("fromType", "friend")])) {
  308. //您有新的收入提醒
  309. WxMessageClass::gatheringIncomeInform($shop, $order);
  310. }
  311. if ($order->fromType == dict::getDict("fromType", "mall")) {
  312. //来自商城的新订单微信通知
  313. WxMessageClass::hdNewOrderInform($shop, $order);
  314. }
  315. $shopExt = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  316. ShopExtClass::newWorkRemind($shopExt, $order);
  317. }
  318. } catch (\Exception $e) {
  319. $transaction->rollBack();
  320. Yii::error("失败原因:" . $e->getMessage());
  321. }
  322. }
  323. //支付宝异步通知
  324. public function actionAliCallback()
  325. {
  326. header("Content-type: text/html; charset=utf-8");
  327. $aliWap = Yii::getAlias("@vendor/alipayWap");
  328. //商户订单号
  329. $orderSn = $_POST['out_trade_no'];
  330. $order = OrderClass::getByCondition(['orderSn' => $orderSn], true);
  331. $aliPId = '2021003122664465';
  332. $aliKey = 'MIIEpAIBAAKCAQEAnefrChYa5FUbGTlZNMTDNG531WrVP+TBPw3/hSmycRlBE/1sg9EryM0S3a9OQKQenRdYOoalqkkBuzt41ygJUC5hfNeaiV2OzphIiAyxUaiL1mSftP5B8joBN3HGG2AKbcvKlWcFoeiGY4ZmV/K0t3fqj/uPP6N/pEtnx6X4Ifgz4WGCDKUXKqT1Pyk2rqyIpel2EYk+HtC+9iicfac9kKQQiksDopb7sYosttdk64Dmjp2nMLDSpR+KOZUaqB39MEeHcJGeTzdVrp+Z9wW/NcJzSkSUb9juA0YjmogdbiKltykVHTbP6J0uPyByvzfyyY22QPqwNPiIPbCN6H543QIDAQABAoIBAQCO0+rOecYjSDO1siDVUTC8KTutR+/R1klRjojUWy3zjQNHYJAZ/0ZaX9wzttDSOWETeL0uWwJYL6coQxf/jVA3PWyirqYyn/R/PFFG9iwhj5HE/8lRvjXKMttM2lV2B34HaDE6yC/ZDmkYdsX1wSvjU81QJRuiVXIsGqSpep8hoVvVAn5sjvoC+zBSUaCsYy+7v7IXc6L8f/oLCBXY1r5j/+F/XFU1uFv4j162eRShQs6jQyVqQGLGCy4sSujj+8mpd6uCyJSIGJQHJlaN6cibTYStU/Z9+I/AP/+wQPVx31OwEC5J2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zsT90Vfjk4wci+bpAqNnpUASDuqZLCzebTuOhxYtGVBqYzhp/CxcC/Mag3ISjUG6jRqyUi15R1e2ruq74sohLGnb4m3nnwRVwnNVAaUcVfV1J0Nb0/9pusrj2t6FCyCkFeWbRDzKhplPQejaET2p5yplwSUpd1gqF+3srIn3erW5uAR6F0Z0NoCMzlo5TEXKWY2XMq97ubueE7Cgp8/c3dh5dkscAT2CCWwRq23aKozp+XMwIDAQAB';
  334. if (getenv('YII_ENV') == 'production') {
  335. if (isset($order->shopId) && $order->shopId == 1) {
  336. //国兰保留使用老的支付宝账号
  337. $aliPId = '2017041906821571';
  338. $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+Hp1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U+6GlF3LAx4VHa1qfn5MqRPfLroJcyPDyGfNe9vna2FnO4E/PH+hVbvPUAwX3XrUvJIm4OgY0JE3HYFlu+O7F4Ln6+Sl+Zv/ZE2nZAvNiyAwW5iVKhKXLieExqwd0NdrP28baBqkkIY+tmV7butGjB52iK1UbU0+1uiaPL4xBxRy6d0LZmYlvdVHp0JRUuwxBLChGgOCo/76DQIDAQAB';
  340. }
  341. }
  342. $config = [
  343. //应用ID,您的APP_ID
  344. 'app_id' => $aliPId,
  345. //商户私钥,您的原始格式RSA私钥
  346. 'merchant_private_key' => $aliKey,
  347. //异步通知地址
  348. 'notify_url' => Yii::$app->params['mallHost'] . "/notice/ali-async",
  349. //同步跳转
  350. 'return_url' => "",
  351. //编码格式
  352. 'charset' => "UTF-8",
  353. //签名方式
  354. 'sign_type' => "RSA2",
  355. //支付宝网关
  356. 'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
  357. //支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
  358. 'alipay_public_key' => $aliPublicKey,
  359. ];
  360. require_once($aliWap . '/wappay/service/AlipayTradeService.php');
  361. require_once($aliWap . '/wappay/buildermodel/AlipayTradeQueryContentBuilder.php');//查询接口
  362. $arr = $_POST;
  363. $aliService = new \AlipayTradeService($config);
  364. $aliService->writeLog(var_export($_POST, true));
  365. $result = $aliService->check($arr);
  366. Yii::info('check:' . $result);
  367. if ($result == false) {
  368. //请不要修改
  369. echo "fail";
  370. util::end();
  371. }
  372. //验证成功后续
  373. $tradeNo = $_POST['trade_no'];//支付宝交易号
  374. $tradeStatus = $_POST['trade_status'];//交易状态
  375. $totalFee = $_POST['total_amount'];
  376. $appId = $_POST['app_id'];
  377. $attach = $_POST['passback_params'];
  378. $attach = urldecode($attach);
  379. //接收流水类型、代金劵
  380. parse_str($attach);
  381. //流水类型
  382. $capitalType = isset($capitalType) ? $capitalType : null;
  383. $RequestBuilder = new \AlipayTradeQueryContentBuilder();
  384. $RequestBuilder->setTradeNo($tradeNo);
  385. //$queryResult = $aliService->Query($RequestBuilder);
  386. //$aliId = $queryResult->buyer_user_id;
  387. //买家的支付宝帐号,不完整,带*号
  388. //$aliAccount = $queryResult->buyer_logon_id;
  389. if (isset($capitalType) == false) {
  390. Yii::warning('capitalType empty, orderSn:' . $orderSn);
  391. util::end();
  392. }
  393. if ($config['app_id'] != $appId) {
  394. Yii::warning('app_id disagree, orderSn:' . $orderSn . ' config app_id:' . $config['app_id'] . ' appId:' . $appId);
  395. util::end();
  396. }
  397. $connection = Yii::$app->db;
  398. $transaction = $connection->beginTransaction();
  399. try {
  400. if ($tradeStatus == 'TRADE_FINISHED' || $tradeStatus == 'TRADE_SUCCESS') {
  401. $aliPay = dict::getDict('payWay', 'alipay');
  402. //支付成功后续流程
  403. payUtil::thirdPay($aliPay, $capitalType, $orderSn, $totalFee, $attach);
  404. $transaction->commit();
  405. //打印小票和语音播报
  406. $order = OrderClass::getByCondition(['orderSn' => $orderSn], true);
  407. ShopExtClass::hdGatheringReport($order);
  408. OrderClass::onlinePrint($order);
  409. $shopId = $order->shopId ?? 0;
  410. $shop = ShopClass::getById($shopId, true);
  411. if (in_array($order->fromType, [dict::getDict("fromType", "shop"), dict::getDict("fromType", "friend")])) {
  412. //您有新的收入提醒
  413. WxMessageClass::gatheringIncomeInform($shop, $order);
  414. }
  415. if ($order->fromType == dict::getDict("fromType", "mall")) {
  416. //来自商城的新订单微信通知
  417. WxMessageClass::hdNewOrderInform($shop, $order);
  418. }
  419. }
  420. //请不要修改
  421. echo "success";
  422. } catch (\Exception $e) {
  423. $transaction->rollBack();
  424. Yii::error("失败原因:" . $e->getMessage());
  425. }
  426. }
  427. //收银台支付回调
  428. public function actionCashPayCallback()
  429. {
  430. header("Content-type: text/html; charset=utf-8");
  431. Yii::$app->params['ptStyle'] = dict::getDict('ptStyle', 'mall');
  432. $postStr = file_get_contents('php://input');//接收微信服务器返回的xml消息体
  433. if (empty($postStr)) {
  434. util::end();
  435. }
  436. $headers = Yii::$app->getRequest()->getHeaders();
  437. $Authorization = $headers->get('Authorization');
  438. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  439. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  440. $params = [
  441. 'appid' => 'OP00002119',
  442. 'serial_no' => '018b08cfddbd',
  443. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  444. 'lklCertificatePath' => $lklCertificatePath,
  445. ];
  446. $laResource = new Lakala($params);
  447. $return = $laResource->signatureVerification($Authorization, $postStr);
  448. $postData = json_decode($postStr, true);
  449. $orderSn = $postData['out_order_no'] ?? '';
  450. $orderTradeInfo = $postData['order_trade_info'] ?? [];
  451. $payMode = $orderTradeInfo['pay_mode'] ?? null;
  452. if (empty($payMode)) {
  453. noticeUtil::push('收银台支付回调失败了,没有找到钱包类型,请注意,orderSn:' . $orderSn, '15280215347');
  454. util::end();
  455. }
  456. $thirdLogNo = $orderTradeInfo['log_no'] ?? '';
  457. $trade_amount = $orderTradeInfo['trade_amount'] ?? 0;
  458. $totalFee = $trade_amount / 100;
  459. if ($totalFee <= 0) {
  460. noticeUtil::push('收银台支付回调验证失败了,金额错误,请注意,orderSn:' . $orderSn, '15280215347');
  461. util::end();
  462. }
  463. $transactionId = $orderTradeInfo['trade_no'] ?? 0;
  464. if ($return == false) {
  465. noticeUtil::push('收银台支付回调验证失败了,请注意,orderSn:' . $orderSn, '15280215347');
  466. util::end();
  467. }
  468. $orderStatus = $postData['order_status'];
  469. $tradeType = $orderTradeInfo['trade_type'] ?? '';
  470. if ($tradeType != 'PAY') {
  471. noticeUtil::push('收银台支付回调验证失败了,不是消费的回调,orderSn:' . $orderSn, '15280215347');
  472. util::end();
  473. }
  474. //接收流水类型、代金劵等
  475. $remarkString = $orderTradeInfo['trade_remark'] ?? '';
  476. $remarkData = json_decode($remarkString, true);
  477. $capitalType = $remarkData['capitalType'] ?? null;
  478. if (!isset($capitalType)) {
  479. $msg = '支付回调失败了,没有找到capitalType,orderSn:' . $orderSn . ' remark:' . $remarkString;
  480. noticeUtil::push($msg, '15280215347');
  481. util::end();
  482. }
  483. $connection = Yii::$app->db;
  484. $transaction = $connection->beginTransaction();
  485. try {
  486. if ($orderStatus == 2) {
  487. $payWayType = null;
  488. if ($payMode == 'ALIPAY') {
  489. $payWayType = dict::getDict('payWay', 'alipay');
  490. }
  491. if ($payMode == 'WECHAT') {
  492. $payWayType = dict::getDict('wxPay', 'alipay');
  493. }
  494. if (isset($payWayType) == false) {
  495. noticeUtil::push('收银台支付回调验证失败了,没有找到支付方式,orderSn:' . $orderSn, '15280215347');
  496. util::end();
  497. }
  498. $attach = '';
  499. payUtil::thirdPay($payWayType, $capitalType, $orderSn, $totalFee, $attach, $transactionId);
  500. $transaction->commit();
  501. if ($capitalType == dict::getDict('capitalType', 'xhOrder', 'id')) {
  502. //避免重复
  503. $cacheKey = 'mall_order_pay_' . $orderSn;
  504. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  505. if (empty($has)) {
  506. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 864000, 'has']);
  507. //打印小票和语音播报
  508. $order = OrderClass::getByCondition(['orderSn' => $orderSn], true);
  509. if (!empty($order)) {
  510. $order->thirdLogNo = $thirdLogNo;
  511. $order->save();
  512. OrderClass::onlinePrint($order);
  513. //付款成功了,通知发跑腿功能,关键词 hd_pay_after_call_pt,多处要同步修改
  514. HdDeliveryOrderClass::payAfter($order);
  515. $shopId = $order->shopId ?? 0;
  516. $shop = ShopClass::getById($shopId, true);
  517. //来自商城的新订单微信通知
  518. WxMessageClass::hdNewOrderInform($shop, $order);
  519. }
  520. }
  521. }
  522. if ($capitalType == dict::getDict('capitalType', 'scanPay', 'id')) {
  523. //避免重复
  524. $myKey = 'mall_order_scan_pay_' . $orderSn;
  525. $has = Yii::$app->redis->executeCommand('GET', [$myKey]);
  526. if (empty($has)) {
  527. Yii::$app->redis->executeCommand('SETEX', [$myKey, 864000, 'has']);
  528. $scan = ScanPayClass::getByCondition(['orderSn' => $orderSn], true);
  529. //微信通知
  530. $shopId = $scan->shopId ?? 0;
  531. $shop = ShopClass::getById($shopId, true);
  532. WxMessageClass::hdScanPayInform($shop, $scan);
  533. ShopExtClass::hdScanPayReport($scan);
  534. }
  535. }
  536. }
  537. //请不要修改
  538. echo "SUCCESS";
  539. } catch (\Exception $e) {
  540. $transaction->rollBack();
  541. Yii::error("失败原因:" . $e->getMessage());
  542. }
  543. }
  544. }