Files
fotospiel-app/app/Jobs/PullPackageFromLemonSqueezy.php
Codex Agent 10c99de1e2
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (push) Has been cancelled
tests / ui (push) Has been cancelled
Migrate billing from Paddle to Lemon Squeezy
2026-02-03 10:59:54 +01:00

83 lines
2.7 KiB
PHP

<?php
namespace App\Jobs;
use App\Models\Package;
use App\Services\LemonSqueezy\LemonSqueezyCatalogService;
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\Arr;
use Illuminate\Support\Facades\Log;
use Throwable;
class PullPackageFromLemonSqueezy implements ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;
public function __construct(private readonly int $packageId) {}
public function handle(LemonSqueezyCatalogService $catalog): void
{
$package = Package::query()->find($this->packageId);
if (! $package) {
return;
}
if (! $package->lemonsqueezy_product_id && ! $package->lemonsqueezy_variant_id) {
Log::channel('lemonsqueezy-sync')->warning('Lemon Squeezy pull skipped for package without linkage', [
'package_id' => $package->id,
]);
return;
}
try {
$product = $package->lemonsqueezy_product_id ? $catalog->fetchProduct($package->lemonsqueezy_product_id) : null;
$price = $package->lemonsqueezy_variant_id ? $catalog->fetchPrice($package->lemonsqueezy_variant_id) : null;
$snapshot = $package->lemonsqueezy_snapshot ?? [];
$snapshot['remote'] = array_filter([
'product' => $product,
'price' => $price,
], static fn ($value) => $value !== null);
$package->forceFill([
'lemonsqueezy_sync_status' => 'pulled',
'lemonsqueezy_synced_at' => now(),
'lemonsqueezy_snapshot' => $snapshot,
])->save();
Log::channel('lemonsqueezy-sync')->info('Lemon Squeezy package pull completed', [
'package_id' => $package->id,
]);
} catch (Throwable $exception) {
Log::channel('lemonsqueezy-sync')->error('Lemon Squeezy package pull failed', [
'package_id' => $package->id,
'message' => $exception->getMessage(),
'exception' => $exception,
]);
$snapshot = $package->lemonsqueezy_snapshot ?? [];
$snapshot['error'] = array_merge(Arr::get($snapshot, 'error', []), [
'message' => $exception->getMessage(),
'class' => $exception::class,
]);
$package->forceFill([
'lemonsqueezy_sync_status' => 'pull-failed',
'lemonsqueezy_synced_at' => now(),
'lemonsqueezy_snapshot' => $snapshot,
])->save();
throw $exception;
}
}
}