@ -12,7 +12,6 @@ use App\Services\Cloud\CloudError;
use App\Services\Cloud\CloudInstancePageService;
use App\Services\Cloud\CloudInvoiceDetailPageService;
use App\Services\Cloud\CloudInvoicePageService;
use App\Services\Cloud\CloudInvoicePresentation;
use App\Services\Cloud\CloudTrafficPoolReadModel;
use App\Services\Cloud\CloudUserIsoFeature;
use App\Services\Cloud\CloudUserIsoService;
@ -305,11 +304,13 @@ final class CloudPageController
$actor = CloudAccess::actor(false);
$user = $request->getAttribute('auth_user') ?? Auth::getUser();
$query = $request->getQueryParams();
$payload = CloudInvoicePageService::queryForUser(
(int) $actor['user_id'],
is_array($query) ? $query : []
$payload = CloudInvoicePageService::withBalance(
CloudInvoicePageService::queryForUser(
(int) $actor['user_id'],
is_array($query) ? $query : []
),
(string) $user->money
);
$payload['balance'] = (string) $user->money;
$rows = $payload['invoices'];
$json = json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_HEX_TAG
| JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_INVALID_UTF8_SUBSTITUTE);
@ -319,7 +320,7 @@ final class CloudPageController
'cloud_invoice_filter' => $payload['purpose'],
'cloud_invoice_filters' => $payload['filters'],
'cloud_invoice_pagination' => $payload['pagination'],
'cloud_invoice_balance' => $this->formatUsd($ payload['balance']) ,
'cloud_invoice_balance' => $payload['balance_display '],
'cloud_invoice_bootstrap_json' => $json === false ? '{}' : $json,
]);
}
@ -487,368 +488,6 @@ final class CloudPageController
return $response->withHeader('Cache-Control', 'private, no-store');
}
private function invoiceListRow(array $invoice): array
{
$purpose = strtolower((string) ($invoice['purpose'] ?? ''));
$status = strtolower((string) ($invoice['status'] ?? ''));
[$statusLabel, $statusClass] = $this->invoiceStatus(
(string) ($invoice['display_status'] ?? $invoice['status'] ?? '')
);
$fulfillment = strtolower((string) ($invoice['fulfillment_status']
?? $invoice['job_status'] ?? $invoice['order_status'] ?? ''));
[$fulfillmentLabel, $fulfillmentClass] = $this->invoiceFulfillmentStatus(
$fulfillment, $purpose
);
$invoice['created_display'] = (int) ($invoice['created_at'] ?? 0) > 0
? date('Y/m/d H:i:s', (int) $invoice['created_at']) : '时间未记录';
$invoice['amount_display'] = $this->formatUsd($invoice['amount'] ?? null);
$invoice['status_label'] = $statusLabel;
$invoice['status_class'] = $statusClass;
$invoice['fulfillment_label'] = $fulfillmentLabel;
$invoice['fulfillment_class'] = $fulfillmentClass;
$invoice['show_fulfillment'] = (($purpose === 'provision'
& & in_array($status, ['paid', 'refunded'], true))
|| (in_array($purpose, ['upgrade', 'ip_change'], true)
& & $this->firstText($invoice['task_id'] ?? null,
$invoice['job_status'] ?? null,
$invoice['fulfillment_status'] ?? null) !== ''))
& & $fulfillmentLabel !== '';
$invoice['context_display'] = '';
if ($purpose === 'renewal') {
$invoice['context_display'] = '对应实例到期 '
. $this->formatTimestamp(
$invoice['renews_expires_at'] ?? $invoice['due_at'] ?? null,
'时间未记录'
);
if (in_array($status, ['open', 'overdue'], true)) {
$invoice['context_display'] .= ' · 最晚支付 '
. $this->formatTimestamp(
$invoice['payment_deadline_at'] ?? null,
'时间未记录'
);
}
} elseif ($purpose === 'provision') {
$invoice['context_display'] = '支付截止 '
. $this->formatTimestamp(
$invoice['payment_deadline_at'] ?? $invoice['due_at'] ?? null,
'时间未记录'
);
} elseif (in_array($purpose, ['upgrade', 'ip_change', 'backup_addon'], true)) {
$target = $this->firstText($invoice['instance_name'] ?? null);
if ($target === '' & & ($invoice['instance_id'] ?? null) !== null) {
$target = '#' . (string) $invoice['instance_id'];
}
$invoice['context_display'] = '目标实例 ' . ($target !== '' ? $target : '实例');
}
return $invoice;
}
private function invoiceDetailView(array $payload): array
{
$invoice = is_array($payload['invoice'] ?? null) ? $payload['invoice'] : [];
$purpose = strtolower((string) ($invoice['purpose'] ?? ''));
[$statusLabel, $statusClass] = $this->invoiceStatus(
(string) ($invoice['display_status'] ?? $invoice['status'] ?? '')
);
$created = $this->formatTimestamp($invoice['created_at'] ?? null,
'时间未记录');
$facts = [
['label' => '账单编号', 'value' => '#' . (string) ($invoice['id'] ?? '—')],
['label' => '创建时间', 'value' => $created],
];
if ($purpose === 'provision') {
$facts[] = ['label' => '支付截止', 'value' => $this->formatTimestamp(
$invoice['payment_deadline_at'] ?? $invoice['due_at'] ?? null
)];
} elseif ($purpose === 'renewal') {
$facts[] = ['label' => '实例到期', 'value' => $this->formatTimestamp(
$invoice['renews_expires_at'] ?? $invoice['due_at'] ?? null
)];
if (($invoice['deletion_at'] ?? null) !== null) {
$facts[] = ['label' => '最晚支付', 'value' => $this->formatTimestamp(
$invoice['deletion_at']
)];
}
} elseif (($invoice['payment_deadline_at'] ?? null) !== null) {
$facts[] = ['label' => '支付截止', 'value' => $this->formatTimestamp(
$invoice['payment_deadline_at']
)];
}
if (($invoice['paid_at'] ?? null) !== null) {
$facts[] = ['label' => '支付时间', 'value' => $this->formatTimestamp(
$invoice['paid_at']
)];
}
if (trim((string) ($invoice['paid_via'] ?? '')) !== '') {
$facts[] = ['label' => '支付方式', 'value' => $this->paymentLabel(
(string) $invoice['paid_via']
)];
}
$lineItems = [];
foreach (is_array($invoice['items'] ?? null) ? $invoice['items'] : []
as $item) {
if (!is_array($item) || !is_numeric($item['amount'] ?? null)) {
continue;
}
$label = trim((string) ($item['label'] ?? ''));
$lineItems[] = [
'label' => $label !== '' ? $label : '费用项目',
'amount_display' => $this->formatUsd($item['amount']),
];
}
$specification = [];
if (in_array($purpose, ['provision', 'renewal', 'upgrade'], true)) {
$subject = $this->firstText($invoice['requested_name'] ?? null,
$invoice['instance_name'] ?? null);
$specification[] = ['label' => '实例', 'value' => $subject !== ''
? $subject : ($purpose === 'provision' ? '待创建实例' : '实例')];
$planName = trim((string) ($invoice['plan_name'] ?? ''));
$specification[] = ['label' => '套餐', 'value' => $planName !== ''
? $planName : '云服务器套餐'];
if (($invoice['cpu_cores'] ?? null) !== null) {
$specification[] = ['label' => 'CPU',
'value' => (string) $invoice['cpu_cores'] . ' 核'];
}
if (($invoice['memory_mib'] ?? null) !== null) {
$specification[] = ['label' => '内存',
'value' => $this->formatMemory((int) $invoice['memory_mib'])];
}
if (($invoice['disk_gib'] ?? null) !== null) {
$specification[] = ['label' => '系统盘',
'value' => (string) $invoice['disk_gib'] . ' GB'];
}
if (($invoice['bandwidth_mbps'] ?? null) !== null) {
$specification[] = ['label' => '带宽',
'value' => (string) $invoice['bandwidth_mbps'] . ' Mbps'];
}
if ($purpose !== 'upgrade') {
$specification[] = ['label' => '服务周期',
'value' => max(0, (int) ($invoice['validity_days'] ?? 0)) . ' 天'];
}
$specification[] = [
'label' => $purpose === 'upgrade'
? '目标周期流量' : '周期流量',
'value' => $this->formatBytes(
max(0, (int) ($invoice['traffic_quota_bytes'] ?? 0)), true
),
];
} elseif (in_array($purpose, ['ip_change', 'backup_addon'], true)) {
$subject = $this->firstText($invoice['instance_name'] ?? null);
if ($subject === '' & & ($invoice['instance_id'] ?? null) !== null) {
$subject = '#' . (string) $invoice['instance_id'];
}
$specification[] = [
'label' => '实例',
'value' => $subject !== '' ? $subject : '实例',
];
$specification[] = [
'label' => '服务内容',
'value' => $purpose === 'ip_change'
? '更换公网 IP' : '自动备份开通',
];
}
$paymentMethods = [];
$payable = ($invoice['payable'] ?? false) === true;
$balancePaymentAllowed = ($payload['balance_payment_allowed'] ?? false) === true;
if ($payable & & $balancePaymentAllowed) {
$paymentMethods[] = [
'id' => 'balance',
'name' => '账户余额',
'description' => '可用余额 ' . $this->formatUsd($payload['balance'] ?? null),
'selected' => true,
];
}
if ($payable) {
foreach (is_array($payload['payment_methods'] ?? null)
? $payload['payment_methods'] : [] as $method) {
if (!is_array($method) || !isset($method['id'], $method['name'])) {
continue;
}
$paymentMethods[] = [
'id' => (string) $method['id'],
'name' => (string) $method['name'],
'description' => trim((string) ($method['description']
?? $method['label'] ?? '')) ?: '在线支付',
'selected' => false,
];
}
}
if ($paymentMethods !== []
& & !in_array(true, array_column($paymentMethods, 'selected'), true)) {
$paymentMethods[0]['selected'] = true;
}
$activePayment = is_array($payload['active_payment'] ?? null)
? $payload['active_payment'] : null;
$activePaymentStatus = strtolower((string) (
$activePayment['status'] ?? ''
));
$activePaymentMessage = match ($activePaymentStatus) {
'creating' => '上一笔支付订单正在创建;您仍可选择其他支付方式。',
'pending' => '已有待支付订单;可继续原订单或选择其他支付方式。',
'review' => '上一笔支付订单结果待核对;可选择其他支付方式,原订单若随后到账将计入账户余额。',
default => '',
};
$hasActivePayment = $activePayment !== null
& & $activePaymentMessage !== '';
$fulfillment = is_array($invoice['fulfillment'] ?? null)
? $invoice['fulfillment'] : [];
$fulfillmentStatus = strtolower($this->firstText(
$fulfillment['job_status'] ?? null,
$invoice['job_status'] ?? null,
$invoice['fulfillment_status'] ?? null,
$fulfillment['order_status'] ?? null,
$invoice['order_status'] ?? null
));
[$fulfillmentLabel, $fulfillmentClass] = $this->invoiceFulfillmentStatus(
$fulfillmentStatus, $purpose
);
$showFulfillment = in_array(
$purpose,
['provision', 'upgrade', 'ip_change'],
true
)
& & (in_array(strtolower((string) ($invoice['status'] ?? '')),
['paid', 'refunded'], true)
|| $this->firstText($fulfillment['task_id'] ?? null,
$invoice['task_id'] ?? null) !== '');
$fulfillmentError = $this->firstText($fulfillment['error'] ?? null,
$invoice['error'] ?? null);
$fulfillmentMessage = $fulfillmentError;
if ($fulfillmentMessage === '') {
$fulfillmentMessage = $fulfillmentLabel !== ''
? $fulfillmentLabel : match ($purpose) {
'upgrade' => '等待升级',
'ip_change' => '等待更换公网 IP',
default => '等待创建',
};
}
$fulfillmentTitle = match ($purpose) {
'upgrade' => '实例升级进度',
'ip_change' => '公网 IP 更换进度',
default => '实例创建进度',
};
$fulfillmentDescription = match ($purpose) {
'upgrade' => '账单扣款后,系统会在后台升级实例;关闭页面不会取消任务。',
'ip_change' => '账单扣款后,系统会在后台更换公网 IP; 关闭页面不会取消任务。',
default => '账单付款后,系统会在后台创建实例;关闭页面不会取消任务。',
};
return [
'id' => (string) ($invoice['id'] ?? ''),
'title' => (string) ($invoice['title'] ?? $invoice['type_label'] ?? '账单'),
'created_display' => $created,
'amount_display' => $this->formatUsd($invoice['amount'] ?? null),
'status_label' => $statusLabel,
'status_class' => $statusClass,
'facts' => $facts,
'show_line_items' => $lineItems !== [],
'line_items' => $lineItems,
'show_specification' => $specification !== [],
'specification' => $specification,
'show_payment' => $hasActivePayment
|| ($payable & & $paymentMethods !== []),
'payment_title' => $payable & & $paymentMethods !== []
? '选择支付方式' : '当前支付状态',
'payment_description' => $hasActivePayment & & $payable
? '已有未完成支付订单;任一订单先到账即结清账单,后续到账款项将计入账户余额。'
: ($payable
? '选择支付方式后,点击“立即支付”将直接发起支付,请勿重复点击。'
: '该账单当前不可创建新的支付订单。'),
'active_payment_message' => $activePaymentMessage,
'active_payment_class' => $activePaymentStatus === 'review'
? 'invoice-payment-error' : 'invoice-payment-note',
'payment_methods' => $paymentMethods,
'show_fulfillment' => $showFulfillment,
'fulfillment_title' => $fulfillmentTitle,
'fulfillment_description' => $fulfillmentDescription,
'fulfillment_message' => $fulfillmentMessage,
'fulfillment_label' => $fulfillmentLabel,
'fulfillment_class' => $fulfillmentClass,
'fulfillment_failed' => $fulfillmentMessage !== ''
& & ($fulfillmentError !== ''
|| in_array($fulfillmentStatus,
['failed', 'refunded', 'cancelled'], true)),
'task_id' => $this->firstText($fulfillment['task_id'] ?? null,
$invoice['task_id'] ?? null),
'instance_id' => $this->firstText($fulfillment['instance_id'] ?? null,
$invoice['instance_id'] ?? null),
];
}
private function invoiceStatus(string $status): array
{
return match (strtolower($status)) {
'open' => ['待支付', 'warning'],
'renewal_grace' => ['已到期,宽限期内可续费', 'warning'],
'overdue' => ['已逾期', 'failed'],
'paid' => ['已支付', 'running'],
'refunded' => ['已退款', 'other'],
'void' => ['已失效', 'other'],
'expired' => ['已失效', 'failed'],
'deleting', 'purge_deleting' => ['实例删除中', 'failed'],
'purge_pending' => ['等待永久删除', 'failed'],
'purge_stopping' => ['删除前正在关机', 'failed'],
'purge_force_required' => ['等待管理员确认强制删除', 'failed'],
'delete_failed' => ['自动删除失败', 'failed'],
'purge_stop_failed' => ['删除前关机失败', 'failed'],
'purge_failed' => ['自动删除失败', 'failed'],
default => [$status !== '' ? $status : '未知', 'other'],
};
}
private function invoiceFulfillmentStatus(string $status,
string $purpose = 'provision'): array
{
if ($purpose === 'upgrade') {
return match (strtolower($status)) {
'queued', 'pending', 'running', 'active' => ['升级中', 'warning'],
'completed', 'succeeded' => ['已升级', 'running'],
'failed' => ['升级失败', 'failed'],
'cancelled', 'canceled' => ['已取消', 'other'],
default => ['', ''],
};
}
if ($purpose === 'ip_change') {
return match (strtolower($status)) {
'queued', 'pending', 'running', 'active' => ['更换中', 'warning'],
'completed', 'succeeded' => ['已更换', 'running'],
'review' => ['待人工核对', 'warning'],
'failed' => ['更换失败', 'failed'],
'cancelled', 'canceled' => ['已取消', 'other'],
default => ['', ''],
};
}
return match (strtolower($status)) {
'awaiting_payment', 'payment_pending', 'draft' => ['等待支付', 'warning'],
'queued', 'pending', 'provisioning', 'running', 'active'
=> ['创建中', 'warning'],
'completed', 'succeeded' => ['已开通', 'running'],
'failed' => ['创建失败', 'failed'],
'refunded' => ['开通失败,已退款', 'failed'],
'cancelled', 'canceled' => ['已取消', 'other'],
default => ['', ''],
};
}
private function formatTimestamp($value, string $fallback = '暂未提供'): string
{
return is_numeric($value) & & (int) $value > 0
? date('Y/m/d H:i:s', (int) $value) : $fallback;
}
private function formatMemory(int $memoryMiB): string
{
$memoryGiB = $memoryMiB / 1024;
return number_format($memoryGiB, $memoryGiB === floor($memoryGiB) ? 0 : 1,
'.', ',') . ' GB';
}
private function formatBytes(int $bytes, bool $unlimitedZero = false): string
{
if ($bytes === 0 & & $unlimitedZero) {
@ -865,39 +504,6 @@ final class CloudPageController
return number_format($amount, $precision, '.', '') . ' ' . $units[$unit];
}
private function paymentLabel(string $paidVia): string
{
return match (strtolower(trim($paidVia))) {
'balance' => '账户余额',
'yipay_alipay' => '易支付 · 支付宝',
'yipay_wxpay' => '易支付 · 微信',
'paytaro' => 'Paytaro · 支付宝',
'stripe_card' => 'Stripe · 银行卡',
'stripe_alipay' => 'Stripe · 支付宝',
'usdt' => 'USDT-TRC20',
default => $paidVia,
};
}
private function firstText(...$values): string
{
foreach ($values as $value) {
$text = trim((string) ($value ?? ''));
if ($text !== '') {
return $text;
}
}
return '';
}
private function formatUsd($value): string
{
if (!is_numeric($value) || (float) $value < 0 ) {
return '$—';
}
return '$' . number_format((float) $value, 2, '.', ',');
}
private function pageNumber($value): int
{
return !is_array($value)