This commit is contained in:
Meghdad committed 2026-08-24 19:44:24 +03:30
commit 8d5446f466
232 files changed
+25416

No files matched your search

@@ -0,0 +1,36 @@
<?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,
],
);
}
}