NoticeController.php 27 KB

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