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.
452 lines
28 KiB
PHP
452 lines
28 KiB
PHP
<?php
|
|
|
|
namespace App\Services {
|
|
final class Config
|
|
{
|
|
public static array $values = [];
|
|
public static function get($name, $default = null)
|
|
{
|
|
return self::$values[$name] ?? $default;
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace App\Services\Cloud {
|
|
final class CloudSecrets
|
|
{
|
|
public static function key(string $purpose): string
|
|
{
|
|
return str_repeat('i', 32);
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace App\Services\Payment {
|
|
/** Keep this lifecycle test independent from configured/live gateways. */
|
|
final class PaytaroConfig
|
|
{
|
|
public static function isUserVisible($user = null): bool
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace {
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use App\Models\CloudInstance;
|
|
use App\Models\CloudInvoice;
|
|
use App\Models\CloudInvoicePayment;
|
|
use App\Models\CloudMailOutbox;
|
|
use App\Models\CloudRenewal;
|
|
use App\Models\CloudServicePeriod;
|
|
use App\Models\User;
|
|
use App\Services\Cloud\CloudError;
|
|
use App\Services\Cloud\CloudInvoicePaymentService;
|
|
use App\Services\Cloud\CloudInvoiceService;
|
|
use Illuminate\Database\Capsule\Manager as Capsule;
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
|
|
$assert = static function ($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->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->string('purpose')->default('renewal'); $table->integer('instance_id')->nullable();
|
|
$table->integer('order_id')->nullable()->unique(); $table->integer('user_id');
|
|
$table->integer('plan_id')->nullable(); $table->integer('renewal_id')->nullable();
|
|
$table->integer('renews_expires_at')->nullable(); $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('paytaro_orders', static function (Blueprint $table): void {
|
|
$table->increments('id'); $table->string('order_no')->unique(); $table->integer('user_id');
|
|
$table->string('status'); $table->string('payment_kind')->nullable();
|
|
$table->text('payment_value')->nullable(); $table->integer('created_at'); $table->integer('updated_at');
|
|
});
|
|
$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 = json_encode(['plan_name' => '2c2g', 'vcpu' => 2,
|
|
'memory_mib' => 2048, 'disk_gib' => 20, 'bandwidth_mbps' => 100]);
|
|
$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();
|
|
$renewalDisplay = $invoice === null ? [] : CloudInvoiceService::display($invoice);
|
|
$assert($invoice !== null && (string) $invoice->purpose === 'renewal'
|
|
&& ($renewalDisplay['purpose'] ?? null) === 'renewal'
|
|
&& ($renewalDisplay['title'] ?? null) === '实例续费'
|
|
&& ($renewalDisplay['type_label'] ?? null) === '实例续费'
|
|
&& ($renewalDisplay['plan_name'] ?? null) === '2c2g'
|
|
&& ($renewalDisplay['cpu_cores'] ?? null) === 2
|
|
&& ($renewalDisplay['memory_mib'] ?? null) === 2048
|
|
&& !array_key_exists('invoice_no', $renewalDisplay)
|
|
&& ($renewalDisplay['order_id'] ?? null) === null
|
|
&& CloudMailOutbox::count() === 1 && (string) $invoice->amount === '12.50',
|
|
'Renewal invoice type, 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.');
|
|
CloudInvoicePaymentService::recordAttempt([
|
|
'invoice_id' => (int) $replacement->id,
|
|
'idempotency_key' => 'record-attempt-cutoff-guard-0001',
|
|
'provider' => 'paytaro',
|
|
], 'PT-RECORD-ATTEMPT', (string) $replacement->amount, 1, $now);
|
|
$assert(CloudInvoicePayment::where('invoice_id', $replacement->id)
|
|
->where('provider_order_no', 'PT-RECORD-ATTEMPT')
|
|
->where('status', 'creating')->exists(),
|
|
'Gateway pre-dispatch billing recheck did not persist a valid attempt.');
|
|
|
|
$makeLifecycleInstance = static function (string $uuid, string $name, int $expiry,
|
|
string $serviceStatus = 'expired_grace', string $networkStatus = 'blocked_expired'
|
|
) 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 = $serviceStatus;
|
|
$candidate->network_status = $networkStatus; $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();
|
|
|
|
$candidatePeriod = new CloudServicePeriod();
|
|
$candidatePeriod->instance_id = $candidate->id; $candidatePeriod->user_id = 1;
|
|
$candidatePeriod->plan_id = 8; $candidatePeriod->starts_at = $expiry - 30 * 86400;
|
|
$candidatePeriod->ends_at = $expiry; $candidatePeriod->traffic_quota_bytes = 4096;
|
|
$candidatePeriod->download_base_bytes = 0; $candidatePeriod->upload_base_bytes = 0;
|
|
$candidatePeriod->status = 'active'; $candidatePeriod->activated_at = $expiry - 30 * 86400;
|
|
$candidatePeriod->created_at = $candidatePeriod->updated_at = $now;
|
|
$candidatePeriod->save();
|
|
$candidate->current_period_id = $candidatePeriod->id; $candidate->save();
|
|
return $candidate;
|
|
};
|
|
$makeLifecycleInvoice = static function (CloudInstance $target, string $number, int $dueAt,
|
|
int $createdAt, string $status = 'open'): CloudInvoice {
|
|
$bill = new CloudInvoice(); $bill->invoice_no = $number; $bill->instance_id = $target->id;
|
|
$bill->user_id = $target->user_id; $bill->plan_id = $target->plan_id;
|
|
$bill->renews_expires_at = $target->expires_at; $bill->due_at = $dueAt;
|
|
$bill->amount = '12.50'; $bill->validity_days = 30; $bill->traffic_quota_bytes = 4096;
|
|
$bill->status = $status; $bill->created_at = $bill->updated_at = $createdAt;
|
|
$bill->save();
|
|
return $bill;
|
|
};
|
|
|
|
// An invoice generated after its service expiry receives the complete
|
|
// grace period from creation, not a shortened or already-expired window.
|
|
$gatewayGraceInstance = $makeLifecycleInstance(
|
|
'11111111-2222-4333-8444-555555555559', 'gateway-grace', $now - 120);
|
|
$gatewayGraceInvoice = $makeLifecycleInvoice($gatewayGraceInstance, 'VC-GRACE-GATEWAY',
|
|
$now - 120, $now - 60);
|
|
$gatewayGraceDisplay = CloudInvoiceService::display($gatewayGraceInvoice);
|
|
$assert((int) ($gatewayGraceDisplay['deletion_at'] ?? 0)
|
|
=== $now - 60 + CloudInvoiceService::PAYMENT_GRACE_SECONDS
|
|
&& ($gatewayGraceDisplay['payable'] ?? false) === true,
|
|
'Invoice display did not expose the authoritative grace deadline and payable state.');
|
|
|
|
// Replaying an existing gateway attempt exercises the same authoritative
|
|
// cutoff validation without making a network request from this test.
|
|
$gatewayPayment = new CloudInvoicePayment(); $gatewayPayment->invoice_id = $gatewayGraceInvoice->id;
|
|
$gatewayPayment->user_id = 1; $gatewayPayment->provider = 'paytaro';
|
|
$gatewayPayment->provider_order_no = 'PT-GRACE-EXISTING';
|
|
$gatewayPayment->idempotency_key = 'gateway-grace-payment-0001';
|
|
$gatewayPayment->amount = '12.50'; $gatewayPayment->status = 'pending';
|
|
$gatewayPayment->created_at = $gatewayPayment->updated_at = $now; $gatewayPayment->save();
|
|
Capsule::table('paytaro_orders')->insert([
|
|
'order_no' => 'PT-GRACE-EXISTING', 'user_id' => 1, 'status' => 'pending',
|
|
'payment_kind' => 'payurl', 'payment_value' => 'https://pay.example.test/order',
|
|
'created_at' => $now, 'updated_at' => $now,
|
|
]);
|
|
$gatewayResult = CloudInvoicePaymentService::create($gatewayGraceInvoice, User::find(1), [
|
|
'method' => 'paytaro', 'idempotency_key' => 'gateway-grace-payment-0001',
|
|
], '192.0.2.12', 'invoice-test');
|
|
$assert(($gatewayResult['status'] ?? '') === 'pending'
|
|
&& ($gatewayResult['method'] ?? '') === 'paytaro'
|
|
&& $gatewayGraceInvoice->fresh()->status === 'open',
|
|
'Gateway payment was rejected while the invoice was still inside its grace period.');
|
|
|
|
$balanceGraceInstance = $makeLifecycleInstance(
|
|
'11111111-2222-4333-8444-555555555560', 'balance-grace', $now - 180);
|
|
$balanceGraceInvoice = $makeLifecycleInvoice($balanceGraceInstance, 'VC-GRACE-BALANCE',
|
|
$now - 180, $now - 90);
|
|
$user = User::find(1); $user->money = '50.00'; $user->save();
|
|
$renewalsBeforeGracePayment = CloudRenewal::count();
|
|
$paidGraceInvoice = CloudInvoiceService::payBalance(
|
|
$balanceGraceInvoice, $actor, 'balance-grace-payment-0001');
|
|
$assert($paidGraceInvoice->status === 'paid' && User::find(1)->money === '37.50'
|
|
&& CloudRenewal::count() === $renewalsBeforeGracePayment + 1
|
|
&& (int) $balanceGraceInstance->fresh()->expires_at > $now,
|
|
'Balance payment did not renew an expired instance inside the grace period.');
|
|
|
|
$balanceCutoffInstance = $makeLifecycleInstance(
|
|
'11111111-2222-4333-8444-555555555561', 'balance-cutoff',
|
|
$now - CloudInvoiceService::PAYMENT_GRACE_SECONDS - 30);
|
|
$balanceCutoffInvoice = $makeLifecycleInvoice($balanceCutoffInstance, 'VC-CUTOFF-BALANCE',
|
|
$balanceCutoffInstance->expires_at, $balanceCutoffInstance->expires_at);
|
|
$user = User::find(1); $user->money = '50.00'; $user->save();
|
|
$renewalsBeforeCutoff = CloudRenewal::count();
|
|
try {
|
|
CloudInvoiceService::payBalance(
|
|
$balanceCutoffInvoice, $actor, 'balance-cutoff-payment-0001');
|
|
throw new RuntimeException('Balance payment unexpectedly passed the deletion cutoff.');
|
|
} catch (CloudError $error) {
|
|
$assert($error->httpStatus === 409, 'Cutoff balance payment returned the wrong conflict status.');
|
|
}
|
|
$balanceCutoffDisplay = CloudInvoiceService::display($balanceCutoffInvoice->fresh());
|
|
$assert(User::find(1)->money === '50.00'
|
|
&& CloudRenewal::count() === $renewalsBeforeCutoff
|
|
&& $balanceCutoffInvoice->fresh()->status === 'expired'
|
|
&& ($balanceCutoffDisplay['payable'] ?? true) === false,
|
|
'Cutoff balance payment deducted money, renewed, or remained payable.');
|
|
|
|
$gatewayCutoffInstance = $makeLifecycleInstance(
|
|
'11111111-2222-4333-8444-555555555562', 'gateway-cutoff',
|
|
$now - CloudInvoiceService::PAYMENT_GRACE_SECONDS);
|
|
$gatewayCutoffInvoice = $makeLifecycleInvoice($gatewayCutoffInstance, 'VC-CUTOFF-GATEWAY',
|
|
$gatewayCutoffInstance->expires_at, $gatewayCutoffInstance->expires_at);
|
|
$gatewayPaymentsBeforeCutoff = CloudInvoicePayment::count();
|
|
try {
|
|
CloudInvoicePaymentService::create($gatewayCutoffInvoice, User::find(1), [
|
|
'method' => 'paytaro', 'idempotency_key' => 'gateway-cutoff-payment-0001',
|
|
], '192.0.2.13', 'invoice-test');
|
|
throw new RuntimeException('Gateway order unexpectedly passed the deletion cutoff.');
|
|
} catch (CloudError $error) {
|
|
$assert($error->httpStatus === 409, 'Cutoff gateway order returned the wrong conflict status.');
|
|
}
|
|
$assert(CloudInvoicePayment::count() === $gatewayPaymentsBeforeCutoff
|
|
&& User::find(1)->money === '50.00'
|
|
&& $gatewayCutoffInvoice->fresh()->status === 'expired',
|
|
'Cutoff gateway order created a payment, deducted money, or stayed open.');
|
|
|
|
$lateInstance = $makeLifecycleInstance(
|
|
'11111111-2222-4333-8444-555555555563', 'late-gateway-credit',
|
|
$now - CloudInvoiceService::PAYMENT_GRACE_SECONDS - 50);
|
|
$lateInvoice = $makeLifecycleInvoice($lateInstance, 'VC-LATE-CREDIT',
|
|
$lateInstance->expires_at, $lateInstance->expires_at);
|
|
$latePayment = new CloudInvoicePayment(); $latePayment->invoice_id = $lateInvoice->id;
|
|
$latePayment->user_id = 1; $latePayment->provider = 'paytaro';
|
|
$latePayment->provider_order_no = 'PT-LATE-CREDIT';
|
|
$latePayment->idempotency_key = 'gateway-late-credit-0001'; $latePayment->amount = '12.50';
|
|
$latePayment->status = 'pending'; $latePayment->created_at = $latePayment->updated_at = $now;
|
|
$latePayment->save();
|
|
$user = User::find(1); $user->money = '62.50'; $user->save();
|
|
$lateExpiry = (int) $lateInstance->expires_at;
|
|
$renewalsBeforeLateCredit = CloudRenewal::count();
|
|
$lateOutcome = CloudInvoiceService::settleCreditedPayment($latePayment);
|
|
$assert($lateOutcome === 'credited_balance' && User::find(1)->money === '62.50'
|
|
&& CloudRenewal::count() === $renewalsBeforeLateCredit
|
|
&& (int) $lateInstance->fresh()->expires_at === $lateExpiry
|
|
&& $lateInvoice->fresh()->status === 'expired',
|
|
'Late gateway credit was consumed for renewal instead of remaining in the wallet.');
|
|
|
|
$expiredBilledInstance = $makeLifecycleInstance(
|
|
'11111111-2222-4333-8444-555555555564', 'expired-billed', $now + 240,
|
|
'active', 'active');
|
|
$expiredBill = $makeLifecycleInvoice($expiredBilledInstance, 'VC-EXPIRED-AUDIT',
|
|
$expiredBilledInstance->expires_at, $now, 'expired');
|
|
$expiredBillCount = CloudInvoice::where('instance_id', $expiredBilledInstance->id)->count();
|
|
CloudInvoiceService::issueDue(100);
|
|
$assert(CloudInvoice::where('instance_id', $expiredBilledInstance->id)->count()
|
|
=== $expiredBillCount
|
|
&& $expiredBill->fresh()->status === 'expired',
|
|
'An expired invoice was reopened or replaced by issueDue.');
|
|
|
|
echo "Cloud invoice generation, grace cutoff and provider-payment safety passed.\n";
|
|
}
|