From 526e59dc2744cc50b22df86cd6316ed1ead8c1a9 Mon Sep 17 00:00:00 2001 From: Codex Agent Date: Mon, 22 Dec 2025 15:55:01 +0100 Subject: [PATCH] Fix Paddle customer lookup for billing --- .../Api/TenantBillingController.php | 17 ++++-- app/Services/Paddle/PaddleCustomerService.php | 43 ++++++++++++++- .../Feature/Api/Tenant/BillingPortalTest.php | 52 +++++++++++++++++++ .../Api/Tenant/BillingTransactionsTest.php | 50 ++++++++++++++++++ 4 files changed, 156 insertions(+), 6 deletions(-) create mode 100644 tests/Feature/Api/Tenant/BillingTransactionsTest.php diff --git a/app/Http/Controllers/Api/TenantBillingController.php b/app/Http/Controllers/Api/TenantBillingController.php index fa10d9d..d8ade7f 100644 --- a/app/Http/Controllers/Api/TenantBillingController.php +++ b/app/Http/Controllers/Api/TenantBillingController.php @@ -32,10 +32,19 @@ class TenantBillingController extends Controller } if (! $tenant->paddle_customer_id) { - return response()->json([ - 'data' => [], - 'message' => 'Tenant has no Paddle customer identifier.', - ]); + try { + $this->paddleCustomers->ensureCustomerId($tenant); + } catch (\Throwable $exception) { + Log::warning('Failed to resolve Paddle customer for tenant', [ + 'tenant_id' => $tenant->id, + 'error' => $exception->getMessage(), + ]); + + return response()->json([ + 'data' => [], + 'message' => 'Failed to resolve Paddle customer.', + ], 502); + } } $cursor = $request->query('cursor'); diff --git a/app/Services/Paddle/PaddleCustomerService.php b/app/Services/Paddle/PaddleCustomerService.php index 38bd1cf..2437268 100644 --- a/app/Services/Paddle/PaddleCustomerService.php +++ b/app/Services/Paddle/PaddleCustomerService.php @@ -17,8 +17,10 @@ class PaddleCustomerService return $tenant->paddle_customer_id; } + $email = $tenant->contact_email ?: ($tenant->user?->email ?? null); + $payload = [ - 'email' => $tenant->contact_email ?: ($tenant->user?->email ?? null), + 'email' => $email, 'name' => $tenant->name, ]; @@ -26,7 +28,19 @@ class PaddleCustomerService throw new PaddleException('Tenant email address required to create Paddle customer.'); } - $response = $this->client->post('/customers', $payload); + try { + $response = $this->client->post('/customers', $payload); + } catch (PaddleException $exception) { + $existingCustomerId = $this->resolveExistingCustomerId($tenant, $email, $exception); + if ($existingCustomerId) { + $tenant->forceFill(['paddle_customer_id' => $existingCustomerId])->save(); + + return $existingCustomerId; + } + + throw $exception; + } + $customerId = Arr::get($response, 'data.id') ?? Arr::get($response, 'id'); if (! $customerId) { @@ -38,4 +52,29 @@ class PaddleCustomerService return $customerId; } + + protected function resolveExistingCustomerId(Tenant $tenant, string $email, PaddleException $exception): ?string + { + if ($exception->status() !== 409 || Arr::get($exception->context(), 'error.code') !== 'customer_already_exists') { + return null; + } + + $response = $this->client->get('/customers', [ + 'email' => $email, + 'per_page' => 1, + ]); + + $customerId = Arr::get($response, 'data.0.id') ?? Arr::get($response, 'data.0.customer_id'); + + if (! $customerId) { + Log::warning('Paddle customer lookup by email returned no id', [ + 'tenant' => $tenant->id, + 'error_code' => Arr::get($exception->context(), 'error.code'), + ]); + + return null; + } + + return $customerId; + } } diff --git a/tests/Feature/Api/Tenant/BillingPortalTest.php b/tests/Feature/Api/Tenant/BillingPortalTest.php index 093bf94..c3b2c23 100644 --- a/tests/Feature/Api/Tenant/BillingPortalTest.php +++ b/tests/Feature/Api/Tenant/BillingPortalTest.php @@ -2,6 +2,7 @@ namespace Tests\Feature\Api\Tenant; +use Illuminate\Http\Client\Request; use Illuminate\Support\Facades\Http; use Tests\Feature\Tenant\TenantTestCase; @@ -36,4 +37,55 @@ class BillingPortalTest extends TenantTestCase 'paddle_customer_id' => 'cus_123', ]); } + + public function test_tenant_can_reuse_existing_paddle_customer_when_customer_already_exists(): void + { + Http::fake(function (Request $request) { + $path = parse_url($request->url(), PHP_URL_PATH); + + if ($path === '/customers' && $request->method() === 'POST') { + return Http::response([ + 'error' => [ + 'type' => 'request_error', + 'code' => 'customer_already_exists', + 'message' => 'Customer already exists.', + ], + ], 409); + } + + if ($path === '/customers' && $request->method() === 'GET') { + return Http::response([ + 'data' => [ + ['id' => 'cus_existing'], + ], + ], 200); + } + + if ($path === '/customer-portal-sessions' && $request->method() === 'POST') { + return Http::response([ + 'data' => [ + 'urls' => [ + 'general' => [ + 'overview' => 'https://portal.example/overview', + ], + ], + ], + ], 200); + } + + return Http::response([], 404); + }); + + $this->tenant->forceFill(['paddle_customer_id' => null])->save(); + + $response = $this->authenticatedRequest('POST', '/api/v1/tenant/billing/portal'); + + $response->assertOk(); + $response->assertJsonPath('url', 'https://portal.example/overview'); + + $this->assertDatabaseHas('tenants', [ + 'id' => $this->tenant->id, + 'paddle_customer_id' => 'cus_existing', + ]); + } } diff --git a/tests/Feature/Api/Tenant/BillingTransactionsTest.php b/tests/Feature/Api/Tenant/BillingTransactionsTest.php new file mode 100644 index 0000000..2289ae7 --- /dev/null +++ b/tests/Feature/Api/Tenant/BillingTransactionsTest.php @@ -0,0 +1,50 @@ +url(), PHP_URL_PATH); + + if ($path === '/customers' && $request->method() === 'POST') { + return Http::response([ + 'data' => ['id' => 'cus_456'], + ], 200); + } + + if ($path === '/transactions' && $request->method() === 'GET') { + return Http::response([ + 'data' => [], + 'meta' => [ + 'pagination' => [ + 'next' => null, + 'previous' => null, + 'has_more' => false, + ], + ], + ], 200); + } + + return Http::response([], 404); + }); + + $this->tenant->forceFill(['paddle_customer_id' => null])->save(); + + $response = $this->authenticatedRequest('GET', '/api/v1/tenant/billing/transactions'); + + $response->assertOk(); + $response->assertJsonPath('data', []); + + $this->assertDatabaseHas('tenants', [ + 'id' => $this->tenant->id, + 'paddle_customer_id' => 'cus_456', + ]); + } +}