Migrate billing from Paddle to Lemon Squeezy
This commit is contained in:
@@ -2,107 +2,48 @@
|
||||
|
||||
namespace Tests\Feature\Api\Tenant;
|
||||
|
||||
use Illuminate\Http\Client\Request;
|
||||
use App\Models\Package;
|
||||
use App\Models\TenantPackage;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Tests\Feature\Tenant\TenantTestCase;
|
||||
|
||||
class BillingPortalTest extends TenantTestCase
|
||||
{
|
||||
public function test_tenant_can_create_paddle_portal_session(): void
|
||||
public function test_tenant_can_fetch_lemonsqueezy_portal_url(): void
|
||||
{
|
||||
Http::fake(function (Request $request) {
|
||||
$url = $request->url();
|
||||
config()->set('lemonsqueezy.base_url', 'https://lemonsqueezy.test');
|
||||
|
||||
if (str_contains($url, '/customers') && $request->method() === 'POST' && ! str_contains($url, '/portal-sessions')) {
|
||||
return Http::response([
|
||||
'data' => ['id' => 'cus_123'],
|
||||
], 200);
|
||||
}
|
||||
$package = Package::factory()->reseller()->create();
|
||||
TenantPackage::factory()->create([
|
||||
'tenant_id' => $this->tenant->id,
|
||||
'package_id' => $package->id,
|
||||
'lemonsqueezy_subscription_id' => 'sub_123',
|
||||
'active' => true,
|
||||
]);
|
||||
|
||||
if (str_contains($url, '/portal-sessions') && $request->method() === 'POST') {
|
||||
return Http::response([
|
||||
'data' => [
|
||||
Http::fake([
|
||||
'https://lemonsqueezy.test/subscriptions/sub_123' => Http::response([
|
||||
'data' => [
|
||||
'id' => 'sub_123',
|
||||
'attributes' => [
|
||||
'urls' => [
|
||||
'general' => [
|
||||
'overview' => 'https://portal.example/overview',
|
||||
],
|
||||
'customer_portal' => 'https://portal.example/overview',
|
||||
],
|
||||
],
|
||||
], 200);
|
||||
}
|
||||
|
||||
return Http::response([], 404);
|
||||
});
|
||||
|
||||
$this->tenant->forceFill(['paddle_customer_id' => null])->save();
|
||||
],
|
||||
], 200),
|
||||
]);
|
||||
|
||||
$response = $this->authenticatedRequest('POST', '/api/v1/tenant/billing/portal');
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJsonPath('url', 'https://portal.example/overview');
|
||||
|
||||
Http::assertSent(function (Request $request): bool {
|
||||
$url = $request->url();
|
||||
|
||||
return $request->hasHeader('Paddle-Version', '1')
|
||||
&& str_contains($url, '/portal-sessions')
|
||||
&& $request->body() === '{}';
|
||||
});
|
||||
|
||||
$this->assertDatabaseHas('tenants', [
|
||||
'id' => $this->tenant->id,
|
||||
'paddle_customer_id' => 'cus_123',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_tenant_can_reuse_existing_paddle_customer_when_customer_already_exists(): void
|
||||
public function test_portal_returns_404_when_no_active_subscription(): void
|
||||
{
|
||||
Http::fake(function (Request $request) {
|
||||
$url = $request->url();
|
||||
|
||||
if (str_contains($url, '/customers') && $request->method() === 'POST' && ! str_contains($url, '/portal-sessions')) {
|
||||
return Http::response([
|
||||
'error' => [
|
||||
'type' => 'request_error',
|
||||
'code' => 'customer_already_exists',
|
||||
'message' => 'Customer already exists.',
|
||||
],
|
||||
], 409);
|
||||
}
|
||||
|
||||
if (str_contains($url, '/customers') && $request->method() === 'GET') {
|
||||
return Http::response([
|
||||
'data' => [
|
||||
['id' => 'cus_existing'],
|
||||
],
|
||||
], 200);
|
||||
}
|
||||
|
||||
if (str_contains($url, '/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',
|
||||
]);
|
||||
$response->assertNotFound();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user