diff --git a/app/Services/Paddle/PaddleClient.php b/app/Services/Paddle/PaddleClient.php index e217d1a..2ca6b6e 100644 --- a/app/Services/Paddle/PaddleClient.php +++ b/app/Services/Paddle/PaddleClient.php @@ -20,17 +20,17 @@ class PaddleClient return $this->send('GET', $endpoint, ['query' => $query]); } - public function post(string $endpoint, array $payload = []): array + public function post(string $endpoint, array|object $payload = []): array { return $this->send('POST', $endpoint, ['json' => $payload]); } - public function patch(string $endpoint, array $payload = []): array + public function patch(string $endpoint, array|object $payload = []): array { return $this->send('PATCH', $endpoint, ['json' => $payload]); } - public function delete(string $endpoint, array $payload = []): array + public function delete(string $endpoint, array|object $payload = []): array { return $this->send('DELETE', $endpoint, ['json' => $payload]); } diff --git a/tests/Feature/Api/Tenant/BillingPortalTest.php b/tests/Feature/Api/Tenant/BillingPortalTest.php index c289758..a4a4c95 100644 --- a/tests/Feature/Api/Tenant/BillingPortalTest.php +++ b/tests/Feature/Api/Tenant/BillingPortalTest.php @@ -10,20 +10,29 @@ class BillingPortalTest extends TenantTestCase { public function test_tenant_can_create_paddle_portal_session(): void { - Http::fake([ - '*paddle.com/customers' => Http::response([ - 'data' => ['id' => 'cus_123'], - ], 200), - '*paddle.com/customers/*/portal-sessions' => Http::response([ - 'data' => [ - 'urls' => [ - 'general' => [ - 'overview' => 'https://portal.example/overview', + Http::fake(function (Request $request) { + $url = $request->url(); + + if (str_contains($url, '/customers') && $request->method() === 'POST' && ! str_contains($url, '/portal-sessions')) { + return Http::response([ + 'data' => ['id' => 'cus_123'], + ], 200); + } + + if (str_contains($url, '/portal-sessions') && $request->method() === 'POST') { + return Http::response([ + 'data' => [ + 'urls' => [ + 'general' => [ + 'overview' => 'https://portal.example/overview', + ], ], ], - ], - ], 200), - ]); + ], 200); + } + + return Http::response([], 404); + }); $this->tenant->forceFill(['paddle_customer_id' => null])->save(); @@ -33,10 +42,10 @@ class BillingPortalTest extends TenantTestCase $response->assertJsonPath('url', 'https://portal.example/overview'); Http::assertSent(function (Request $request): bool { - $path = parse_url($request->url(), PHP_URL_PATH); + $url = $request->url(); return $request->hasHeader('Paddle-Version', '1') - && $path === '/customers/cus_123/portal-sessions' + && str_contains($url, '/portal-sessions') && $request->body() === '{}'; }); @@ -49,9 +58,9 @@ class BillingPortalTest extends TenantTestCase 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); + $url = $request->url(); - if ($path === '/customers' && $request->method() === 'POST') { + if (str_contains($url, '/customers') && $request->method() === 'POST' && ! str_contains($url, '/portal-sessions')) { return Http::response([ 'error' => [ 'type' => 'request_error', @@ -61,7 +70,7 @@ class BillingPortalTest extends TenantTestCase ], 409); } - if ($path === '/customers' && $request->method() === 'GET') { + if (str_contains($url, '/customers') && $request->method() === 'GET') { return Http::response([ 'data' => [ ['id' => 'cus_existing'], @@ -69,7 +78,7 @@ class BillingPortalTest extends TenantTestCase ], 200); } - if ($path === '/customers/cus_existing/portal-sessions' && $request->method() === 'POST') { + if (str_contains($url, '/portal-sessions') && $request->method() === 'POST') { return Http::response([ 'data' => [ 'urls' => [