NoticeController.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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. }
  202. } catch (\Exception $e) {
  203. $transaction->rollBack();
  204. Yii::error("失败原因:" . $e->getMessage());
  205. }
  206. }
  207. //微信支付异步回调
  208. public function actionWxCallback()
  209. {
  210. Yii::$app->params['ptStyle'] = dict::getDict('ptStyle', 'mall');
  211. $postStr = file_get_contents('php://input');//接收微信服务器返回的xml消息体
  212. if (empty($postStr)) {
  213. util::end();
  214. }
  215. $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  216. if ($postObj->return_code != 'SUCCESS') {
  217. Yii::warning('return_code error:' . $postObj->return_code);
  218. util::end();
  219. }
  220. $orderSn = $postObj->out_trade_no;
  221. $total_fee = $postObj->total_fee;
  222. $totalFee = $total_fee / 100;
  223. $attach = $postObj->attach;
  224. $transactionId = $postObj->transaction_id ?? '';
  225. //接收流水类型、代金劵
  226. parse_str($attach);
  227. //流水类型
  228. $capitalType = $capitalType ?? null;
  229. if (!isset($capitalType)) {
  230. Yii::warning('capitalType empty,orderSn:' . $orderSn);
  231. util::end();
  232. }
  233. $connection = Yii::$app->db;
  234. $transaction = $connection->beginTransaction();
  235. try {
  236. $wxPay = dict::getDict('payWay', 'wxPay');
  237. //支付成功后续流程
  238. payUtil::thirdPay($wxPay, $capitalType, $orderSn, $totalFee, $attach, $transactionId);
  239. $transaction->commit();
  240. echo '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
  241. //解决重复通知
  242. $cacheKey = 'mall_order_pay_' . $orderSn;
  243. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  244. if (empty($has)) {
  245. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 864000, 'has']);
  246. //打印小票和语音播报
  247. $order = OrderClass::getByCondition(['orderSn' => $orderSn], true);
  248. ShopExtClass::hdGatheringReport($order);
  249. OrderClass::onlinePrint($order);
  250. $shopId = $order->shopId ?? 0;
  251. $shop = ShopClass::getById($shopId, true);
  252. if (in_array($order->fromType, [dict::getDict("fromType", "shop"), dict::getDict("fromType", "friend")])) {
  253. //您有新的收入提醒
  254. WxMessageClass::gatheringIncomeInform($shop, $order);
  255. }
  256. if ($order->fromType == dict::getDict("fromType", "mall")) {
  257. //来自商城的新订单微信通知
  258. WxMessageClass::hdNewOrderInform($shop, $order);
  259. }
  260. $shopExt = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  261. ShopExtClass::newWorkRemind($shopExt, $order);
  262. }
  263. } catch (\Exception $e) {
  264. $transaction->rollBack();
  265. Yii::error("失败原因:" . $e->getMessage());
  266. }
  267. }
  268. //支付宝异步通知
  269. public function actionAliCallback()
  270. {
  271. header("Content-type: text/html; charset=utf-8");
  272. $aliWap = Yii::getAlias("@vendor/alipayWap");
  273. //商户订单号
  274. $orderSn = $_POST['out_trade_no'];
  275. $order = OrderClass::getByCondition(['orderSn' => $orderSn], true);
  276. $aliPId = '2021003122664465';
  277. $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/+wQPVx31OwEC5J2xBfSHsezMsHSF7HQnfpQSz5AsOYZ3MOkiglAoGBAPV8sZz3C1gQ6WX5eKZE7PEUSxvAHsP+X6AAA6sWfH4cZq1uGJXgiDpTqN5dOqfNpNAU8gBDNTx4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t6FCyCkFeWbRDzKhplPQejaET2p5yplwSUpd1gqF+3srIn3erW5uAR6F0Z0NoCMzlo5TEXKWY2XMq97ubueE7Cgp8/c3dh5dkscAT2CCWwRq23aKozp+XMwIDAQAB';
  279. if (getenv('YII_ENV') == 'production') {
  280. if (isset($order->shopId) && $order->shopId == 1) {
  281. //国兰保留使用老的支付宝账号
  282. $aliPId = '2017041906821571';
  283. $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+Jp0T5R21pzuYaJkPuGlNEHA13b5T3r1l2ltomSEdaShvD5EZ9WtErYV5zpSv7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hKXLieExqwd0NdrP28baBqkkIY+tmV7butGjB52iK1UbU0+1uiaPL4xBxRy6d0LZmYlvdVHp0JRUuwxBLChGgOCo/76DQIDAQAB';
  285. }
  286. }
  287. $config = [
  288. //应用ID,您的APP_ID
  289. 'app_id' => $aliPId,
  290. //商户私钥,您的原始格式RSA私钥
  291. 'merchant_private_key' => $aliKey,
  292. //异步通知地址
  293. 'notify_url' => Yii::$app->params['mallHost'] . "/notice/ali-async",
  294. //同步跳转
  295. 'return_url' => "",
  296. //编码格式
  297. 'charset' => "UTF-8",
  298. //签名方式
  299. 'sign_type' => "RSA2",
  300. //支付宝网关
  301. 'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
  302. //支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
  303. 'alipay_public_key' => $aliPublicKey,
  304. ];
  305. require_once($aliWap . '/wappay/service/AlipayTradeService.php');
  306. require_once($aliWap . '/wappay/buildermodel/AlipayTradeQueryContentBuilder.php');//查询接口
  307. $arr = $_POST;
  308. $aliService = new \AlipayTradeService($config);
  309. $aliService->writeLog(var_export($_POST, true));
  310. $result = $aliService->check($arr);
  311. Yii::info('check:' . $result);
  312. if ($result == false) {
  313. //请不要修改
  314. echo "fail";
  315. util::end();
  316. }
  317. //验证成功后续
  318. $tradeNo = $_POST['trade_no'];//支付宝交易号
  319. $tradeStatus = $_POST['trade_status'];//交易状态
  320. $totalFee = $_POST['total_amount'];
  321. $appId = $_POST['app_id'];
  322. $attach = $_POST['passback_params'];
  323. $attach = urldecode($attach);
  324. //接收流水类型、代金劵
  325. parse_str($attach);
  326. //流水类型
  327. $capitalType = isset($capitalType) ? $capitalType : null;
  328. $RequestBuilder = new \AlipayTradeQueryContentBuilder();
  329. $RequestBuilder->setTradeNo($tradeNo);
  330. //$queryResult = $aliService->Query($RequestBuilder);
  331. //$aliId = $queryResult->buyer_user_id;
  332. //买家的支付宝帐号,不完整,带*号
  333. //$aliAccount = $queryResult->buyer_logon_id;
  334. if (isset($capitalType) == false) {
  335. Yii::warning('capitalType empty, orderSn:' . $orderSn);
  336. util::end();
  337. }
  338. if ($config['app_id'] != $appId) {
  339. Yii::warning('app_id disagree, orderSn:' . $orderSn . ' config app_id:' . $config['app_id'] . ' appId:' . $appId);
  340. util::end();
  341. }
  342. $connection = Yii::$app->db;
  343. $transaction = $connection->beginTransaction();
  344. try {
  345. if ($tradeStatus == 'TRADE_FINISHED' || $tradeStatus == 'TRADE_SUCCESS') {
  346. $aliPay = dict::getDict('payWay', 'alipay');
  347. //支付成功后续流程
  348. payUtil::thirdPay($aliPay, $capitalType, $orderSn, $totalFee, $attach);
  349. $transaction->commit();
  350. //打印小票和语音播报
  351. $order = OrderClass::getByCondition(['orderSn' => $orderSn], true);
  352. ShopExtClass::hdGatheringReport($order);
  353. OrderClass::onlinePrint($order);
  354. $shopId = $order->shopId ?? 0;
  355. $shop = ShopClass::getById($shopId, true);
  356. if (in_array($order->fromType, [dict::getDict("fromType", "shop"), dict::getDict("fromType", "friend")])) {
  357. //您有新的收入提醒
  358. WxMessageClass::gatheringIncomeInform($shop, $order);
  359. }
  360. if ($order->fromType == dict::getDict("fromType", "mall")) {
  361. //来自商城的新订单微信通知
  362. WxMessageClass::hdNewOrderInform($shop, $order);
  363. }
  364. }
  365. //请不要修改
  366. echo "success";
  367. } catch (\Exception $e) {
  368. $transaction->rollBack();
  369. Yii::error("失败原因:" . $e->getMessage());
  370. }
  371. }
  372. //收银台支付回调
  373. public function actionCashPayCallback()
  374. {
  375. header("Content-type: text/html; charset=utf-8");
  376. Yii::$app->params['ptStyle'] = dict::getDict('ptStyle', 'mall');
  377. $postStr = file_get_contents('php://input');//接收微信服务器返回的xml消息体
  378. if (empty($postStr)) {
  379. util::end();
  380. }
  381. $headers = Yii::$app->getRequest()->getHeaders();
  382. $Authorization = $headers->get('Authorization');
  383. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  384. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  385. $params = [
  386. 'appid' => 'OP00002119',
  387. 'serial_no' => '018b08cfddbd',
  388. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  389. 'lklCertificatePath' => $lklCertificatePath,
  390. ];
  391. $laResource = new Lakala($params);
  392. $return = $laResource->signatureVerification($Authorization, $postStr);
  393. $postData = json_decode($postStr, true);
  394. $orderSn = $postData['out_order_no'] ?? '';
  395. $orderTradeInfo = $postData['order_trade_info'] ?? [];
  396. $payMode = $orderTradeInfo['pay_mode'] ?? null;
  397. if (empty($payMode)) {
  398. noticeUtil::push('收银台支付回调失败了,没有找到钱包类型,请注意,orderSn:' . $orderSn, '15280215347');
  399. util::end();
  400. }
  401. $thirdLogNo = $orderTradeInfo['log_no'] ?? '';
  402. $trade_amount = $orderTradeInfo['trade_amount'] ?? 0;
  403. $totalFee = $trade_amount / 100;
  404. if ($totalFee <= 0) {
  405. noticeUtil::push('收银台支付回调验证失败了,金额错误,请注意,orderSn:' . $orderSn, '15280215347');
  406. util::end();
  407. }
  408. $transactionId = $orderTradeInfo['trade_no'] ?? 0;
  409. if ($return == false) {
  410. noticeUtil::push('收银台支付回调验证失败了,请注意,orderSn:' . $orderSn, '15280215347');
  411. util::end();
  412. }
  413. $orderStatus = $postData['order_status'];
  414. $tradeType = $orderTradeInfo['trade_type'] ?? '';
  415. if ($tradeType != 'PAY') {
  416. noticeUtil::push('收银台支付回调验证失败了,不是消费的回调,orderSn:' . $orderSn, '15280215347');
  417. util::end();
  418. }
  419. //接收流水类型、代金劵等
  420. $remarkString = $orderTradeInfo['trade_remark'] ?? '';
  421. $remarkData = json_decode($remarkString, true);
  422. $capitalType = $remarkData['capitalType'] ?? null;
  423. if (!isset($capitalType)) {
  424. $msg = '支付回调失败了,没有找到capitalType,orderSn:' . $orderSn . ' remark:' . $remarkString;
  425. noticeUtil::push($msg, '15280215347');
  426. util::end();
  427. }
  428. $connection = Yii::$app->db;
  429. $transaction = $connection->beginTransaction();
  430. try {
  431. if ($orderStatus == 2) {
  432. $payWayType = null;
  433. if ($payMode == 'ALIPAY') {
  434. $payWayType = dict::getDict('payWay', 'alipay');
  435. }
  436. if ($payMode == 'WECHAT') {
  437. $payWayType = dict::getDict('wxPay', 'alipay');
  438. }
  439. if (isset($payWayType) == false) {
  440. noticeUtil::push('收银台支付回调验证失败了,没有找到支付方式,orderSn:' . $orderSn, '15280215347');
  441. util::end();
  442. }
  443. $attach = '';
  444. payUtil::thirdPay($payWayType, $capitalType, $orderSn, $totalFee, $attach, $transactionId);
  445. $transaction->commit();
  446. if ($capitalType == dict::getDict('capitalType', 'xhOrder', 'id')) {
  447. //避免重复
  448. $cacheKey = 'mall_order_pay_' . $orderSn;
  449. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  450. if (empty($has)) {
  451. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 864000, 'has']);
  452. //打印小票和语音播报
  453. $order = OrderClass::getByCondition(['orderSn' => $orderSn], true);
  454. if (!empty($order)) {
  455. $order->thirdLogNo = $thirdLogNo;
  456. $order->save();
  457. OrderClass::onlinePrint($order);
  458. //付款成功了,通知发跑腿功能,关键词 hd_pay_after_call_pt,多处要同步修改
  459. HdDeliveryOrderClass::payAfter($order);
  460. $shopId = $order->shopId ?? 0;
  461. $shop = ShopClass::getById($shopId, true);
  462. //来自商城的新订单微信通知
  463. WxMessageClass::hdNewOrderInform($shop, $order);
  464. }
  465. }
  466. }
  467. if ($capitalType == dict::getDict('capitalType', 'scanPay', 'id')) {
  468. //避免重复
  469. $myKey = 'mall_order_scan_pay_' . $orderSn;
  470. $has = Yii::$app->redis->executeCommand('GET', [$myKey]);
  471. if (empty($has)) {
  472. Yii::$app->redis->executeCommand('SETEX', [$myKey, 864000, 'has']);
  473. $scan = ScanPayClass::getByCondition(['orderSn' => $orderSn], true);
  474. //微信通知
  475. $shopId = $scan->shopId ?? 0;
  476. $shop = ShopClass::getById($shopId, true);
  477. WxMessageClass::hdScanPayInform($shop, $scan);
  478. ShopExtClass::hdScanPayReport($scan);
  479. }
  480. }
  481. }
  482. //请不要修改
  483. echo "SUCCESS";
  484. } catch (\Exception $e) {
  485. $transaction->rollBack();
  486. Yii::error("失败原因:" . $e->getMessage());
  487. }
  488. }
  489. }