PurchaseClearController.php 19 KB

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