addConnection(['driver' => 'sqlite', 'database' => ':memory:'], 'default'); $capsule->setAsGlobal(); $capsule->bootEloquent(); $schema = $capsule->schema('default'); $schema->create('user', static function (Blueprint $table): void { $table->increments('id'); $table->string('money'); $table->integer('enable')->default(1); $table->integer('restrict_recharge')->default(0); $table->integer('restrict_balance_purchase')->default(0); }); $schema->create('cloud_instances', static function (Blueprint $table): void { $table->increments('id'); $table->integer('host_id'); $table->integer('user_id'); $table->integer('order_id')->nullable(); $table->integer('plan_id')->nullable(); $table->integer('current_period_id')->nullable(); $table->string('uuid'); $table->string('name'); $table->string('status'); $table->string('service_status'); $table->string('network_status'); $table->integer('expires_at')->nullable(); $table->string('renewal_price')->nullable(); $table->integer('renewal_days')->default(0); $table->integer('renewal_traffic_bytes')->default(0); $table->integer('traffic_quota_bytes')->default(0); $table->integer('traffic_download_bytes')->default(0); $table->integer('traffic_upload_bytes')->default(0); $table->integer('lifetime_download_bytes')->default(0); $table->integer('lifetime_upload_bytes')->default(0); $table->integer('traffic_synced_at')->default(0); $table->integer('suspended_at')->nullable(); $table->integer('network_suspended_at')->nullable(); $table->text('spec_json'); $table->text('credential_cipher')->nullable(); $table->integer('created_at'); $table->integer('updated_at'); }); $schema->create('cloud_service_periods', static function (Blueprint $table): void { $table->increments('id'); $table->integer('instance_id'); $table->integer('user_id'); $table->integer('plan_id')->nullable(); $table->integer('order_id')->nullable(); $table->integer('renewal_id')->nullable(); $table->integer('starts_at'); $table->integer('ends_at'); $table->integer('traffic_quota_bytes'); $table->integer('download_base_bytes')->nullable(); $table->integer('upload_base_bytes')->nullable(); $table->integer('download_used_bytes')->default(0); $table->integer('upload_used_bytes')->default(0); $table->string('status'); $table->integer('activated_at')->nullable(); $table->integer('created_at'); $table->integer('updated_at'); $table->unique('order_id'); $table->unique('renewal_id'); }); $schema->create('cloud_renewals', static function (Blueprint $table): void { $table->increments('id'); $table->integer('instance_id'); $table->integer('user_id'); $table->integer('plan_id')->nullable(); $table->integer('period_id')->nullable(); $table->string('idempotency_key'); $table->string('request_hash'); $table->string('amount'); $table->integer('validity_days'); $table->integer('traffic_bytes_added'); $table->integer('previous_expires_at'); $table->integer('new_expires_at'); $table->integer('created_at'); $table->unique(['user_id', 'idempotency_key']); }); $schema->create('cloud_jobs', static function (Blueprint $table): void { $table->increments('id'); $table->integer('instance_id')->nullable(); $table->string('kind'); $table->string('status'); $table->integer('updated_at')->default(0); }); $schema->create('cloud_invoices', static function (Blueprint $table): void { $table->increments('id'); $table->string('invoice_no')->unique(); $table->integer('instance_id'); $table->integer('user_id'); $table->integer('plan_id')->nullable(); $table->integer('renewal_id')->nullable(); $table->integer('renews_expires_at'); $table->integer('due_at'); $table->string('amount'); $table->integer('validity_days'); $table->integer('traffic_quota_bytes'); $table->string('status'); $table->string('paid_via')->nullable(); $table->string('payment_reference')->nullable(); $table->integer('paid_at')->nullable(); $table->integer('created_at'); $table->integer('updated_at'); $table->index(['instance_id', 'renews_expires_at', 'status']); $table->unique('renewal_id'); }); $schema->create('cloud_invoice_payments', static function (Blueprint $table): void { $table->increments('id'); $table->integer('invoice_id'); $table->integer('user_id'); $table->string('provider'); $table->string('provider_order_no'); $table->string('idempotency_key'); $table->string('amount'); $table->string('status'); $table->integer('created_at'); $table->integer('updated_at'); $table->unique(['provider', 'provider_order_no']); $table->unique(['user_id', 'idempotency_key']); }); $schema->create('cloud_mail_outbox', static function (Blueprint $table): void { $table->increments('id'); $table->integer('invoice_id'); $table->string('kind'); $table->string('status'); $table->integer('attempts'); $table->integer('available_at'); $table->string('lease_token')->nullable(); $table->integer('lease_until'); $table->integer('sent_at')->nullable(); $table->string('last_error'); $table->integer('created_at'); $table->integer('updated_at'); $table->unique(['invoice_id', 'kind']); }); $user = new User(); $user->id = 1; $user->money = '50.00'; $user->enable = 1; $user->restrict_balance_purchase = 0; $user->save(); $now = time(); $instance = new CloudInstance(); $instance->host_id = 1; $instance->user_id = 1; $instance->plan_id = 8; $instance->uuid = '11111111-2222-4333-8444-555555555555'; $instance->name = 'invoice-vm'; $instance->status = 'active'; $instance->service_status = 'active'; $instance->network_status = 'active'; $instance->expires_at = $now + CloudInvoiceService::REMINDER_SECONDS; $instance->renewal_price = '12.50'; $instance->renewal_days = 30; $instance->renewal_traffic_bytes = 4096; $instance->traffic_quota_bytes = 4096; $instance->spec_json = '{}'; $instance->created_at = $instance->updated_at = $now; $instance->save(); $period = new CloudServicePeriod(); $period->instance_id = $instance->id; $period->user_id = 1; $period->plan_id = 8; $period->starts_at = $now - 100; $period->ends_at = $instance->expires_at; $period->traffic_quota_bytes = 4096; $period->download_base_bytes = 0; $period->upload_base_bytes = 0; $period->status = 'active'; $period->activated_at = $now - 100; $period->created_at = $period->updated_at = $now; $period->save(); $instance->current_period_id = $period->id; $instance->save(); $assert(CloudInvoiceService::issueDue() === 1 && CloudInvoiceService::issueDue() === 0, 'T-7 invoice generation was not idempotent.'); $invoice = CloudInvoice::first(); $assert($invoice !== null && CloudMailOutbox::count() === 1 && (string) $invoice->amount === '12.50', 'Invoice snapshot or reminder outbox is missing.'); $actor = ['user_id' => 1, 'is_admin' => false, 'is_super_admin' => false, 'can_manage' => false, 'session_id' => '']; $paid = CloudInvoiceService::payBalance($invoice, $actor, 'invoice-balance-test-0001'); $expiry = (int) $instance->expires_at; $assert($paid->status === 'paid' && User::find(1)->money === '37.50' && CloudRenewal::count() === 1 && (int) $instance->fresh()->expires_at === $expiry + 30 * 86400, 'Balance invoice settlement was not atomic.'); CloudInvoiceService::payBalance($paid->fresh(), $actor, 'invoice-balance-test-0001'); $assert(User::find(1)->money === '37.50' && CloudRenewal::count() === 1, 'Repeated invoice payment debited or renewed twice.'); CloudInvoiceService::deliverReminders(); $assert(CloudMailOutbox::first()->status === 'cancelled', 'A settled invoice reminder remained in the retry queue.'); $currentExpiry = (int) $instance->fresh()->expires_at; $second = new CloudInvoice(); $second->invoice_no = 'VC-GATEWAY'; $second->instance_id = $instance->id; $second->user_id = 1; $second->plan_id = 8; $second->renews_expires_at = $currentExpiry; $second->due_at = $currentExpiry; $second->amount = '12.50'; $second->validity_days = 30; $second->traffic_quota_bytes = 4096; $second->status = 'open'; $second->created_at = $second->updated_at = $now; $second->save(); $payment = new CloudInvoicePayment(); $payment->invoice_id = $second->id; $payment->user_id = 1; $payment->provider = 'paytaro'; $payment->provider_order_no = 'PT-GATEWAY'; $payment->idempotency_key = 'invoice-gateway-test-0001'; $payment->amount = '12.50'; $payment->status = 'pending'; $payment->created_at = $payment->updated_at = $now; $payment->save(); $user = User::find(1); $user->money = '50.00'; $user->save(); $outcome = CloudInvoiceService::settleCreditedPayment($payment); $assert($outcome === 'paid_invoice' && User::find(1)->money === '37.50' && $second->fresh()->status === 'paid' && CloudRenewal::count() === 2 && (int) $instance->fresh()->expires_at === $currentExpiry + 30 * 86400, 'Provider credit was not consumed exactly once to settle the invoice.'); try { // `$second` is deliberately the stale pre-callback model. Starting a // provider order from it must not overwrite the freshly paid row. CloudInvoicePaymentService::create($second, User::find(1), [ 'method' => 'paytaro', 'idempotency_key' => 'stale-paid-invoice-guard-0001', ], '192.0.2.10', 'invoice-test'); throw new RuntimeException('Stale paid invoice unexpectedly created another payment.'); } catch (CloudError $error) { $assert($error->httpStatus === 409, 'Stale paid invoice returned the wrong conflict status.'); } $assert($second->fresh()->status === 'paid' && CloudRenewal::count() === 2, 'A stale provider-order request overwrote the paid invoice state.'); $third = new CloudInvoice(); $third->invoice_no = 'VC-STALE'; $third->instance_id = $instance->id; $third->user_id = 1; $third->plan_id = 8; $third->renews_expires_at = $currentExpiry - 1; $third->due_at = $currentExpiry - 1; $third->amount = '12.50'; $third->validity_days = 30; $third->traffic_quota_bytes = 4096; $third->status = 'open'; $third->created_at = $third->updated_at = $now; $third->save(); $late = new CloudInvoicePayment(); $late->invoice_id = $third->id; $late->user_id = 1; $late->provider = 'paytaro'; $late->provider_order_no = 'PT-STALE'; $late->idempotency_key = 'invoice-gateway-test-0002'; $late->amount = '12.50'; $late->status = 'pending'; $late->created_at = $late->updated_at = $now; $late->save(); $user = User::find(1); $user->money = '50.00'; $user->save(); $outcome = CloudInvoiceService::settleCreditedPayment($late); $assert($outcome === 'credited_balance' && User::find(1)->money === '50.00' && $third->fresh()->status === 'void' && CloudRenewal::count() === 2, 'Late provider payment was lost or renewed a stale invoice.'); $actualExpiry = (int) $instance->fresh()->expires_at; $fourth = new CloudInvoice(); $fourth->invoice_no = 'VC-AMOUNT'; $fourth->instance_id = $instance->id; $fourth->user_id = 1; $fourth->plan_id = 8; $fourth->renews_expires_at = $actualExpiry; $fourth->due_at = $actualExpiry; $fourth->amount = '12.50'; $fourth->validity_days = 30; $fourth->traffic_quota_bytes = 4096; $fourth->status = 'open'; $fourth->created_at = $fourth->updated_at = $now; $fourth->save(); $mismatch = new CloudInvoicePayment(); $mismatch->invoice_id = $fourth->id; $mismatch->user_id = 1; $mismatch->provider = 'paytaro'; $mismatch->provider_order_no = 'PT-AMOUNT'; $mismatch->idempotency_key = 'invoice-gateway-test-0003'; $mismatch->amount = '1.00'; $mismatch->status = 'pending'; $mismatch->created_at = $mismatch->updated_at = $now; $mismatch->save(); $user = User::find(1); $user->money = '51.00'; $user->save(); $outcome = CloudInvoiceService::settleCreditedPayment($mismatch); $assert($outcome === 'credited_balance' && User::find(1)->money === '51.00' && $fourth->fresh()->status === 'open' && CloudRenewal::count() === 2, 'Mismatched provider payment amount unexpectedly settled an invoice.'); $makeDueInstance = static function (string $uuid, string $name, int $expiry) use ($now): CloudInstance { $candidate = new CloudInstance(); $candidate->host_id = 1; $candidate->user_id = 1; $candidate->plan_id = 8; $candidate->uuid = $uuid; $candidate->name = $name; $candidate->status = 'active'; $candidate->service_status = 'active'; $candidate->network_status = 'active'; $candidate->expires_at = $expiry; $candidate->renewal_price = '12.50'; $candidate->renewal_days = 30; $candidate->renewal_traffic_bytes = 4096; $candidate->traffic_quota_bytes = 4096; $candidate->spec_json = '{}'; $candidate->created_at = $candidate->updated_at = $now; $candidate->save(); return $candidate; }; $alreadyBilled = $makeDueInstance('11111111-2222-4333-8444-555555555556', 'already-billed', $now + 60); $existingInvoice = new CloudInvoice(); $existingInvoice->invoice_no = 'VC-EXISTING'; $existingInvoice->instance_id = $alreadyBilled->id; $existingInvoice->user_id = 1; $existingInvoice->plan_id = 8; $existingInvoice->renews_expires_at = $alreadyBilled->expires_at; $existingInvoice->due_at = $alreadyBilled->expires_at; $existingInvoice->amount = '12.50'; $existingInvoice->validity_days = 30; $existingInvoice->traffic_quota_bytes = 4096; $existingInvoice->status = 'open'; $existingInvoice->created_at = $existingInvoice->updated_at = $now; $existingInvoice->save(); $nextDue = $makeDueInstance('11111111-2222-4333-8444-555555555557', 'next-due', $now + 120); $assert(CloudInvoiceService::issueDue(1) === 1 && CloudInvoice::where('instance_id', $nextDue->id) ->where('renews_expires_at', $nextDue->expires_at)->exists(), 'Already billed instances starved a later invoice batch.'); $corrected = $makeDueInstance('11111111-2222-4333-8444-555555555558', 'corrected-plan', $now + 180); $assert(CloudInvoiceService::issueDue(10) === 1, 'Correctable invoice fixture was not generated.'); $oldInvoice = CloudInvoice::where('instance_id', $corrected->id)->first(); $corrected->renewal_price = '13.00'; $corrected->save(); try { CloudInvoicePaymentService::create($oldInvoice, User::find(1), [ 'method' => 'paytaro', 'idempotency_key' => 'corrected-invoice-provider-0001', ], '192.0.2.11', 'invoice-test'); throw new RuntimeException('Changed plan snapshot unexpectedly created a payment order.'); } catch (CloudError $error) { $assert($error->httpStatus === 409, 'Changed snapshot returned the wrong conflict status.'); } $assert($oldInvoice->fresh()->status === 'void' && CloudInvoiceService::issueDue(10) === 1, 'Void invoice prevented a corrected replacement from being issued.'); $replacement = CloudInvoice::where('instance_id', $corrected->id) ->where('status', 'open')->orderByDesc('id')->first(); $assert($replacement !== null && (string) $replacement->amount === '13.00' && CloudInvoice::where('instance_id', $corrected->id)->count() === 2, 'Corrected invoice did not retain the void audit row and current snapshot.'); echo "Cloud invoice generation, balance settlement and provider-payment safety passed.\n"; }