Migrate billing from Paddle to Lemon Squeezy
This commit is contained in:
130
app/Console/Commands/LemonSqueezyRegisterWebhooks.php
Normal file
130
app/Console/Commands/LemonSqueezyRegisterWebhooks.php
Normal file
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Services\LemonSqueezy\LemonSqueezyClient;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class LemonSqueezyRegisterWebhooks extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'lemonsqueezy:webhooks:register
|
||||
{--url= : Destination URL for Lemon Squeezy webhooks}
|
||||
{--events=* : Override event types to subscribe}
|
||||
{--secret= : Override the webhook signing secret}
|
||||
{--test-mode : Register the webhook in test mode}
|
||||
{--dry-run : Output payload without creating the destination}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Register Lemon Squeezy webhook notification settings.';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
public function handle(LemonSqueezyClient $client): int
|
||||
{
|
||||
$destination = (string) ($this->option('url') ?: $this->defaultWebhookUrl());
|
||||
|
||||
if ($destination === '') {
|
||||
$this->error('Webhook destination URL is required. Use --url=...');
|
||||
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
$events = collect((array) $this->option('events'))
|
||||
->filter()
|
||||
->map(fn ($event) => trim((string) $event))
|
||||
->filter()
|
||||
->values()
|
||||
->all();
|
||||
|
||||
if ($events === []) {
|
||||
$events = config('lemonsqueezy.webhook_events', []);
|
||||
}
|
||||
|
||||
if ($events === [] || ! is_array($events)) {
|
||||
$this->error('No webhook events configured. Set config(lemonsqueezy.webhook_events) or pass --events.');
|
||||
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
$secret = (string) ($this->option('secret') ?: config('lemonsqueezy.webhook_secret'));
|
||||
if ($secret === '') {
|
||||
$this->error('Webhook signing secret is required. Set LEMONSQUEEZY_WEBHOOK_SECRET or pass --secret.');
|
||||
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
$storeId = (string) config('lemonsqueezy.store_id');
|
||||
if ($storeId === '') {
|
||||
$this->error('Lemon Squeezy store id is required. Set LEMONSQUEEZY_STORE_ID.');
|
||||
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
$testMode = (bool) $this->option('test-mode') || (bool) config('lemonsqueezy.test_mode', false);
|
||||
|
||||
$attributes = array_filter([
|
||||
'url' => $destination,
|
||||
'events' => $events,
|
||||
'secret' => $secret,
|
||||
'test_mode' => $testMode ? true : null,
|
||||
], static fn ($value) => $value !== null && $value !== '');
|
||||
|
||||
$payload = [
|
||||
'data' => [
|
||||
'type' => 'webhooks',
|
||||
'attributes' => $attributes,
|
||||
'relationships' => [
|
||||
'store' => [
|
||||
'data' => [
|
||||
'type' => 'stores',
|
||||
'id' => $storeId,
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
if ((bool) $this->option('dry-run')) {
|
||||
$this->line(json_encode($payload, JSON_PRETTY_PRINT));
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
|
||||
$response = $client->post('/webhooks', $payload);
|
||||
$data = Arr::get($response, 'data', $response);
|
||||
$id = Arr::get($data, 'id');
|
||||
|
||||
Log::channel('lemonsqueezy-sync')->info('Lemon Squeezy webhook registered', [
|
||||
'webhook_id' => $id,
|
||||
'destination' => $destination,
|
||||
'test_mode' => $testMode,
|
||||
]);
|
||||
|
||||
$this->info('Lemon Squeezy webhook registered.');
|
||||
|
||||
if ($id) {
|
||||
$this->line('ID: '.$id);
|
||||
}
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
|
||||
protected function defaultWebhookUrl(): string
|
||||
{
|
||||
$base = rtrim((string) config('app.url'), '/');
|
||||
|
||||
return $base !== '' ? $base.'/lemonsqueezy/webhook' : '';
|
||||
}
|
||||
}
|
||||
@@ -2,23 +2,23 @@
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Jobs\PullPackageFromPaddle;
|
||||
use App\Jobs\SyncPackageToPaddle;
|
||||
use App\Jobs\PullPackageFromLemonSqueezy;
|
||||
use App\Jobs\SyncPackageToLemonSqueezy;
|
||||
use App\Models\Package;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class PaddleSyncPackages extends Command
|
||||
class LemonSqueezySyncPackages extends Command
|
||||
{
|
||||
protected $signature = 'paddle:sync-packages
|
||||
protected $signature = 'lemonsqueezy:sync-packages
|
||||
{--package=* : Limit sync to the given package IDs or slugs}
|
||||
{--dry-run : Generate payload snapshots without calling Paddle}
|
||||
{--pull : Fetch remote Paddle state instead of pushing local changes}
|
||||
{--allow-unmapped : Allow sync when packages are missing Paddle product/price IDs}
|
||||
{--dry-run : Generate payload snapshots without calling Lemon Squeezy}
|
||||
{--pull : Fetch remote Lemon Squeezy state instead of pushing local changes}
|
||||
{--allow-unmapped : Allow sync when packages are missing Lemon Squeezy product/variant IDs}
|
||||
{--queue : Dispatch jobs onto the queue instead of running synchronously}';
|
||||
|
||||
protected $description = 'Synchronise local packages with Paddle products and prices.';
|
||||
protected $description = 'Synchronise local packages with Lemon Squeezy products and variants.';
|
||||
|
||||
public function handle(): int
|
||||
{
|
||||
@@ -52,7 +52,7 @@ class PaddleSyncPackages extends Command
|
||||
});
|
||||
|
||||
$this->info(sprintf(
|
||||
'Queued %d package %s for Paddle %s.',
|
||||
'Queued %d package %s for Lemon Squeezy %s.',
|
||||
$packages->count(),
|
||||
Str::plural('entry', $packages->count()),
|
||||
$pull ? 'pull' : 'sync'
|
||||
@@ -97,22 +97,22 @@ class PaddleSyncPackages extends Command
|
||||
|
||||
protected function guardUnmappedPackages(Collection $packages): bool
|
||||
{
|
||||
$unmapped = $packages->filter(fn (Package $package) => blank($package->paddle_product_id) || blank($package->paddle_price_id));
|
||||
$unmapped = $packages->filter(fn (Package $package) => blank($package->lemonsqueezy_product_id) || blank($package->lemonsqueezy_variant_id));
|
||||
|
||||
if ($unmapped->isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$this->error('Unmapped Paddle package IDs detected. Resolve legacy mappings or pass --allow-unmapped.');
|
||||
$this->error('Unmapped Lemon Squeezy package IDs detected. Resolve mappings or pass --allow-unmapped.');
|
||||
$this->table(
|
||||
['ID', 'Slug', 'Missing'],
|
||||
$unmapped->map(function (Package $package): array {
|
||||
$missing = [];
|
||||
if (blank($package->paddle_product_id)) {
|
||||
if (blank($package->lemonsqueezy_product_id)) {
|
||||
$missing[] = 'product_id';
|
||||
}
|
||||
if (blank($package->paddle_price_id)) {
|
||||
$missing[] = 'price_id';
|
||||
if (blank($package->lemonsqueezy_variant_id)) {
|
||||
$missing[] = 'variant_id';
|
||||
}
|
||||
|
||||
return [
|
||||
@@ -133,26 +133,26 @@ class PaddleSyncPackages extends Command
|
||||
];
|
||||
|
||||
if ($queue) {
|
||||
SyncPackageToPaddle::dispatch($package->id, $context);
|
||||
SyncPackageToLemonSqueezy::dispatch($package->id, $context);
|
||||
$this->line(sprintf('> queued sync for package #%d (%s)', $package->id, $package->slug));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
SyncPackageToPaddle::dispatchSync($package->id, $context);
|
||||
SyncPackageToLemonSqueezy::dispatchSync($package->id, $context);
|
||||
$this->line(sprintf('> synced package #%d (%s)', $package->id, $package->slug));
|
||||
}
|
||||
|
||||
protected function dispatchPullJob(Package $package, bool $queue): void
|
||||
{
|
||||
if ($queue) {
|
||||
PullPackageFromPaddle::dispatch($package->id);
|
||||
PullPackageFromLemonSqueezy::dispatch($package->id);
|
||||
$this->line(sprintf('> queued pull for package #%d (%s)', $package->id, $package->slug));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
PullPackageFromPaddle::dispatchSync($package->id);
|
||||
PullPackageFromLemonSqueezy::dispatchSync($package->id);
|
||||
$this->line(sprintf('> pulled package #%d (%s)', $package->id, $package->slug));
|
||||
}
|
||||
}
|
||||
@@ -1,132 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Services\Paddle\PaddleClient;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class PaddleRegisterWebhooks extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'paddle:webhooks:register
|
||||
{--url= : Destination URL for Paddle webhooks}
|
||||
{--description= : Description for the webhook destination}
|
||||
{--events=* : Override event types to subscribe}
|
||||
{--traffic-source=all : platform|simulation|all}
|
||||
{--include-sensitive : Include sensitive fields in webhook payloads}
|
||||
{--show-secret : Output the endpoint secret key}
|
||||
{--dry-run : Output payload without creating the destination}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Register Paddle webhook notification settings.';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
public function handle(PaddleClient $client): int
|
||||
{
|
||||
$destination = (string) ($this->option('url') ?: $this->defaultWebhookUrl());
|
||||
|
||||
if ($destination === '') {
|
||||
$this->error('Webhook destination URL is required. Use --url=...');
|
||||
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
$events = collect((array) $this->option('events'))
|
||||
->filter()
|
||||
->map(fn ($event) => trim((string) $event))
|
||||
->filter()
|
||||
->values()
|
||||
->all();
|
||||
|
||||
if ($events === []) {
|
||||
$events = config('paddle.webhook_events', []);
|
||||
}
|
||||
|
||||
if ($events === [] || ! is_array($events)) {
|
||||
$this->error('No webhook events configured. Set config(paddle.webhook_events) or pass --events.');
|
||||
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
$trafficSource = (string) $this->option('traffic-source');
|
||||
$allowedSources = ['platform', 'simulation', 'all'];
|
||||
|
||||
if (! in_array($trafficSource, $allowedSources, true)) {
|
||||
$this->error(sprintf('Invalid traffic source. Use one of: %s', implode(', ', $allowedSources)));
|
||||
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
$payload = [
|
||||
'type' => 'url',
|
||||
'destination' => $destination,
|
||||
'description' => $this->resolveDescription(),
|
||||
'subscribed_events' => $events,
|
||||
'traffic_source' => $trafficSource,
|
||||
'include_sensitive_fields' => (bool) $this->option('include-sensitive'),
|
||||
];
|
||||
|
||||
if ((bool) $this->option('dry-run')) {
|
||||
$this->line(json_encode($payload, JSON_PRETTY_PRINT));
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
|
||||
$response = $client->post('/notification-settings', $payload);
|
||||
$data = Arr::get($response, 'data', $response);
|
||||
$id = Arr::get($data, 'id');
|
||||
$secret = Arr::get($data, 'endpoint_secret_key');
|
||||
|
||||
Log::channel('paddle-sync')->info('Paddle webhook registered', [
|
||||
'notification_setting_id' => $id,
|
||||
'destination' => $destination,
|
||||
'traffic_source' => $trafficSource,
|
||||
]);
|
||||
|
||||
$this->info('Paddle webhook registered.');
|
||||
|
||||
if ($id) {
|
||||
$this->line('ID: '.$id);
|
||||
}
|
||||
|
||||
if ($secret && $this->option('show-secret')) {
|
||||
$this->line('Secret: '.$secret);
|
||||
} elseif ($secret) {
|
||||
$this->line('Secret returned (hidden). Use --show-secret to display.');
|
||||
}
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
|
||||
protected function defaultWebhookUrl(): string
|
||||
{
|
||||
$base = rtrim((string) config('app.url'), '/');
|
||||
|
||||
return $base !== '' ? $base.'/paddle/webhook' : '';
|
||||
}
|
||||
|
||||
protected function resolveDescription(): string
|
||||
{
|
||||
$description = (string) $this->option('description');
|
||||
|
||||
if ($description !== '') {
|
||||
return $description;
|
||||
}
|
||||
|
||||
$environment = (string) config('paddle.environment', 'production');
|
||||
|
||||
return sprintf('Fotospiel Paddle webhooks (%s)', $environment);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user