PurchaseClearController.php 16 KB

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