request->post(); $ghsId = $post['ghsId'] ?? 0; $ghs = GhsClass::getById($ghsId, true); if (empty($ghs)) { util::fail('没有供货商'); } //解决重复请求问题 $staffId = $this->shopAdminId ?? 0; $cacheKey = 'hd_clear_order_pay_' . $ghsId . '_' . $staffId; $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]); if (!empty($has)) { util::fail('请稍等5秒再操作'); } Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 5, 'has']); //老的没有清的结账单直接过期掉 $list = PurchaseClearClass::getAllByCondition(['ghsId' => $ghsId, 'status' => PurchaseClearClass::STATUS_AWAIT_PAY], null, '*', null, true); if (!empty($list)) { $ghsShopId = $ghs->shopId ?? 0; $ghsShop = ShopClass::getById($ghsShopId, true); foreach ($list as $item) { $item->status = PurchaseClearClass::STATUS_EXPIRE; $item->save(); if (!empty($ghsShop)) { $orderSn = $item->orderSn ?? ''; $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem'; $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer'; $params = [ 'appid' => 'OP00002119', 'serial_no' => '018b08cfddbd', 'merchant_no' => $ghsShop->lklSjNo, 'term_no' => $ghsShop->lklScanTermNo, 'merchantPrivateKeyPath' => $merchantPrivateKeyPath, 'lklCertificatePath' => $lklCertificatePath, ]; $laResource = new Lakala($params); $closeParams = [ 'orderSn' => $orderSn, ]; $response = $laResource->close($closeParams); if (isset($response['code']) == false || $response['code'] != 'BBS00000') { //$msg = $response['msg'] ?? ''; //noticeUtil::push("\n花店结账单:" . $orderSn . "\n关单失败了\n{$msg} ", '15280215347'); } else { //noticeUtil::push("\n花店结账单:" . $orderSn . "\n关单成功", '15280215347'); } } } } $post['ghsShopId'] = $ghs->shopId ?? 0; $post['sjId'] = $this->sjId; $post['shopId'] = $this->shopId; $post['clearStyle'] = dict::getDict('clearStyle', 'hd2Gys'); $post['customShopAdminId'] = $this->shopAdminId ?? 0; $post['customShopId'] = $this->shopId ?? 0; $shopAdmin = $this->shopAdmin; $shopAdminName = $shopAdmin['name'] ?? ''; $post['customShopAdminName'] = $shopAdminName; $respond = PurchaseClearClass::addOrder($post, $ghs); //前端显示的付款倒计时 $hasTime = dict::getDict('order_pay_has_time'); $aheadTime = dict::getDict('order_online_pay_has_ahead_time'); $remainSecond = $hasTime - $aheadTime; $respond['remainSecond'] = $remainSecond; $getPayType = dict::getDict('getPayType'); $respond['getPayType'] = $getPayType; util::success($respond); } public function actionWxPay() { ini_set('date.timezone', 'Asia/Shanghai'); $post = Yii::$app->request->post(); $couponId = $post['couponId'] ?? 0; $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0; $order = PurchaseClearClass::getByCondition(['orderSn' => $orderSn], true); if (empty($order)) { util::fail('账单号无效'); } if ($order->status == PurchaseClearClass::STATUS_HAS_PAY) { util::fail('账单已结清'); } if ($order->status == PurchaseClearClass::STATUS_EXPIRE) { util::fail('账单已取消'); } //避免重复提交 util::checkRepeatCommit($orderSn, 8); $oldOrderSn = $orderSn; $current = time(); $id = $order->id; $deadline = $order->deadline; if ($current > strtotime($deadline)) { $order->status = PurchaseClearClass::STATUS_EXPIRE; $order->save(); util::fail('订单已经失效,请重新发起结算'); } $totalFee = $order->actPrice; $openId = ''; if (isset($post['currentMiniOpenId']) && !empty($post['currentMiniOpenId'])) { $openId = $post['currentMiniOpenId']; //noticeUtil::push('花店结账时,通过前端获取了openId:' . $openId, '15280215347'); } if (empty($openId)) { if (isset($this->admin) && !empty($this->admin)) { if (isset($this->admin->miniOpenId) && !empty($this->admin->miniOpenId)) { $openId = $this->admin->miniOpenId; noticeUtil::push('花店结账时,通过数据库获取了openId:' . $openId, '15280215347'); } } } if (empty($openId)) { util::fail('没有找到openId'); } $capitalType = dict::getDict('capitalType', 'hdPurchaseClear', 'id'); $ghsShopId = $order->ghsShopId ?? 0; $ghsShop = ShopClass::getById($ghsShopId, true); if (empty($ghsShop)) { util::fail('没有供货商门店信息'); } if (isset($ghsShop->lklSjNo) && empty($ghsShop->lklSjNo)) { util::fail('商家支付功能未开通'); } //已经唤起过微信支付了,根据拉卡拉的规则,需要生成新的订单号 if ($order->changePrice == 2) { //老单号需要关单,否则有付款成功的风险,老单号关单成功了,才能生成新单号 $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem'; $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer'; $params = [ 'appid' => 'OP00002119', 'serial_no' => '018b08cfddbd', 'merchant_no' => $ghsShop->lklSjNo, 'term_no' => $ghsShop->lklScanTermNo, 'merchantPrivateKeyPath' => $merchantPrivateKeyPath, 'lklCertificatePath' => $lklCertificatePath, ]; $laResource = new Lakala($params); $closeParams = [ 'orderSn' => $oldOrderSn, ]; $response = $laResource->close($closeParams); if (!isset($response['code']) || $response['code'] != 'BBS00000') { $msg = $response['msg'] ?? ''; $code = $response['code'] ?? 0; $customMobile = $order->customMobile ?? ''; $ghsName = $order->ghsName ?? ''; //noticeUtil::push('第一个,花店结账,单号:' . $oldOrderSn . ',关单没有成功,原因:【' . $msg . '】,' . $code . ',客户手机号:' . $customMobile . ',供货商:' . $ghsName, '15280215347'); } //收银台关单 $closeParams = ['orderSn' => $orderSn]; $response = $laResource->cashClose($closeParams); if (!isset($response['code']) || $response['code'] != '000000') { $msg = $response['msg'] ?? ''; $code = $response['code'] ?? 0; //noticeUtil::push('第二个,花店结账,单号:' . $oldOrderSn . ',收银台关单没有成功,原因:【' . $msg . '】,' . $code, '15280215347'); } $newOrderSn = orderSn::getPurchaseClearSn(); $orderSn = $newOrderSn; $order->orderSn = $newOrderSn; $order->save(); } $order->changePrice = 2; $order->save(); //花卉宝代为申请的微信支付 $merchantExtend = WxOpenClass::getWxInfo(); $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem'; $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer'; $params = [ 'appid' => 'OP00002119', 'serial_no' => '018b08cfddbd', 'merchant_no' => $ghsShop->lklSjNo, 'term_no' => $ghsShop->lklScanTermNo, 'merchantPrivateKeyPath' => $merchantPrivateKeyPath, 'lklCertificatePath' => $lklCertificatePath, ]; $laResource = new Lakala($params); $notifyUrl = Yii::$app->params['hdHost'] . "/notice/pay-callback"; $name = '采购单结算 ' . $orderSn; $wxParams = [ 'orderSn' => $orderSn, 'amount' => $totalFee, 'capitalType' => $capitalType, 'notifyUrl' => $notifyUrl, 'wxAppId' => $merchantExtend['miniAppId'], 'openId' => $openId, 'subject' => $name, 'couponId' => $couponId, ]; $response = $laResource->driveWxPay($wxParams); if (isset($response['code']) == false || $response['code'] != 'BBS00000') { $errMsg = $response['msg'] ?? ''; $ghsName = $order->ghsName ?? ''; $ghsShopName = $order->ghsShopName ?? ''; $ghsFullName = $ghsName . $ghsShopName; $customName = $order->customName ?? ''; $customFullName = $customName; noticeUtil::push("\n提示:结账付款失败\n原因:" . $errMsg . "。\n商家:" . $ghsFullName . "\n客户:" . $customFullName, '15280215347'); util::fail('结账付款失败,' . $errMsg . ',请联系门店'); } $newParams = isset($response['resp_data']['acc_resp_fields']) ? $response['resp_data']['acc_resp_fields'] : []; $appId = $newParams['app_id'] ?? ''; $nonceStr = $newParams['nonce_str'] ?? ''; $paySign = $newParams['pay_sign'] ?? ''; $package = $newParams['package'] ?? ''; $signType = $newParams['sign_type'] ?? ''; $timeStamp = $newParams['time_stamp'] ?? ''; $new = [ 'id' => $id, 'appId' => $appId, 'nonceStr' => $nonceStr, 'paySign' => $paySign, 'package' => $package, 'signType' => $signType, 'timeStamp' => $timeStamp, 'orderSn' => $orderSn, ]; util::success($new); } public function actionAliPay() { ini_set('date.timezone', 'Asia/Shanghai'); $post = Yii::$app->request->post(); $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0; $order = PurchaseClearClass::getByCondition(['orderSn' => $orderSn], true); if (empty($order)) { util::fail('账单号无效'); } if ($order->status == PurchaseClearClass::STATUS_HAS_PAY) { util::fail('账单已结清!'); } if ($order->status == PurchaseClearClass::STATUS_EXPIRE) { util::fail('账单已取消!'); } $oldOrderSn = $orderSn; $current = time(); $deadline = $order->deadline; if ($current > strtotime($deadline)) { $order->status = PurchaseClearClass::STATUS_EXPIRE; $order->save(); util::fail('订单已经失效,请重新发起结算'); } $totalFee = $order->actPrice; //避免重复提交 util::checkRepeatCommit($orderSn, 8); $capitalType = dict::getDict('capitalType', 'hdPurchaseClear', 'id'); $ghsShopId = $order->ghsShopId ?? 0; $ghsShop = ShopClass::getById($ghsShopId, true); if (empty($ghsShop)) { util::fail('没有供货商门店信息'); } if (isset($ghsShop->lklSjNo) && empty($ghsShop->lklSjNo)) { util::fail('商家支付功能未开通'); } //已经唤起支付,根据拉卡拉的规则,需要生成新的订单号 if ($order->changePrice == 2) { //老单号需要关单,否则有付款成功的风险,老单号关单成功了,才能生成新单号 $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem'; $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer'; $params = [ 'appid' => 'OP00002119', 'serial_no' => '018b08cfddbd', 'merchant_no' => $ghsShop->lklSjNo, 'term_no' => $ghsShop->lklScanTermNo, 'merchantPrivateKeyPath' => $merchantPrivateKeyPath, 'lklCertificatePath' => $lklCertificatePath, ]; $laResource = new Lakala($params); $closeParams = [ 'orderSn' => $oldOrderSn, ]; $response = $laResource->cashClose($closeParams); if (isset($response['code']) == false || $response['code'] != '000000') { $msg = $response['msg'] ?? ''; $code = $response['code'] ?? 0; if (!in_array($code, ['000091'])) { noticeUtil::push("花店结账模块 " . $oldOrderSn . " 关单没有成功 收银台-支付宝模块 " . $msg, '15280215347'); } } $newOrderSn = orderSn::getPurchaseClearSn(); $orderSn = $newOrderSn; $order->orderSn = $newOrderSn; $order->save(); } $order->changePrice = 2; $order->save(); $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem'; $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer'; $params = [ 'appid' => 'OP00002119', 'serial_no' => '018b08cfddbd', 'merchant_no' => $ghsShop->lklSjNo, 'term_no' => $ghsShop->lklScanTermNo, 'merchantPrivateKeyPath' => $merchantPrivateKeyPath, 'lklCertificatePath' => $lklCertificatePath, ]; $laResource = new Lakala($params); $notifyUrl = Yii::$app->params['hdHost'] . "/notice/cash-pay-callback"; $wxParams = [ 'orderSn' => $orderSn, 'amount' => $totalFee, 'capitalType' => $capitalType, 'notifyUrl' => $notifyUrl, 'subject' => $orderSn, ]; $response = $laResource->cashierPay($wxParams); if (isset($response['code']) == false || $response['code'] != '000000') { $errMsg = $response['msg'] ?? ''; $ghsName = $order->ghsName ?? ''; $ghsShopName = $order->ghsShopName ?? ''; $ghsFullName = $ghsName . $ghsShopName; $customName = $order->customName ?? ''; $customFullName = $customName; noticeUtil::push("\n提示:结账付款失败\n原因:" . $errMsg . "。\n商家:" . $ghsFullName . "\n客户:" . $customFullName, '15280215347'); util::fail('结账付款失败,' . $errMsg . ',请联系门店'); } $payUrl = $response['resp_data']['counter_url'] ? $response['resp_data']['counter_url'] : ''; $hasRenew = $ghsShop->hasRenew ?? 0; $new = [ 'payUrl' => $payUrl, 'orderSn' => $orderSn, 'hasRenew' => 1, ]; util::success($new); } }