id ?? 0); $userId = is_array($userData) ? ($userData['id'] ?? 0) : ($userData->id ?? 0); try { if ($orderId) { Db::transaction(function () use ($orderId, $userId) { $order = Order::where('id', $orderId)->lock(true)->find(); if (!$order) { return; } $user = User::get($userId ?: $order->user_id); $ext = is_array($order->ext) ? $order->ext : (json_decode($order->ext ?? '', true) ?: []); if ($order->type == 'recharge') { // 幂等防重:检查是否已经执行过充值入账 if (empty($ext['recharge_processed']) && $order->status !== Order::STATUS_COMPLETED) { $rule = $ext['rule'] ?? []; $money = (isset($rule['money']) && $rule['money'] > 0) ? $rule['money'] : 0; $gift_type = $rule['gift_type'] ?? 'money'; $gift = (isset($rule['gift']) && $rule['gift'] > 0) ? $rule['gift'] : 0; if ($money > 0) { WalletService::change($user, 'money', $money, 'order_recharge', [ 'order_id' => $order->id, 'order_sn' => $order->order_sn, 'order_type' => 'trade_order', ]); } if ($gift > 0) { WalletService::change($user, $gift_type, $gift, 'recharge_gift', [ 'order_id' => $order->id, 'order_sn' => $order->order_sn, 'order_type' => 'trade_order', ]); } $ext['recharge_processed'] = time(); $order->status = Order::STATUS_COMPLETED; $order->ext = $ext; $order->save(); } } $uploadshoppingInfo = new WechatMiniProgramShop(Wechat::miniProgram()); // 微信小程序,使用 微信支付, 并且存在微信发货管理权限时,才推送发货消息 if ($order->platform == 'WechatMiniProgram' && $order->pay_type == 'wechat' && $uploadshoppingInfo->isTradeManaged()) { $uploadshoppingInfo->tradeUploadShippingInfos($order); } }); } // 删除 job $job->delete(); } catch (HttpResponseException $e) { $data = $e->getResponse()->getData(); $message = $data ? ($data['msg'] ?? '') : $e->getMessage(); format_log_error($e, 'TradeOrderPaid.paid.HttpResponseException', $message); $this->handleFailure($job, $orderId, $message); } catch (\Throwable $e) { $message = $e->getMessage(); format_log_error($e, 'TradeOrderPaid.paid', $message); $this->handleFailure($job, $orderId, $message); } } /** * 生产级失败与阶梯重试处理 */ protected function handleFailure(Job $job, $orderId, $errorMessage) { $attempts = $job->attempts(); $maxAttempts = 5; if ($attempts >= $maxAttempts) { \think\Log::record("[TradeOrderPaid CRITICAL] 充值订单 ID:{$orderId} 处理连续失败 {$attempts} 次已达上限: {$errorMessage}", 'error'); $this->failed([ 'job' => 'trade\OrderPaid@paid', 'order_id' => $orderId, 'error' => $errorMessage, 'attempts' => $attempts, 'failed_time' => time(), ]); $job->delete(); } else { $delay = min(60, $attempts * 5); $job->release($delay); } } }