You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
683 lines
24 KiB
PHP
683 lines
24 KiB
PHP
<?php
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use App\Models\CloudInstance;
|
|
use App\Models\User;
|
|
use App\Services\Cloud\CloudAutomaticBackupBillingService;
|
|
use App\Services\Cloud\CloudError;
|
|
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');
|
|
});
|
|
$schema->create('cloud_instances', static function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
$table->integer('user_id');
|
|
$table->integer('offer_id');
|
|
$table->integer('current_period_id')->nullable();
|
|
$table->string('name');
|
|
$table->string('status');
|
|
$table->string('service_status');
|
|
$table->integer('expires_at')->nullable();
|
|
$table->string('renewal_price')->nullable();
|
|
$table->integer('renewal_days');
|
|
$table->integer('renewal_traffic_bytes')->default(0);
|
|
$table->text('spec_json');
|
|
$table->integer('updated_at');
|
|
});
|
|
$schema->create('cloud_offers', static function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
$table->integer('plan_id');
|
|
$table->integer('resource_pool_id');
|
|
$table->string('code');
|
|
$table->string('name');
|
|
$table->string('price');
|
|
$table->string('backup_price');
|
|
$table->string('currency');
|
|
$table->integer('price_version');
|
|
$table->integer('revision');
|
|
$table->integer('status');
|
|
$table->integer('stock');
|
|
$table->integer('sort');
|
|
$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('traffic_pool_group_id')->nullable();
|
|
$table->string('traffic_quota_mode')->default('finite');
|
|
$table->integer('starts_at');
|
|
$table->integer('ends_at');
|
|
$table->string('status');
|
|
$table->integer('created_at');
|
|
$table->integer('updated_at');
|
|
});
|
|
$schema->create('cloud_backup_subscriptions', static function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
$table->integer('instance_id')->unique();
|
|
$table->integer('user_id');
|
|
$table->integer('offer_id');
|
|
$table->boolean('enabled');
|
|
$table->boolean('renew_next_period');
|
|
$table->string('frequency');
|
|
$table->integer('weekday');
|
|
$table->integer('retention_count');
|
|
$table->string('timezone');
|
|
$table->string('price');
|
|
$table->string('currency');
|
|
$table->integer('price_version');
|
|
$table->integer('state_revision')->default(1);
|
|
$table->integer('next_run_at');
|
|
$table->integer('deferred_until')->default(0);
|
|
$table->string('defer_reason')->default('');
|
|
$table->integer('deferred_at')->nullable();
|
|
$table->integer('defer_count')->default(0);
|
|
$table->integer('last_scheduled_at')->nullable();
|
|
$table->integer('last_backup_id')->nullable();
|
|
$table->integer('created_at');
|
|
$table->integer('updated_at');
|
|
});
|
|
$schema->create('cloud_backup_period_entitlements', static function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
$table->integer('subscription_id');
|
|
$table->integer('service_period_id')->unique();
|
|
$table->integer('instance_id');
|
|
$table->integer('user_id');
|
|
$table->integer('offer_id');
|
|
$table->integer('invoice_id');
|
|
$table->string('source');
|
|
$table->integer('starts_at');
|
|
$table->integer('ends_at');
|
|
$table->string('full_period_price');
|
|
$table->string('amount');
|
|
$table->string('currency');
|
|
$table->integer('price_version');
|
|
$table->integer('paid_at');
|
|
$table->integer('created_at');
|
|
$table->integer('updated_at');
|
|
});
|
|
$schema->create('cloud_invoices', static function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
$table->string('invoice_no')->unique();
|
|
$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->integer('order_item_id')->nullable();
|
|
$table->string('code');
|
|
$table->string('label');
|
|
$table->string('amount');
|
|
$table->string('currency');
|
|
$table->text('snapshot_json');
|
|
$table->integer('created_at');
|
|
$table->unique(['invoice_id', 'code']);
|
|
});
|
|
$schema->create('cloud_invoice_payments', static function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
$table->integer('invoice_id');
|
|
$table->integer('user_id');
|
|
$table->string('status');
|
|
});
|
|
$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->text('last_error');
|
|
$table->integer('created_at');
|
|
$table->integer('updated_at');
|
|
$table->unique(['invoice_id', 'kind']);
|
|
});
|
|
$schema->create('cloud_jobs', static function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
$table->integer('invoice_id')->nullable();
|
|
$table->string('kind');
|
|
$table->string('phase');
|
|
$table->string('outcome')->nullable();
|
|
$table->string('effect_resolution')->default('not_applicable');
|
|
$table->boolean('safe_to_resubmit')->nullable();
|
|
});
|
|
|
|
$now = 2_000_000_000;
|
|
$startsAt = $now - (15 * 86400);
|
|
$endsAt = $now + (15 * 86400);
|
|
|
|
$user = new User();
|
|
$user->id = 7;
|
|
$user->enable = 1;
|
|
$user->money = '50.00';
|
|
$user->save();
|
|
|
|
$capsule->table('cloud_offers')->insert([
|
|
'id' => 9,
|
|
'plan_id' => 4,
|
|
'resource_pool_id' => 2,
|
|
'code' => 'half-period-offer',
|
|
'name' => 'Half period offer',
|
|
'price' => '20.00',
|
|
'backup_price' => '10.00',
|
|
'currency' => 'USD',
|
|
'price_version' => 3,
|
|
'revision' => 1,
|
|
'status' => 1,
|
|
'stock' => -1,
|
|
'sort' => 0,
|
|
'created_at' => $startsAt,
|
|
'updated_at' => $startsAt,
|
|
]);
|
|
|
|
$instance = new CloudInstance();
|
|
$instance->id = 11;
|
|
$instance->user_id = 7;
|
|
$instance->offer_id = 9;
|
|
$instance->name = 'billing-test-vm';
|
|
$instance->status = 'active';
|
|
$instance->service_status = 'active';
|
|
$instance->expires_at = $endsAt;
|
|
$instance->renewal_price = '20.00';
|
|
$instance->renewal_days = 30;
|
|
$instance->renewal_traffic_bytes = 0;
|
|
$instance->spec_json = '{}';
|
|
$instance->updated_at = $startsAt;
|
|
$instance->save();
|
|
|
|
$periodId = $capsule->table('cloud_service_periods')->insertGetId([
|
|
'instance_id' => 11,
|
|
'user_id' => 7,
|
|
'plan_id' => 4,
|
|
'order_id' => 1,
|
|
'renewal_id' => null,
|
|
'starts_at' => $startsAt,
|
|
'ends_at' => $endsAt,
|
|
'status' => 'active',
|
|
'created_at' => $startsAt,
|
|
'updated_at' => $startsAt,
|
|
]);
|
|
$instance->current_period_id = $periodId;
|
|
$instance->save();
|
|
|
|
$quote = CloudAutomaticBackupBillingService::quote($instance, $user, $now);
|
|
$assert($quote['activation_amount'] === '5.00',
|
|
'Half-period automatic backup quote must be exactly $5.00.');
|
|
$assert($quote['full_period_amount'] === '10.00'
|
|
&& $quote['period_ends_at'] === $endsAt
|
|
&& $quote['future_period_count'] === 0
|
|
&& $quote['state_revision'] === 0
|
|
&& $quote['can_enable'] === true,
|
|
'Automatic backup quote did not expose the verified billing period.');
|
|
|
|
$actor = [
|
|
'user_id' => 7,
|
|
'request_idempotency_key' => 'backup-enable-test-0001',
|
|
];
|
|
$enabled = CloudAutomaticBackupBillingService::enable(
|
|
$instance,
|
|
$user,
|
|
$actor,
|
|
['expected_amount' => '5.00', 'expected_state_revision' => 0],
|
|
$now
|
|
);
|
|
$user->refresh();
|
|
$assert($enabled['state'] === 'active'
|
|
&& $enabled['charged_amount'] === '5.00'
|
|
&& (string) $user->money === '45.00',
|
|
'Paid activation did not atomically debit the quoted wallet amount.');
|
|
$assert($capsule->table('cloud_invoices')->where('purpose', 'backup_addon')
|
|
->where('status', 'paid')->count() === 1
|
|
&& $capsule->table('cloud_backup_period_entitlements')->count() === 1
|
|
&& $capsule->table('cloud_backup_subscriptions')
|
|
->where('enabled', 1)->where('renew_next_period', 1)
|
|
->where('state_revision', 1)->count() === 1,
|
|
'Paid activation did not create its invoice, entitlement and subscription.');
|
|
|
|
$replayed = CloudAutomaticBackupBillingService::enable(
|
|
$instance,
|
|
$user,
|
|
$actor,
|
|
['expected_amount' => '5.00', 'expected_state_revision' => 0],
|
|
$now
|
|
);
|
|
$user->refresh();
|
|
$assert($replayed['invoice_id'] === $enabled['invoice_id']
|
|
&& (string) $user->money === '45.00'
|
|
&& $capsule->table('cloud_invoices')->count() === 1
|
|
&& $capsule->table('cloud_backup_period_entitlements')->count() === 1,
|
|
'Repeated activation was not idempotent.');
|
|
|
|
$changedReplayRejected = false;
|
|
try {
|
|
CloudAutomaticBackupBillingService::enable(
|
|
$instance,
|
|
$user,
|
|
$actor,
|
|
['expected_amount' => '4.99', 'expected_state_revision' => 0],
|
|
$now
|
|
);
|
|
} catch (CloudError $error) {
|
|
$changedReplayRejected = $error->httpStatus === 409;
|
|
}
|
|
$nextRunBeforeDuplicate = (int) $capsule->table(
|
|
'cloud_backup_subscriptions'
|
|
)->where('instance_id', 11)->value('next_run_at');
|
|
$activeDuplicateRejected = false;
|
|
try {
|
|
CloudAutomaticBackupBillingService::enable(
|
|
$instance,
|
|
$user,
|
|
[
|
|
'user_id' => 7,
|
|
'request_idempotency_key' => 'backup-enable-active-0001',
|
|
],
|
|
['expected_amount' => '0.00', 'expected_state_revision' => 1],
|
|
$now
|
|
);
|
|
} catch (CloudError $error) {
|
|
$activeDuplicateRejected = $error->httpStatus === 409;
|
|
}
|
|
$assert($changedReplayRejected && $activeDuplicateRejected
|
|
&& $capsule->table('cloud_invoices')->count() === 1
|
|
&& (int) $capsule->table('cloud_backup_subscriptions')
|
|
->where('instance_id', 11)->value('next_run_at')
|
|
=== $nextRunBeforeDuplicate,
|
|
'Changed or duplicate activation request altered the paid schedule.');
|
|
|
|
$renewalInvoiceId = $capsule->table('cloud_invoices')->insertGetId([
|
|
'invoice_no' => 'RENEW-BACKUP-TOGGLE-1',
|
|
'purpose' => 'renewal', 'instance_id' => 11, 'order_id' => null,
|
|
'user_id' => 7, 'plan_id' => null, 'offer_id' => 9,
|
|
'renewal_id' => null, 'renews_expires_at' => $endsAt,
|
|
'due_at' => $endsAt, 'amount' => '30.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_invoice_items')->insert([
|
|
[
|
|
'invoice_id' => $renewalInvoiceId, 'order_item_id' => null,
|
|
'code' => 'instance', 'label' => '实例套餐续费',
|
|
'amount' => '20.00', 'currency' => 'USD',
|
|
'snapshot_json' => '{}', 'created_at' => $now,
|
|
],
|
|
[
|
|
'invoice_id' => $renewalInvoiceId, 'order_item_id' => null,
|
|
'code' => 'automatic_backup', 'label' => '自动备份续费',
|
|
'amount' => '10.00', 'currency' => 'USD',
|
|
'snapshot_json' => json_encode([
|
|
'subscription_id' => (int) $capsule->table(
|
|
'cloud_backup_subscriptions'
|
|
)->where('instance_id', 11)->value('id'),
|
|
], JSON_THROW_ON_ERROR),
|
|
'created_at' => $now,
|
|
],
|
|
]);
|
|
$paymentId = $capsule->table('cloud_invoice_payments')->insertGetId([
|
|
'invoice_id' => $renewalInvoiceId, 'user_id' => 7,
|
|
'status' => 'pending',
|
|
]);
|
|
$pendingBlocked = false;
|
|
try {
|
|
CloudAutomaticBackupBillingService::disable(
|
|
$instance,
|
|
$user,
|
|
['user_id' => 7],
|
|
$now
|
|
);
|
|
} catch (CloudError $error) {
|
|
$pendingBlocked = $error->httpStatus === 409;
|
|
}
|
|
$assert($pendingBlocked
|
|
&& $capsule->table('cloud_backup_subscriptions')
|
|
->where('instance_id', 11)->where('enabled', 1)
|
|
->where('renew_next_period', 1)->count() === 1
|
|
&& $capsule->table('cloud_invoices')
|
|
->where('id', $renewalInvoiceId)->value('status') === 'open',
|
|
'Pending renewal payment did not roll back the backup-policy change.');
|
|
$capsule->table('cloud_invoice_payments')->where('id', $paymentId)->delete();
|
|
|
|
$disabled = CloudAutomaticBackupBillingService::disable(
|
|
$instance,
|
|
$user,
|
|
['user_id' => 7],
|
|
$now
|
|
);
|
|
$user->refresh();
|
|
$assert($disabled['state'] === 'paused'
|
|
&& $disabled['activation_amount'] === '0.00'
|
|
&& $disabled['state_revision'] === 2
|
|
&& $disabled['can_enable'] === true
|
|
&& $disabled['can_disable'] === false
|
|
&& (string) $user->money === '45.00',
|
|
'Disabling did not preserve the already-paid period without refunding it.');
|
|
$assert($capsule->table('cloud_backup_subscriptions')
|
|
->where('enabled', 0)->where('renew_next_period', 0)
|
|
->where('state_revision', 2)->count() === 1
|
|
&& $capsule->table('cloud_backup_period_entitlements')->count() === 1,
|
|
'Disabling mutated the paid entitlement or retained future renewal.');
|
|
$assert($capsule->table('cloud_invoices')->where('id', $renewalInvoiceId)
|
|
->value('status') === 'void',
|
|
'Disabling did not void the stale unpaid backup renewal invoice.');
|
|
$replacementRenewal = $capsule->table('cloud_invoices')
|
|
->where('instance_id', 11)
|
|
->where('purpose', 'renewal')
|
|
->where('status', '<>', 'void')
|
|
->orderByDesc('id')
|
|
->first();
|
|
$assert($replacementRenewal === null
|
|
|| ((int) $replacementRenewal->renews_expires_at === $endsAt
|
|
&& (int) $replacementRenewal->due_at === $endsAt),
|
|
'Backup policy change moved the frozen renewal grace anchor.');
|
|
|
|
$invoiceCountBeforeReenable = $capsule->table('cloud_invoices')->count();
|
|
$reenableActor = [
|
|
'user_id' => 7,
|
|
'request_idempotency_key' => 'backup-enable-test-0002',
|
|
];
|
|
$reenabled = CloudAutomaticBackupBillingService::enable(
|
|
$instance,
|
|
$user,
|
|
$reenableActor,
|
|
['expected_amount' => '0.00', 'expected_state_revision' => 2],
|
|
$now
|
|
);
|
|
$user->refresh();
|
|
$assert($reenabled['state'] === 'active'
|
|
&& $reenabled['charged_amount'] === '0.00'
|
|
&& $reenabled['invoice_id'] === null
|
|
&& $reenabled['state_revision'] === 3
|
|
&& (string) $user->money === '45.00'
|
|
&& $capsule->table('cloud_invoices')->count()
|
|
=== $invoiceCountBeforeReenable
|
|
&& $capsule->table('cloud_backup_period_entitlements')->count() === 1,
|
|
'Re-enabling an already-paid period created an invoice or duplicated cover.');
|
|
|
|
$reenableReplay = CloudAutomaticBackupBillingService::enable(
|
|
$instance,
|
|
$user,
|
|
$reenableActor,
|
|
['expected_amount' => '0.00', 'expected_state_revision' => 2],
|
|
$now
|
|
);
|
|
$assert($reenableReplay['state'] === 'active'
|
|
&& $reenableReplay['invoice_id'] === null
|
|
&& $reenableReplay['state_revision'] === 3
|
|
&& $capsule->table('cloud_invoices')->count()
|
|
=== $invoiceCountBeforeReenable,
|
|
'A retried free reactivation created an invoice or advanced state twice.');
|
|
|
|
CloudAutomaticBackupBillingService::disable(
|
|
$instance,
|
|
$user,
|
|
['user_id' => 7],
|
|
$now
|
|
);
|
|
$staleReenableRejected = false;
|
|
try {
|
|
CloudAutomaticBackupBillingService::enable(
|
|
$instance,
|
|
$user,
|
|
$reenableActor,
|
|
['expected_amount' => '0.00', 'expected_state_revision' => 2],
|
|
$now
|
|
);
|
|
} catch (CloudError $error) {
|
|
$staleReenableRejected = $error->httpStatus === 409;
|
|
}
|
|
$assert($staleReenableRejected
|
|
&& $capsule->table('cloud_backup_subscriptions')
|
|
->where('instance_id', 11)->where('enabled', 0)
|
|
->where('renew_next_period', 0)->where('state_revision', 4)
|
|
->count() === 1
|
|
&& $capsule->table('cloud_invoices')->count()
|
|
=== $invoiceCountBeforeReenable,
|
|
'A stale free-reactivation retry reopened a later-disabled subscription.');
|
|
|
|
$prepaidUser = new User();
|
|
$prepaidUser->id = 8;
|
|
$prepaidUser->enable = 1;
|
|
$prepaidUser->money = '20.00';
|
|
$prepaidUser->save();
|
|
$prepaid = new CloudInstance();
|
|
$prepaid->id = 12;
|
|
$prepaid->user_id = 8;
|
|
$prepaid->offer_id = 9;
|
|
$prepaid->name = 'prepaid-period-vm';
|
|
$prepaid->status = 'active';
|
|
$prepaid->service_status = 'active';
|
|
$prepaid->expires_at = $endsAt + (30 * 86400);
|
|
$prepaid->renewal_price = '20.00';
|
|
$prepaid->renewal_days = 30;
|
|
$prepaid->renewal_traffic_bytes = 0;
|
|
$prepaid->spec_json = '{}';
|
|
$prepaid->updated_at = $startsAt;
|
|
$prepaid->save();
|
|
$prepaidCurrent = $capsule->table('cloud_service_periods')->insertGetId([
|
|
'instance_id' => 12, 'user_id' => 8, 'plan_id' => 4,
|
|
'order_id' => 2, 'renewal_id' => null, 'starts_at' => $startsAt,
|
|
'ends_at' => $endsAt, 'status' => 'exhausted',
|
|
'created_at' => $startsAt, 'updated_at' => $startsAt,
|
|
]);
|
|
$capsule->table('cloud_service_periods')->insert([
|
|
'instance_id' => 12, 'user_id' => 8, 'plan_id' => 4,
|
|
'order_id' => null, 'renewal_id' => 2, 'starts_at' => $endsAt,
|
|
'ends_at' => $endsAt + (30 * 86400), 'status' => 'scheduled',
|
|
'created_at' => $startsAt, 'updated_at' => $startsAt,
|
|
]);
|
|
$prepaid->current_period_id = $prepaidCurrent;
|
|
$prepaid->save();
|
|
$prepaidQuote = CloudAutomaticBackupBillingService::quote(
|
|
$prepaid,
|
|
$prepaidUser,
|
|
$now
|
|
);
|
|
$assert($prepaidQuote['activation_amount'] === '15.00'
|
|
&& $prepaidQuote['future_period_count'] === 1,
|
|
'Activation did not add the full fee for a prepaid future period.');
|
|
CloudAutomaticBackupBillingService::enable(
|
|
$prepaid,
|
|
$prepaidUser,
|
|
[
|
|
'user_id' => 8,
|
|
'request_idempotency_key' => 'backup-enable-prepaid-0001',
|
|
],
|
|
['expected_amount' => '15.00', 'expected_state_revision' => 0],
|
|
$now
|
|
);
|
|
$prepaidUser->refresh();
|
|
$assert((string) $prepaidUser->money === '5.00'
|
|
&& $capsule->table('cloud_backup_period_entitlements')
|
|
->where('instance_id', 12)->count() === 2,
|
|
'Prepaid periods were not covered atomically by one activation.');
|
|
|
|
$capsule->table('cloud_offers')->insert([
|
|
'id' => 10,
|
|
'plan_id' => 5,
|
|
'resource_pool_id' => 2,
|
|
'code' => 'upgraded-offer',
|
|
'name' => 'Upgraded offer',
|
|
'price' => '30.00',
|
|
'backup_price' => '12.00',
|
|
'currency' => 'USD',
|
|
'price_version' => 4,
|
|
'revision' => 1,
|
|
'status' => 1,
|
|
'stock' => -1,
|
|
'sort' => 0,
|
|
'created_at' => $now,
|
|
'updated_at' => $now,
|
|
]);
|
|
$capsule->table('cloud_instances')->where('id', 12)->update([
|
|
'offer_id' => 10,
|
|
]);
|
|
$capsule->table('cloud_backup_subscriptions')->where('instance_id', 12)
|
|
->update([
|
|
'offer_id' => 10,
|
|
'price' => '12.00',
|
|
'price_version' => 4,
|
|
'updated_at' => $now,
|
|
]);
|
|
$prepaid->refresh();
|
|
$postUpgradeQuote = CloudAutomaticBackupBillingService::quote(
|
|
$prepaid,
|
|
$prepaidUser,
|
|
$now
|
|
);
|
|
$assert($postUpgradeQuote['state'] === 'active'
|
|
&& $postUpgradeQuote['activation_amount'] === '0.00'
|
|
&& $postUpgradeQuote['full_period_amount'] === '12.00'
|
|
&& $capsule->table('cloud_backup_period_entitlements')
|
|
->where('instance_id', 12)->where('offer_id', 9)->count() === 2,
|
|
'A plan upgrade invalidated immutable paid backup entitlements.');
|
|
|
|
$poorUser = new User();
|
|
$poorUser->id = 9;
|
|
$poorUser->enable = 1;
|
|
$poorUser->money = '4.99';
|
|
$poorUser->save();
|
|
$poor = new CloudInstance();
|
|
$poor->id = 13;
|
|
$poor->user_id = 9;
|
|
$poor->offer_id = 9;
|
|
$poor->name = 'insufficient-balance-vm';
|
|
$poor->status = 'active';
|
|
$poor->service_status = 'active';
|
|
$poor->expires_at = $endsAt;
|
|
$poor->renewal_price = '20.00';
|
|
$poor->renewal_days = 30;
|
|
$poor->renewal_traffic_bytes = 0;
|
|
$poor->spec_json = '{}';
|
|
$poor->updated_at = $startsAt;
|
|
$poor->save();
|
|
$poorPeriod = $capsule->table('cloud_service_periods')->insertGetId([
|
|
'instance_id' => 13, 'user_id' => 9, 'plan_id' => 4,
|
|
'order_id' => 3, 'renewal_id' => null, 'starts_at' => $startsAt,
|
|
'ends_at' => $endsAt, 'status' => 'active',
|
|
'created_at' => $startsAt, 'updated_at' => $startsAt,
|
|
]);
|
|
$poor->current_period_id = $poorPeriod;
|
|
$poor->save();
|
|
$rejected = false;
|
|
try {
|
|
CloudAutomaticBackupBillingService::enable(
|
|
$poor,
|
|
$poorUser,
|
|
[
|
|
'user_id' => 9,
|
|
'request_idempotency_key' => 'backup-enable-poor-00001',
|
|
],
|
|
['expected_amount' => '5.00', 'expected_state_revision' => 0],
|
|
$now
|
|
);
|
|
} catch (CloudError $error) {
|
|
$rejected = $error->getMessage() === '账户余额不足';
|
|
}
|
|
$poorUser->refresh();
|
|
$assert($rejected && (string) $poorUser->money === '4.99'
|
|
&& $capsule->table('cloud_backup_subscriptions')
|
|
->where('instance_id', 13)->count() === 0
|
|
&& $capsule->table('cloud_backup_period_entitlements')
|
|
->where('instance_id', 13)->count() === 0
|
|
&& $capsule->table('cloud_invoices')
|
|
->where('instance_id', 13)->count() === 0,
|
|
'Insufficient balance left partial automatic-backup billing state.');
|
|
|
|
// Once the service period has expired, the public disable flow is rejected by
|
|
// the final billing quote. Every earlier subscription/invoice mutation must
|
|
// therefore roll back; in particular it must not replace the fixed renewal
|
|
// deadline with a new three-day window starting at the request time.
|
|
$expiredAt = $now - 60;
|
|
$capsule->table('cloud_instances')->where('id', 11)->update([
|
|
'expires_at' => $expiredAt,
|
|
]);
|
|
$capsule->table('cloud_backup_subscriptions')->where('instance_id', 11)->update([
|
|
'enabled' => 1,
|
|
'renew_next_period' => 1,
|
|
'state_revision' => 5,
|
|
'updated_at' => $now,
|
|
]);
|
|
$fixedRenewalId = $capsule->table('cloud_invoices')->insertGetId([
|
|
'invoice_no' => 'RENEW-BACKUP-EXPIRED-1',
|
|
'purpose' => 'renewal', 'instance_id' => 11, 'order_id' => null,
|
|
'user_id' => 7, 'plan_id' => null, 'offer_id' => 9,
|
|
'renewal_id' => null, 'renews_expires_at' => $expiredAt,
|
|
'due_at' => $expiredAt, 'amount' => '30.00', 'currency' => 'USD',
|
|
'validity_days' => 30, 'traffic_quota_bytes' => 0,
|
|
'status' => 'open', 'paid_via' => null, 'payment_reference' => null,
|
|
'paid_at' => null, 'created_at' => $expiredAt, 'updated_at' => $now,
|
|
]);
|
|
$expiredDisableRejected = false;
|
|
try {
|
|
CloudAutomaticBackupBillingService::disable(
|
|
CloudInstance::find(11),
|
|
User::find(7),
|
|
['user_id' => 7],
|
|
$now
|
|
);
|
|
} catch (CloudError $error) {
|
|
$expiredDisableRejected = $error->httpStatus === 409;
|
|
}
|
|
$fixedRenewalAfter = $capsule->table('cloud_invoices')
|
|
->where('instance_id', 11)
|
|
->where('purpose', 'renewal')
|
|
->where('status', '<>', 'void')
|
|
->get();
|
|
$assert($expiredDisableRejected
|
|
&& $capsule->table('cloud_backup_subscriptions')
|
|
->where('instance_id', 11)
|
|
->where('enabled', 1)
|
|
->where('renew_next_period', 1)
|
|
->where('state_revision', 5)
|
|
->count() === 1
|
|
&& $fixedRenewalAfter->count() === 1
|
|
&& (int) $fixedRenewalAfter->first()->id === $fixedRenewalId
|
|
&& (int) $fixedRenewalAfter->first()->due_at === $expiredAt,
|
|
'Rejected expired backup disable changed the frozen renewal deadline.');
|
|
|
|
echo "Automatic backup mid-period billing tests passed.\n";
|