$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); } $hd = HdClass::getLockById($hdId); if (empty($hd)) { $msg = '充值回调失败,没有找到花店'; util::fail($msg); } $shop = ShopClass::getById($shopId, true); $rechargeWeal = $shop->rechargeWeal ?? 0; //充值赠送: 0未设置 2充值送红包--红包非余额支付可用,3充值送红包--红包非余额支付不可用 $balanceGive = 0; //如果客户有欠款、结账充值、售后充值、售后付款充值,不参与优惠 if ($custom->balance >= 0 && $recharge->settleId == 0 && $recharge->shOrderId == 0 && $recharge->refundOrderId == 0) { if ($rechargeWeal == 1) { $weal = RechargeSqClass::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; } } } } elseif ($rechargeWeal == 2 || $rechargeWeal == 3) { //充值送红包 // 1.根据充值金额,查询符合条件的红包规则 $maxHbRule = RechargeShbClass::getByCondition(['shopId' => $shopId, 'amount<=' => $amount], false, 'allHbAmount DESC'); if (!empty($maxHbRule)) { $currentTime = time(); $rulesJson = $maxHbRule['rules'] ?? ''; $rulesArr = json_decode($rulesJson, true); if (is_array($rulesArr) && !empty($rulesArr)) { foreach ($rulesArr as $rule) { $hbAmount = $rule['hbAmount']; $hbNum = $rule['hbNum']; $miniCost = $rule['miniCost']; $duration = $rule['duration']; $effectiveType = $rule['effectiveType'] ?? 2; $effectiveDate = $rule['effectiveDate'] ?? ''; $effectiveDays = $rule['effectiveDays'] ?? 0; if ($effectiveType == 1 && !empty($effectiveDate) && $effectiveDate != '0000-00-00') { $beginTime = strtotime($effectiveDate); if (!$beginTime) { $beginTime = $currentTime; } } else { $beginTime = $currentTime + 86400 * $effectiveDays; } //给用户生成红包 $data = [ 'mainId' => $mainId, 'shopId' => $shopId, 'userId' => $recharge->userId, 'customId' => $customId, 'amount' => $hbAmount, 'minConsume' => $miniCost, 'beginTime' => $beginTime, 'endTime' => $duration == 0 ? 4102416000 : ($beginTime + 86400 * $duration), 'willTime' => $duration == 0 ? 4102416000 : ($beginTime + 86400 * $duration), 'duration' => $duration, 'getType' => 2, //取得方式 0新人领取 1门店发放 2充值赠送 'remark' => '充值赠送红包', ]; $manageData = [ 'getType' => 2, 'getTargetId' => 0, 'createStaffId' => 0, 'createStaffName' => '', 'remark' => $data['remark'] ?? '', ]; for ($i = 0; $i < $hbNum; $i++) { $hb = HbClass::add($data); $manageData['hbId'] = $hb['id']; HbManageClass::add($manageData); } } } } } else { //其他 } } //是否开启充值满多少成为 VIP $shopExt = ShopExtClass::getByCondition(['shopId' => $shopId], false, 'reachVip'); if (!empty($shopExt) && $shopExt['reachVip'] > 0.00 && bccomp($amount, $shopExt['reachVip'], 2) >= 0) { $custom->vip = 1; } $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); } //充值的余额 $beforeBalancePay = $custom->balancePay; $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); $changeAmount = bccomp($beforeBalancePay, 0, 2) === -1 ? $custom->balancePay : $balancePay; // 根据余额欠款,计算变化金额,用于计算积分与成长值 $addIntegralAndGrowth = bccomp($changeAmount, 0, 2) === 1; // 如果变化金额大于0,则计算积分与成长值 if ($addIntegralAndGrowth) { //积分与成长值 $custom->integral = bcadd($custom->integral, $changeAmount, 2); $custom->growth = bcadd($custom->growth, $changeAmount, 2); // 由于没有更新 buyAmount,所以得额外添加更新等级 $memberData = CustomClass::getCustomExpenseLevel($custom->growth, $mainId); // 设置等级多处需要同步修改的,请搜索:getCustomExpenseLevel CustomClass::updateById($customId, [ 'member' => (int) ($memberData['level'] ?? 0), 'memberName' => (string) ($memberData['name'] ?? ''), ]); } $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, 'giveAmount' => $balanceGive, '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); } } if ($addIntegralAndGrowth) { $createTime = date('Y-m-d H:i:s'); //成长值变动记录 $growthData = [ 'shopId' => $shopId, 'userId' => $custom->userId, 'userName' => $custom->name, 'event' => $event, 'relateId' => $relateId, 'relateType' => 0, 'num' => $changeAmount, 'growth' => $custom->growth, 'staffId' => $staffId, //操作人(员工id) 'staffName' => $staffName, //员工名称 'createTime' => $createTime, ]; UserGrowthClass::add($growthData); //积分变动记录 $integralData = [ 'shopId' => $shopId, 'userId' => $custom->userId, 'userName' => $custom->name, 'event' => $event, 'relateId' => $relateId, 'relateType' => 0, 'num' => $changeAmount, 'integral' => $custom->integral, 'staffId' => $staffId, //操作人(员工id) 'staffName' => $staffName, //员工名称 'createTime' => $createTime, ]; UserIntegralClass::add($integralData); } return $returnChange; } /** * 售后付款单是否已经充值过了 ssh 20250801 */ public static function checkShOrderRecharge($order) { $orderId = $order->id; $has = self::getByCondition(['shOrderId' => $orderId, 'payStatus' => 1]); if (!empty($has)) { util::fail('已经充值过了,无需再次操作'); } } }