Migrate billing from Paddle to Lemon Squeezy
This commit is contained in:
167
app/Jobs/SyncPackageAddonToLemonSqueezy.php
Normal file
167
app/Jobs/SyncPackageAddonToLemonSqueezy.php
Normal file
@@ -0,0 +1,167 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs;
|
||||
|
||||
use App\Models\PackageAddon;
|
||||
use App\Services\LemonSqueezy\Exceptions\LemonSqueezyException;
|
||||
use App\Services\LemonSqueezy\LemonSqueezyAddonCatalogService;
|
||||
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 SyncPackageAddonToLemonSqueezy implements ShouldQueue
|
||||
{
|
||||
use Dispatchable;
|
||||
use InteractsWithQueue;
|
||||
use Queueable;
|
||||
use SerializesModels;
|
||||
|
||||
/**
|
||||
* @param array{dry_run?: bool, product?: array<string, mixed>, price?: array<string, mixed>} $options
|
||||
*/
|
||||
public function __construct(private readonly int $addonId, private readonly array $options = []) {}
|
||||
|
||||
public function handle(LemonSqueezyAddonCatalogService $catalog): void
|
||||
{
|
||||
$addon = PackageAddon::query()->find($this->addonId);
|
||||
|
||||
if (! $addon) {
|
||||
return;
|
||||
}
|
||||
|
||||
$dryRun = (bool) ($this->options['dry_run'] ?? false);
|
||||
$productOverrides = Arr::get($this->options, 'product', []);
|
||||
$priceOverrides = Arr::get($this->options, 'price', []);
|
||||
|
||||
if ($dryRun) {
|
||||
$this->storeDryRunSnapshot($addon, $productOverrides, $priceOverrides);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Mark syncing (metadata)
|
||||
$addon->forceFill([
|
||||
'metadata' => array_merge($addon->metadata ?? [], [
|
||||
'lemonsqueezy_sync_status' => 'syncing',
|
||||
'lemonsqueezy_synced_at' => now()->toIso8601String(),
|
||||
]),
|
||||
])->save();
|
||||
|
||||
try {
|
||||
$payloadOverrides = $this->buildPayloadOverrides($addon, $productOverrides, $priceOverrides);
|
||||
|
||||
$productResponse = $addon->metadata['lemonsqueezy_product_id'] ?? null
|
||||
? $catalog->updateProduct($addon->metadata['lemonsqueezy_product_id'], $addon, $payloadOverrides['product'])
|
||||
: $catalog->createProduct($addon, $payloadOverrides['product']);
|
||||
|
||||
$productId = (string) ($productResponse['id'] ?? $addon->metadata['lemonsqueezy_product_id'] ?? null);
|
||||
|
||||
if (! $productId) {
|
||||
throw new LemonSqueezyException('Lemon Squeezy product ID missing after addon sync.');
|
||||
}
|
||||
|
||||
$priceResponse = $addon->variant_id
|
||||
? $catalog->updatePrice($addon->variant_id, $addon, array_merge($payloadOverrides['price'], ['product_id' => $productId]))
|
||||
: $catalog->createPrice($addon, $productId, $payloadOverrides['price']);
|
||||
|
||||
$priceId = (string) ($priceResponse['id'] ?? $addon->variant_id);
|
||||
|
||||
if (! $priceId) {
|
||||
throw new LemonSqueezyException('Lemon Squeezy variant ID missing after addon sync.');
|
||||
}
|
||||
|
||||
$addon->forceFill([
|
||||
'variant_id' => $priceId,
|
||||
'metadata' => array_merge($addon->metadata ?? [], [
|
||||
'lemonsqueezy_sync_status' => 'synced',
|
||||
'lemonsqueezy_synced_at' => now()->toIso8601String(),
|
||||
'lemonsqueezy_product_id' => $productId,
|
||||
'lemonsqueezy_snapshot' => [
|
||||
'product' => $productResponse,
|
||||
'price' => $priceResponse,
|
||||
'payload' => $payloadOverrides,
|
||||
],
|
||||
]),
|
||||
])->save();
|
||||
} catch (Throwable $exception) {
|
||||
Log::channel('lemonsqueezy-sync')->error('Lemon Squeezy addon sync failed', [
|
||||
'addon_id' => $addon->id,
|
||||
'message' => $exception->getMessage(),
|
||||
'exception' => $exception,
|
||||
]);
|
||||
|
||||
$addon->forceFill([
|
||||
'metadata' => array_merge($addon->metadata ?? [], [
|
||||
'lemonsqueezy_sync_status' => 'failed',
|
||||
'lemonsqueezy_synced_at' => now()->toIso8601String(),
|
||||
'lemonsqueezy_error' => [
|
||||
'message' => $exception->getMessage(),
|
||||
'class' => $exception::class,
|
||||
],
|
||||
]),
|
||||
])->save();
|
||||
|
||||
throw $exception;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $productOverrides
|
||||
* @param array<string, mixed> $priceOverrides
|
||||
* @return array{product: array<string, mixed>, price: array<string, mixed>}
|
||||
*/
|
||||
protected function buildPayloadOverrides(PackageAddon $addon, array $productOverrides, array $priceOverrides): array
|
||||
{
|
||||
// Reuse Package model payload builder shape via duck typing
|
||||
$baseProduct = [
|
||||
'name' => $addon->label,
|
||||
'description' => sprintf('Addon %s', $addon->key),
|
||||
'type' => 'standard',
|
||||
'custom_data' => [
|
||||
'addon_key' => $addon->key,
|
||||
'increments' => $addon->increments,
|
||||
],
|
||||
];
|
||||
|
||||
$basePrice = [
|
||||
'description' => $addon->label,
|
||||
'custom_data' => [
|
||||
'addon_key' => $addon->key,
|
||||
],
|
||||
];
|
||||
|
||||
return [
|
||||
'product' => array_merge($baseProduct, $productOverrides),
|
||||
'price' => array_merge($basePrice, $priceOverrides),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $productOverrides
|
||||
* @param array<string, mixed> $priceOverrides
|
||||
*/
|
||||
protected function storeDryRunSnapshot(PackageAddon $addon, array $productOverrides, array $priceOverrides): void
|
||||
{
|
||||
$payloadOverrides = $this->buildPayloadOverrides($addon, $productOverrides, $priceOverrides);
|
||||
|
||||
$addon->forceFill([
|
||||
'metadata' => array_merge($addon->metadata ?? [], [
|
||||
'lemonsqueezy_sync_status' => 'dry-run',
|
||||
'lemonsqueezy_synced_at' => now()->toIso8601String(),
|
||||
'lemonsqueezy_snapshot' => [
|
||||
'dry_run' => true,
|
||||
'payload' => $payloadOverrides,
|
||||
],
|
||||
]),
|
||||
])->save();
|
||||
|
||||
Log::channel('lemonsqueezy-sync')->info('Lemon Squeezy addon dry-run snapshot generated', [
|
||||
'addon_id' => $addon->id,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user