Fix Paddle customer lookup for billing

This commit is contained in:
Codex Agent
2025-12-22 15:55:01 +01:00
parent d78ed0961b
commit 526e59dc27
4 changed files with 156 additions and 6 deletions

View File

@@ -32,10 +32,19 @@ class TenantBillingController extends Controller
}
if (! $tenant->paddle_customer_id) {
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' => 'Tenant has no Paddle customer identifier.',
]);
'message' => 'Failed to resolve Paddle customer.',
], 502);
}
}
$cursor = $request->query('cursor');

View File

@@ -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.');
}
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;
}
}

View File

@@ -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',
]);
}
}

View File

@@ -0,0 +1,50 @@
<?php
namespace Tests\Feature\Api\Tenant;
use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Http;
use Tests\Feature\Tenant\TenantTestCase;
class BillingTransactionsTest extends TenantTestCase
{
public function test_transactions_endpoint_creates_missing_paddle_customer_id(): void
{
Http::fake(function (Request $request) {
$path = parse_url($request->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',
]);
}
}