37 lines
1.4 KiB
PHP
37 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Services\CafeBazaar;
|
|
|
|
use App\Models\BazaarSubscription;
|
|
use App\Models\MobileApp;
|
|
use Carbon\CarbonImmutable;
|
|
use Illuminate\Support\Arr;
|
|
|
|
class BazaarSubscriptionRecorder
|
|
{
|
|
public function record(MobileApp $app, string $subscriptionId, string $purchaseToken, BazaarResponse $response): ?BazaarSubscription
|
|
{
|
|
if (! $response->successful()) {
|
|
return null;
|
|
}
|
|
|
|
$startsAt = CarbonImmutable::createFromTimestampMs((int) Arr::get($response->body, 'initiationTimestampMsec'));
|
|
$validUntil = CarbonImmutable::createFromTimestampMs((int) Arr::get($response->body, 'validUntilTimestampMsec'));
|
|
|
|
return BazaarSubscription::updateOrCreate(
|
|
['mobile_app_id' => $app->id, 'purchase_token_hash' => hash('sha256', $purchaseToken)],
|
|
[
|
|
'purchase_token' => $purchaseToken,
|
|
'subscription_id' => $subscriptionId,
|
|
'linked_subscription_token' => Arr::get($response->body, 'linkedSubscriptionToken'),
|
|
'status' => $validUntil->isFuture() ? 'active' : 'expired',
|
|
'auto_renewing' => (bool) Arr::get($response->body, 'autoRenewing', false),
|
|
'starts_at' => $startsAt,
|
|
'valid_until' => $validUntil,
|
|
'validated_at' => now(),
|
|
'bazaar_response' => $response->body,
|
|
],
|
|
);
|
|
}
|
|
}
|