|
|
|
|
@ -11,6 +11,7 @@ use App\Models\CloudOffer;
|
|
|
|
|
use App\Models\CloudOrder;
|
|
|
|
|
use App\Models\CloudOrderItem;
|
|
|
|
|
use App\Models\CloudRenewal;
|
|
|
|
|
use App\Models\CloudServicePeriod;
|
|
|
|
|
use App\Models\CloudJob;
|
|
|
|
|
use App\Models\Shop;
|
|
|
|
|
use App\Models\User;
|
|
|
|
|
@ -1228,6 +1229,7 @@ final class CloudInvoiceService
|
|
|
|
|
'amount' => (string) $item->amount,
|
|
|
|
|
'currency' => (string) $item->currency,
|
|
|
|
|
])->all();
|
|
|
|
|
$renewalResult = self::renewalResult($invoice, $purpose);
|
|
|
|
|
return [
|
|
|
|
|
'id' => (string) $invoice->id,
|
|
|
|
|
'title' => $title,
|
|
|
|
|
@ -1276,6 +1278,7 @@ final class CloudInvoiceService
|
|
|
|
|
] : null,
|
|
|
|
|
'amount' => (string) $invoice->amount,
|
|
|
|
|
'items' => $items,
|
|
|
|
|
'renewal_result' => $renewalResult,
|
|
|
|
|
'validity_days' => (int) $invoice->validity_days,
|
|
|
|
|
'service_days' => (int) $invoice->validity_days,
|
|
|
|
|
'traffic_quota_bytes' => (int) $invoice->traffic_quota_bytes,
|
|
|
|
|
@ -1292,6 +1295,50 @@ final class CloudInvoiceService
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return only the immutable rows created by this paid renewal. Never use
|
|
|
|
|
* the instance's current expiry: later renewals may already have moved it.
|
|
|
|
|
*/
|
|
|
|
|
private static function renewalResult(
|
|
|
|
|
CloudInvoice $invoice,
|
|
|
|
|
string $purpose
|
|
|
|
|
): ?array {
|
|
|
|
|
if ($purpose !== 'renewal'
|
|
|
|
|
|| (string) $invoice->status !== 'paid'
|
|
|
|
|
|| $invoice->renewal_id === null
|
|
|
|
|
|| $invoice->instance_id === null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
$renewal = CloudRenewal::where('id', (int) $invoice->renewal_id)
|
|
|
|
|
->where('user_id', (int) $invoice->user_id)
|
|
|
|
|
->where('instance_id', (int) $invoice->instance_id)
|
|
|
|
|
->first([
|
|
|
|
|
'id', 'instance_id', 'user_id', 'period_id',
|
|
|
|
|
'validity_days', 'previous_expires_at', 'new_expires_at',
|
|
|
|
|
]);
|
|
|
|
|
if ($renewal === null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
$period = $renewal->period_id === null ? null
|
|
|
|
|
: CloudServicePeriod::where('id', (int) $renewal->period_id)
|
|
|
|
|
->where('renewal_id', (int) $renewal->id)
|
|
|
|
|
->where('user_id', (int) $invoice->user_id)
|
|
|
|
|
->where('instance_id', (int) $invoice->instance_id)
|
|
|
|
|
->first(['id', 'starts_at', 'ends_at']);
|
|
|
|
|
return [
|
|
|
|
|
'previous_expires_at' => max(
|
|
|
|
|
0,
|
|
|
|
|
(int) $renewal->previous_expires_at
|
|
|
|
|
),
|
|
|
|
|
'new_expires_at' => max(0, (int) $renewal->new_expires_at),
|
|
|
|
|
'period_starts_at' => $period === null
|
|
|
|
|
? null : max(0, (int) $period->starts_at),
|
|
|
|
|
'period_ends_at' => $period === null
|
|
|
|
|
? null : max(0, (int) $period->ends_at),
|
|
|
|
|
'validity_days' => max(0, (int) $renewal->validity_days),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function payBalance(CloudInvoice $invoice, array $actor, string $idempotency): CloudInvoice
|
|
|
|
|
{
|
|
|
|
|
return self::payBalanceResult($invoice, $actor, $idempotency)['invoice'];
|
|
|
|
|
|