Migrate billing from Paddle to Lemon Squeezy
This commit is contained in:
@@ -16,7 +16,7 @@ class GiftVoucherLookupTest extends TestCase
|
||||
'code' => 'GIFT-TESTCODE',
|
||||
'amount' => 59.00,
|
||||
'currency' => 'EUR',
|
||||
'paddle_checkout_id' => 'chk_look_123',
|
||||
'lemonsqueezy_checkout_id' => 'chk_look_123',
|
||||
'status' => GiftVoucher::STATUS_ISSUED,
|
||||
]);
|
||||
|
||||
|
||||
@@ -2,52 +2,34 @@
|
||||
|
||||
namespace Tests\Feature\Api\Marketing;
|
||||
|
||||
use App\Enums\CouponType;
|
||||
use App\Models\Coupon;
|
||||
use App\Models\Package;
|
||||
use App\Models\Tenant;
|
||||
use App\Models\User;
|
||||
use App\Services\Paddle\PaddleDiscountService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Mockery;
|
||||
use Tests\TestCase;
|
||||
|
||||
class CouponPreviewTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
Mockery::close();
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function test_guest_can_preview_coupon(): void
|
||||
{
|
||||
$package = Package::factory()->create([
|
||||
'paddle_price_id' => 'pri_test',
|
||||
'lemonsqueezy_variant_id' => 'pri_test',
|
||||
'price' => 100,
|
||||
]);
|
||||
|
||||
$coupon = Coupon::factory()->create([
|
||||
'code' => 'SAVE20',
|
||||
'paddle_discount_id' => 'dsc_123',
|
||||
'type' => CouponType::PERCENTAGE,
|
||||
'amount' => 20,
|
||||
'lemonsqueezy_discount_id' => 'dsc_123',
|
||||
'per_customer_limit' => null,
|
||||
]);
|
||||
$coupon->packages()->attach($package);
|
||||
|
||||
$this->instance(PaddleDiscountService::class, Mockery::mock(PaddleDiscountService::class, function ($mock) {
|
||||
$mock->shouldReceive('previewDiscount')->andReturn([
|
||||
'totals' => [
|
||||
'currency_code' => 'EUR',
|
||||
'subtotal' => 10000,
|
||||
'discount' => 2000,
|
||||
'tax' => 0,
|
||||
'total' => 8000,
|
||||
],
|
||||
]);
|
||||
}));
|
||||
|
||||
$response = $this->postJson(route('api.v1.marketing.coupons.preview'), [
|
||||
'package_id' => $package->id,
|
||||
'code' => 'save20',
|
||||
@@ -62,7 +44,7 @@ class CouponPreviewTest extends TestCase
|
||||
public function test_invalid_coupon_returns_validation_error(): void
|
||||
{
|
||||
$package = Package::factory()->create([
|
||||
'paddle_price_id' => 'pri_test_invalid',
|
||||
'lemonsqueezy_variant_id' => 'pri_test_invalid',
|
||||
]);
|
||||
|
||||
$this->postJson(route('api.v1.marketing.coupons.preview'), [
|
||||
@@ -75,12 +57,12 @@ class CouponPreviewTest extends TestCase
|
||||
public function test_coupon_with_per_customer_limit_requires_login(): void
|
||||
{
|
||||
$package = Package::factory()->create([
|
||||
'paddle_price_id' => 'pri_test_login',
|
||||
'lemonsqueezy_variant_id' => 'pri_test_login',
|
||||
]);
|
||||
|
||||
$coupon = Coupon::factory()->create([
|
||||
'code' => 'LIMITED',
|
||||
'paddle_discount_id' => 'dsc_login',
|
||||
'lemonsqueezy_discount_id' => 'dsc_login',
|
||||
'per_customer_limit' => 1,
|
||||
]);
|
||||
$coupon->packages()->attach($package);
|
||||
@@ -107,29 +89,20 @@ class CouponPreviewTest extends TestCase
|
||||
]);
|
||||
|
||||
$package = Package::factory()->create([
|
||||
'paddle_price_id' => 'pri_test_logged_in',
|
||||
'lemonsqueezy_variant_id' => 'pri_test_logged_in',
|
||||
'price' => 120,
|
||||
]);
|
||||
|
||||
$coupon = Coupon::factory()->create([
|
||||
'code' => 'LIMITEDTENANT',
|
||||
'paddle_discount_id' => 'dsc_logged_in',
|
||||
'type' => CouponType::FLAT,
|
||||
'amount' => 20,
|
||||
'currency' => 'EUR',
|
||||
'lemonsqueezy_discount_id' => 'dsc_logged_in',
|
||||
'per_customer_limit' => 1,
|
||||
]);
|
||||
$coupon->packages()->attach($package);
|
||||
|
||||
$this->instance(PaddleDiscountService::class, Mockery::mock(PaddleDiscountService::class, function ($mock) {
|
||||
$mock->shouldReceive('previewDiscount')->andReturn([
|
||||
'totals' => [
|
||||
'currency_code' => 'EUR',
|
||||
'subtotal' => 12000,
|
||||
'discount' => 2000,
|
||||
'tax' => 0,
|
||||
'total' => 10000,
|
||||
],
|
||||
]);
|
||||
}));
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('api.v1.marketing.coupons.preview'), [
|
||||
'package_id' => $package->id,
|
||||
'code' => 'limitedtenant',
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ use Tests\Feature\Tenant\TenantTestCase;
|
||||
|
||||
class BillingTransactionsTest extends TenantTestCase
|
||||
{
|
||||
public function test_transactions_endpoint_creates_missing_paddle_customer_id(): void
|
||||
public function test_transactions_endpoint_creates_missing_lemonsqueezy_customer_id(): void
|
||||
{
|
||||
Http::fake(function (Request $request) {
|
||||
$path = parse_url($request->url(), PHP_URL_PATH);
|
||||
@@ -35,7 +35,7 @@ class BillingTransactionsTest extends TenantTestCase
|
||||
return Http::response([], 404);
|
||||
});
|
||||
|
||||
$this->tenant->forceFill(['paddle_customer_id' => null])->save();
|
||||
$this->tenant->forceFill(['lemonsqueezy_customer_id' => null])->save();
|
||||
|
||||
$response = $this->authenticatedRequest('GET', '/api/v1/tenant/billing/transactions');
|
||||
|
||||
@@ -44,7 +44,7 @@ class BillingTransactionsTest extends TenantTestCase
|
||||
|
||||
$this->assertDatabaseHas('tenants', [
|
||||
'id' => $this->tenant->id,
|
||||
'paddle_customer_id' => 'cus_456',
|
||||
'lemonsqueezy_customer_id' => 'cus_456',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user