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.
253 lines
9.8 KiB
PHP
253 lines
9.8 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
/**
|
|
* Pure administrator billing adapter.
|
|
*
|
|
* It deliberately keeps invoice, order and payment records independent and
|
|
* only adds display fields beneath `presentation`.
|
|
*/
|
|
final class CloudAdminBillingPresentation
|
|
{
|
|
public static function page(string $view, array $payload): array
|
|
{
|
|
$view = in_array($view, ['invoices', 'orders', 'payments'], true)
|
|
? $view : 'invoices';
|
|
$items = [];
|
|
foreach (is_array($payload['items'] ?? null) ? $payload['items'] : []
|
|
as $item) {
|
|
if (!is_array($item)) {
|
|
continue;
|
|
}
|
|
$item['presentation'] = self::row($view, $item);
|
|
$items[] = $item;
|
|
}
|
|
$payload['items'] = $items;
|
|
if (is_array($payload['summary'] ?? null)) {
|
|
$summary = $payload['summary'];
|
|
$summary['presentation'] = self::summary($summary);
|
|
$payload['summary'] = $summary;
|
|
}
|
|
$payload['presentation'] = [
|
|
'catalog' => CloudBillingPresentationCatalog::filterOptions(),
|
|
];
|
|
return $payload;
|
|
}
|
|
|
|
public static function detail(array $payload): array
|
|
{
|
|
$invoice = is_array($payload['invoice'] ?? null)
|
|
? $payload['invoice'] : [];
|
|
if ($invoice !== []) {
|
|
$invoice['presentation'] = self::invoice($invoice);
|
|
$payload['invoice'] = $invoice;
|
|
}
|
|
if (is_array($payload['order'] ?? null)) {
|
|
$order = $payload['order'];
|
|
$order['presentation'] = self::order($order);
|
|
$payload['order'] = $order;
|
|
}
|
|
if (is_array($payload['job'] ?? null)) {
|
|
$job = $payload['job'];
|
|
$job['presentation'] = [
|
|
'status' => self::taskStatus($job),
|
|
'updated_display' => CloudBillingPresentationCatalog::timestamp(
|
|
$job['updated_at'] ?? null
|
|
),
|
|
];
|
|
$payload['job'] = $job;
|
|
}
|
|
if (is_array($payload['instance'] ?? null)) {
|
|
$instance = $payload['instance'];
|
|
$instance['presentation'] = [
|
|
'expires_display' => CloudBillingPresentationCatalog::timestamp(
|
|
$instance['expires_at'] ?? null
|
|
),
|
|
];
|
|
$payload['instance'] = $instance;
|
|
}
|
|
$attempts = [];
|
|
foreach (is_array($payload['payment_attempts'] ?? null)
|
|
? $payload['payment_attempts'] : [] as $attempt) {
|
|
if (!is_array($attempt)) {
|
|
continue;
|
|
}
|
|
$attempt['presentation'] = self::payment($attempt);
|
|
$attempts[] = $attempt;
|
|
}
|
|
$payload['payment_attempts'] = $attempts;
|
|
if (is_array($payload['funds'] ?? null)) {
|
|
$funds = $payload['funds'];
|
|
$currency = $invoice['currency'] ?? 'USD';
|
|
$funds['presentation'] = [
|
|
'settled_amount_display' => CloudBillingPresentationCatalog::money(
|
|
$funds['settled_amount'] ?? null, $currency
|
|
),
|
|
'refund_amount_display' => ($funds['refund_amount'] ?? null) === null
|
|
? '无已确认退款记录'
|
|
: CloudBillingPresentationCatalog::money(
|
|
$funds['refund_amount'], $currency
|
|
),
|
|
'balance_retained_amount_display' =>
|
|
CloudBillingPresentationCatalog::money(
|
|
$funds['balance_retained_amount'] ?? null, $currency
|
|
),
|
|
'refunded_at_display' => CloudBillingPresentationCatalog::timestamp(
|
|
$funds['refunded_at'] ?? null
|
|
),
|
|
];
|
|
$payload['funds'] = $funds;
|
|
}
|
|
$timeline = [];
|
|
foreach (is_array($payload['timeline'] ?? null)
|
|
? $payload['timeline'] : [] as $entry) {
|
|
if (!is_array($entry)) {
|
|
continue;
|
|
}
|
|
$entry['at_display'] = CloudBillingPresentationCatalog::timestamp(
|
|
$entry['at'] ?? null, '时间未记录'
|
|
);
|
|
$timeline[] = $entry;
|
|
}
|
|
$payload['timeline'] = $timeline;
|
|
return $payload;
|
|
}
|
|
|
|
private static function row(string $view, array $item): array
|
|
{
|
|
return match ($view) {
|
|
'orders' => self::order($item),
|
|
'payments' => self::payment($item),
|
|
default => self::invoice($item),
|
|
};
|
|
}
|
|
|
|
private static function summary(array $summary): array
|
|
{
|
|
$fields = ['total_amount', 'outstanding_amount', 'paid_amount',
|
|
'refunded_amount', 'paid_invoice_amount',
|
|
'credited_balance_amount'];
|
|
$presentation = [];
|
|
$mixedCurrency = ($summary['mixed_currency'] ?? false) === true
|
|
|| (int) ($summary['currency_count'] ?? 0) > 1;
|
|
foreach ($fields as $field) {
|
|
if (array_key_exists($field, $summary)) {
|
|
$presentation[$field . '_display'] = $mixedCurrency
|
|
? '多币种'
|
|
: CloudBillingPresentationCatalog::money(
|
|
$summary[$field], $summary['currency'] ?? 'USD'
|
|
);
|
|
}
|
|
}
|
|
$presentation['mixed_currency'] = $mixedCurrency;
|
|
return $presentation;
|
|
}
|
|
|
|
private static function invoice(array $invoice): array
|
|
{
|
|
$purpose = $invoice['purpose'] ?? null;
|
|
return [
|
|
'purpose_label' => CloudBillingPresentationCatalog::purposeLabel($purpose),
|
|
'amount_display' => CloudBillingPresentationCatalog::money(
|
|
$invoice['amount'] ?? null, $invoice['currency'] ?? 'USD'
|
|
),
|
|
'financial_status' => CloudBillingPresentationCatalog::status(
|
|
'financial', $invoice['financial_status'] ?? null
|
|
),
|
|
'fulfillment_status' => CloudBillingPresentationCatalog::status(
|
|
'fulfillment', $invoice['fulfillment_status'] ?? null, $purpose
|
|
),
|
|
'provider_label' => CloudBillingPresentationCatalog::providerLabel(
|
|
$invoice['payment_method'] ?? null
|
|
),
|
|
'created_display' => CloudBillingPresentationCatalog::timestamp(
|
|
$invoice['created_at'] ?? null
|
|
),
|
|
'updated_display' => CloudBillingPresentationCatalog::timestamp(
|
|
$invoice['updated_at'] ?? null
|
|
),
|
|
'due_display' => CloudBillingPresentationCatalog::timestamp(
|
|
$invoice['due_at'] ?? null
|
|
),
|
|
'payment_deadline_display' => CloudBillingPresentationCatalog::timestamp(
|
|
$invoice['payment_deadline_at'] ?? null
|
|
),
|
|
'paid_display' => CloudBillingPresentationCatalog::timestamp(
|
|
$invoice['paid_at'] ?? null
|
|
),
|
|
'renews_expires_display' => CloudBillingPresentationCatalog::timestamp(
|
|
$invoice['renews_expires_at'] ?? null
|
|
),
|
|
];
|
|
}
|
|
|
|
private static function order(array $order): array
|
|
{
|
|
return [
|
|
'amount_display' => CloudBillingPresentationCatalog::money(
|
|
$order['amount'] ?? null, $order['currency'] ?? 'USD'
|
|
),
|
|
'financial_status' => CloudBillingPresentationCatalog::status(
|
|
'financial', $order['financial_status'] ?? null
|
|
),
|
|
'fulfillment_status' => CloudBillingPresentationCatalog::status(
|
|
'fulfillment', $order['fulfillment_status'] ?? null
|
|
),
|
|
'created_display' => CloudBillingPresentationCatalog::timestamp(
|
|
$order['created_at'] ?? null
|
|
),
|
|
'updated_display' => CloudBillingPresentationCatalog::timestamp(
|
|
$order['updated_at'] ?? $order['created_at'] ?? null
|
|
),
|
|
];
|
|
}
|
|
|
|
private static function payment(array $payment): array
|
|
{
|
|
return [
|
|
'provider_label' => CloudBillingPresentationCatalog::providerLabel(
|
|
$payment['provider'] ?? null
|
|
),
|
|
'amount_display' => CloudBillingPresentationCatalog::money(
|
|
$payment['amount'] ?? null, $payment['currency'] ?? 'USD'
|
|
),
|
|
'payment_status' => CloudBillingPresentationCatalog::status(
|
|
'payment', $payment['payment_status'] ?? $payment['status'] ?? null
|
|
),
|
|
'invoice_financial_status' => CloudBillingPresentationCatalog::status(
|
|
'financial', $payment['invoice_financial_status'] ?? null
|
|
),
|
|
'purpose_label' => CloudBillingPresentationCatalog::purposeLabel(
|
|
$payment['purpose'] ?? null
|
|
),
|
|
'created_display' => CloudBillingPresentationCatalog::timestamp(
|
|
$payment['created_at'] ?? null
|
|
),
|
|
'updated_display' => CloudBillingPresentationCatalog::timestamp(
|
|
$payment['updated_at'] ?? null
|
|
),
|
|
];
|
|
}
|
|
|
|
private static function taskStatus(array $task): array
|
|
{
|
|
if (($task['completed'] ?? null) === false
|
|
&& (!array_key_exists('status', $task)
|
|
|| ($task['status'] ?? null) === 'active')) {
|
|
return CloudBillingPresentationCatalog::status('fulfillment', 'active');
|
|
}
|
|
if (($task['completed'] ?? null) === true
|
|
&& in_array($task['status'] ?? null, ['succeeded', 'failed'], true)) {
|
|
return CloudBillingPresentationCatalog::status(
|
|
'fulfillment', $task['status']
|
|
);
|
|
}
|
|
return [
|
|
'code' => 'invalid', 'label' => '任务状态协议无效',
|
|
'tone' => 'danger',
|
|
'class' => CloudBillingPresentationCatalog::badgeClass('danger'),
|
|
];
|
|
}
|
|
}
|