PurchaseClearController.php 15 KB

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