PurchaseClearController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. //如果是虚拟关系,暂时允许花店一键结账
  85. $live = $ghs->live ?? 1;
  86. $respond['live'] = $live;
  87. util::success($respond);
  88. }
  89. public function actionWxPay()
  90. {
  91. ini_set('date.timezone', 'Asia/Shanghai');
  92. $post = Yii::$app->request->post();
  93. $couponId = $post['couponId'] ?? 0;
  94. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  95. $order = PurchaseClearClass::getByCondition(['orderSn' => $orderSn], true);
  96. if (empty($order)) {
  97. util::fail('账单号无效');
  98. }
  99. if ($order->status == PurchaseClearClass::STATUS_HAS_PAY) {
  100. util::fail('账单已结清');
  101. }
  102. if ($order->status == PurchaseClearClass::STATUS_EXPIRE) {
  103. util::fail('账单已取消');
  104. }
  105. //避免重复提交
  106. util::checkRepeatCommit($orderSn, 8);
  107. $oldOrderSn = $orderSn;
  108. $current = time();
  109. $id = $order->id;
  110. $deadline = $order->deadline;
  111. if ($current > strtotime($deadline)) {
  112. $order->status = PurchaseClearClass::STATUS_EXPIRE;
  113. $order->save();
  114. util::fail('订单已经失效,请重新发起结算');
  115. }
  116. $totalFee = $order->actPrice;
  117. $openId = '';
  118. if (isset($post['currentMiniOpenId']) && !empty($post['currentMiniOpenId'])) {
  119. $openId = $post['currentMiniOpenId'];
  120. //noticeUtil::push('花店结账时,通过前端获取了openId:' . $openId, '15280215347');
  121. }
  122. if (empty($openId)) {
  123. if (isset($this->admin) && !empty($this->admin)) {
  124. if (isset($this->admin->miniOpenId) && !empty($this->admin->miniOpenId)) {
  125. $openId = $this->admin->miniOpenId;
  126. noticeUtil::push('花店结账时,通过数据库获取了openId:' . $openId, '15280215347');
  127. }
  128. }
  129. }
  130. if (empty($openId)) {
  131. util::fail('没有找到openId');
  132. }
  133. $capitalType = dict::getDict('capitalType', 'hdPurchaseClear', 'id');
  134. $ghsShopId = $order->ghsShopId ?? 0;
  135. $ghsShop = ShopClass::getById($ghsShopId, true);
  136. if (empty($ghsShop)) {
  137. util::fail('没有供货商门店信息');
  138. }
  139. if (isset($ghsShop->lklSjNo) && empty($ghsShop->lklSjNo)) {
  140. util::fail('商家支付功能未开通');
  141. }
  142. //已经唤起过微信支付了,根据拉卡拉的规则,需要生成新的订单号
  143. if ($order->changePrice == 2) {
  144. //老单号需要关单,否则有付款成功的风险,老单号关单成功了,才能生成新单号
  145. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  146. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  147. $params = [
  148. 'appid' => 'OP00002119',
  149. 'serial_no' => '018b08cfddbd',
  150. 'merchant_no' => $ghsShop->lklSjNo,
  151. 'term_no' => $ghsShop->lklScanTermNo,
  152. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  153. 'lklCertificatePath' => $lklCertificatePath,
  154. ];
  155. $laResource = new Lakala($params);
  156. $closeParams = [
  157. 'orderSn' => $oldOrderSn,
  158. ];
  159. $response = $laResource->close($closeParams);
  160. if (!isset($response['code']) || $response['code'] != 'BBS00000') {
  161. $msg = $response['msg'] ?? '';
  162. $code = $response['code'] ?? 0;
  163. $customMobile = $order->customMobile ?? '';
  164. $ghsName = $order->ghsName ?? '';
  165. //noticeUtil::push('第一个,花店结账,单号:' . $oldOrderSn . ',关单没有成功,原因:【' . $msg . '】,' . $code . ',客户手机号:' . $customMobile . ',供货商:' . $ghsName, '15280215347');
  166. }
  167. //收银台关单
  168. $closeParams = ['orderSn' => $orderSn];
  169. $response = $laResource->cashClose($closeParams);
  170. if (!isset($response['code']) || $response['code'] != '000000') {
  171. $msg = $response['msg'] ?? '';
  172. $code = $response['code'] ?? 0;
  173. //noticeUtil::push('第二个,花店结账,单号:' . $oldOrderSn . ',收银台关单没有成功,原因:【' . $msg . '】,' . $code, '15280215347');
  174. }
  175. $newOrderSn = orderSn::getPurchaseClearSn();
  176. $orderSn = $newOrderSn;
  177. $order->orderSn = $newOrderSn;
  178. $order->save();
  179. }
  180. $order->changePrice = 2;
  181. $order->save();
  182. //花卉宝代为申请的微信支付
  183. $merchantExtend = WxOpenClass::getWxInfo();
  184. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  185. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  186. $params = [
  187. 'appid' => 'OP00002119',
  188. 'serial_no' => '018b08cfddbd',
  189. 'merchant_no' => $ghsShop->lklSjNo,
  190. 'term_no' => $ghsShop->lklScanTermNo,
  191. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  192. 'lklCertificatePath' => $lklCertificatePath,
  193. ];
  194. $laResource = new Lakala($params);
  195. $notifyUrl = Yii::$app->params['hdHost'] . "/notice/pay-callback";
  196. $name = '采购单结算 ' . $orderSn;
  197. $wxParams = [
  198. 'orderSn' => $orderSn,
  199. 'amount' => $totalFee,
  200. 'capitalType' => $capitalType,
  201. 'notifyUrl' => $notifyUrl,
  202. 'wxAppId' => $merchantExtend['miniAppId'],
  203. 'openId' => $openId,
  204. 'subject' => $name,
  205. 'couponId' => $couponId,
  206. ];
  207. $response = $laResource->driveWxPay($wxParams);
  208. if (isset($response['code']) == false || $response['code'] != 'BBS00000') {
  209. $errMsg = $response['msg'] ?? '';
  210. $ghsName = $order->ghsName ?? '';
  211. $ghsShopName = $order->ghsShopName ?? '';
  212. $ghsFullName = $ghsName . $ghsShopName;
  213. $customName = $order->customName ?? '';
  214. $customFullName = $customName;
  215. noticeUtil::push("\n提示:结账付款失败\n原因:" . $errMsg . "。\n商家:" . $ghsFullName . "\n客户:" . $customFullName, '15280215347');
  216. util::fail('结账付款失败,' . $errMsg . ',请联系门店');
  217. }
  218. $newParams = isset($response['resp_data']['acc_resp_fields']) ? $response['resp_data']['acc_resp_fields'] : [];
  219. $appId = $newParams['app_id'] ?? '';
  220. $nonceStr = $newParams['nonce_str'] ?? '';
  221. $paySign = $newParams['pay_sign'] ?? '';
  222. $package = $newParams['package'] ?? '';
  223. $signType = $newParams['sign_type'] ?? '';
  224. $timeStamp = $newParams['time_stamp'] ?? '';
  225. $new = [
  226. 'id' => $id,
  227. 'appId' => $appId,
  228. 'nonceStr' => $nonceStr,
  229. 'paySign' => $paySign,
  230. 'package' => $package,
  231. 'signType' => $signType,
  232. 'timeStamp' => $timeStamp,
  233. 'orderSn' => $orderSn,
  234. ];
  235. util::success($new);
  236. }
  237. public function actionAliPay()
  238. {
  239. ini_set('date.timezone', 'Asia/Shanghai');
  240. $post = Yii::$app->request->post();
  241. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  242. $order = PurchaseClearClass::getByCondition(['orderSn' => $orderSn], true);
  243. if (empty($order)) {
  244. util::fail('账单号无效');
  245. }
  246. if ($order->status == PurchaseClearClass::STATUS_HAS_PAY) {
  247. util::fail('账单已结清!');
  248. }
  249. if ($order->status == PurchaseClearClass::STATUS_EXPIRE) {
  250. util::fail('账单已取消!');
  251. }
  252. $oldOrderSn = $orderSn;
  253. $current = time();
  254. $deadline = $order->deadline;
  255. if ($current > strtotime($deadline)) {
  256. $order->status = PurchaseClearClass::STATUS_EXPIRE;
  257. $order->save();
  258. util::fail('订单已经失效,请重新发起结算');
  259. }
  260. $totalFee = $order->actPrice;
  261. //避免重复提交
  262. util::checkRepeatCommit($orderSn, 8);
  263. $capitalType = dict::getDict('capitalType', 'hdPurchaseClear', 'id');
  264. $ghsShopId = $order->ghsShopId ?? 0;
  265. $ghsShop = ShopClass::getById($ghsShopId, true);
  266. if (empty($ghsShop)) {
  267. util::fail('没有供货商门店信息');
  268. }
  269. if (isset($ghsShop->lklSjNo) && empty($ghsShop->lklSjNo)) {
  270. util::fail('商家支付功能未开通');
  271. }
  272. //已经唤起支付,根据拉卡拉的规则,需要生成新的订单号
  273. if ($order->changePrice == 2) {
  274. //老单号需要关单,否则有付款成功的风险,老单号关单成功了,才能生成新单号
  275. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  276. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  277. $params = [
  278. 'appid' => 'OP00002119',
  279. 'serial_no' => '018b08cfddbd',
  280. 'merchant_no' => $ghsShop->lklSjNo,
  281. 'term_no' => $ghsShop->lklScanTermNo,
  282. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  283. 'lklCertificatePath' => $lklCertificatePath,
  284. ];
  285. $laResource = new Lakala($params);
  286. $closeParams = [
  287. 'orderSn' => $oldOrderSn,
  288. ];
  289. $response = $laResource->cashClose($closeParams);
  290. if (isset($response['code']) == false || $response['code'] != '000000') {
  291. $msg = $response['msg'] ?? '';
  292. $code = $response['code'] ?? 0;
  293. if (!in_array($code, ['000091'])) {
  294. noticeUtil::push("花店结账模块 " . $oldOrderSn . " 关单没有成功 收银台-支付宝模块 " . $msg, '15280215347');
  295. }
  296. }
  297. $newOrderSn = orderSn::getPurchaseClearSn();
  298. $orderSn = $newOrderSn;
  299. $order->orderSn = $newOrderSn;
  300. $order->save();
  301. }
  302. $order->changePrice = 2;
  303. $order->save();
  304. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  305. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  306. $params = [
  307. 'appid' => 'OP00002119',
  308. 'serial_no' => '018b08cfddbd',
  309. 'merchant_no' => $ghsShop->lklSjNo,
  310. 'term_no' => $ghsShop->lklScanTermNo,
  311. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  312. 'lklCertificatePath' => $lklCertificatePath,
  313. ];
  314. $laResource = new Lakala($params);
  315. $notifyUrl = Yii::$app->params['hdHost'] . "/notice/cash-pay-callback";
  316. $wxParams = [
  317. 'orderSn' => $orderSn,
  318. 'amount' => $totalFee,
  319. 'capitalType' => $capitalType,
  320. 'notifyUrl' => $notifyUrl,
  321. 'subject' => $orderSn,
  322. ];
  323. $response = $laResource->cashierPay($wxParams);
  324. if (isset($response['code']) == false || $response['code'] != '000000') {
  325. $errMsg = $response['msg'] ?? '';
  326. $ghsName = $order->ghsName ?? '';
  327. $ghsShopName = $order->ghsShopName ?? '';
  328. $ghsFullName = $ghsName . $ghsShopName;
  329. $customName = $order->customName ?? '';
  330. $customFullName = $customName;
  331. noticeUtil::push("\n提示:结账付款失败\n原因:" . $errMsg . "。\n商家:" . $ghsFullName . "\n客户:" . $customFullName, '15280215347');
  332. util::fail('结账付款失败,' . $errMsg . ',请联系门店');
  333. }
  334. $payUrl = $response['resp_data']['counter_url'] ? $response['resp_data']['counter_url'] : '';
  335. $hasRenew = $ghsShop->hasRenew ?? 0;
  336. $new = [
  337. 'payUrl' => $payUrl,
  338. 'orderSn' => $orderSn,
  339. 'hasRenew' => 1,
  340. ];
  341. util::success($new);
  342. }
  343. }