PurchaseClearController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. <?php
  2. namespace hd\controllers;
  3. use biz\ghs\classes\GhsClass;
  4. use bizGhs\custom\classes\AccountMoneyClass;
  5. use bizHd\purchase\classes\OrderCgClearClass;
  6. use bizHd\purchase\classes\PurchaseClearClass;
  7. use bizHd\shop\classes\ShopClass;
  8. use bizHd\wx\classes\WxOpenClass;
  9. use common\components\dict;
  10. use common\components\noticeUtil;
  11. use common\components\orderSn;
  12. use Yii;
  13. use common\components\util;
  14. use common\components\lakala\Lakala;
  15. class PurchaseClearController extends BaseController
  16. {
  17. public $guestAccess = ['wx-pay', 'create-order', 'ali-pay'];
  18. //生成结帐订单 ssh 2021.1.26
  19. public function actionCreateOrder()
  20. {
  21. //util::fail('请下午2点后再试');
  22. $transaction = Yii::$app->db->beginTransaction();
  23. try {
  24. $post = Yii::$app->request->post();
  25. $ghsId = $post['ghsId'] ?? 0;
  26. $ghs = GhsClass::getLockById($ghsId);
  27. if (empty($ghs)) {
  28. util::fail('没有供货商');
  29. }
  30. //解决重复请求问题
  31. $staffId = $this->shopAdminId ?? 0;
  32. $cacheKey = 'hd_clear_order_pay_' . $ghsId . '_' . $staffId;
  33. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  34. if (!empty($has)) {
  35. util::fail('请稍等5秒再操作');
  36. }
  37. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 5, 'has']);
  38. //检查余额是否有问题
  39. //\bizHd\ghs\classes\GhsClass::checkBalance($ghs);
  40. //老的没有清的结账单直接过期掉
  41. $list = PurchaseClearClass::getAllByCondition(['ghsId' => $ghsId, 'status' => PurchaseClearClass::STATUS_AWAIT_PAY], null, '*', null, true);
  42. if (!empty($list)) {
  43. $ghsShopId = $ghs->shopId ?? 0;
  44. $ghsShop = ShopClass::getById($ghsShopId, true);
  45. foreach ($list as $item) {
  46. $item->status = PurchaseClearClass::STATUS_EXPIRE;
  47. $item->save();
  48. OrderCgClearClass::markExpireByClearId($item->id);
  49. if (!empty($ghsShop)) {
  50. $orderSn = $item->orderSn ?? '';
  51. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  52. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  53. $params = [
  54. 'appid' => 'OP00002119',
  55. 'serial_no' => '018b08cfddbd',
  56. 'merchant_no' => $ghsShop->lklSjNo,
  57. 'term_no' => $ghsShop->lklScanTermNo,
  58. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  59. 'lklCertificatePath' => $lklCertificatePath,
  60. ];
  61. $laResource = new Lakala($params);
  62. $closeParams = [
  63. 'orderSn' => $orderSn,
  64. ];
  65. $response = $laResource->close($closeParams);
  66. if (isset($response['code']) == false || $response['code'] != 'BBS00000') {
  67. //$msg = $response['msg'] ?? '';
  68. //noticeUtil::push("\n花店结账单:" . $orderSn . "\n关单失败了\n{$msg} ", '15280215347');
  69. } else {
  70. //noticeUtil::push("\n花店结账单:" . $orderSn . "\n关单成功", '15280215347');
  71. }
  72. }
  73. }
  74. }
  75. $post['ghsShopId'] = $ghs->shopId ?? 0;
  76. $post['sjId'] = $this->sjId;
  77. $post['shopId'] = $this->shopId;
  78. $post['clearStyle'] = dict::getDict('clearStyle', 'hd2Gys');
  79. $post['customShopAdminId'] = $this->shopAdminId ?? 0;
  80. $post['customShopId'] = $this->shopId ?? 0;
  81. $shopAdmin = $this->shopAdmin;
  82. $shopAdminName = $shopAdmin['name'] ?? '';
  83. $post['customShopAdminName'] = $shopAdminName;
  84. $respond = PurchaseClearClass::addOrder($post, $ghs);
  85. //前端显示的付款倒计时
  86. $hasTime = dict::getDict('order_pay_has_time');
  87. $aheadTime = dict::getDict('order_online_pay_has_ahead_time');
  88. $remainSecond = $hasTime - $aheadTime;
  89. $respond['remainSecond'] = $remainSecond;
  90. $getPayType = dict::getDict('getPayType');
  91. $respond['getPayType'] = $getPayType;
  92. //如果是虚拟关系,暂时允许花店一键结账
  93. $live = $ghs->live ?? 1;
  94. $respond['live'] = $live;
  95. $transaction->commit();
  96. util::success($respond);
  97. } catch (\Exception $e) {
  98. $transaction->rollBack();
  99. util::fail('创建结账单失败:' . $e->getMessage());
  100. noticeUtil::push('创建结账单失败:' . $e->getMessage(), '15280215347');
  101. }
  102. }
  103. public function actionWxPay()
  104. {
  105. ini_set('date.timezone', 'Asia/Shanghai');
  106. $transaction = Yii::$app->db->beginTransaction();
  107. try {
  108. $post = Yii::$app->request->post();
  109. $couponId = $post['couponId'] ?? 0;
  110. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  111. $order = PurchaseClearClass::getByCondition(['orderSn' => $orderSn], true);
  112. if (empty($order)) {
  113. util::fail('账单号无效');
  114. }
  115. if ($order->status == PurchaseClearClass::STATUS_HAS_PAY) {
  116. util::fail('账单已结清');
  117. }
  118. if ($order->status == PurchaseClearClass::STATUS_EXPIRE) {
  119. util::fail('账单已取消');
  120. }
  121. //避免重复提交
  122. util::checkRepeatCommit($orderSn, 8);
  123. $oldOrderSn = $orderSn;
  124. $current = time();
  125. $id = $order->id;
  126. $deadline = $order->deadline;
  127. if ($current > strtotime($deadline)) {
  128. $order->status = PurchaseClearClass::STATUS_EXPIRE;
  129. $order->save();
  130. util::fail('订单已经失效,请重新发起结算');
  131. }
  132. $totalFee = $order->actPrice;
  133. $openId = '';
  134. if (isset($post['currentMiniOpenId']) && !empty($post['currentMiniOpenId'])) {
  135. $openId = $post['currentMiniOpenId'];
  136. //noticeUtil::push('花店结账时,通过前端获取了openId:' . $openId, '15280215347');
  137. }
  138. if (empty($openId)) {
  139. if (isset($this->admin) && !empty($this->admin)) {
  140. if (isset($this->admin->miniOpenId) && !empty($this->admin->miniOpenId)) {
  141. $openId = $this->admin->miniOpenId;
  142. noticeUtil::push('花店结账时,通过数据库获取了openId:' . $openId, '15280215347');
  143. }
  144. }
  145. }
  146. if (empty($openId)) {
  147. util::fail('没有找到openId');
  148. }
  149. $capitalType = dict::getDict('capitalType', 'hdPurchaseClear', 'id');
  150. $ghsShopId = $order->ghsShopId ?? 0;
  151. $ghsShop = ShopClass::getById($ghsShopId, true);
  152. if (empty($ghsShop)) {
  153. util::fail('没有供货商门店信息');
  154. }
  155. if (isset($ghsShop->lklSjNo) && empty($ghsShop->lklSjNo)) {
  156. util::fail('商家支付功能未开通');
  157. }
  158. //已经唤起过微信支付了,根据拉卡拉的规则,需要生成新的订单号
  159. if ($order->changePrice == 2) {
  160. //老单号需要关单,否则有付款成功的风险,老单号关单成功了,才能生成新单号
  161. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  162. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  163. $params = [
  164. 'appid' => 'OP00002119',
  165. 'serial_no' => '018b08cfddbd',
  166. 'merchant_no' => $ghsShop->lklSjNo,
  167. 'term_no' => $ghsShop->lklScanTermNo,
  168. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  169. 'lklCertificatePath' => $lklCertificatePath,
  170. ];
  171. $laResource = new Lakala($params);
  172. $closeParams = [
  173. 'orderSn' => $oldOrderSn,
  174. ];
  175. $response = $laResource->close($closeParams);
  176. if (!isset($response['code']) || $response['code'] != 'BBS00000') {
  177. $msg = $response['msg'] ?? '';
  178. $code = $response['code'] ?? 0;
  179. $customMobile = $order->customMobile ?? '';
  180. $ghsName = $order->ghsName ?? '';
  181. //noticeUtil::push('第一个,花店结账,单号:' . $oldOrderSn . ',关单没有成功,原因:【' . $msg . '】,' . $code . ',客户手机号:' . $customMobile . ',供货商:' . $ghsName, '15280215347');
  182. }
  183. //收银台关单
  184. $closeParams = ['orderSn' => $orderSn];
  185. $response = $laResource->cashClose($closeParams);
  186. if (!isset($response['code']) || $response['code'] != '000000') {
  187. $msg = $response['msg'] ?? '';
  188. $code = $response['code'] ?? 0;
  189. //noticeUtil::push('第二个,花店结账,单号:' . $oldOrderSn . ',收银台关单没有成功,原因:【' . $msg . '】,' . $code, '15280215347');
  190. }
  191. $newOrderSn = orderSn::getPurchaseClearSn();
  192. $orderSn = $newOrderSn;
  193. $order->orderSn = $newOrderSn;
  194. $order->save();
  195. OrderCgClearClass::updateClearSnByClearId($id, $newOrderSn);
  196. // 将老单号与订单 ID 的映射存入 Redis,有效期 24 小时 (86400秒)
  197. $redisKey = 'hd_clear_old_sn_map_' . $oldOrderSn;
  198. Yii::$app->redis->executeCommand('SETEX', [$redisKey, 86400, $id]);
  199. }
  200. $order->changePrice = 2;
  201. $order->save();
  202. //花卉宝代为申请的微信支付
  203. $merchantExtend = WxOpenClass::getWxInfo();
  204. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  205. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  206. $params = [
  207. 'appid' => 'OP00002119',
  208. 'serial_no' => '018b08cfddbd',
  209. 'merchant_no' => $ghsShop->lklSjNo,
  210. 'term_no' => $ghsShop->lklScanTermNo,
  211. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  212. 'lklCertificatePath' => $lklCertificatePath,
  213. ];
  214. $laResource = new Lakala($params);
  215. $notifyUrl = Yii::$app->params['hdHost'] . "/notice/pay-callback";
  216. $name = '采购单结算 ' . $orderSn;
  217. $wxParams = [
  218. 'orderSn' => $orderSn,
  219. 'amount' => $totalFee,
  220. 'capitalType' => $capitalType,
  221. 'notifyUrl' => $notifyUrl,
  222. 'wxAppId' => $merchantExtend['miniAppId'],
  223. 'openId' => $openId,
  224. 'subject' => $name,
  225. 'couponId' => $couponId,
  226. ];
  227. $response = $laResource->driveWxPay($wxParams);
  228. if (isset($response['code']) == false || $response['code'] != 'BBS00000') {
  229. $errMsg = $response['msg'] ?? '';
  230. $ghsName = $order->ghsName ?? '';
  231. $ghsShopName = $order->ghsShopName ?? '';
  232. $ghsFullName = $ghsName . $ghsShopName;
  233. $customName = $order->customName ?? '';
  234. $customFullName = $customName;
  235. noticeUtil::push("\n提示:结账付款失败\n原因:" . $errMsg . "。\n商家:" . $ghsFullName . "\n客户:" . $customFullName, '15280215347');
  236. util::fail('结账付款失败,' . $errMsg . ',请联系门店');
  237. }
  238. $newParams = isset($response['resp_data']['acc_resp_fields']) ? $response['resp_data']['acc_resp_fields'] : [];
  239. $appId = $newParams['app_id'] ?? '';
  240. $nonceStr = $newParams['nonce_str'] ?? '';
  241. $paySign = $newParams['pay_sign'] ?? '';
  242. $package = $newParams['package'] ?? '';
  243. $signType = $newParams['sign_type'] ?? '';
  244. $timeStamp = $newParams['time_stamp'] ?? '';
  245. $new = [
  246. 'id' => $id,
  247. 'appId' => $appId,
  248. 'nonceStr' => $nonceStr,
  249. 'paySign' => $paySign,
  250. 'package' => $package,
  251. 'signType' => $signType,
  252. 'timeStamp' => $timeStamp,
  253. 'orderSn' => $orderSn,
  254. ];
  255. $transaction->commit();
  256. util::success($new);
  257. } catch (\Exception $e) {
  258. $transaction->rollBack();
  259. util::fail('发起微信支付失败:' . $e->getMessage());
  260. noticeUtil::push('结账发起微信支付失败:' . $e->getMessage(), '15280215347');
  261. }
  262. }
  263. public function actionAliPay()
  264. {
  265. ini_set('date.timezone', 'Asia/Shanghai');
  266. $transaction = Yii::$app->db->beginTransaction();
  267. try {
  268. $post = Yii::$app->request->post();
  269. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  270. $order = PurchaseClearClass::getByCondition(['orderSn' => $orderSn], true);
  271. if (empty($order)) {
  272. util::fail('账单号无效');
  273. }
  274. if ($order->status == PurchaseClearClass::STATUS_HAS_PAY) {
  275. util::fail('账单已结清!');
  276. }
  277. if ($order->status == PurchaseClearClass::STATUS_EXPIRE) {
  278. util::fail('账单已取消!');
  279. }
  280. $oldOrderSn = $orderSn;
  281. $current = time();
  282. $deadline = $order->deadline;
  283. if ($current > strtotime($deadline)) {
  284. $order->status = PurchaseClearClass::STATUS_EXPIRE;
  285. $order->save();
  286. util::fail('订单已经失效,请重新发起结算');
  287. }
  288. $totalFee = $order->actPrice;
  289. //避免重复提交
  290. util::checkRepeatCommit($orderSn, 8);
  291. $capitalType = dict::getDict('capitalType', 'hdPurchaseClear', 'id');
  292. $ghsShopId = $order->ghsShopId ?? 0;
  293. $ghsShop = ShopClass::getById($ghsShopId, true);
  294. if (empty($ghsShop)) {
  295. util::fail('没有供货商门店信息');
  296. }
  297. if (isset($ghsShop->lklSjNo) && empty($ghsShop->lklSjNo)) {
  298. util::fail('商家支付功能未开通');
  299. }
  300. //已经唤起支付,根据拉卡拉的规则,需要生成新的订单号
  301. if ($order->changePrice == 2) {
  302. //老单号需要关单,否则有付款成功的风险,老单号关单成功了,才能生成新单号
  303. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  304. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  305. $params = [
  306. 'appid' => 'OP00002119',
  307. 'serial_no' => '018b08cfddbd',
  308. 'merchant_no' => $ghsShop->lklSjNo,
  309. 'term_no' => $ghsShop->lklScanTermNo,
  310. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  311. 'lklCertificatePath' => $lklCertificatePath,
  312. ];
  313. $laResource = new Lakala($params);
  314. $closeParams = [
  315. 'orderSn' => $oldOrderSn,
  316. ];
  317. $response = $laResource->cashClose($closeParams);
  318. if (isset($response['code']) == false || $response['code'] != '000000') {
  319. $msg = $response['msg'] ?? '';
  320. $code = $response['code'] ?? 0;
  321. if (!in_array($code, ['000091'])) {
  322. noticeUtil::push("花店结账模块 " . $oldOrderSn . " 关单没有成功 收银台-支付宝模块 " . $msg, '15280215347');
  323. }
  324. }
  325. $newOrderSn = orderSn::getPurchaseClearSn();
  326. $orderSn = $newOrderSn;
  327. $order->orderSn = $newOrderSn;
  328. $order->save();
  329. OrderCgClearClass::updateClearSnByClearId($order->id, $newOrderSn);
  330. // 将老单号与订单 ID 的映射存入 Redis,有效期 24 小时 (86400秒)
  331. $redisKey = 'hd_clear_old_sn_map_' . $oldOrderSn;
  332. Yii::$app->redis->executeCommand('SETEX', [$redisKey, 86400, $order->id]);
  333. }
  334. $order->changePrice = 2;
  335. $order->save();
  336. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  337. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  338. $params = [
  339. 'appid' => 'OP00002119',
  340. 'serial_no' => '018b08cfddbd',
  341. 'merchant_no' => $ghsShop->lklSjNo,
  342. 'term_no' => $ghsShop->lklScanTermNo,
  343. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  344. 'lklCertificatePath' => $lklCertificatePath,
  345. ];
  346. $laResource = new Lakala($params);
  347. $notifyUrl = Yii::$app->params['hdHost'] . "/notice/cash-pay-callback";
  348. $wxParams = [
  349. 'orderSn' => $orderSn,
  350. 'amount' => $totalFee,
  351. 'capitalType' => $capitalType,
  352. 'notifyUrl' => $notifyUrl,
  353. 'subject' => $orderSn,
  354. ];
  355. $response = $laResource->cashierPay($wxParams);
  356. if (!isset($response['code']) || $response['code'] != '000000') {
  357. $errMsg = $response['msg'] ?? '';
  358. $ghsName = $order->ghsName ?? '';
  359. $ghsShopName = $order->ghsShopName ?? '';
  360. $ghsFullName = $ghsName . $ghsShopName;
  361. $customName = $order->customName ?? '';
  362. $customFullName = $customName;
  363. noticeUtil::push("\n提示:结账付款失败\n原因:" . $errMsg . "。\n商家:" . $ghsFullName . "\n客户:" . $customFullName, '15280215347');
  364. util::fail('结账付款失败,' . $errMsg . ',请联系门店');
  365. }
  366. $payUrl = $response['resp_data']['counter_url'] ? $response['resp_data']['counter_url'] : '';
  367. $hasRenew = $ghsShop->hasRenew ?? 0;
  368. $new = [
  369. 'payUrl' => $payUrl,
  370. 'orderSn' => $orderSn,
  371. 'hasRenew' => 1,
  372. ];
  373. $transaction->commit();
  374. util::success($new);
  375. } catch (\Exception $e) {
  376. $transaction->rollBack();
  377. noticeUtil::push('结账发起支付宝支付失败:' . $e->getMessage(), '15280215347');
  378. }
  379. }
  380. }