PurchaseClearController.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace hd\controllers;
  3. use biz\ghs\classes\GhsClass;
  4. use bizHd\purchase\classes\PurchaseClearClass;
  5. use bizHd\shop\classes\ShopClass;
  6. use bizHd\wx\classes\WxOpenClass;
  7. use common\components\dict;
  8. use common\components\noticeUtil;
  9. use common\components\orderSn;
  10. use Yii;
  11. use common\components\util;
  12. use common\components\lakala\Lakala;
  13. class PurchaseClearController extends BaseController
  14. {
  15. public $guestAccess = ['wx-pay', 'create-order'];
  16. //生成结帐订单 ssh 2021.1.26
  17. public function actionCreateOrder()
  18. {
  19. $post = Yii::$app->request->post();
  20. $ghsId = $post['ghsId'] ?? 0;
  21. $ghs = GhsClass::getById($ghsId, true);
  22. //解决重复请求问题
  23. $staffId = $this->shopAdminId ?? 0;
  24. $cacheKey = 'hd_clear_order_pay_' . $ghsId . '_' . $staffId;
  25. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  26. if (!empty($has)) {
  27. util::fail('请稍等5秒再操作');
  28. }
  29. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 5, 'has']);
  30. //老的没有清的结账单直接过期掉
  31. $list = PurchaseClearClass::getAllByCondition(['ghsId' => $ghsId, 'status' => PurchaseClearClass::STATUS_AWAIT_PAY], null, '*', null, true);
  32. if (!empty($list)) {
  33. foreach ($list as $item) {
  34. $item->status = PurchaseClearClass::STATUS_EXPIRE;
  35. $item->save();
  36. }
  37. }
  38. $post['ghsShopId'] = $ghs->shopId ?? 0;
  39. $post['sjId'] = $this->sjId;
  40. $post['shopId'] = $this->shopId;
  41. $post['clearStyle'] = dict::getDict('clearStyle', 'hd2Gys');
  42. $post['customShopAdminId'] = $this->shopAdminId ?? 0;
  43. $post['customShopId'] = $this->shopId ?? 0;
  44. $shopAdmin = $this->shopAdmin;
  45. $shopAdminName = $shopAdmin['name'] ?? '';
  46. $post['customShopAdminName'] = $shopAdminName;
  47. $respond = PurchaseClearClass::addOrder($post, $ghs);
  48. //前端显示的付款倒计时
  49. $hasTime = dict::getDict('order_pay_has_time');
  50. $aheadTime = dict::getDict('order_online_pay_has_ahead_time');
  51. $remainSecond = $hasTime - $aheadTime;
  52. $respond['remainSecond'] = $remainSecond;
  53. util::success($respond);
  54. }
  55. public function actionWxPay()
  56. {
  57. ini_set('date.timezone', 'Asia/Shanghai');
  58. $post = Yii::$app->request->post();
  59. $couponId = $post['couponId'] ?? 0;
  60. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  61. $order = PurchaseClearClass::getByCondition(['orderSn' => $orderSn], true);
  62. if (empty($order)) {
  63. util::fail('账单号无效');
  64. }
  65. if ($order->status == PurchaseClearClass::STATUS_HAS_PAY) {
  66. util::fail('账单已结清了');
  67. }
  68. if ($order->status == PurchaseClearClass::STATUS_EXPIRE) {
  69. util::fail('账单已取消了');
  70. }
  71. $oldOrderSn = $orderSn;
  72. $current = time();
  73. $id = $order->id;
  74. $deadline = $order->deadline;
  75. if ($current > strtotime($deadline)) {
  76. $order->status = PurchaseClearClass::STATUS_EXPIRE;
  77. $order->save();
  78. util::fail('订单已经失效,请重新发起结算');
  79. }
  80. $name = '采购单结算';
  81. $totalFee = $order->actPrice;
  82. //强制使用小程序的miniOpenId
  83. $openId = isset($this->admin) && !empty($this->admin) ? $this->admin->miniOpenId : '';
  84. if (empty($openId)) {
  85. if (isset($post['currentMiniOpenId']) && !empty($post['currentMiniOpenId'])) {
  86. $openId = $post['currentMiniOpenId'];
  87. }
  88. }
  89. if (empty($openId)) {
  90. util::fail('没有openId');
  91. }
  92. $capitalType = dict::getDict('capitalType', 'hdPurchaseClear', 'id');
  93. $ghsShopId = $order->ghsShopId ?? 0;
  94. $ghsShop = ShopClass::getById($ghsShopId, true);
  95. if (empty($ghsShop)) {
  96. util::fail('没有供货商门店信息');
  97. }
  98. if (isset($ghsShop->lklSjNo) && empty($ghsShop->lklSjNo)) {
  99. util::fail('商家支付功能冻结');
  100. }
  101. //已经唤起过微信支付了,根据拉卡拉的规则,需要生成新的订单号
  102. if ($order->changePrice == 2) {
  103. $newOrderSn = orderSn::getPurchaseClearSn();
  104. $orderSn = $newOrderSn;
  105. $order->orderSn = $newOrderSn;
  106. $order->save();
  107. //老单号需要关单,否则有付款成功的风险
  108. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  109. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  110. $params = [
  111. 'appid' => 'OP00002119',
  112. 'serial_no' => '018b08cfddbd',
  113. 'merchant_no' => $ghsShop->lklSjNo,
  114. 'term_no' => $ghsShop->lklScanTermNo,
  115. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  116. 'lklCertificatePath' => $lklCertificatePath,
  117. ];
  118. $laResource = new Lakala($params);
  119. $closeParams = [
  120. 'orderSn' => $oldOrderSn,
  121. ];
  122. $response = $laResource->close($closeParams);
  123. if (isset($response['code']) == false || $response['code'] != 'BBS00000') {
  124. $msg = $response['msg'] ?? '';
  125. noticeUtil::push('花店结账,单号:' . $oldOrderSn . ',关单没有成功,' . $msg, '15280215347');
  126. } else {
  127. noticeUtil::push('花店结账,单号:' . $oldOrderSn . ',关单成功了哦', '15280215347');
  128. }
  129. }
  130. $order->changePrice = 2;
  131. $order->save();
  132. //花卉宝代为申请的微信支付
  133. $merchantExtend = WxOpenClass::getWxInfo();
  134. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  135. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  136. $params = [
  137. 'appid' => 'OP00002119',
  138. 'serial_no' => '018b08cfddbd',
  139. 'merchant_no' => $ghsShop->lklSjNo,
  140. 'term_no' => $ghsShop->lklScanTermNo,
  141. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  142. 'lklCertificatePath' => $lklCertificatePath,
  143. ];
  144. $laResource = new Lakala($params);
  145. $notifyUrl = Yii::$app->params['hdHost'] . "/notice/pay-callback";
  146. $wxParams = [
  147. 'orderSn' => $orderSn,
  148. 'amount' => $totalFee,
  149. 'capitalType' => $capitalType,
  150. 'notifyUrl' => $notifyUrl,
  151. 'wxAppId' => $merchantExtend['miniAppId'],
  152. 'openId' => $openId,
  153. 'subject' => $name,
  154. 'couponId' => $couponId,
  155. ];
  156. $response = $laResource->driveWxPay($wxParams);
  157. if (isset($response['code']) == false || $response['code'] != 'BBS00000') {
  158. $errMsg = $response['msg'] ?? '';
  159. noticeUtil::push('结账付款失败,原因:' . $errMsg, '15280215347');
  160. util::fail('结账付款失败');
  161. }
  162. $newParams = isset($response['resp_data']['acc_resp_fields']) ? $response['resp_data']['acc_resp_fields'] : [];
  163. $appId = $newParams['app_id'] ?? '';
  164. $nonceStr = $newParams['nonce_str'] ?? '';
  165. $paySign = $newParams['pay_sign'] ?? '';
  166. $package = $newParams['package'] ?? '';
  167. $signType = $newParams['sign_type'] ?? '';
  168. $timeStamp = $newParams['time_stamp'] ?? '';
  169. $new = [
  170. 'id' => $id,
  171. 'appId' => $appId,
  172. 'nonceStr' => $nonceStr,
  173. 'paySign' => $paySign,
  174. 'package' => $package,
  175. 'signType' => $signType,
  176. 'timeStamp' => $timeStamp,
  177. 'orderSn' => $orderSn,
  178. ];
  179. util::success($new);
  180. }
  181. }