PurchaseClearController.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. $getPayType = dict::getDict('getPayType');
  54. $respond['getPayType'] = $getPayType;
  55. util::success($respond);
  56. }
  57. public function actionWxPay()
  58. {
  59. ini_set('date.timezone', 'Asia/Shanghai');
  60. $post = Yii::$app->request->post();
  61. $couponId = $post['couponId'] ?? 0;
  62. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  63. $order = PurchaseClearClass::getByCondition(['orderSn' => $orderSn], true);
  64. if (empty($order)) {
  65. util::fail('账单号无效');
  66. }
  67. if ($order->status == PurchaseClearClass::STATUS_HAS_PAY) {
  68. util::fail('账单已结清了');
  69. }
  70. if ($order->status == PurchaseClearClass::STATUS_EXPIRE) {
  71. util::fail('账单已取消了');
  72. }
  73. $oldOrderSn = $orderSn;
  74. $current = time();
  75. $id = $order->id;
  76. $deadline = $order->deadline;
  77. if ($current > strtotime($deadline)) {
  78. $order->status = PurchaseClearClass::STATUS_EXPIRE;
  79. $order->save();
  80. util::fail('订单已经失效,请重新发起结算');
  81. }
  82. $name = '采购单结算';
  83. $totalFee = $order->actPrice;
  84. $openId = '';
  85. if (isset($post['currentMiniOpenId']) && !empty($post['currentMiniOpenId'])) {
  86. $openId = $post['currentMiniOpenId'];
  87. //noticeUtil::push('花店结账时,通过前端获取了openId:' . $openId, '15280215347');
  88. }
  89. if (empty($openId)) {
  90. if (isset($this->admin) && !empty($this->admin)) {
  91. if (isset($this->admin->miniOpenId) && !empty($this->admin->miniOpenId)) {
  92. $openId = $this->admin->miniOpenId;
  93. noticeUtil::push('花店结账时,通过数据库获取了openId:' . $openId, '15280215347');
  94. }
  95. }
  96. }
  97. if (empty($openId)) {
  98. util::fail('没有找到openId');
  99. }
  100. $capitalType = dict::getDict('capitalType', 'hdPurchaseClear', 'id');
  101. $ghsShopId = $order->ghsShopId ?? 0;
  102. $ghsShop = ShopClass::getById($ghsShopId, true);
  103. if (empty($ghsShop)) {
  104. util::fail('没有供货商门店信息');
  105. }
  106. if (isset($ghsShop->lklSjNo) && empty($ghsShop->lklSjNo)) {
  107. util::fail('商家支付功能冻结');
  108. }
  109. //已经唤起过微信支付了,根据拉卡拉的规则,需要生成新的订单号
  110. if ($order->changePrice == 2) {
  111. $newOrderSn = orderSn::getPurchaseClearSn();
  112. $orderSn = $newOrderSn;
  113. $order->orderSn = $newOrderSn;
  114. $order->save();
  115. //老单号需要关单,否则有付款成功的风险
  116. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  117. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  118. $params = [
  119. 'appid' => 'OP00002119',
  120. 'serial_no' => '018b08cfddbd',
  121. 'merchant_no' => $ghsShop->lklSjNo,
  122. 'term_no' => $ghsShop->lklScanTermNo,
  123. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  124. 'lklCertificatePath' => $lklCertificatePath,
  125. ];
  126. $laResource = new Lakala($params);
  127. $closeParams = [
  128. 'orderSn' => $oldOrderSn,
  129. ];
  130. $response = $laResource->close($closeParams);
  131. if (isset($response['code']) == false || $response['code'] != 'BBS00000') {
  132. $msg = $response['msg'] ?? '';
  133. noticeUtil::push('花店结账,单号:' . $oldOrderSn . ',关单没有成功,' . $msg, '15280215347');
  134. } else {
  135. //noticeUtil::push('花店结账,单号:' . $oldOrderSn . ',关单成功了哦', '15280215347');
  136. }
  137. }
  138. $order->changePrice = 2;
  139. $order->save();
  140. //花卉宝代为申请的微信支付
  141. $merchantExtend = WxOpenClass::getWxInfo();
  142. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  143. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  144. $params = [
  145. 'appid' => 'OP00002119',
  146. 'serial_no' => '018b08cfddbd',
  147. 'merchant_no' => $ghsShop->lklSjNo,
  148. 'term_no' => $ghsShop->lklScanTermNo,
  149. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  150. 'lklCertificatePath' => $lklCertificatePath,
  151. ];
  152. $laResource = new Lakala($params);
  153. $notifyUrl = Yii::$app->params['hdHost'] . "/notice/pay-callback";
  154. $wxParams = [
  155. 'orderSn' => $orderSn,
  156. 'amount' => $totalFee,
  157. 'capitalType' => $capitalType,
  158. 'notifyUrl' => $notifyUrl,
  159. 'wxAppId' => $merchantExtend['miniAppId'],
  160. 'openId' => $openId,
  161. 'subject' => $name,
  162. 'couponId' => $couponId,
  163. ];
  164. $response = $laResource->driveWxPay($wxParams);
  165. if (isset($response['code']) == false || $response['code'] != 'BBS00000') {
  166. $errMsg = $response['msg'] ?? '';
  167. $ghsName = $order->ghsName ?? '';
  168. $ghsShopName = $order->ghsShopName ?? '';
  169. $ghsFullName = $ghsName . $ghsShopName;
  170. $customName = $order->customName ?? '';
  171. $customFullName = $customName;
  172. noticeUtil::push("\n提示:结账付款失败\n原因:" . $errMsg . "。\n商家:" . $ghsFullName . "\n客户:" . $customFullName, '15280215347');
  173. util::fail('结账付款失败,' . $errMsg);
  174. }
  175. $newParams = isset($response['resp_data']['acc_resp_fields']) ? $response['resp_data']['acc_resp_fields'] : [];
  176. $appId = $newParams['app_id'] ?? '';
  177. $nonceStr = $newParams['nonce_str'] ?? '';
  178. $paySign = $newParams['pay_sign'] ?? '';
  179. $package = $newParams['package'] ?? '';
  180. $signType = $newParams['sign_type'] ?? '';
  181. $timeStamp = $newParams['time_stamp'] ?? '';
  182. $new = [
  183. 'id' => $id,
  184. 'appId' => $appId,
  185. 'nonceStr' => $nonceStr,
  186. 'paySign' => $paySign,
  187. 'package' => $package,
  188. 'signType' => $signType,
  189. 'timeStamp' => $timeStamp,
  190. 'orderSn' => $orderSn,
  191. ];
  192. util::success($new);
  193. }
  194. }