shish vor 1 Monat
Ursprung
Commit
a052eab160

+ 317 - 292
app-hd/controllers/PurchaseClearController.php

@@ -23,146 +23,200 @@ class PurchaseClearController extends BaseController
     //生成结帐订单 ssh 2021.1.26
     public function actionCreateOrder()
     {
-        $post = Yii::$app->request->post();
-        $ghsId = $post['ghsId'] ?? 0;
-        $ghs = GhsClass::getById($ghsId, true);
-        if (empty($ghs)) {
-            util::fail('没有供货商');
-        }
-        // 花店向供货商结账建单前:挂账并入净 balance
-        AccountMoneyClass::ensureGhsMoneyReady($ghs, true);
-        //解决重复请求问题
-        $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']);
+        $transaction = Yii::$app->db->beginTransaction();
+        try {
+            $post = Yii::$app->request->post();
+            $ghsId = $post['ghsId'] ?? 0;
+            $ghs = GhsClass::getById($ghsId, true);
+            if (empty($ghs)) {
+                util::fail('没有供货商');
+            }
+            // 花店向供货商结账建单前:挂账并入净 balance
+            AccountMoneyClass::ensureGhsMoneyReady($ghs, true);
+            //解决重复请求问题
+            $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();
-                OrderCgClearClass::markExpireByClearId($item->id);
+            //老的没有清的结账单直接过期掉
+            $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();
+                    OrderCgClearClass::markExpireByClearId($item->id);
 
-                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');
+                    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;
+            $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;
 
-        //如果是虚拟关系,暂时允许花店一键结账
-        $live = $ghs->live ?? 1;
-        $respond['live'] = $live;
+            //如果是虚拟关系,暂时允许花店一键结账
+            $live = $ghs->live ?? 1;
+            $respond['live'] = $live;
 
-        util::success($respond);
+            $transaction->commit();
+            util::success($respond);
+        } catch (\Exception $e) {
+            $transaction->rollBack();
+            util::fail('创建结账单失败:' . $e->getMessage());
+            noticeUtil::push('创建结账单失败:' . $e->getMessage(), '15280215347');
+        }
     }
 
     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('账单已取消');
-        }
+        $transaction = Yii::$app->db->beginTransaction();
+        try {
+            $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);
+            //避免重复提交
+            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');
+            $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 (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 (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();
+                OrderCgClearClass::updateClearSnByClearId($id, $newOrderSn);
+            }
+            $order->changePrice = 2;
+            $order->save();
 
-        //已经唤起过微信支付了,根据拉卡拉的规则,需要生成新的订单号
-        if ($order->changePrice == 2) {
+            //花卉宝代为申请的微信支付
+            $merchantExtend = WxOpenClass::getWxInfo();
 
-            //老单号需要关单,否则有付款成功的风险,老单号关单成功了,才能生成新单号
             $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
             $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
             $params = [
@@ -174,134 +228,131 @@ class PurchaseClearController extends BaseController
                 'lklCertificatePath' => $lklCertificatePath,
             ];
             $laResource = new Lakala($params);
-            $closeParams = [
-                'orderSn' => $oldOrderSn,
+            $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->close($closeParams);
-            if (!isset($response['code']) || $response['code'] != 'BBS00000') {
-                $msg = $response['msg'] ?? '';
-                $code = $response['code'] ?? 0;
-                $customMobile = $order->customMobile ?? '';
+            $response = $laResource->driveWxPay($wxParams);
+            if (isset($response['code']) == false || $response['code'] != 'BBS00000') {
+                $errMsg = $response['msg'] ?? '';
                 $ghsName = $order->ghsName ?? '';
-                //noticeUtil::push('第一个,花店结账,单号:' . $oldOrderSn . ',关单没有成功,原因:【' . $msg . '】,' . $code . ',客户手机号:' . $customMobile . ',供货商:' . $ghsName, '15280215347');
+                $ghsShopName = $order->ghsShopName ?? '';
+                $ghsFullName = $ghsName . $ghsShopName;
+                $customName = $order->customName ?? '';
+                $customFullName = $customName;
+                noticeUtil::push("\n提示:结账付款失败\n原因:" . $errMsg . "。\n商家:" . $ghsFullName . "\n客户:" . $customFullName, '15280215347');
+                util::fail('结账付款失败,' . $errMsg . ',请联系门店');
             }
-
-            //收银台关单
-            $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,
+            ];
+            $transaction->commit();
+            util::success($new);
+        } catch (\Exception $e) {
+            $transaction->rollBack();
+            util::fail('发起微信支付失败:' . $e->getMessage());
+            noticeUtil::push('结账发起微信支付失败:' . $e->getMessage(), '15280215347');
         }
-        $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;
+        $transaction = Yii::$app->db->beginTransaction();
+        try {
+            $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);
+            //避免重复提交
+            util::checkRepeatCommit($orderSn, 8);
 
-        $capitalType = dict::getDict('capitalType', 'hdPurchaseClear', 'id');
-        $ghsShopId = $order->ghsShopId ?? 0;
-        $ghsShop = ShopClass::getById($ghsShopId, true);
-        if (empty($ghsShop)) {
-            util::fail('没有供货商门店信息');
-        }
+            $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 (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();
+                OrderCgClearClass::updateClearSnByClearId($order->id, $newOrderSn);
+            }
+            $order->changePrice = 2;
+            $order->save();
 
-        //已经唤起支付,根据拉卡拉的规则,需要生成新的订单号
-        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 = [
@@ -313,64 +364,38 @@ class PurchaseClearController extends BaseController
                 'lklCertificatePath' => $lklCertificatePath,
             ];
             $laResource = new Lakala($params);
-            $closeParams = [
-                'orderSn' => $oldOrderSn,
+            $notifyUrl = Yii::$app->params['hdHost'] . "/notice/cash-pay-callback";
+            $wxParams = [
+                'orderSn' => $orderSn,
+                'amount' => $totalFee,
+                'capitalType' => $capitalType,
+                'notifyUrl' => $notifyUrl,
+                'subject' => $orderSn,
             ];
-            $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');
-                }
+            $response = $laResource->cashierPay($wxParams);
+            if (!isset($response['code']) || $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 . ',请联系门店');
             }
-
-            $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,
+            ];
+            $transaction->commit();
+            util::success($new);
+        } catch (\Exception $e) {
+            $transaction->rollBack();
+            noticeUtil::push('结账发起支付宝支付失败:' . $e->getMessage(), '15280215347');
         }
-        $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);
     }
 
-}
+}

+ 21 - 0
biz-hd/purchase/classes/OrderCgClearClass.php

@@ -137,6 +137,27 @@ class OrderCgClearClass extends BaseClass
         self::markStatusByClearId($clearId, self::STATUS_HAS_PAY);
     }
 
+    /**
+     * 更新关联表中的结账单号 (clearSn)
+     * 用途:当微信/支付宝支付由于拉卡拉规则需要生成新的结账单号时,同步更新关联表中的单号,保持数据一致性
+     * 谁用:PurchaseClearController 微信/支付宝支付接口
+     */
+    public static function updateClearSnByClearId($clearId, $newClearSn)
+    {
+        $clearId = intval($clearId);
+        if ($clearId <= 0 || empty($newClearSn)) {
+            return;
+        }
+        $list = self::getAllByCondition(['clearId' => $clearId], null, '*', null, true);
+        if (empty($list)) {
+            return;
+        }
+        foreach ($list as $item) {
+            $item->clearSn = $newClearSn;
+            $item->save();
+        }
+    }
+
     public static function markExpireByClearId($clearId)
     {
         self::markStatusByClearId($clearId, self::STATUS_EXPIRE);