PurchaseClearController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. //老单号需要关单,否则有付款成功的风险,老单号关单成功了,才能生成新单号
  112. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  113. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  114. $params = [
  115. 'appid' => 'OP00002119',
  116. 'serial_no' => '018b08cfddbd',
  117. 'merchant_no' => $ghsShop->lklSjNo,
  118. 'term_no' => $ghsShop->lklScanTermNo,
  119. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  120. 'lklCertificatePath' => $lklCertificatePath,
  121. ];
  122. $laResource = new Lakala($params);
  123. $closeParams = [
  124. 'orderSn' => $oldOrderSn,
  125. ];
  126. $response = $laResource->close($closeParams);
  127. if (isset($response['code']) == false || $response['code'] != 'BBS00000') {
  128. $msg = $response['msg'] ?? '';
  129. noticeUtil::push('重点注意,花店结账,单号:' . $oldOrderSn . ',关单没有成功,原因:' . $msg . ',可能造成无法结账', '15280215347');
  130. util::fail('出错了,请取消本单重试');
  131. }
  132. $newOrderSn = orderSn::getPurchaseClearSn();
  133. $orderSn = $newOrderSn;
  134. $order->orderSn = $newOrderSn;
  135. $order->save();
  136. }
  137. $order->changePrice = 2;
  138. $order->save();
  139. //花卉宝代为申请的微信支付
  140. $merchantExtend = WxOpenClass::getWxInfo();
  141. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  142. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  143. $params = [
  144. 'appid' => 'OP00002119',
  145. 'serial_no' => '018b08cfddbd',
  146. 'merchant_no' => $ghsShop->lklSjNo,
  147. 'term_no' => $ghsShop->lklScanTermNo,
  148. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  149. 'lklCertificatePath' => $lklCertificatePath,
  150. ];
  151. $laResource = new Lakala($params);
  152. $notifyUrl = Yii::$app->params['hdHost'] . "/notice/pay-callback";
  153. $wxParams = [
  154. 'orderSn' => $orderSn,
  155. 'amount' => $totalFee,
  156. 'capitalType' => $capitalType,
  157. 'notifyUrl' => $notifyUrl,
  158. 'wxAppId' => $merchantExtend['miniAppId'],
  159. 'openId' => $openId,
  160. 'subject' => $name,
  161. 'couponId' => $couponId,
  162. ];
  163. $response = $laResource->driveWxPay($wxParams);
  164. if (isset($response['code']) == false || $response['code'] != 'BBS00000') {
  165. $errMsg = $response['msg'] ?? '';
  166. $ghsName = $order->ghsName ?? '';
  167. $ghsShopName = $order->ghsShopName ?? '';
  168. $ghsFullName = $ghsName . $ghsShopName;
  169. $customName = $order->customName ?? '';
  170. $customFullName = $customName;
  171. noticeUtil::push("\n提示:结账付款失败\n原因:" . $errMsg . "。\n商家:" . $ghsFullName . "\n客户:" . $customFullName, '15280215347');
  172. util::fail('结账付款失败,' . $errMsg . ',请联系门店');
  173. }
  174. $newParams = isset($response['resp_data']['acc_resp_fields']) ? $response['resp_data']['acc_resp_fields'] : [];
  175. $appId = $newParams['app_id'] ?? '';
  176. $nonceStr = $newParams['nonce_str'] ?? '';
  177. $paySign = $newParams['pay_sign'] ?? '';
  178. $package = $newParams['package'] ?? '';
  179. $signType = $newParams['sign_type'] ?? '';
  180. $timeStamp = $newParams['time_stamp'] ?? '';
  181. $new = [
  182. 'id' => $id,
  183. 'appId' => $appId,
  184. 'nonceStr' => $nonceStr,
  185. 'paySign' => $paySign,
  186. 'package' => $package,
  187. 'signType' => $signType,
  188. 'timeStamp' => $timeStamp,
  189. 'orderSn' => $orderSn,
  190. ];
  191. util::success($new);
  192. }
  193. public function actionAliPay()
  194. {
  195. ini_set('date.timezone', 'Asia/Shanghai');
  196. $post = Yii::$app->request->post();
  197. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  198. $order = PurchaseClearClass::getByCondition(['orderSn' => $orderSn], true);
  199. if (empty($order)) {
  200. util::fail('账单号无效');
  201. }
  202. if ($order->status == PurchaseClearClass::STATUS_HAS_PAY) {
  203. util::fail('账单已结清了');
  204. }
  205. if ($order->status == PurchaseClearClass::STATUS_EXPIRE) {
  206. util::fail('账单已取消了');
  207. }
  208. $oldOrderSn = $orderSn;
  209. $current = time();
  210. $deadline = $order->deadline;
  211. if ($current > strtotime($deadline)) {
  212. $order->status = PurchaseClearClass::STATUS_EXPIRE;
  213. $order->save();
  214. util::fail('订单已经失效,请重新发起结算');
  215. }
  216. $totalFee = $order->actPrice;
  217. $capitalType = dict::getDict('capitalType', 'hdPurchaseClear', 'id');
  218. $ghsShopId = $order->ghsShopId ?? 0;
  219. $ghsShop = ShopClass::getById($ghsShopId, true);
  220. if (empty($ghsShop)) {
  221. util::fail('没有供货商门店信息');
  222. }
  223. if (isset($ghsShop->lklSjNo) && empty($ghsShop->lklSjNo)) {
  224. util::fail('商家支付功能未开通');
  225. }
  226. //已经唤起支付,根据拉卡拉的规则,需要生成新的订单号
  227. if ($order->changePrice == 2) {
  228. //老单号需要关单,否则有付款成功的风险,老单号关单成功了,才能生成新单号
  229. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  230. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  231. $params = [
  232. 'appid' => 'OP00002119',
  233. 'serial_no' => '018b08cfddbd',
  234. 'merchant_no' => $ghsShop->lklSjNo,
  235. 'term_no' => $ghsShop->lklScanTermNo,
  236. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  237. 'lklCertificatePath' => $lklCertificatePath,
  238. ];
  239. $laResource = new Lakala($params);
  240. $closeParams = [
  241. 'orderSn' => $oldOrderSn,
  242. ];
  243. $response = $laResource->cashClose($closeParams);
  244. if (isset($response['code']) == false || $response['code'] != '000000') {
  245. $msg = $response['msg'] ?? '';
  246. noticeUtil::push("\n花店结账" . $oldOrderSn . ",关单没有成功 \n 收银台-支付宝模块 \n 原因:" . $msg . ',可能造成无法结账', '15280215347');
  247. }
  248. $newOrderSn = orderSn::getPurchaseClearSn();
  249. $orderSn = $newOrderSn;
  250. $order->orderSn = $newOrderSn;
  251. $order->save();
  252. }
  253. $order->changePrice = 2;
  254. $order->save();
  255. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  256. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  257. $params = [
  258. 'appid' => 'OP00002119',
  259. 'serial_no' => '018b08cfddbd',
  260. 'merchant_no' => $ghsShop->lklSjNo,
  261. 'term_no' => $ghsShop->lklScanTermNo,
  262. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  263. 'lklCertificatePath' => $lklCertificatePath,
  264. ];
  265. $laResource = new Lakala($params);
  266. $notifyUrl = Yii::$app->params['hdHost'] . "/notice/cash-pay-callback";
  267. $wxParams = [
  268. 'orderSn' => $orderSn,
  269. 'amount' => $totalFee,
  270. 'capitalType' => $capitalType,
  271. 'notifyUrl' => $notifyUrl,
  272. 'subject' => $orderSn,
  273. ];
  274. $response = $laResource->cashierPay($wxParams);
  275. if (isset($response['code']) == false || $response['code'] != '000000') {
  276. $errMsg = $response['msg'] ?? '';
  277. $ghsName = $order->ghsName ?? '';
  278. $ghsShopName = $order->ghsShopName ?? '';
  279. $ghsFullName = $ghsName . $ghsShopName;
  280. $customName = $order->customName ?? '';
  281. $customFullName = $customName;
  282. noticeUtil::push("\n提示:结账付款失败\n原因:" . $errMsg . "。\n商家:" . $ghsFullName . "\n客户:" . $customFullName, '15280215347');
  283. util::fail('结账付款失败,' . $errMsg . ',请联系门店');
  284. }
  285. $payUrl = $response['resp_data']['counter_url'] ? $response['resp_data']['counter_url'] : '';
  286. $new = [
  287. 'payUrl' => $payUrl,
  288. 'orderSn' => $orderSn,
  289. ];
  290. util::success($new);
  291. }
  292. }