user_id !== (int) $actor['user_id']) { throw new CloudError('只能为自己持有的实例续费', 403); } $requestHash = hash_hmac('sha256', 'renew:' . (int) $instance->id, CloudSecrets::key('payload_key')); return (new User())->getConnection()->transaction(function () use ($instance, $actor, $idempotency, $requestHash) { $user = User::where('id', (int) $actor['user_id'])->lockForUpdate()->first(); if ($user === null || (int) $user->enable !== 1) { throw new CloudError('账户不可用', 403); } if ((int) $user->restrict_balance_purchase === 1) { throw new CloudError('账户套餐购买服务已被管理员停用', 403); } $existing = CloudRenewal::where('user_id', $user->id) ->where('idempotency_key', $idempotency)->first(); if ($existing !== null) { if (!hash_equals((string) $existing->request_hash, $requestHash)) { throw new CloudError('同一请求标识不能用于不同实例', 409); } return $existing; } $locked = CloudInstance::where('id', $instance->id)->lockForUpdate()->first(); if ($locked === null || $locked->status === 'deleted' || (int) $locked->user_id !== (int) $user->id) { throw new CloudError('实例不存在或无权续费', 404); } if (CloudInvoiceService::instanceDeleting($locked)) { throw new CloudError('实例已超过续费宽限期,正在删除或等待核对', 409); } if (CloudJob::where('instance_id', $locked->id) ->whereIn('kind', ['delete', 'expiry_delete']) ->whereIn('status', ['queued', 'running', 'uncertain'])->exists()) { throw new CloudError('实例正在删除或等待核对,不能续费', 409); } $days = (int) $locked->renewal_days; $price = $locked->renewal_price === null ? -1 : CloudMoney::cents((string) $locked->renewal_price); if ($days < 1 || $price < 0 || $locked->expires_at === null) { throw new CloudError('此实例没有可续费的套餐快照', 409); } $now = time(); $invoice = CloudInvoice::where('instance_id', $locked->id) ->where('renews_expires_at', $locked->expires_at) ->where('status', '<>', 'void')->orderByDesc('id') ->lockForUpdate()->first(); $deadline = $invoice === null ? (int) $locked->expires_at + CloudInvoiceService::PAYMENT_GRACE_SECONDS : CloudInvoiceService::paymentDeadline($invoice); if (($invoice !== null && (string) $invoice->status === 'expired') || $deadline <= $now) { throw new CloudError('续费宽限期已结束,实例将被删除', 409); } $balance = CloudMoney::cents((string) $user->money); if ($balance < $price) { throw new CloudError('账户余额不足', 409); } $currentPeriod = $locked->current_period_id === null ? null : CloudServicePeriod::where('id', $locked->current_period_id) ->where('instance_id', $locked->id)->lockForUpdate()->first(); if ($currentPeriod !== null && in_array((string) $currentPeriod->status, ['active', 'exhausted'], true) && (int) $currentPeriod->ends_at <= $now) { if ($currentPeriod->download_base_bytes !== null && $currentPeriod->upload_base_bytes !== null) { $currentPeriod->download_used_bytes = max(0, (int) $locked->lifetime_download_bytes - (int) $currentPeriod->download_base_bytes); $currentPeriod->upload_used_bytes = max(0, (int) $locked->lifetime_upload_bytes - (int) $currentPeriod->upload_base_bytes); } $currentPeriod->status = 'finished'; $currentPeriod->updated_at = $now; $currentPeriod->save(); $locked->current_period_id = null; } CloudServicePeriod::where('instance_id', $locked->id) ->where('status', 'scheduled')->where('ends_at', '<=', $now) ->update(['status' => 'finished', 'updated_at' => $now]); $maximumScheduled = max(1, min(120, (int) \App\Services\Config::get('cloud_max_renewal_periods_ahead', 24))); if (CloudServicePeriod::where('instance_id', $locked->id) ->where('status', 'scheduled')->count() >= $maximumScheduled) { throw new CloudError('提前续费周期已达到上限,请在后续账期开始后再续费', 409); } $lastPeriod = CloudServicePeriod::where('instance_id', $locked->id) ->orderByDesc('ends_at')->orderByDesc('id')->lockForUpdate()->first(); if ($lastPeriod === null) { throw new CloudError('实例缺少有效的服务周期,无法续费', 409); } $previousExpiry = max(0, (int) $locked->expires_at, (int) $lastPeriod->ends_at); $startsAt = max($now, $previousExpiry); $newExpiry = $startsAt + $days * 86400; if ($newExpiry < $startsAt) { throw new CloudError('续费时间超出系统范围', 409); } $addedTraffic = max(0, (int) $locked->renewal_traffic_bytes); $renewal = new CloudRenewal(); $renewal->instance_id = $locked->id; $renewal->user_id = $user->id; $renewal->plan_id = $locked->plan_id; $renewal->idempotency_key = $idempotency; $renewal->request_hash = $requestHash; $renewal->amount = CloudMoney::decimal($price); $renewal->validity_days = $days; $renewal->traffic_bytes_added = $addedTraffic; $renewal->previous_expires_at = $previousExpiry; $renewal->new_expires_at = $newExpiry; $renewal->created_at = $now; $renewal->save(); $period = new CloudServicePeriod(); $period->instance_id = $locked->id; $period->user_id = $user->id; $period->plan_id = $locked->plan_id; $period->renewal_id = $renewal->id; $period->starts_at = $startsAt; $period->ends_at = $newExpiry; $period->traffic_quota_bytes = $addedTraffic; $period->status = $startsAt <= $now ? 'active' : 'scheduled'; $period->activated_at = $startsAt <= $now ? $now : null; if ($period->status === 'active') { $baselineReady = (int) $locked->traffic_synced_at >= $startsAt; $period->download_base_bytes = $baselineReady ? max(0, (int) $locked->lifetime_download_bytes) : null; $period->upload_base_bytes = $baselineReady ? max(0, (int) $locked->lifetime_upload_bytes) : null; } $period->created_at = $period->updated_at = $now; $period->save(); $renewal->period_id = $period->id; $renewal->save(); $user->money = CloudMoney::decimal($balance - $price); $user->save(); $locked->expires_at = $newExpiry; if ($period->status === 'active') { $locked->current_period_id = $period->id; $locked->traffic_quota_bytes = $addedTraffic; $locked->traffic_download_bytes = 0; $locked->traffic_upload_bytes = 0; if (!in_array((string) $locked->service_status, ['active', 'exempt'], true)) { $legacySuspend = $locked->suspended_at !== null || CloudJob::where('instance_id', $locked->id) ->where('kind', 'service_suspend') ->whereIn('status', ['queued', 'running', 'uncertain'])->exists(); $locked->service_status = $legacySuspend ? 'resume_pending' : 'active'; } if ((string) $locked->network_status !== 'active') { $locked->network_status = 'restore_pending'; $locked->network_suspended_at = null; } } $locked->updated_at = $now; $locked->save(); return $renewal; }, 3); } public static function display(CloudRenewal $renewal): array { return [ 'id' => (string) $renewal->id, 'instance_id' => (string) $renewal->instance_id, 'amount' => (string) $renewal->amount, 'validity_days' => (int) $renewal->validity_days, 'traffic_bytes_added' => (int) $renewal->traffic_bytes_added, 'period_id' => $renewal->period_id === null ? null : (string) $renewal->period_id, 'previous_expires_at' => (int) $renewal->previous_expires_at, 'new_expires_at' => (int) $renewal->new_expires_at, 'created_at' => (int) $renewal->created_at, ]; } }