PurchaseClearController.php 16 KB

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