where(['order_no' => $orderNo])->find(); if (empty($order)) throw new Exception("订单不存在"); if ($order['status'] !== 2) throw new Exception("不可发起支付"); // 创建支付行为 $this->createPaymentAction($orderNo, $payTitle, $payAmount); // 检查能否支付 [$total, $count] = UserBalanceService::amount($order['uuid'], [$orderNo]); if ($payAmount > $total - $count) throw new Exception("可抵扣余额不足"); try { // 扣减用户余额 $this->app->db->transaction(function () use ($order, $payAmount) { // 更新订单余额 ShopOrder::mk()->where(['order_no' => $order['order_no']])->update([ 'payment_balance' => $payAmount, ]); // 扣除余额金额 DataUserBalance::mUpdate([ 'uuid' => $order['uuid'], 'code' => "KC{$order['order_no']}", 'name' => "账户余额支付", 'remark' => "支付订单 {$order['order_no']} 的扣除余额 {$payAmount} 元", 'amount' => -$payAmount, ], 'code'); // 更新支付行为 $this->updatePaymentAction($order['order_no'], CodeExtend::uniqidDate(20), $payAmount, '账户余额支付'); }); // 刷新用户余额 UserBalanceService::amount($order['uuid']); return ['code' => 1, 'info' => '余额支付完成']; } catch (\Exception $exception) { return ['code' => 0, 'info' => $exception->getMessage()]; } } }