PurchaseClearController.php 15 KB

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