'cookie']; use App\Models\User; use App\Services\Cloud\CloudError; use App\Services\Cloud\CloudInvoiceDetailPageService; use Illuminate\Database\Capsule\Manager as Capsule; use Illuminate\Database\Schema\Blueprint; $assert = static function (bool $condition, string $message): void { if (!$condition) { throw new RuntimeException($message); } }; $capsule = new Capsule(); $capsule->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->integer('enable'); $table->string('money'); $table->string('user_name'); $table->string('email'); }); $schema->create('cloud_orders', static function (Blueprint $table): void { $table->increments('id'); $table->integer('user_id'); $table->integer('instance_id')->nullable(); $table->string('status'); $table->text('spec_json'); }); $schema->create('cloud_invoices', static function (Blueprint $table): void { $table->increments('id'); $table->string('purpose'); $table->integer('instance_id')->nullable(); $table->integer('order_id')->nullable(); $table->integer('user_id'); $table->integer('plan_id')->nullable(); $table->integer('offer_id')->nullable(); $table->integer('renewal_id')->nullable(); $table->integer('renews_expires_at')->nullable(); $table->integer('due_at'); $table->string('amount'); $table->string('currency'); $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'); }); $schema->create('cloud_invoice_items', static function (Blueprint $table): void { $table->increments('id'); $table->integer('invoice_id'); $table->string('code'); $table->string('label'); $table->string('amount'); $table->string('currency'); }); $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'); }); $schema->create('cloud_jobs', static function (Blueprint $table): void { $table->increments('id'); $table->integer('order_id')->nullable(); $table->integer('invoice_id')->nullable(); $table->integer('instance_id')->nullable(); $table->integer('user_id'); $table->string('kind'); $table->string('request_idempotency_key')->nullable(); $table->string('phase'); $table->string('outcome')->nullable(); $table->integer('attempts')->default(0); $table->integer('updated_at')->default(0); $table->string('error_code')->nullable(); $table->string('effect_resolution')->default('not_applicable'); $table->boolean('safe_to_resubmit')->nullable(); $table->text('sealed_payload')->nullable(); }); $schema->create('stripe_orders', static function (Blueprint $table): void { $table->increments('id'); $table->string('order_no'); $table->integer('user_id'); $table->text('checkout_url')->nullable(); }); $now = time(); foreach ([1, 2] as $userId) { Capsule::table('user')->insert([ 'id' => $userId, 'enable' => 1, 'money' => '20.00', 'user_name' => 'customer-' . $userId, 'email' => 'customer-' . $userId . '@example.test', ]); } Capsule::table('cloud_orders')->insert([ 'id' => 11, 'user_id' => 1, 'instance_id' => null, 'status' => 'awaiting_payment', 'spec_json' => json_encode([ 'name' => 'resume-test-vm', 'plan_name' => 'Resume plan', ], JSON_THROW_ON_ERROR), ]); Capsule::table('cloud_invoices')->insert([ 'id' => 21, 'purpose' => 'provision', 'instance_id' => null, 'order_id' => 11, 'user_id' => 1, 'plan_id' => 3, 'offer_id' => 4, 'renewal_id' => null, 'renews_expires_at' => null, 'due_at' => $now + 3600, 'amount' => '10.00', 'currency' => 'USD', 'validity_days' => 30, 'traffic_quota_bytes' => 0, 'status' => 'open', 'paid_via' => null, 'payment_reference' => null, 'paid_at' => null, 'created_at' => $now, 'updated_at' => $now, ]); Capsule::table('cloud_jobs')->insert([ 'id' => 31, 'order_id' => null, 'invoice_id' => 21, 'instance_id' => null, 'user_id' => 1, 'kind' => 'payment_create', 'request_idempotency_key' => 'stripe-resume-key-0001', 'phase' => 'accepted', 'outcome' => null, 'attempts' => 1, 'updated_at' => $now, 'error_code' => null, 'effect_resolution' => 'not_applicable', 'safe_to_resubmit' => null, 'sealed_payload' => 'not-readable-by-page', ]); $user = User::findOrFail(1); $creating = CloudInvoiceDetailPageService::forUser(21, 1, $user); $assert(($creating['active_payment']['status'] ?? null) === 'creating' && ($creating['active_payment']['task_id'] ?? null) === '31' && ($creating['active_payment']['payment'] ?? null) === null && ($creating['active_payment']['can_submit'] ?? false) === true && $creating['payment_methods'] === [] && $creating['balance_payment_allowed'] === true && ($creating['presentation']['show_payment'] ?? false) === true && ($creating['presentation']['active_payment_key'] ?? null) === 'billing.payment.active.creating', 'A durable payment-create task blocked a new payment choice.'); Capsule::table('cloud_jobs')->where('id', 31)->update([ 'phase' => 'terminal', 'outcome' => 'succeeded', 'effect_resolution' => 'applied', 'safe_to_resubmit' => 0, ]); Capsule::table('cloud_invoice_payments')->insert([ 'id' => 41, 'invoice_id' => 21, 'user_id' => 1, 'provider' => 'stripe_card', 'provider_order_no' => 'stripe-resume-order-1', 'idempotency_key' => 'stripe-resume-key-0001', 'amount' => '10.00', 'status' => 'pending', 'created_at' => $now, 'updated_at' => $now, ]); Capsule::table('stripe_orders')->insert([ 'id' => 51, 'order_no' => 'stripe-resume-order-1', 'user_id' => 1, 'checkout_url' => 'https://checkout.stripe.test/resume-safe', ]); $pending = CloudInvoiceDetailPageService::forUser(21, 1, $user); $assert(($pending['active_payment']['status'] ?? null) === 'pending' && ($pending['active_payment']['task_id'] ?? null) === null && ($pending['active_payment']['payment']['payment_kind'] ?? null) === 'payurl' && ($pending['active_payment']['payment']['payment_value'] ?? null) === 'https://checkout.stripe.test/resume-safe' && !array_key_exists( 'provider_order_no', $pending['active_payment']['payment'] ?? [] ) && !array_key_exists( 'idempotency_key', $pending['active_payment']['payment'] ?? [] ) && ($pending['active_payment']['can_submit'] ?? false) === true && $pending['payment_methods'] === [] && $pending['balance_payment_allowed'] === true, 'A pending provider payment blocked a new payment choice.'); $pendingStatus = CloudInvoiceDetailPageService::statusForUser(21, 1); $assert(($pendingStatus['invoice']['id'] ?? null) === '21' && ($pendingStatus['active_payment']['status'] ?? null) === 'pending' && ($pendingStatus['active_payment']['payment']['payment_value'] ?? null) === 'https://checkout.stripe.test/resume-safe' && ($pendingStatus['presentation_patch']['payment']['message_key'] ?? null) === 'billing.payment.active.pending' && ($pendingStatus['presentation_patch']['polling']['required'] ?? false) === true, 'Lightweight invoice polling omitted the active provider payment.'); Capsule::table('cloud_invoice_payments')->where('id', 41)->update([ 'status' => 'expired', 'updated_at' => $now + 1, ]); $expiredStatus = CloudInvoiceDetailPageService::statusForUser(21, 1); $assert(($expiredStatus['invoice']['payable'] ?? false) === true && array_key_exists('active_payment', $expiredStatus) && $expiredStatus['active_payment'] === null, 'A terminal provider order remained projected as an active payment lock.'); Capsule::table('cloud_invoice_payments')->where('id', 41)->update([ 'status' => 'pending', 'updated_at' => $now, ]); Capsule::table('cloud_invoices')->where('id', 21)->update([ 'due_at' => $now - 1, ]); $pastDeadline = CloudInvoiceDetailPageService::forUser(21, 1, $user); $assert(($pastDeadline['invoice']['payable'] ?? true) === false && ($pastDeadline['active_payment']['status'] ?? null) === 'pending' && ($pastDeadline['active_payment']['payment']['payment_value'] ?? null) === 'https://checkout.stripe.test/resume-safe', 'Crossing the invoice deadline hid an already-created provider payment.'); Capsule::table('cloud_invoices')->where('id', 21)->update([ 'due_at' => $now + 3600, ]); Capsule::table('stripe_orders')->where('id', 51)->update(['user_id' => 2]); $foreignProviderOrder = CloudInvoiceDetailPageService::forUser(21, 1, $user); $assert(($foreignProviderOrder['active_payment']['status'] ?? null) === 'review' && ($foreignProviderOrder['active_payment']['payment'] ?? null) === null, 'A provider order owned by another user leaked through payment recovery.'); Capsule::table('stripe_orders')->where('id', 51)->update(['user_id' => 1]); Capsule::table('cloud_invoice_payments')->where('id', 41)->update([ 'status' => 'creation_unknown', 'updated_at' => $now + 1, ]); Capsule::table('cloud_jobs')->where('id', 31)->update([ 'phase' => 'terminal', 'outcome' => 'failed', 'effect_resolution' => 'unknown', 'safe_to_resubmit' => 0, ]); $unknown = CloudInvoiceDetailPageService::forUser(21, 1, $user); $assert(($unknown['active_payment']['status'] ?? null) === 'review' && ($unknown['active_payment']['task_id'] ?? null) === '31' && ($unknown['active_payment']['payment'] ?? null) === null && ($unknown['active_payment']['can_submit'] ?? false) === true && $unknown['payment_methods'] === [] && $unknown['balance_payment_allowed'] === true, 'An unknown payment-creation effect blocked a distinct payment choice.'); Capsule::table('cloud_invoice_payments')->insert([ 'id' => 42, 'invoice_id' => 21, 'user_id' => 1, 'provider' => 'stripe_alipay', 'provider_order_no' => 'stripe-resume-order-2', 'idempotency_key' => 'stripe-resume-key-0002', 'amount' => '10.00', 'status' => 'pending', 'created_at' => $now + 2, 'updated_at' => $now + 2, ]); Capsule::table('stripe_orders')->insert([ 'id' => 52, 'order_no' => 'stripe-resume-order-2', 'user_id' => 1, 'checkout_url' => 'https://checkout.stripe.test/resume-new', ]); $latest = CloudInvoiceDetailPageService::forUser(21, 1, $user); $assert(($latest['active_payment']['status'] ?? null) === 'pending' && ($latest['active_payment']['payment']['payment_value'] ?? null) === 'https://checkout.stripe.test/resume-new' && ($latest['active_payment']['can_submit'] ?? false) === true, 'An older unknown task was merged into a newer provider payment.'); $foreignRejected = false; try { CloudInvoiceDetailPageService::forUser(21, 2, User::findOrFail(2)); } catch (CloudError $error) { $foreignRejected = $error->httpStatus === 404; } $assert($foreignRejected, 'Another user could restore an owner payment task or provider link.'); Capsule::table('cloud_invoice_payments')->where('id', 41)->update([ 'status' => 'not_created', ]); Capsule::table('cloud_jobs')->where('id', 31)->update([ 'effect_resolution' => 'not_applied', 'safe_to_resubmit' => 1, ]); Capsule::table('cloud_invoices')->where('id', 21)->update([ 'status' => 'paid', 'paid_at' => $now + 2, 'paid_via' => 'balance', ]); $terminal = CloudInvoiceDetailPageService::forUser( 21, 1, $user->fresh() ); $assert(($terminal['active_payment'] ?? null) === null, 'A terminal or settled payment attempt was restored as active.'); echo "Cloud invoice cross-session payment resume checks passed.\n";