PurchaseClearController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. $oldOrderSn = $orderSn;
  103. $current = time();
  104. $id = $order->id;
  105. $deadline = $order->deadline;
  106. if ($current > strtotime($deadline)) {
  107. $order->status = PurchaseClearClass::STATUS_EXPIRE;
  108. $order->save();
  109. util::fail('订单已经失效,请重新发起结算');
  110. }
  111. $totalFee = $order->actPrice;
  112. $openId = '';
  113. if (isset($post['currentMiniOpenId']) && !empty($post['currentMiniOpenId'])) {
  114. $openId = $post['currentMiniOpenId'];
  115. //noticeUtil::push('花店结账时,通过前端获取了openId:' . $openId, '15280215347');
  116. }
  117. if (empty($openId)) {
  118. if (isset($this->admin) && !empty($this->admin)) {
  119. if (isset($this->admin->miniOpenId) && !empty($this->admin->miniOpenId)) {
  120. $openId = $this->admin->miniOpenId;
  121. noticeUtil::push('花店结账时,通过数据库获取了openId:' . $openId, '15280215347');
  122. }
  123. }
  124. }
  125. if (empty($openId)) {
  126. util::fail('没有找到openId');
  127. }
  128. $capitalType = dict::getDict('capitalType', 'hdPurchaseClear', 'id');
  129. $ghsShopId = $order->ghsShopId ?? 0;
  130. $ghsShop = ShopClass::getById($ghsShopId, true);
  131. if (empty($ghsShop)) {
  132. util::fail('没有供货商门店信息');
  133. }
  134. if (isset($ghsShop->lklSjNo) && empty($ghsShop->lklSjNo)) {
  135. util::fail('商家支付功能未开通');
  136. }
  137. //已经唤起过微信支付了,根据拉卡拉的规则,需要生成新的订单号
  138. if ($order->changePrice == 2) {
  139. //老单号需要关单,否则有付款成功的风险,老单号关单成功了,才能生成新单号
  140. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  141. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  142. $params = [
  143. 'appid' => 'OP00002119',
  144. 'serial_no' => '018b08cfddbd',
  145. 'merchant_no' => $ghsShop->lklSjNo,
  146. 'term_no' => $ghsShop->lklScanTermNo,
  147. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  148. 'lklCertificatePath' => $lklCertificatePath,
  149. ];
  150. $laResource = new Lakala($params);
  151. $closeParams = [
  152. 'orderSn' => $oldOrderSn,
  153. ];
  154. $response = $laResource->close($closeParams);
  155. if (!isset($response['code']) || $response['code'] != 'BBS00000') {
  156. $msg = $response['msg'] ?? '';
  157. $code = $response['code'] ?? 0;
  158. $customMobile = $order->customMobile ?? '';
  159. $ghsName = $order->ghsName ?? '';
  160. noticeUtil::push('花店结账,单号:' . $oldOrderSn . ',关单没有成功,原因:【' . $msg . '】,' . $code . ',客户手机号:' . $customMobile . ',供货商:' . $ghsName, '15280215347');
  161. //util::fail('出错了,请取消本单重试');
  162. } else {
  163. noticeUtil::push('花店结账,单号:' . $oldOrderSn . ',关单有成功!!!!', '15280215347');
  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. } else {
  173. noticeUtil::push('花店结账,单号:' . $oldOrderSn . ',收银台关单有成功OK', '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. $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. }