PurchaseClearController.php 16 KB

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