PurchaseClearController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. }
  164. //收银台关单
  165. $closeParams = ['orderSn' => $orderSn];
  166. $response = $laResource->cashClose($closeParams);
  167. if (!isset($response['code']) || $response['code'] != '000000') {
  168. $msg = $response['msg'] ?? '';
  169. $code = $response['code'] ?? 0;
  170. //noticeUtil::push('第二个,花店结账,单号:' . $oldOrderSn . ',收银台关单没有成功,原因:【' . $msg . '】,' . $code, '15280215347');
  171. }
  172. $newOrderSn = orderSn::getPurchaseClearSn();
  173. $orderSn = $newOrderSn;
  174. $order->orderSn = $newOrderSn;
  175. $order->save();
  176. }
  177. $order->changePrice = 2;
  178. $order->save();
  179. //花卉宝代为申请的微信支付
  180. $merchantExtend = WxOpenClass::getWxInfo();
  181. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  182. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  183. $params = [
  184. 'appid' => 'OP00002119',
  185. 'serial_no' => '018b08cfddbd',
  186. 'merchant_no' => $ghsShop->lklSjNo,
  187. 'term_no' => $ghsShop->lklScanTermNo,
  188. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  189. 'lklCertificatePath' => $lklCertificatePath,
  190. ];
  191. $laResource = new Lakala($params);
  192. $notifyUrl = Yii::$app->params['hdHost'] . "/notice/pay-callback";
  193. $name = '采购单结算 ' . $orderSn;
  194. $wxParams = [
  195. 'orderSn' => $orderSn,
  196. 'amount' => $totalFee,
  197. 'capitalType' => $capitalType,
  198. 'notifyUrl' => $notifyUrl,
  199. 'wxAppId' => $merchantExtend['miniAppId'],
  200. 'openId' => $openId,
  201. 'subject' => $name,
  202. 'couponId' => $couponId,
  203. ];
  204. $response = $laResource->driveWxPay($wxParams);
  205. if (isset($response['code']) == false || $response['code'] != 'BBS00000') {
  206. $errMsg = $response['msg'] ?? '';
  207. $ghsName = $order->ghsName ?? '';
  208. $ghsShopName = $order->ghsShopName ?? '';
  209. $ghsFullName = $ghsName . $ghsShopName;
  210. $customName = $order->customName ?? '';
  211. $customFullName = $customName;
  212. noticeUtil::push("\n提示:结账付款失败\n原因:" . $errMsg . "。\n商家:" . $ghsFullName . "\n客户:" . $customFullName, '15280215347');
  213. util::fail('结账付款失败,' . $errMsg . ',请联系门店');
  214. }
  215. $newParams = isset($response['resp_data']['acc_resp_fields']) ? $response['resp_data']['acc_resp_fields'] : [];
  216. $appId = $newParams['app_id'] ?? '';
  217. $nonceStr = $newParams['nonce_str'] ?? '';
  218. $paySign = $newParams['pay_sign'] ?? '';
  219. $package = $newParams['package'] ?? '';
  220. $signType = $newParams['sign_type'] ?? '';
  221. $timeStamp = $newParams['time_stamp'] ?? '';
  222. $new = [
  223. 'id' => $id,
  224. 'appId' => $appId,
  225. 'nonceStr' => $nonceStr,
  226. 'paySign' => $paySign,
  227. 'package' => $package,
  228. 'signType' => $signType,
  229. 'timeStamp' => $timeStamp,
  230. 'orderSn' => $orderSn,
  231. ];
  232. util::success($new);
  233. }
  234. public function actionAliPay()
  235. {
  236. ini_set('date.timezone', 'Asia/Shanghai');
  237. $post = Yii::$app->request->post();
  238. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  239. $order = PurchaseClearClass::getByCondition(['orderSn' => $orderSn], true);
  240. if (empty($order)) {
  241. util::fail('账单号无效');
  242. }
  243. if ($order->status == PurchaseClearClass::STATUS_HAS_PAY) {
  244. util::fail('账单已结清!');
  245. }
  246. if ($order->status == PurchaseClearClass::STATUS_EXPIRE) {
  247. util::fail('账单已取消!');
  248. }
  249. $oldOrderSn = $orderSn;
  250. $current = time();
  251. $deadline = $order->deadline;
  252. if ($current > strtotime($deadline)) {
  253. $order->status = PurchaseClearClass::STATUS_EXPIRE;
  254. $order->save();
  255. util::fail('订单已经失效,请重新发起结算');
  256. }
  257. $totalFee = $order->actPrice;
  258. //避免重复提交
  259. util::checkRepeatCommit($orderSn, 8);
  260. $capitalType = dict::getDict('capitalType', 'hdPurchaseClear', 'id');
  261. $ghsShopId = $order->ghsShopId ?? 0;
  262. $ghsShop = ShopClass::getById($ghsShopId, true);
  263. if (empty($ghsShop)) {
  264. util::fail('没有供货商门店信息');
  265. }
  266. if (isset($ghsShop->lklSjNo) && empty($ghsShop->lklSjNo)) {
  267. util::fail('商家支付功能未开通');
  268. }
  269. //已经唤起支付,根据拉卡拉的规则,需要生成新的订单号
  270. if ($order->changePrice == 2) {
  271. //老单号需要关单,否则有付款成功的风险,老单号关单成功了,才能生成新单号
  272. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  273. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  274. $params = [
  275. 'appid' => 'OP00002119',
  276. 'serial_no' => '018b08cfddbd',
  277. 'merchant_no' => $ghsShop->lklSjNo,
  278. 'term_no' => $ghsShop->lklScanTermNo,
  279. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  280. 'lklCertificatePath' => $lklCertificatePath,
  281. ];
  282. $laResource = new Lakala($params);
  283. $closeParams = [
  284. 'orderSn' => $oldOrderSn,
  285. ];
  286. $response = $laResource->cashClose($closeParams);
  287. if (isset($response['code']) == false || $response['code'] != '000000') {
  288. $msg = $response['msg'] ?? '';
  289. $code = $response['code'] ?? 0;
  290. if (!in_array($code, ['000091'])) {
  291. noticeUtil::push("花店结账模块 " . $oldOrderSn . " 关单没有成功 收银台-支付宝模块 " . $msg, '15280215347');
  292. }
  293. }
  294. $newOrderSn = orderSn::getPurchaseClearSn();
  295. $orderSn = $newOrderSn;
  296. $order->orderSn = $newOrderSn;
  297. $order->save();
  298. }
  299. $order->changePrice = 2;
  300. $order->save();
  301. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  302. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  303. $params = [
  304. 'appid' => 'OP00002119',
  305. 'serial_no' => '018b08cfddbd',
  306. 'merchant_no' => $ghsShop->lklSjNo,
  307. 'term_no' => $ghsShop->lklScanTermNo,
  308. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  309. 'lklCertificatePath' => $lklCertificatePath,
  310. ];
  311. $laResource = new Lakala($params);
  312. $notifyUrl = Yii::$app->params['hdHost'] . "/notice/cash-pay-callback";
  313. $wxParams = [
  314. 'orderSn' => $orderSn,
  315. 'amount' => $totalFee,
  316. 'capitalType' => $capitalType,
  317. 'notifyUrl' => $notifyUrl,
  318. 'subject' => $orderSn,
  319. ];
  320. $response = $laResource->cashierPay($wxParams);
  321. if (isset($response['code']) == false || $response['code'] != '000000') {
  322. $errMsg = $response['msg'] ?? '';
  323. $ghsName = $order->ghsName ?? '';
  324. $ghsShopName = $order->ghsShopName ?? '';
  325. $ghsFullName = $ghsName . $ghsShopName;
  326. $customName = $order->customName ?? '';
  327. $customFullName = $customName;
  328. noticeUtil::push("\n提示:结账付款失败\n原因:" . $errMsg . "。\n商家:" . $ghsFullName . "\n客户:" . $customFullName, '15280215347');
  329. util::fail('结账付款失败,' . $errMsg . ',请联系门店');
  330. }
  331. $payUrl = $response['resp_data']['counter_url'] ? $response['resp_data']['counter_url'] : '';
  332. $hasRenew = $ghsShop->hasRenew ?? 0;
  333. $new = [
  334. 'payUrl' => $payUrl,
  335. 'orderSn' => $orderSn,
  336. 'hasRenew' => 1,
  337. ];
  338. util::success($new);
  339. }
  340. }