Fix renewal billing visibility and expiry deadlines
parent
88f38d933f
commit
f3f655ba2d
@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Cloud;
|
||||
|
||||
use App\Models\Configurations;
|
||||
|
||||
/** Administrator-controlled payment window for newly provisioned instances. */
|
||||
final class CloudProvisioningBillingConfig
|
||||
{
|
||||
public const PAYMENT_WINDOW_MINUTES_KEY =
|
||||
'cloud_provision_invoice_payment_window_minutes';
|
||||
public const DEFAULT_PAYMENT_WINDOW_MINUTES = 30;
|
||||
public const MIN_PAYMENT_WINDOW_MINUTES = 1;
|
||||
public const MAX_PAYMENT_WINDOW_MINUTES = 10080;
|
||||
|
||||
public static function paymentWindowMinutes(): int
|
||||
{
|
||||
$stored = Configurations::where(
|
||||
'name',
|
||||
self::PAYMENT_WINDOW_MINUTES_KEY
|
||||
)->value('value');
|
||||
try {
|
||||
return self::validatePaymentWindowMinutes(
|
||||
$stored === null ? self::DEFAULT_PAYMENT_WINDOW_MINUTES : $stored
|
||||
);
|
||||
} catch (\InvalidArgumentException $error) {
|
||||
throw new CloudError(
|
||||
'新建实例账单支付期限配置无效,请联系管理员',
|
||||
503
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public static function paymentWindowSeconds(): int
|
||||
{
|
||||
return self::paymentWindowMinutes() * 60;
|
||||
}
|
||||
|
||||
public static function validatePaymentWindowMinutes($value): int
|
||||
{
|
||||
if (is_array($value) || is_object($value)) {
|
||||
throw new \InvalidArgumentException(
|
||||
'新建实例账单支付期限必须是整数分钟'
|
||||
);
|
||||
}
|
||||
$raw = trim((string) $value);
|
||||
if (preg_match('/^[0-9]+$/D', $raw) !== 1) {
|
||||
throw new \InvalidArgumentException(
|
||||
'新建实例账单支付期限必须是整数分钟'
|
||||
);
|
||||
}
|
||||
$minutes = (int) $raw;
|
||||
if ($minutes < self::MIN_PAYMENT_WINDOW_MINUTES
|
||||
|| $minutes > self::MAX_PAYMENT_WINDOW_MINUTES) {
|
||||
throw new \InvalidArgumentException(
|
||||
'新建实例账单支付期限必须为 1 至 10080 分钟'
|
||||
);
|
||||
}
|
||||
return $minutes;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
require dirname(__DIR__) . '/vendor/autoload.php';
|
||||
|
||||
use App\Services\Cloud\CloudInvoicePageService;
|
||||
use App\Services\Cloud\CloudInvoiceService;
|
||||
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('cloud_instances', static function (Blueprint $table): void {
|
||||
$table->increments('id');
|
||||
$table->integer('user_id');
|
||||
$table->string('status');
|
||||
});
|
||||
$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('renews_expires_at')->nullable();
|
||||
$table->integer('due_at');
|
||||
$table->string('amount');
|
||||
$table->string('status');
|
||||
$table->integer('created_at');
|
||||
});
|
||||
|
||||
$now = time();
|
||||
$expiry = $now + 3600;
|
||||
Capsule::table('cloud_instances')->insert([
|
||||
['id' => 1, 'user_id' => 1, 'status' => 'active'],
|
||||
['id' => 2, 'user_id' => 2, 'status' => 'active'],
|
||||
]);
|
||||
Capsule::table('cloud_invoices')->insert([
|
||||
'id' => 1,
|
||||
'purpose' => 'renewal',
|
||||
'instance_id' => 1,
|
||||
'order_id' => null,
|
||||
'user_id' => 1,
|
||||
'renews_expires_at' => $expiry,
|
||||
'due_at' => $expiry,
|
||||
'amount' => '10.00',
|
||||
'status' => 'paid',
|
||||
'created_at' => $now - 100,
|
||||
]);
|
||||
Capsule::table('cloud_invoices')->insert([
|
||||
'id' => 2,
|
||||
'purpose' => 'renewal',
|
||||
'instance_id' => 2,
|
||||
'order_id' => null,
|
||||
'user_id' => 2,
|
||||
'renews_expires_at' => $expiry,
|
||||
'due_at' => $expiry,
|
||||
'amount' => '20.00',
|
||||
'status' => 'open',
|
||||
'created_at' => $now - 99,
|
||||
]);
|
||||
for ($number = 0; $number < 12; $number++) {
|
||||
Capsule::table('cloud_invoices')->insert([
|
||||
'purpose' => 'ip_change',
|
||||
'instance_id' => null,
|
||||
'order_id' => null,
|
||||
'user_id' => 1,
|
||||
'renews_expires_at' => null,
|
||||
'due_at' => $now + $number,
|
||||
'amount' => '1.00',
|
||||
'status' => 'paid',
|
||||
'created_at' => $now + $number,
|
||||
]);
|
||||
}
|
||||
|
||||
$all = CloudInvoicePageService::forUser(1, 1, 10);
|
||||
$assert($all['purpose'] === 'all'
|
||||
&& $all['pagination']['total'] === 13
|
||||
&& count($all['invoices']) === 10
|
||||
&& !in_array('1', array_column($all['invoices'], 'id'), true),
|
||||
'Newest-first all-invoice pagination did not reproduce the hidden older renewal case.');
|
||||
|
||||
$renewals = CloudInvoicePageService::forUser(1, 1, 10, 'renewal');
|
||||
$invoice = $renewals['invoices'][0] ?? [];
|
||||
$assert($renewals['purpose'] === 'renewal'
|
||||
&& $renewals['pagination']['total'] === 1
|
||||
&& count($renewals['invoices']) === 1
|
||||
&& ($invoice['id'] ?? null) === '1'
|
||||
&& ($invoice['purpose'] ?? null) === 'renewal'
|
||||
&& ($invoice['renews_expires_at'] ?? null) === $expiry
|
||||
&& ($invoice['payment_deadline_at'] ?? null)
|
||||
=== $expiry + CloudInvoiceService::PAYMENT_GRACE_SECONDS,
|
||||
'The user renewal filter hid the owner invoice or returned the wrong fixed deadline.');
|
||||
$assert(CloudInvoicePageService::normalizePurpose('invalid') === 'all'
|
||||
&& CloudInvoicePageService::normalizePurpose(['renewal']) === 'all',
|
||||
'Invoice purpose filtering accepted an unsupported or structured value.');
|
||||
|
||||
echo "User renewal invoice filtering, ownership and deadline checks passed.\n";
|
||||
@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
require dirname(__DIR__) . '/vendor/autoload.php';
|
||||
|
||||
use App\Models\CloudInvoice;
|
||||
use App\Services\Cloud\CloudInvoiceService;
|
||||
use App\Services\Cloud\CloudProvisioningBillingConfig;
|
||||
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);
|
||||
}
|
||||
};
|
||||
|
||||
$System_Config = [];
|
||||
$capsule = new Capsule();
|
||||
$capsule->addConnection(['driver' => 'sqlite', 'database' => ':memory:'], 'default');
|
||||
$capsule->setAsGlobal();
|
||||
$capsule->bootEloquent();
|
||||
$capsule->schema('default')->create('configurations', static function (Blueprint $table): void {
|
||||
$table->increments('id');
|
||||
$table->string('name')->unique();
|
||||
$table->text('value')->nullable();
|
||||
});
|
||||
|
||||
$assert(CloudProvisioningBillingConfig::paymentWindowMinutes() === 30,
|
||||
'Missing provisioning payment-window configuration did not use 30 minutes.');
|
||||
Capsule::table('configurations')->insert([
|
||||
'name' => CloudProvisioningBillingConfig::PAYMENT_WINDOW_MINUTES_KEY,
|
||||
'value' => '60',
|
||||
]);
|
||||
$assert(CloudProvisioningBillingConfig::paymentWindowSeconds() === 3600,
|
||||
'One-hour provisioning payment-window configuration was not applied.');
|
||||
Capsule::table('configurations')
|
||||
->where('name', CloudProvisioningBillingConfig::PAYMENT_WINDOW_MINUTES_KEY)
|
||||
->update(['value' => '1440']);
|
||||
$assert(CloudProvisioningBillingConfig::paymentWindowSeconds() === 86400,
|
||||
'One-day provisioning payment-window configuration was not applied.');
|
||||
|
||||
foreach (['0', '10081', '1.5', 'hour', ''] as $invalid) {
|
||||
try {
|
||||
CloudProvisioningBillingConfig::validatePaymentWindowMinutes($invalid);
|
||||
throw new RuntimeException('Invalid provisioning payment window was accepted: ' . $invalid);
|
||||
} catch (InvalidArgumentException $error) {
|
||||
// Expected.
|
||||
}
|
||||
}
|
||||
|
||||
$renewal = new CloudInvoice();
|
||||
$renewal->purpose = 'renewal';
|
||||
$renewal->renews_expires_at = 1700000000;
|
||||
$renewal->due_at = 1699999900;
|
||||
$renewal->created_at = 1700003600;
|
||||
$assert(CloudInvoiceService::paymentDeadline($renewal)
|
||||
=== 1700000000 + CloudInvoiceService::PAYMENT_GRACE_SECONDS,
|
||||
'Late renewal invoice creation extended the fixed instance deletion cutoff.');
|
||||
|
||||
$provision = new CloudInvoice();
|
||||
$provision->purpose = 'provision';
|
||||
$provision->due_at = 1700007200;
|
||||
$provision->created_at = 1700000000;
|
||||
$assert(CloudInvoiceService::paymentDeadline($provision) === 1700007200,
|
||||
'Provision invoice ignored its configured absolute payment deadline.');
|
||||
|
||||
$root = dirname(__DIR__);
|
||||
$invoiceService = file_get_contents($root . '/app/Services/Cloud/CloudInvoiceService.php');
|
||||
$configController = file_get_contents($root . '/app/Controllers/Admin/ConfigController.php');
|
||||
$configTemplate = file_get_contents($root . '/resources/views/venix/admin/config.tpl');
|
||||
$assert(str_contains($invoiceService,
|
||||
'CloudProvisioningBillingConfig::paymentWindowSeconds()')
|
||||
&& str_contains($configController,
|
||||
'CloudProvisioningBillingConfig::PAYMENT_WINDOW_MINUTES_KEY')
|
||||
&& str_contains($configController,
|
||||
'CloudProvisioningBillingConfig::validatePaymentWindowMinutes(')
|
||||
&& str_contains($configTemplate,
|
||||
'name="cloud_provision_invoice_payment_window_minutes"')
|
||||
&& str_contains($configTemplate, '1440 表示 1 天'),
|
||||
'Provisioning payment-window admin configuration is incomplete.');
|
||||
|
||||
echo "Cloud provisioning payment-window configuration checks passed.\n";
|
||||
Loading…
Reference in New Issue