Files
fotospiel-app/app/Jobs/SyncCouponToLemonSqueezy.php
2026-02-03 10:59:54 +01:00

69 lines
2.1 KiB
PHP

<?php
namespace App\Jobs;
use App\Models\Coupon;
use App\Services\LemonSqueezy\Exceptions\LemonSqueezyException;
use App\Services\LemonSqueezy\LemonSqueezyDiscountService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
class SyncCouponToLemonSqueezy implements ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;
public function __construct(
public Coupon $coupon,
public bool $archive = false,
) {}
public function handle(LemonSqueezyDiscountService $discounts): void
{
try {
if ($this->archive) {
$discounts->archiveDiscount($this->coupon);
$this->coupon->forceFill([
'lemonsqueezy_discount_id' => null,
'lemonsqueezy_snapshot' => null,
'lemonsqueezy_last_synced_at' => now(),
])->save();
return;
}
$data = $discounts->updateDiscount($this->coupon);
$this->coupon->forceFill([
'lemonsqueezy_discount_id' => $data['id'] ?? $this->coupon->lemonsqueezy_discount_id,
'lemonsqueezy_snapshot' => $data,
'lemonsqueezy_last_synced_at' => now(),
])->save();
} catch (LemonSqueezyException $exception) {
Log::channel('lemonsqueezy-sync')->error('Failed syncing coupon to Lemon Squeezy', [
'coupon_id' => $this->coupon->id,
'message' => $exception->getMessage(),
'status' => $exception->status(),
'context' => $exception->context(),
]);
$this->coupon->forceFill([
'lemonsqueezy_snapshot' => [
'error' => $exception->getMessage(),
'status' => $exception->status(),
'context' => $exception->context(),
],
])->save();
throw $exception;
}
}
}