$customId, 'status' => 0], null, '*', null, true); if (!empty($hasList)) { $shop = \bizHd\merchant\classes\ShopClass::getById($shopId, true); if (empty($shop)) { util::fail('没有找到门店'); } foreach ($hasList as $k => $recharge) { $payRequest = $recharge->payRequest ?? 0; if ($payRequest == 1 || $payRequest == 2) { $closeOrderSn = $recharge->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' => $shop->lklSjNo, 'term_no' => $shop->lklScanTermNo, 'merchantPrivateKeyPath' => $merchantPrivateKeyPath, 'lklCertificatePath' => $lklCertificatePath, ]; $laResource = new Lakala($params); $closeParams = ['orderSn' => $closeOrderSn]; if ($payRequest == 1) { //聚合支付 $response = $laResource->close($closeParams); if (!isset($response['code']) || $response['code'] != 'BBS00000') { $msg = $response['msg'] ?? ''; noticeUtil::push('充值换单,聚合支付,没有成功,单号:' . $closeOrderSn . ',关单没有成功,编号966558,' . $msg, '15280215347'); //util::fail('老订单取消失败,请联系管理员'); } } else { //收银台支付 $response = $laResource->cashClose($closeParams); if (!isset($response['code']) || $response['code'] != '000000') { $msg = $response['msg'] ?? ''; noticeUtil::push('充值换单,收银台的单号:' . $closeOrderSn . ',关单没有成功,编号66996,' . $msg, '15280215347'); //util::fail('旧订单取消失败,请联系管理员'); } } } $recharge->status = 2; $recharge->save(); $settleId = $recharge->settleId ?? 0; $settle = SettleClass::getById($settleId, true); if (!empty($settle)) { $settle->status = 3; $settle->save(); } } } return self::add($data, true); } //第三支付后流程 ssh 2021.4.27 public static function thirdPay($payWay, $orderSn, $totalFee, $attach) { $recharge = RechargeClass::getByCondition(['orderSn' => $orderSn], true); if (empty($recharge)) { noticeUtil::push('充值付款成功,收到回调,但没有找到充值记录,orderSn:' . $orderSn, '15280215347'); return false; } if ($totalFee != $recharge->amount) { noticeUtil::push("充值付款成功,收到回调,但金额不一致 {$totalFee} {$recharge->amount},orderSn:" . $orderSn, '15280215347'); return false; } if ($recharge->payStatus == 1) { noticeUtil::push("充值付款成功,收到回调,但订单是已经支付过了,orderSn:" . $orderSn, '15280215347'); return false; } $id = $recharge->id; $recharge = RechargeClass::getLockById($id); $params = [ 'onlinePay' => 2, 'side' => 1, ]; self::complete($recharge, $payWay, $params); return $recharge; } //处理充值流程 ssh 20250321 public static function complete($recharge, $payWay, $params) { $onlinePay = $params['onlinePay']; //0 花店端操作,1 客户端操作 $side = $params['side'] ?? 0; $recharge->onlinePay = $onlinePay; $recharge->payWay = $payWay; $recharge->payStatus = 1; $recharge->status = 1; $recharge->save(); $shopId = $recharge->shopId; $mainId = $recharge->mainId; $hdId = $recharge->hdId; $hdInfo = HdClass::getById($hdId, true); $hdName = $hdInfo->name ?? ""; $amount = $recharge->amount; $staffId = $recharge->staffId; $payWay = $recharge->payWay; $staffName = $recharge->staffName; $customId = $recharge->customId; $customName = $recharge->customName; $capitalType = dict::getDict('capitalType', 'xhRecharge', 'id'); $relateId = $recharge->id; $shOrderId = $recharge->shOrderId ?? 0; $shOrderSn = $recharge->shOrderSn ?? ''; $custom = CustomClass::getLockById($customId); if (empty($custom)) { $msg = '充值回调失败,没有找到客户'; util::fail($msg); } $oldLevel = $custom->member ?? 0; $hd = HdClass::getLockById($hdId); if (empty($hd)) { $msg = '充值回调失败,没有找到花店'; util::fail($msg); } $shop = ShopClass::getById($shopId, true); $rechargeWeal = $shop->rechargeWeal ?? 0; $balanceGive = 0; //如果客户有欠款、结账充值、售后充值、售后付款充值,不参与优惠 if ($custom->balance >= 0 && $recharge->settleId == 0 && $recharge->shOrderId == 0 && $recharge->refundOrderId == 0) { if ($rechargeWeal == 1) { $weal = MemberWealClass::getAllByCondition(['shopId' => $shopId], 'recharge ASC', '*'); if (!empty($weal)) { //优惠方式2:充多少送多少 foreach ($weal as $k => $v) { $thisRecharge = $v['recharge'] ?? 0; $thisGive = $v['give'] ?? 0; if ($amount >= $thisRecharge) { $recharge->giveAmount = $thisGive; $recharge->save(); //增加赠送的金额 $balanceGive = $thisGive; } } } } } $balancePay = $amount; if ($onlinePay == 2) { $event = $payWay == 0 ? '线上微信 充值' : '线上支付宝 充值'; } else { $payWayName = dict::getDict('payWayName'); $channel = $payWayName[$payWay] ?? '微信'; $event = "线下{$channel} 充值"; } $event .= floatval($balancePay); //赠送的余额 if ($balanceGive > 0) { $event .= "(送" . floatval($balanceGive) . ")"; $custom->balanceGive = bcadd($custom->balanceGive, $balanceGive, 2); $hd->balanceGive = bcadd($hd->balanceGive, $balanceGive, 2); } //充值的余额 $custom->balancePay = bcadd($custom->balancePay, $balancePay, 2); $hd->balancePay = bcadd($hd->balancePay, $balancePay, 2); //总余额 $totalBalance = bcadd($balancePay, $balanceGive, 2); $custom->balance = bcadd($custom->balance, $totalBalance, 2); $hd->balance = bcadd($hd->balance, $totalBalance, 2); $custom->save(); $hd->save(); if ($hd->balance != $custom->balance) { $msg = '充值金额不一致。编号:' . floatval($hd->balance) . ' / ' . floatval($custom->balance); Yii::info($msg); util::fail($msg); } if ($hd->balancePay != $custom->balancePay) { $msg = '充值金额有异常。编号:' . floatval($hd->balancePay) . ' / ' . floatval($custom->balancePay); Yii::info($msg); util::fail($msg); } if ($hd->balanceGive != $custom->balanceGive) { $msg = '充值金额异常。编号:' . floatval($hd->balanceGive) . ' / ' . floatval($custom->balanceGive); Yii::info($msg); util::fail($msg); } $addBalance = bcadd($hd->balanceGive, $hd->balancePay, 2); if (floatval($hd->balance) != floatval($addBalance)) { util::fail('充值金额有问题哦,编号:' . $customId); } $settleId = $recharge->settleId; $settleAmount = 0; $settleNo = ''; $realClearAmount = 0; if (!empty($settleId)) { //如果有结账单,按结账单销账 $set = SettleClass::getById($settleId, true); $res = SettleClass::clearSettle($set, $recharge); $realClearAmount = $res['totalClearAmount'] ?? 0; $settleAmount = $set->actPrice ?? 0; $settleNo = $set->orderSn ?? ''; } else { //没有结账单,按顺序去销订单欠款 $orderList = OrderClass::getAllByCondition(['customId' => $customId, 'debt' => 1], 'id asc', '*', null, true); if (!empty($orderList)) { $lastRemainDebtAmount = 0; $needClearIdMap = []; $hasOver = false; foreach ($orderList as $order) { $orderId = $order->id ?? 0; $orderSn = $order->orderSn ?? ''; //销订单全部的欠款!!! $remainDebtPrice = $order->remainDebtPrice ?? 0; $currentTotalDebtAmount = bcadd($lastRemainDebtAmount, $remainDebtPrice, 2); if ($currentTotalDebtAmount > $totalBalance) { if (!$hasOver) { $lastClearOrderAmount = bcsub($totalBalance, $lastRemainDebtAmount, 2); if ($lastClearOrderAmount > 0) { //销订单部分的欠款!!! $lastClearOrderId = $orderId; $needClearIdMap[] = ['orderId' => $lastClearOrderId, 'clearAmount' => $lastClearOrderAmount, 'orderSn' => $orderSn]; } } $hasOver = true; continue; } $lastRemainDebtAmount = bcadd($lastRemainDebtAmount, $remainDebtPrice, 2); $needClearIdMap[] = ['orderId' => $orderId, 'clearAmount' => $remainDebtPrice, 'orderSn' => $orderSn]; } if (!empty($needClearIdMap)) { //创建结账单,并且销结账单 $staffId = $recharge->staffId ?? 0; $staffName = $recharge->staffName ?? ''; $settleParams = ['staffId' => $staffId, 'staffName' => $staffName]; $set = SettleClass::addSettle($needClearIdMap, $shop, $custom, $hd, $settleParams); $settleId = $set->id; $settleNo = $set->orderSn ?? ''; $settleAmount = $set->actPrice ?? 0; $res = SettleClass::clearSettle($set); $realClearAmount = $res['totalClearAmount'] ?? 0; } } } if ($settleId > 0) { if (floatval($totalBalance) != floatval($realClearAmount) && $hd->balance < 0) { $rechargeId = $recharge->id; noticeUtil::push("注意,充值单,结账金额不一致:getBalance:{$totalBalance} totalAmount:{$realClearAmount},recharge:{$rechargeId}", '15280215347'); util::fail('充值和结账金额异常哈'); } if (floatval($totalBalance) != floatval($settleAmount) && $hd->balance < 0) { $rechargeId = $recharge->id; noticeUtil::push("注意,充值单,结账单不一致:getBalance:{$totalBalance} settleAmount:{$settleAmount},settleId:{$settleId} recharge:{$rechargeId}", '15280215347'); util::fail('充值和结账金额异常呢'); } if (floatval($realClearAmount) != floatval($settleAmount)) { $rechargeId = $recharge->id; noticeUtil::push("注意,结账金额有问题,编号 settleId:{$settleId} recharge:{$rechargeId}", '15280215347'); util::fail('充值结账金额异常呢'); } } $remark = $recharge->remark; $change = [ 'customId' => $customId, 'customName' => $customName, 'relateId' => $relateId, 'onlinePay' => $onlinePay, 'capitalType' => $capitalType, 'amount' => $totalBalance, 'balance' => $hd->balance, 'io' => 1, 'side' => $side, 'payWay' => $payWay, 'event' => $event, 'staffId' => $staffId, 'staffName' => $staffName, 'shopId' => $shopId, 'mainId' => $mainId, 'settleId' => $settleId, 'settleNo' => $settleNo, 'settleAmount' => $settleAmount, 'remark' => $remark, 'shOrderId' => $shOrderId, 'shOrderSn' => $shOrderSn, 'hdId' => $hdId, 'hdName' => $hdName, ]; $returnChange = BalanceChangeClass::add($change, true); $payChange = [ 'customId' => $customId, 'customName' => $customName, 'relateId' => $relateId, 'onlinePay' => $onlinePay, 'capitalType' => $capitalType, 'amount' => $balancePay, 'balance' => $hd->balancePay, 'io' => 1, 'side' => $side, 'payWay' => $payWay, 'event' => $event, 'staffId' => $staffId, 'staffName' => $staffName, 'shopId' => $shopId, 'mainId' => $mainId, 'settleId' => $settleId, 'settleNo' => $settleNo, 'settleAmount' => $settleAmount, 'remark' => $remark, 'shOrderId' => $shOrderId, 'shOrderSn' => $shOrderSn, 'hdId' => $hdId, 'hdName' => $hdName, ]; BalancePayChangeClass::add($payChange, true); if ($balanceGive > 0) { $givChange = [ 'customId' => $customId, 'customName' => $customName, 'relateId' => $relateId, 'onlinePay' => $onlinePay, 'capitalType' => $capitalType, 'amount' => $balanceGive, 'balance' => $hd->balanceGive, 'io' => 1, 'side' => $side, 'payWay' => $payWay, 'event' => $event, 'staffId' => $staffId, 'staffName' => $staffName, 'shopId' => $shopId, 'mainId' => $mainId, 'remark' => $remark, 'hdId' => $hdId, 'hdName' => $hdName, ]; BalanceGiveChangeClass::add($givChange, true); } if ($recharge->onlinePay == 2) { //如果线上充值,门店余额增加 $shop = ShopClass::getLockById($shopId); $main = MainClass::getLockById($mainId); ShopClass::skRechargeAddBalance($main, $shop, $recharge); } else { if ($recharge->payWay == 4) { //现在现金充值要增加现金 $main = MainClass::getLockById($mainId); $cashData = []; $cashData['mainId'] = $mainId; $cashData['sjId'] = 0; $cashData['amount'] = $amount; $cashData['staffId'] = $staffId; $cashData['staffName'] = $staffName; $cashData['customName'] = $customName; $cashData['ptStyle'] = dict::getDict('ptStyle', 'hd'); ShopMoneyClass::skRecharge($cashData, $main); } } return $returnChange; } /** * 售后付款单是否已经充值过了 ssh 20250801 */ public static function checkShOrderRecharge($order) { $orderId = $order->id; $has = self::getByCondition(['shOrderId' => $orderId, 'payStatus' => 1]); if (!empty($has)) { util::fail('已经充值过了,无需再次操作'); } } }