@ -53,39 +53,26 @@ final class CloudInvoicePresentation
$facts = [
['label' => '账单编号', 'value' => '#' . (string) ($invoice['id'] ?? '—')],
['label' => '创建时间', 'value' => $created],
self::timestampFact('创建时间', $invoice['created_at'] ?? null,
'时间未记录'),
];
if ($purpose === 'provision') {
$facts[] = [
'label' => '支付截止',
'value' => self::formatTimestamp(
$invoice['payment_deadline_at'] ?? $invoice['due_at'] ?? null
),
];
$facts[] = self::timestampFact('支付截止',
$invoice['payment_deadline_at'] ?? $invoice['due_at'] ?? null);
} elseif ($purpose === 'renewal') {
$facts[] = [
'label' => '实例到期',
'value' => self::formatTimestamp(
$invoice['renews_expires_at'] ?? $invoice['due_at'] ?? null
),
];
$facts[] = self::timestampFact('实例到期',
$invoice['renews_expires_at'] ?? $invoice['due_at'] ?? null);
if (($invoice['deletion_at'] ?? null) !== null) {
$facts[] = [
'label' => '最晚支付',
'value' => self::formatTimestamp($invoice['deletion_at']),
];
$facts[] = self::timestampFact('最晚支付',
$invoice['deletion_at']);
}
} elseif (($invoice['payment_deadline_at'] ?? null) !== null) {
$facts[] = [
'label' => '支付截止',
'value' => self::formatTimestamp($invoice['payment_deadline_at']),
];
$facts[] = self::timestampFact('支付截止',
$invoice['payment_deadline_at']);
}
if (($invoice['paid_at'] ?? null) !== null) {
$facts[] = [
'label' => '支付时间',
'value' => self::formatTimestamp($invoice['paid_at']),
];
$facts[] = self::timestampFact('支付时间',
$invoice['paid_at']);
}
if (trim((string) ($invoice['paid_via'] ?? '')) !== '') {
$facts[] = [
@ -118,6 +105,7 @@ final class CloudInvoicePresentation
'id' => (string) ($invoice['id'] ?? ''),
'title' => self::invoiceTitle($invoice, $purpose),
'number_text' => '账单 #' . (string) ($invoice['id'] ?? '—'),
'created_at' => self::timestampValue($invoice['created_at'] ?? null),
'created_text' => '创建于 ' . $created,
'subtitle' => '账单 #' . (string) ($invoice['id'] ?? '—')
. ' · ' . $created,
@ -286,6 +274,9 @@ final class CloudInvoicePresentation
) !== ''))
& & $fulfillmentLabel !== '';
$invoice['context_display'] = self::context($invoice, $purpose, $status);
$invoice['context_segments'] = self::contextSegments(
$invoice, $purpose, $status
);
$invoice['polling_required'] = in_array($status, ['open', 'overdue'], true)
|| (in_array($purpose, ['upgrade', 'ip_change'], true)
& & $fulfillmentStatus === 'active');
@ -325,6 +316,50 @@ final class CloudInvoicePresentation
return '';
}
/**
* Preserve the server-owned context vocabulary while exposing canonical
* timestamps for browser-local formatting.
*
* @param array< string , mixed > $invoice
* @return array< int , array { kind: string , value: int | string } >
*/
private static function contextSegments(array $invoice, string $purpose,
string $status): array
{
if ($purpose === 'renewal') {
$segments = [
['kind' => 'text', 'value' => '对应实例到期 '],
['kind' => 'timestamp', 'value' => self::timestampValue(
$invoice['renews_expires_at'] ?? $invoice['due_at'] ?? null
) ?? 0],
];
if (in_array($status, ['open', 'overdue'], true)) {
$segments[] = ['kind' => 'text', 'value' => ' · 最晚支付 '];
$segments[] = ['kind' => 'timestamp', 'value' =>
self::timestampValue($invoice['payment_deadline_at'] ?? null)
?? 0];
}
return $segments;
}
if ($purpose === 'provision') {
return [
['kind' => 'text', 'value' => '支付截止 '],
['kind' => 'timestamp', 'value' => self::timestampValue(
$invoice['payment_deadline_at'] ?? $invoice['due_at'] ?? null
) ?? 0],
];
}
if (in_array($purpose, ['upgrade', 'ip_change', 'backup_addon'], true)) {
$target = self::firstText($invoice['instance_name'] ?? null);
if ($target === '' & & ($invoice['instance_id'] ?? null) !== null) {
$target = '#' . (string) $invoice['instance_id'];
}
return [['kind' => 'text', 'value' => '目标实例 '
. ($target !== '' ? $target : '实例')]];
}
return [];
}
/**
* @param array< string , mixed > $payload
* @param array< string , mixed > $invoice
@ -734,6 +769,22 @@ final class CloudInvoicePresentation
);
}
/** @return array{label: string, value: string, timestamp: int|null} */
private static function timestampFact(string $label, $value,
string $fallback = '暂未提供'): array
{
return [
'label' => $label,
'value' => self::formatTimestamp($value, $fallback),
'timestamp' => self::timestampValue($value),
];
}
private static function timestampValue($value): ?int
{
return is_numeric($value) & & (int) $value > 0 ? (int) $value : null;
}
private static function formatMemory(int $memoryMiB): string
{
$memoryGiB = $memoryMiB / 1024;