NoticeController.php 27 KB

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