NoticeController.php 24 KB

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