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',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -328,9 +328,9 @@ class CheckoutAuthTest extends TestCase
|
||||
->has('auth')
|
||||
->has('auth.user')
|
||||
->has('googleAuth')
|
||||
->has('paddle')
|
||||
->has('paddle.environment')
|
||||
->has('paddle.client_token')
|
||||
->has('lemonsqueezy')
|
||||
->has('lemonsqueezy.store_id')
|
||||
->has('lemonsqueezy.test_mode')
|
||||
->where('package.id', $package->id)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -58,7 +58,7 @@ class CheckoutSessionStatusTest extends TestCase
|
||||
$response->assertForbidden();
|
||||
}
|
||||
|
||||
public function test_session_status_recovers_completed_paddle_transaction(): void
|
||||
public function test_session_status_recovers_completed_lemonsqueezy_order(): void
|
||||
{
|
||||
$tenant = Tenant::factory()->create();
|
||||
$user = User::factory()->for($tenant)->create([
|
||||
@@ -74,34 +74,34 @@ class CheckoutSessionStatusTest extends TestCase
|
||||
$session = $sessions->createOrResume($user, $package, [
|
||||
'tenant' => $tenant,
|
||||
]);
|
||||
$sessions->selectProvider($session, CheckoutSession::PROVIDER_PADDLE);
|
||||
$sessions->selectProvider($session, CheckoutSession::PROVIDER_LEMONSQUEEZY);
|
||||
$session->forceFill([
|
||||
'provider_metadata' => [
|
||||
'paddle_checkout_id' => 'chk_123',
|
||||
'lemonsqueezy_checkout_id' => 'chk_123',
|
||||
],
|
||||
])->save();
|
||||
|
||||
config()->set([
|
||||
'paddle.api_key' => 'test-key',
|
||||
'paddle.base_url' => 'https://paddle.test',
|
||||
'paddle.environment' => 'sandbox',
|
||||
'lemonsqueezy.api_key' => 'test-key',
|
||||
'lemonsqueezy.base_url' => 'https://lemonsqueezy.test',
|
||||
]);
|
||||
|
||||
Http::fake([
|
||||
'https://paddle.test/*' => Http::response([
|
||||
'https://lemonsqueezy.test/checkouts/chk_123' => Http::response([
|
||||
'data' => [
|
||||
[
|
||||
'id' => 'txn_123',
|
||||
'status' => 'completed',
|
||||
'details' => [
|
||||
'totals' => [
|
||||
'currency_code' => 'EUR',
|
||||
'total' => ['amount' => 9900],
|
||||
],
|
||||
],
|
||||
'custom_data' => [
|
||||
'checkout_session_id' => $session->id,
|
||||
],
|
||||
'id' => 'chk_123',
|
||||
'attributes' => [
|
||||
'order_id' => 'ord_123',
|
||||
],
|
||||
],
|
||||
], 200),
|
||||
'https://lemonsqueezy.test/orders/ord_123' => Http::response([
|
||||
'data' => [
|
||||
'id' => 'ord_123',
|
||||
'attributes' => [
|
||||
'status' => 'paid',
|
||||
'currency' => 'EUR',
|
||||
'total' => 9900,
|
||||
],
|
||||
],
|
||||
], 200),
|
||||
@@ -128,7 +128,7 @@ class CheckoutSessionStatusTest extends TestCase
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_session_confirm_recovers_completed_paddle_transaction(): void
|
||||
public function test_session_confirm_recovers_completed_lemonsqueezy_order(): void
|
||||
{
|
||||
$tenant = Tenant::factory()->create();
|
||||
$user = User::factory()->for($tenant)->create([
|
||||
@@ -144,27 +144,21 @@ class CheckoutSessionStatusTest extends TestCase
|
||||
$session = $sessions->createOrResume($user, $package, [
|
||||
'tenant' => $tenant,
|
||||
]);
|
||||
$sessions->selectProvider($session, CheckoutSession::PROVIDER_PADDLE);
|
||||
$sessions->selectProvider($session, CheckoutSession::PROVIDER_LEMONSQUEEZY);
|
||||
|
||||
config()->set([
|
||||
'paddle.api_key' => 'test-key',
|
||||
'paddle.base_url' => 'https://paddle.test',
|
||||
'paddle.environment' => 'sandbox',
|
||||
'lemonsqueezy.api_key' => 'test-key',
|
||||
'lemonsqueezy.base_url' => 'https://lemonsqueezy.test',
|
||||
]);
|
||||
|
||||
Http::fake([
|
||||
'https://paddle.test/transactions/txn_987' => Http::response([
|
||||
'https://lemonsqueezy.test/orders/ord_987' => Http::response([
|
||||
'data' => [
|
||||
'id' => 'txn_987',
|
||||
'status' => 'completed',
|
||||
'details' => [
|
||||
'totals' => [
|
||||
'currency_code' => 'EUR',
|
||||
'total' => ['amount' => 7900],
|
||||
],
|
||||
],
|
||||
'custom_data' => [
|
||||
'checkout_session_id' => $session->id,
|
||||
'id' => 'ord_987',
|
||||
'attributes' => [
|
||||
'status' => 'paid',
|
||||
'currency' => 'EUR',
|
||||
'total' => 7900,
|
||||
],
|
||||
],
|
||||
], 200),
|
||||
@@ -176,8 +170,8 @@ class CheckoutSessionStatusTest extends TestCase
|
||||
$this->actingAs($user);
|
||||
|
||||
$response = $this->postJson(route('checkout.session.confirm', $session), [
|
||||
'transaction_id' => 'txn_987',
|
||||
'checkout_id' => 'che_987',
|
||||
'order_id' => 'ord_987',
|
||||
'checkout_id' => 'chk_987',
|
||||
]);
|
||||
|
||||
$response->assertOk()
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\CheckoutSession;
|
||||
use App\Models\Package;
|
||||
use App\Models\Tenant;
|
||||
use App\Models\User;
|
||||
@@ -24,8 +25,8 @@ class CheckoutSessionLocalConfirmationTest extends TestCase
|
||||
$tenant = Tenant::factory()->create();
|
||||
$user = User::factory()->for($tenant)->create();
|
||||
$package = Package::factory()->create([
|
||||
'paddle_price_id' => 'pri_123',
|
||||
'paddle_product_id' => 'pro_123',
|
||||
'lemonsqueezy_variant_id' => 'pri_123',
|
||||
'lemonsqueezy_product_id' => 'pro_123',
|
||||
'price' => 120,
|
||||
]);
|
||||
|
||||
@@ -33,7 +34,7 @@ class CheckoutSessionLocalConfirmationTest extends TestCase
|
||||
$session = $sessions->createOrResume($user, $package, [
|
||||
'tenant' => $tenant,
|
||||
]);
|
||||
$sessions->selectProvider($session, 'paddle');
|
||||
$sessions->selectProvider($session, CheckoutSession::PROVIDER_LEMONSQUEEZY);
|
||||
|
||||
$this->actingAs($user);
|
||||
$this->withSession(['_token' => 'test-token']);
|
||||
@@ -41,7 +42,7 @@ class CheckoutSessionLocalConfirmationTest extends TestCase
|
||||
$response = $this->postJson(
|
||||
route('checkout.session.confirm', $session),
|
||||
[
|
||||
'transaction_id' => 'txn_123',
|
||||
'order_id' => 'ord_123',
|
||||
'checkout_id' => 'chk_123',
|
||||
],
|
||||
[
|
||||
@@ -60,7 +61,7 @@ class CheckoutSessionLocalConfirmationTest extends TestCase
|
||||
$this->assertDatabaseHas('package_purchases', [
|
||||
'tenant_id' => $tenant->id,
|
||||
'package_id' => $package->id,
|
||||
'provider_id' => 'txn_123',
|
||||
'provider_id' => 'ord_123',
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('tenant_packages', [
|
||||
|
||||
@@ -71,7 +71,7 @@ class DashboardPageTest extends TestCase
|
||||
'package_id' => $package->id,
|
||||
'price' => 149.00,
|
||||
'type' => 'reseller_subscription',
|
||||
'provider' => 'paddle',
|
||||
'provider' => 'lemonsqueezy',
|
||||
'purchased_at' => now()->subDay(),
|
||||
]);
|
||||
|
||||
|
||||
@@ -71,7 +71,7 @@ class FullUserFlowTest extends TestCase
|
||||
$this->assertAuthenticated();
|
||||
$loginResponse->assertRedirect(CheckoutRoutes::wizardUrl($freePackage->id, 'de'));
|
||||
|
||||
// Schritt 3: Paid Package Bestellung (Mock Paddle)
|
||||
// Schritt 3: Paid Package Bestellung (Mock Lemon Squeezy)
|
||||
$paidPackage = Package::factory()->reseller()->create(['price' => 10]);
|
||||
|
||||
// Simuliere Kauf (GET zu buy.packages, aber da es Redirect ist, prüfe Session oder folge)
|
||||
@@ -92,8 +92,8 @@ class FullUserFlowTest extends TestCase
|
||||
'tenant_id' => $tenant->id,
|
||||
'package_id' => $paidPackage->id,
|
||||
'type' => 'reseller_subscription',
|
||||
'provider' => 'paddle',
|
||||
'provider_id' => 'paddle_txn_123',
|
||||
'provider' => 'lemonsqueezy',
|
||||
'provider_id' => 'ord_123',
|
||||
'price' => 10,
|
||||
'purchased_at' => now(),
|
||||
]);
|
||||
@@ -105,7 +105,7 @@ class FullUserFlowTest extends TestCase
|
||||
'tenant_id' => $tenant->id,
|
||||
'package_id' => $paidPackage->id,
|
||||
'type' => 'reseller_subscription',
|
||||
'provider' => 'paddle',
|
||||
'provider' => 'lemonsqueezy',
|
||||
]);
|
||||
|
||||
$this->assertEquals(1, PackagePurchase::where('tenant_id', $tenant->id)->count());
|
||||
|
||||
@@ -7,12 +7,12 @@ use App\Models\Package;
|
||||
use App\Models\Tenant;
|
||||
use App\Models\User;
|
||||
use App\Services\Coupons\CouponService;
|
||||
use App\Services\Paddle\PaddleCheckoutService;
|
||||
use App\Services\LemonSqueezy\LemonSqueezyCheckoutService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Mockery;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PaddleCheckoutControllerTest extends TestCase
|
||||
class LemonSqueezyCheckoutControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
@@ -26,20 +26,20 @@ class PaddleCheckoutControllerTest extends TestCase
|
||||
public function test_authenticated_user_can_create_checkout_with_coupon(): void
|
||||
{
|
||||
$tenant = Tenant::factory()->create([
|
||||
'paddle_customer_id' => 'cus_123',
|
||||
'lemonsqueezy_customer_id' => 'cus_123',
|
||||
]);
|
||||
|
||||
$user = User::factory()->for($tenant)->create();
|
||||
|
||||
$package = Package::factory()->create([
|
||||
'paddle_price_id' => 'pri_123',
|
||||
'paddle_product_id' => 'pro_123',
|
||||
'lemonsqueezy_variant_id' => 'pri_123',
|
||||
'lemonsqueezy_product_id' => 'pro_123',
|
||||
'price' => 120,
|
||||
]);
|
||||
|
||||
$coupon = Coupon::factory()->create([
|
||||
'code' => 'SAVE15',
|
||||
'paddle_discount_id' => 'dsc_123',
|
||||
'lemonsqueezy_discount_id' => 'dsc_123',
|
||||
]);
|
||||
$coupon->packages()->attach($package);
|
||||
|
||||
@@ -66,18 +66,18 @@ class PaddleCheckoutControllerTest extends TestCase
|
||||
]);
|
||||
$this->instance(CouponService::class, $couponServiceMock);
|
||||
|
||||
$paddleServiceMock = Mockery::mock(PaddleCheckoutService::class);
|
||||
$paddleServiceMock->shouldReceive('createCheckout')
|
||||
$checkoutServiceMock = Mockery::mock(LemonSqueezyCheckoutService::class);
|
||||
$checkoutServiceMock->shouldReceive('createCheckout')
|
||||
->once()
|
||||
->andReturn([
|
||||
'checkout_url' => 'https://example.com/checkout/test',
|
||||
'id' => 'chk_123',
|
||||
]);
|
||||
$this->instance(PaddleCheckoutService::class, $paddleServiceMock);
|
||||
$this->instance(LemonSqueezyCheckoutService::class, $checkoutServiceMock);
|
||||
|
||||
$this->be($user);
|
||||
|
||||
$response = $this->postJson(route('paddle.checkout.create'), [
|
||||
$response = $this->postJson(route('lemonsqueezy.checkout.create'), [
|
||||
'package_id' => $package->id,
|
||||
'coupon_code' => 'SAVE15',
|
||||
'accepted_terms' => true,
|
||||
49
tests/Feature/LemonSqueezyRegisterWebhooksCommandTest.php
Normal file
49
tests/Feature/LemonSqueezyRegisterWebhooksCommandTest.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Services\LemonSqueezy\LemonSqueezyClient;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Mockery;
|
||||
use Tests\TestCase;
|
||||
|
||||
class LemonSqueezyRegisterWebhooksCommandTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
Mockery::close();
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function test_registers_webhook_with_configured_events(): void
|
||||
{
|
||||
config([
|
||||
'lemonsqueezy.webhook_events' => ['order_created', 'subscription_created'],
|
||||
'lemonsqueezy.store_id' => 'store_123',
|
||||
'lemonsqueezy.webhook_secret' => 'secret_123',
|
||||
]);
|
||||
|
||||
$client = Mockery::mock(LemonSqueezyClient::class);
|
||||
$client->shouldReceive('post')
|
||||
->once()
|
||||
->with('/webhooks', Mockery::on(function (array $payload): bool {
|
||||
$attributes = $payload['data']['attributes'] ?? [];
|
||||
$store = $payload['data']['relationships']['store']['data']['id'] ?? null;
|
||||
|
||||
return ($attributes['url'] ?? null) === 'https://example.test/lemonsqueezy/webhook'
|
||||
&& ($attributes['events'] ?? []) === ['order_created', 'subscription_created']
|
||||
&& ($attributes['secret'] ?? null) === 'secret_123'
|
||||
&& $store === 'store_123';
|
||||
}))
|
||||
->andReturn(['data' => ['id' => 'wh_123']]);
|
||||
|
||||
$this->app->instance(LemonSqueezyClient::class, $client);
|
||||
|
||||
$this->artisan('lemonsqueezy:webhooks:register', [
|
||||
'--url' => 'https://example.test/lemonsqueezy/webhook',
|
||||
])->assertExitCode(0);
|
||||
}
|
||||
}
|
||||
62
tests/Feature/LemonSqueezyReturnTest.php
Normal file
62
tests/Feature/LemonSqueezyReturnTest.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use Illuminate\Support\Facades\Config;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Tests\TestCase;
|
||||
|
||||
class LemonSqueezyReturnTest extends TestCase
|
||||
{
|
||||
public function test_return_redirects_to_success_url_for_paid_order(): void
|
||||
{
|
||||
Config::set('lemonsqueezy.api_key', 'test_key');
|
||||
Config::set('lemonsqueezy.base_url', 'https://lemonsqueezy.test');
|
||||
Config::set('app.url', 'https://fotospiel-app.test');
|
||||
|
||||
Http::fake([
|
||||
'https://lemonsqueezy.test/orders/ord_123' => Http::response([
|
||||
'data' => [
|
||||
'id' => 'ord_123',
|
||||
'attributes' => [
|
||||
'status' => 'paid',
|
||||
'custom_data' => [
|
||||
'success_url' => 'https://fotospiel-app.test/event-admin/mobile/events/slug/photos?addon_success=1',
|
||||
'return_url' => 'https://fotospiel-app.test/event-admin/mobile/events/slug/photos',
|
||||
],
|
||||
],
|
||||
],
|
||||
], 200),
|
||||
]);
|
||||
|
||||
$response = $this->get('/lemonsqueezy/return?order_id=ord_123');
|
||||
|
||||
$response->assertRedirect('https://fotospiel-app.test/event-admin/mobile/events/slug/photos?addon_success=1');
|
||||
}
|
||||
|
||||
public function test_return_redirects_to_return_url_when_not_completed(): void
|
||||
{
|
||||
Config::set('lemonsqueezy.api_key', 'test_key');
|
||||
Config::set('lemonsqueezy.base_url', 'https://lemonsqueezy.test');
|
||||
Config::set('app.url', 'https://fotospiel-app.test');
|
||||
|
||||
Http::fake([
|
||||
'https://lemonsqueezy.test/orders/ord_456' => Http::response([
|
||||
'data' => [
|
||||
'id' => 'ord_456',
|
||||
'attributes' => [
|
||||
'status' => 'failed',
|
||||
'custom_data' => [
|
||||
'success_url' => 'https://fotospiel-app.test/event-admin/mobile/events/slug/photos?addon_success=1',
|
||||
'return_url' => 'https://fotospiel-app.test/event-admin/mobile/events/slug/photos',
|
||||
],
|
||||
],
|
||||
],
|
||||
], 200),
|
||||
]);
|
||||
|
||||
$response = $this->get('/lemonsqueezy/return?order_id=ord_456');
|
||||
|
||||
$response->assertRedirect('https://fotospiel-app.test/event-admin/mobile/events/slug/photos');
|
||||
}
|
||||
}
|
||||
@@ -2,32 +2,32 @@
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Jobs\SyncPackageToPaddle;
|
||||
use App\Jobs\SyncPackageToLemonSqueezy;
|
||||
use App\Models\Package;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Bus as BusFacade;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PaddleSyncPackagesCommandTest extends TestCase
|
||||
class LemonSqueezySyncPackagesCommandTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_command_dispatches_jobs_for_packages(): void
|
||||
{
|
||||
Package::factory()->count(2)->create([
|
||||
'paddle_product_id' => 'pro_test',
|
||||
'paddle_price_id' => 'pri_test',
|
||||
'lemonsqueezy_product_id' => 'pro_test',
|
||||
'lemonsqueezy_variant_id' => 'pri_test',
|
||||
]);
|
||||
|
||||
BusFacade::fake();
|
||||
|
||||
$this->artisan('paddle:sync-packages', [
|
||||
$this->artisan('lemonsqueezy:sync-packages', [
|
||||
'--dry-run' => true,
|
||||
'--queue' => true,
|
||||
])->assertExitCode(0);
|
||||
|
||||
BusFacade::assertDispatched(SyncPackageToPaddle::class, 2);
|
||||
BusFacade::assertDispatched(SyncPackageToLemonSqueezy::class, 2);
|
||||
}
|
||||
|
||||
public function test_command_filters_packages_by_id(): void
|
||||
@@ -37,13 +37,13 @@ class PaddleSyncPackagesCommandTest extends TestCase
|
||||
|
||||
BusFacade::fake();
|
||||
|
||||
$this->artisan('paddle:sync-packages', [
|
||||
$this->artisan('lemonsqueezy:sync-packages', [
|
||||
'--dry-run' => true,
|
||||
'--queue' => true,
|
||||
'--package' => [$package->id],
|
||||
])->assertExitCode(0);
|
||||
|
||||
BusFacade::assertDispatched(SyncPackageToPaddle::class, function (SyncPackageToPaddle $job) use ($package) {
|
||||
BusFacade::assertDispatched(SyncPackageToLemonSqueezy::class, function (SyncPackageToLemonSqueezy $job) use ($package) {
|
||||
return $this->getJobPackageId($job) === $package->id;
|
||||
});
|
||||
}
|
||||
@@ -51,39 +51,39 @@ class PaddleSyncPackagesCommandTest extends TestCase
|
||||
public function test_command_blocks_bulk_sync_with_unmapped_packages(): void
|
||||
{
|
||||
Package::factory()->create([
|
||||
'paddle_product_id' => null,
|
||||
'paddle_price_id' => null,
|
||||
'lemonsqueezy_product_id' => null,
|
||||
'lemonsqueezy_variant_id' => null,
|
||||
]);
|
||||
|
||||
BusFacade::fake();
|
||||
|
||||
$this->artisan('paddle:sync-packages', [
|
||||
$this->artisan('lemonsqueezy:sync-packages', [
|
||||
'--dry-run' => true,
|
||||
'--queue' => true,
|
||||
])->assertExitCode(Command::FAILURE);
|
||||
|
||||
BusFacade::assertNotDispatched(SyncPackageToPaddle::class);
|
||||
BusFacade::assertNotDispatched(SyncPackageToLemonSqueezy::class);
|
||||
}
|
||||
|
||||
public function test_command_allows_unmapped_packages_when_overridden(): void
|
||||
{
|
||||
Package::factory()->create([
|
||||
'paddle_product_id' => null,
|
||||
'paddle_price_id' => null,
|
||||
'lemonsqueezy_product_id' => null,
|
||||
'lemonsqueezy_variant_id' => null,
|
||||
]);
|
||||
|
||||
BusFacade::fake();
|
||||
|
||||
$this->artisan('paddle:sync-packages', [
|
||||
$this->artisan('lemonsqueezy:sync-packages', [
|
||||
'--dry-run' => true,
|
||||
'--queue' => true,
|
||||
'--allow-unmapped' => true,
|
||||
])->assertExitCode(Command::SUCCESS);
|
||||
|
||||
BusFacade::assertDispatched(SyncPackageToPaddle::class, 1);
|
||||
BusFacade::assertDispatched(SyncPackageToLemonSqueezy::class, 1);
|
||||
}
|
||||
|
||||
protected function getJobPackageId(SyncPackageToPaddle $job): int
|
||||
protected function getJobPackageId(SyncPackageToLemonSqueezy $job): int
|
||||
{
|
||||
$reflection = new \ReflectionClass($job);
|
||||
$property = $reflection->getProperty('packageId');
|
||||
233
tests/Feature/LemonSqueezyWebhookControllerTest.php
Normal file
233
tests/Feature/LemonSqueezyWebhookControllerTest.php
Normal file
@@ -0,0 +1,233 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\CheckoutSession;
|
||||
use App\Models\IntegrationWebhookEvent;
|
||||
use App\Models\Package;
|
||||
use App\Models\PackagePurchase;
|
||||
use App\Models\Tenant;
|
||||
use App\Models\TenantPackage;
|
||||
use App\Models\User;
|
||||
use App\Services\Checkout\CheckoutSessionService;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Arr;
|
||||
use Tests\TestCase;
|
||||
|
||||
class LemonSqueezyWebhookControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_order_created_finalises_checkout(): void
|
||||
{
|
||||
config(['lemonsqueezy.webhook_secret' => 'test_secret']);
|
||||
|
||||
[$tenant, $package, $session] = $this->prepareSession();
|
||||
|
||||
$payload = [
|
||||
'meta' => [
|
||||
'event_id' => 'evt_123',
|
||||
'event_name' => 'order_created',
|
||||
'custom_data' => [
|
||||
'checkout_session_id' => $session->id,
|
||||
'tenant_id' => (string) $tenant->id,
|
||||
'package_id' => (string) $package->id,
|
||||
],
|
||||
],
|
||||
'data' => [
|
||||
'id' => 'ord_123',
|
||||
'attributes' => [
|
||||
'status' => 'paid',
|
||||
'checkout_id' => 'chk_456',
|
||||
'subtotal' => 10000,
|
||||
'discount_total' => 1000,
|
||||
'tax' => 1900,
|
||||
'total' => 10900,
|
||||
'currency' => 'EUR',
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$signature = hash_hmac('sha256', json_encode($payload), 'test_secret');
|
||||
|
||||
$response = $this->withHeader('X-Signature', $signature)
|
||||
->postJson('/lemonsqueezy/webhook', $payload);
|
||||
|
||||
$response->assertOk()->assertJson(['status' => 'processed']);
|
||||
|
||||
$this->assertDatabaseHas('integration_webhook_events', [
|
||||
'provider' => 'lemonsqueezy',
|
||||
'event_id' => 'evt_123',
|
||||
'event_type' => 'order_created',
|
||||
'status' => IntegrationWebhookEvent::STATUS_PROCESSED,
|
||||
]);
|
||||
|
||||
$session->refresh();
|
||||
|
||||
$this->assertEquals(CheckoutSession::STATUS_COMPLETED, $session->status);
|
||||
$this->assertSame('lemonsqueezy', $session->provider);
|
||||
$this->assertSame('ord_123', Arr::get($session->provider_metadata, 'lemonsqueezy_order_id'));
|
||||
|
||||
$this->assertTrue(
|
||||
TenantPackage::query()
|
||||
->where('tenant_id', $tenant->id)
|
||||
->where('package_id', $package->id)
|
||||
->where('active', true)
|
||||
->exists()
|
||||
);
|
||||
|
||||
$purchase = PackagePurchase::query()
|
||||
->where('tenant_id', $tenant->id)
|
||||
->where('package_id', $package->id)
|
||||
->where('provider', 'lemonsqueezy')
|
||||
->first();
|
||||
|
||||
$this->assertNotNull($purchase);
|
||||
$this->assertSame(109.0, (float) $purchase->price);
|
||||
$this->assertSame('EUR', Arr::get($purchase->metadata, 'currency'));
|
||||
$this->assertSame(109.0, (float) Arr::get($purchase->metadata, 'lemonsqueezy_totals.total'));
|
||||
$this->assertSame(109.0, (float) $session->amount_total);
|
||||
}
|
||||
|
||||
public function test_duplicate_order_is_idempotent(): void
|
||||
{
|
||||
config(['lemonsqueezy.webhook_secret' => 'test_secret']);
|
||||
|
||||
[$tenant, $package, $session] = $this->prepareSession();
|
||||
|
||||
$payload = [
|
||||
'meta' => [
|
||||
'event_name' => 'order_created',
|
||||
'custom_data' => [
|
||||
'checkout_session_id' => $session->id,
|
||||
'tenant_id' => (string) $tenant->id,
|
||||
'package_id' => (string) $package->id,
|
||||
],
|
||||
],
|
||||
'data' => [
|
||||
'id' => 'ord_dup',
|
||||
'attributes' => [
|
||||
'status' => 'paid',
|
||||
'total' => 9900,
|
||||
'currency' => 'EUR',
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$signature = hash_hmac('sha256', json_encode($payload), 'test_secret');
|
||||
|
||||
$first = $this->withHeader('X-Signature', $signature)
|
||||
->postJson('/lemonsqueezy/webhook', $payload);
|
||||
|
||||
$first->assertOk()->assertJson(['status' => 'processed']);
|
||||
|
||||
$second = $this->withHeader('X-Signature', $signature)
|
||||
->postJson('/lemonsqueezy/webhook', $payload);
|
||||
|
||||
$second->assertOk()->assertJson(['status' => 'processed']);
|
||||
|
||||
$this->assertSame(1, PackagePurchase::query()->count());
|
||||
|
||||
$session->refresh();
|
||||
$this->assertEquals(CheckoutSession::STATUS_COMPLETED, $session->status);
|
||||
$this->assertEquals('ord_dup', Arr::get($session->provider_metadata, 'lemonsqueezy_order_id'));
|
||||
}
|
||||
|
||||
public function test_subscription_updated_creates_tenant_package(): void
|
||||
{
|
||||
config(['lemonsqueezy.webhook_secret' => 'test_secret']);
|
||||
|
||||
$tenant = Tenant::factory()->create([
|
||||
'subscription_status' => 'free',
|
||||
]);
|
||||
|
||||
$package = Package::factory()->reseller()->create([
|
||||
'price' => 129,
|
||||
'lemonsqueezy_variant_id' => 'var_sub_1',
|
||||
]);
|
||||
|
||||
$payload = [
|
||||
'meta' => [
|
||||
'event_name' => 'subscription_updated',
|
||||
'custom_data' => [
|
||||
'tenant_id' => (string) $tenant->id,
|
||||
'package_id' => (string) $package->id,
|
||||
],
|
||||
],
|
||||
'data' => [
|
||||
'id' => 'sub_123',
|
||||
'attributes' => [
|
||||
'status' => 'active',
|
||||
'customer_id' => 'cus_123',
|
||||
'variant_id' => 'var_sub_1',
|
||||
'renews_at' => Carbon::now()->addMonth()->toIso8601String(),
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$signature = hash_hmac('sha256', json_encode($payload), 'test_secret');
|
||||
|
||||
$response = $this->withHeader('X-Signature', $signature)
|
||||
->postJson('/lemonsqueezy/webhook', $payload);
|
||||
|
||||
$response->assertOk()->assertJson(['status' => 'processed']);
|
||||
|
||||
$tenant->refresh();
|
||||
|
||||
$tenantPackage = TenantPackage::where('tenant_id', $tenant->id)
|
||||
->where('package_id', $package->id)
|
||||
->first();
|
||||
|
||||
$this->assertNotNull($tenantPackage);
|
||||
$this->assertSame('sub_123', $tenantPackage->lemonsqueezy_subscription_id);
|
||||
$this->assertTrue($tenantPackage->active);
|
||||
$this->assertEquals('active', $tenant->subscription_status);
|
||||
$this->assertNotNull($tenant->subscription_expires_at);
|
||||
}
|
||||
|
||||
public function test_rejects_invalid_signature(): void
|
||||
{
|
||||
config(['lemonsqueezy.webhook_secret' => 'secret']);
|
||||
|
||||
$response = $this->withHeader('X-Signature', 'invalid')
|
||||
->postJson('/lemonsqueezy/webhook', ['meta' => ['event_name' => 'order_created']]);
|
||||
|
||||
$response->assertStatus(400)->assertJson(['status' => 'invalid']);
|
||||
}
|
||||
|
||||
public function test_unhandled_event_returns_accepted(): void
|
||||
{
|
||||
config(['lemonsqueezy.webhook_secret' => null]);
|
||||
|
||||
$response = $this->postJson('/lemonsqueezy/webhook', [
|
||||
'meta' => ['event_name' => 'order_unknown'],
|
||||
'data' => [],
|
||||
]);
|
||||
|
||||
$response->assertStatus(202)->assertJson(['status' => 'ignored']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{\App\Models\Tenant, \App\Models\Package, \App\Models\CheckoutSession}
|
||||
*/
|
||||
protected function prepareSession(): array
|
||||
{
|
||||
$user = User::factory()->create(['email_verified_at' => now()]);
|
||||
$tenant = Tenant::factory()->create(['user_id' => $user->id]);
|
||||
$user->forceFill(['tenant_id' => $tenant->id])->save();
|
||||
|
||||
$package = Package::factory()->create([
|
||||
'type' => 'endcustomer',
|
||||
'price' => 99,
|
||||
'lemonsqueezy_variant_id' => 'var_123',
|
||||
]);
|
||||
|
||||
/** @var CheckoutSessionService $sessions */
|
||||
$sessions = app(CheckoutSessionService::class);
|
||||
$session = $sessions->createOrResume($user, $package, ['tenant' => $tenant]);
|
||||
$sessions->selectProvider($session, CheckoutSession::PROVIDER_LEMONSQUEEZY);
|
||||
|
||||
return [$tenant, $package, $session];
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,7 @@ use App\Models\Tenant;
|
||||
use App\Models\TenantPackage;
|
||||
use App\Models\User;
|
||||
use App\Notifications\Customer\WithdrawalConfirmed;
|
||||
use App\Services\Paddle\PaddleTransactionService;
|
||||
use App\Services\LemonSqueezy\LemonSqueezyOrderService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Notification;
|
||||
use Inertia\Testing\AssertableInertia as Assert;
|
||||
@@ -30,8 +30,8 @@ class WithdrawalConfirmationTest extends TestCase
|
||||
$purchase = PackagePurchase::factory()->create([
|
||||
'tenant_id' => $tenant->id,
|
||||
'package_id' => $package->id,
|
||||
'provider' => 'paddle',
|
||||
'provider_id' => 'txn_123',
|
||||
'provider' => 'lemonsqueezy',
|
||||
'provider_id' => 'ord_123',
|
||||
'refunded' => false,
|
||||
'type' => 'endcustomer_event',
|
||||
'purchased_at' => now()->subDays(2),
|
||||
@@ -60,8 +60,8 @@ class WithdrawalConfirmationTest extends TestCase
|
||||
$purchase = PackagePurchase::factory()->create([
|
||||
'tenant_id' => $tenant->id,
|
||||
'package_id' => $package->id,
|
||||
'provider' => 'paddle',
|
||||
'provider_id' => 'txn_456',
|
||||
'provider' => 'lemonsqueezy',
|
||||
'provider_id' => 'ord_456',
|
||||
'refunded' => false,
|
||||
'type' => 'endcustomer_event',
|
||||
'purchased_at' => now()->subDays(5),
|
||||
@@ -73,7 +73,7 @@ class WithdrawalConfirmationTest extends TestCase
|
||||
'active' => true,
|
||||
]);
|
||||
|
||||
$this->mock(PaddleTransactionService::class, function ($mock) {
|
||||
$this->mock(LemonSqueezyOrderService::class, function ($mock) {
|
||||
$mock->shouldReceive('refund')
|
||||
->once()
|
||||
->andReturn([]);
|
||||
@@ -105,8 +105,8 @@ class WithdrawalConfirmationTest extends TestCase
|
||||
$purchase = PackagePurchase::factory()->create([
|
||||
'tenant_id' => $tenant->id,
|
||||
'package_id' => $package->id,
|
||||
'provider' => 'paddle',
|
||||
'provider_id' => 'txn_789',
|
||||
'provider' => 'lemonsqueezy',
|
||||
'provider_id' => 'ord_789',
|
||||
'refunded' => false,
|
||||
'type' => 'endcustomer_event',
|
||||
'purchased_at' => now()->subDays(3),
|
||||
@@ -120,7 +120,7 @@ class WithdrawalConfirmationTest extends TestCase
|
||||
'purchased_at' => now(),
|
||||
]);
|
||||
|
||||
$this->mock(PaddleTransactionService::class, function ($mock) {
|
||||
$this->mock(LemonSqueezyOrderService::class, function ($mock) {
|
||||
$mock->shouldReceive('refund')->never();
|
||||
});
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ use App\Services\Checkout\CheckoutSessionService;
|
||||
use App\Services\Checkout\CheckoutWebhookService;
|
||||
use App\Services\Coupons\CouponRedemptionService;
|
||||
use App\Services\GiftVouchers\GiftVoucherService;
|
||||
use App\Services\Paddle\PaddleSubscriptionService;
|
||||
use App\Services\LemonSqueezy\LemonSqueezySubscriptionService;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Mockery;
|
||||
@@ -70,18 +70,19 @@ class PackageSoftDeleteTest extends TestCase
|
||||
$this->assertTrue($activePackage->is($tenantPackage));
|
||||
}
|
||||
|
||||
public function test_paddle_subscription_event_handles_soft_deleted_package(): void
|
||||
public function test_lemonsqueezy_subscription_event_handles_soft_deleted_package(): void
|
||||
{
|
||||
$tenant = Tenant::factory()->create();
|
||||
$package = Package::factory()->reseller()->create([
|
||||
'price' => 29.00,
|
||||
'lemonsqueezy_variant_id' => 'var_123',
|
||||
]);
|
||||
|
||||
$package->delete();
|
||||
|
||||
$sessionService = Mockery::mock(CheckoutSessionService::class);
|
||||
$assignmentService = Mockery::mock(CheckoutAssignmentService::class);
|
||||
$subscriptionService = Mockery::mock(PaddleSubscriptionService::class);
|
||||
$subscriptionService = Mockery::mock(LemonSqueezySubscriptionService::class);
|
||||
$couponRedemptions = Mockery::mock(CouponRedemptionService::class);
|
||||
$giftVouchers = Mockery::mock(GiftVoucherService::class);
|
||||
|
||||
@@ -96,20 +97,25 @@ class PackageSoftDeleteTest extends TestCase
|
||||
Carbon::setTestNow(now());
|
||||
|
||||
$event = [
|
||||
'event_type' => 'subscription.updated',
|
||||
'data' => [
|
||||
'id' => 'sub_123',
|
||||
'status' => 'active',
|
||||
'meta' => [
|
||||
'event_name' => 'subscription_updated',
|
||||
'custom_data' => [
|
||||
'tenant_id' => $tenant->id,
|
||||
'package_id' => $package->id,
|
||||
],
|
||||
'next_billing_date' => now()->addMonth()->toIso8601String(),
|
||||
'customer_id' => 'cus_456',
|
||||
],
|
||||
'data' => [
|
||||
'id' => 'sub_123',
|
||||
'attributes' => [
|
||||
'status' => 'active',
|
||||
'renews_at' => now()->addMonth()->toIso8601String(),
|
||||
'customer_id' => 'cus_456',
|
||||
'variant_id' => 'var_123',
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$this->assertTrue($service->handlePaddleEvent($event));
|
||||
$this->assertTrue($service->handleLemonSqueezyEvent($event));
|
||||
|
||||
$tenantPackage = TenantPackage::where('tenant_id', $tenant->id)
|
||||
->where('package_id', $package->id)
|
||||
@@ -118,11 +124,11 @@ class PackageSoftDeleteTest extends TestCase
|
||||
$this->assertNotNull($tenantPackage);
|
||||
$this->assertNotNull($tenantPackage->package);
|
||||
$this->assertTrue($tenantPackage->package->is($package));
|
||||
$this->assertSame('sub_123', $tenantPackage->paddle_subscription_id);
|
||||
$this->assertSame('sub_123', $tenantPackage->lemonsqueezy_subscription_id);
|
||||
$this->assertTrue($tenantPackage->active);
|
||||
|
||||
$tenant->refresh();
|
||||
$this->assertSame('active', $tenant->subscription_status);
|
||||
$this->assertSame('cus_456', $tenant->paddle_customer_id);
|
||||
$this->assertSame('cus_456', $tenant->lemonsqueezy_customer_id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Services\Paddle\PaddleClient;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Mockery;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PaddleRegisterWebhooksCommandTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
Mockery::close();
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function test_registers_webhook_with_configured_events(): void
|
||||
{
|
||||
config([
|
||||
'paddle.webhook_events' => ['transaction.completed', 'subscription.created'],
|
||||
]);
|
||||
|
||||
$client = Mockery::mock(PaddleClient::class);
|
||||
$client->shouldReceive('post')
|
||||
->once()
|
||||
->with('/notification-settings', Mockery::on(function (array $payload): bool {
|
||||
return $payload['destination'] === 'https://example.test/paddle/webhook'
|
||||
&& $payload['subscribed_events'] === ['transaction.completed', 'subscription.created']
|
||||
&& $payload['traffic_source'] === 'simulation';
|
||||
}))
|
||||
->andReturn(['data' => ['id' => 'ntfset_123']]);
|
||||
|
||||
$this->app->instance(PaddleClient::class, $client);
|
||||
|
||||
$this->artisan('paddle:webhooks:register', [
|
||||
'--url' => 'https://example.test/paddle/webhook',
|
||||
'--traffic-source' => 'simulation',
|
||||
])->assertExitCode(0);
|
||||
}
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use Illuminate\Support\Facades\Config;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PaddleReturnTest extends TestCase
|
||||
{
|
||||
public function test_return_redirects_to_success_url_for_completed_transaction(): void
|
||||
{
|
||||
Config::set('paddle.api_key', 'test_key');
|
||||
Config::set('paddle.base_url', 'https://paddle.test');
|
||||
Config::set('paddle.environment', 'sandbox');
|
||||
Config::set('app.url', 'https://fotospiel-app.test');
|
||||
|
||||
Http::fake([
|
||||
'https://paddle.test/transactions/txn_123' => Http::response([
|
||||
'data' => [
|
||||
'id' => 'txn_123',
|
||||
'status' => 'completed',
|
||||
'custom_data' => [
|
||||
'success_url' => 'https://fotospiel-app.test/event-admin/mobile/events/slug/photos?addon_success=1',
|
||||
'cancel_url' => 'https://fotospiel-app.test/event-admin/mobile/events/slug/photos',
|
||||
],
|
||||
],
|
||||
], 200),
|
||||
]);
|
||||
|
||||
$response = $this->get('/paddle/return?_ptxn=txn_123');
|
||||
|
||||
$response->assertRedirect('https://fotospiel-app.test/event-admin/mobile/events/slug/photos?addon_success=1');
|
||||
}
|
||||
|
||||
public function test_return_redirects_to_cancel_url_when_not_completed(): void
|
||||
{
|
||||
Config::set('paddle.api_key', 'test_key');
|
||||
Config::set('paddle.base_url', 'https://paddle.test');
|
||||
Config::set('paddle.environment', 'sandbox');
|
||||
Config::set('app.url', 'https://fotospiel-app.test');
|
||||
|
||||
Http::fake([
|
||||
'https://paddle.test/transactions/txn_456' => Http::response([
|
||||
'data' => [
|
||||
'id' => 'txn_456',
|
||||
'status' => 'failed',
|
||||
'custom_data' => [
|
||||
'success_url' => 'https://fotospiel-app.test/event-admin/mobile/events/slug/photos?addon_success=1',
|
||||
'cancel_url' => 'https://fotospiel-app.test/event-admin/mobile/events/slug/photos',
|
||||
],
|
||||
],
|
||||
], 200),
|
||||
]);
|
||||
|
||||
$response = $this->get('/paddle/return?_ptxn=txn_456');
|
||||
|
||||
$response->assertRedirect('https://fotospiel-app.test/event-admin/mobile/events/slug/photos');
|
||||
}
|
||||
}
|
||||
@@ -1,356 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\CheckoutSession;
|
||||
use App\Models\IntegrationWebhookEvent;
|
||||
use App\Models\Package;
|
||||
use App\Models\PackagePurchase;
|
||||
use App\Models\Tenant;
|
||||
use App\Models\TenantPackage;
|
||||
use App\Models\User;
|
||||
use App\Services\Checkout\CheckoutSessionService;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Arr;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PaddleWebhookControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_transaction_completed_finalises_checkout(): void
|
||||
{
|
||||
config(['paddle.webhook_secret' => 'test_secret']);
|
||||
|
||||
[$tenant, $package, $session] = $this->prepareSession();
|
||||
|
||||
$payload = [
|
||||
'event_id' => 'evt_123',
|
||||
'event_type' => 'transaction.completed',
|
||||
'data' => [
|
||||
'id' => 'txn_123',
|
||||
'status' => 'completed',
|
||||
'checkout_id' => 'chk_456',
|
||||
'details' => [
|
||||
'totals' => [
|
||||
'subtotal' => ['amount' => '10000'],
|
||||
'discount' => ['amount' => '1000'],
|
||||
'tax' => ['amount' => '1900'],
|
||||
'total' => ['amount' => '10900'],
|
||||
'currency_code' => 'EUR',
|
||||
],
|
||||
],
|
||||
'custom_data' => [
|
||||
'checkout_session_id' => $session->id,
|
||||
'tenant_id' => (string) $tenant->id,
|
||||
'package_id' => (string) $package->id,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$timestamp = time();
|
||||
$signature = hash_hmac('sha256', $timestamp.':'.json_encode($payload), 'test_secret');
|
||||
$header = sprintf('ts=%s,h1=%s', $timestamp, $signature);
|
||||
|
||||
$response = $this->withHeader('Paddle-Signature', $header)
|
||||
->postJson('/paddle/webhook', $payload);
|
||||
|
||||
$response->assertOk()->assertJson(['status' => 'processed']);
|
||||
|
||||
$this->assertDatabaseHas('integration_webhook_events', [
|
||||
'provider' => 'paddle',
|
||||
'event_id' => 'evt_123',
|
||||
'event_type' => 'transaction.completed',
|
||||
'status' => IntegrationWebhookEvent::STATUS_PROCESSED,
|
||||
]);
|
||||
|
||||
$session->refresh();
|
||||
|
||||
$this->assertEquals(CheckoutSession::STATUS_COMPLETED, $session->status);
|
||||
$this->assertSame('paddle', $session->provider);
|
||||
$this->assertSame('txn_123', Arr::get($session->provider_metadata, 'paddle_transaction_id'));
|
||||
|
||||
$this->assertTrue(
|
||||
TenantPackage::query()
|
||||
->where('tenant_id', $tenant->id)
|
||||
->where('package_id', $package->id)
|
||||
->where('active', true)
|
||||
->exists()
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
PackagePurchase::query()
|
||||
->where('tenant_id', $tenant->id)
|
||||
->where('package_id', $package->id)
|
||||
->where('provider', 'paddle')
|
||||
->exists()
|
||||
);
|
||||
|
||||
$purchase = PackagePurchase::query()
|
||||
->where('tenant_id', $tenant->id)
|
||||
->where('package_id', $package->id)
|
||||
->first();
|
||||
|
||||
$this->assertNotNull($purchase);
|
||||
$this->assertSame(109.0, (float) $purchase->price);
|
||||
$this->assertSame('EUR', Arr::get($purchase->metadata, 'currency'));
|
||||
$this->assertSame(109.0, (float) Arr::get($purchase->metadata, 'paddle_totals.total'));
|
||||
$this->assertSame(109.0, (float) $session->amount_total);
|
||||
}
|
||||
|
||||
public function test_duplicate_transaction_is_idempotent(): void
|
||||
{
|
||||
config(['paddle.webhook_secret' => 'test_secret']);
|
||||
|
||||
[$tenant, $package, $session] = $this->prepareSession();
|
||||
|
||||
$payload = [
|
||||
'event_type' => 'transaction.completed',
|
||||
'data' => [
|
||||
'id' => 'txn_dup',
|
||||
'status' => 'completed',
|
||||
'checkout_id' => 'chk_dup',
|
||||
'custom_data' => [
|
||||
'checkout_session_id' => $session->id,
|
||||
'tenant_id' => (string) $tenant->id,
|
||||
'package_id' => (string) $package->id,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$timestamp = time();
|
||||
$signature = hash_hmac('sha256', $timestamp.':'.json_encode($payload), 'test_secret');
|
||||
$header = sprintf('ts=%s,h1=%s', $timestamp, $signature);
|
||||
|
||||
$first = $this->withHeader('Paddle-Signature', $header)
|
||||
->postJson('/paddle/webhook', $payload);
|
||||
|
||||
$first->assertOk()->assertJson(['status' => 'processed']);
|
||||
|
||||
$second = $this->withHeader('Paddle-Signature', $header)
|
||||
->postJson('/paddle/webhook', $payload);
|
||||
|
||||
$second->assertStatus(200)->assertJson(['status' => 'processed']);
|
||||
|
||||
$this->assertSame(1, PackagePurchase::query()->count());
|
||||
|
||||
$session->refresh();
|
||||
$this->assertEquals(CheckoutSession::STATUS_COMPLETED, $session->status);
|
||||
$this->assertEquals('txn_dup', Arr::get($session->provider_metadata, 'paddle_transaction_id'));
|
||||
}
|
||||
|
||||
public function test_transaction_completed_updates_tenant_status_for_one_time_package(): void
|
||||
{
|
||||
config(['paddle.webhook_secret' => 'test_secret']);
|
||||
|
||||
$user = User::factory()->create(['email_verified_at' => now()]);
|
||||
$tenant = Tenant::factory()->create([
|
||||
'user_id' => $user->id,
|
||||
'subscription_status' => 'free',
|
||||
]);
|
||||
$user->forceFill(['tenant_id' => $tenant->id])->save();
|
||||
|
||||
$package = Package::factory()->create([
|
||||
'type' => 'endcustomer',
|
||||
'price' => 49,
|
||||
'paddle_price_id' => 'price_one_time',
|
||||
]);
|
||||
|
||||
/** @var CheckoutSessionService $sessions */
|
||||
$sessions = app(CheckoutSessionService::class);
|
||||
$session = $sessions->createOrResume($user, $package, ['tenant' => $tenant]);
|
||||
$sessions->selectProvider($session, CheckoutSession::PROVIDER_PADDLE);
|
||||
|
||||
$payload = [
|
||||
'event_type' => 'transaction.completed',
|
||||
'data' => [
|
||||
'id' => 'txn_one_time',
|
||||
'status' => 'completed',
|
||||
'details' => [
|
||||
'totals' => [
|
||||
'total' => ['amount' => '4900'],
|
||||
'currency_code' => 'EUR',
|
||||
],
|
||||
],
|
||||
'custom_data' => [
|
||||
'checkout_session_id' => $session->id,
|
||||
'tenant_id' => (string) $tenant->id,
|
||||
'package_id' => (string) $package->id,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$timestamp = time();
|
||||
$signature = hash_hmac('sha256', $timestamp.':'.json_encode($payload), 'test_secret');
|
||||
$header = sprintf('ts=%s,h1=%s', $timestamp, $signature);
|
||||
|
||||
$response = $this->withHeader('Paddle-Signature', $header)
|
||||
->postJson('/paddle/webhook', $payload);
|
||||
|
||||
$response->assertOk()->assertJson(['status' => 'processed']);
|
||||
|
||||
$tenant->refresh();
|
||||
|
||||
$this->assertSame('active', $tenant->subscription_status);
|
||||
$this->assertNotNull($tenant->subscription_expires_at);
|
||||
}
|
||||
|
||||
public function test_rejects_invalid_signature(): void
|
||||
{
|
||||
config(['paddle.webhook_secret' => 'secret']);
|
||||
|
||||
$response = $this->withHeader('Paddle-Signature', 'invalid')
|
||||
->postJson('/paddle/webhook', ['event_type' => 'transaction.completed']);
|
||||
|
||||
$response->assertStatus(400)->assertJson(['status' => 'invalid']);
|
||||
}
|
||||
|
||||
public function test_unhandled_event_returns_accepted(): void
|
||||
{
|
||||
config(['paddle.webhook_secret' => null]);
|
||||
|
||||
$response = $this->postJson('/paddle/webhook', [
|
||||
'event_type' => 'transaction.unknown',
|
||||
'data' => [],
|
||||
]);
|
||||
|
||||
$response->assertStatus(202)->assertJson(['status' => 'ignored']);
|
||||
}
|
||||
|
||||
public function test_subscription_activation_creates_tenant_package(): void
|
||||
{
|
||||
config(['paddle.webhook_secret' => 'test_secret']);
|
||||
|
||||
$tenant = Tenant::factory()->create([
|
||||
'paddle_customer_id' => 'cus_123',
|
||||
'subscription_status' => 'free',
|
||||
]);
|
||||
|
||||
$package = Package::factory()->create([
|
||||
'type' => 'reseller',
|
||||
'price' => 129,
|
||||
'paddle_price_id' => 'price_sub_1',
|
||||
]);
|
||||
|
||||
$payload = [
|
||||
'event_type' => 'subscription.created',
|
||||
'data' => [
|
||||
'id' => 'sub_123',
|
||||
'status' => 'active',
|
||||
'customer_id' => 'cus_123',
|
||||
'created_at' => Carbon::now()->subDay()->toIso8601String(),
|
||||
'next_billing_date' => Carbon::now()->addMonth()->toIso8601String(),
|
||||
'custom_data' => [
|
||||
'tenant_id' => (string) $tenant->id,
|
||||
'package_id' => (string) $package->id,
|
||||
],
|
||||
'items' => [
|
||||
[
|
||||
'price_id' => 'price_sub_1',
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$signature = hash_hmac('sha256', json_encode($payload), 'test_secret');
|
||||
|
||||
$response = $this->withHeader('Paddle-Webhook-Signature', $signature)
|
||||
->postJson('/paddle/webhook', $payload);
|
||||
|
||||
$response->assertOk()->assertJson(['status' => 'processed']);
|
||||
|
||||
$tenant->refresh();
|
||||
|
||||
$tenantPackage = TenantPackage::where('tenant_id', $tenant->id)
|
||||
->where('package_id', $package->id)
|
||||
->first();
|
||||
|
||||
$this->assertNotNull($tenantPackage);
|
||||
$this->assertSame('sub_123', $tenantPackage->paddle_subscription_id);
|
||||
$this->assertTrue($tenantPackage->active);
|
||||
$this->assertEquals('active', $tenant->subscription_status);
|
||||
$this->assertNotNull($tenant->subscription_expires_at);
|
||||
}
|
||||
|
||||
public function test_subscription_cancellation_marks_package_inactive(): void
|
||||
{
|
||||
config(['paddle.webhook_secret' => 'test_secret']);
|
||||
|
||||
$tenant = Tenant::factory()->create([
|
||||
'paddle_customer_id' => 'cus_cancel',
|
||||
'subscription_status' => 'active',
|
||||
]);
|
||||
|
||||
$package = Package::factory()->create([
|
||||
'type' => 'reseller',
|
||||
'price' => 199,
|
||||
'paddle_price_id' => 'price_cancel',
|
||||
]);
|
||||
|
||||
TenantPackage::factory()->create([
|
||||
'tenant_id' => $tenant->id,
|
||||
'package_id' => $package->id,
|
||||
'paddle_subscription_id' => 'sub_cancel',
|
||||
'active' => true,
|
||||
]);
|
||||
|
||||
$payload = [
|
||||
'event_type' => 'subscription.cancelled',
|
||||
'data' => [
|
||||
'id' => 'sub_cancel',
|
||||
'status' => 'cancelled',
|
||||
'customer_id' => 'cus_cancel',
|
||||
'custom_data' => [
|
||||
'tenant_id' => (string) $tenant->id,
|
||||
'package_id' => (string) $package->id,
|
||||
],
|
||||
'items' => [
|
||||
[
|
||||
'price_id' => 'price_cancel',
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$signature = hash_hmac('sha256', json_encode($payload), 'test_secret');
|
||||
|
||||
$response = $this->withHeader('Paddle-Webhook-Signature', $signature)
|
||||
->postJson('/paddle/webhook', $payload);
|
||||
|
||||
$response->assertOk()->assertJson(['status' => 'processed']);
|
||||
|
||||
$tenant->refresh();
|
||||
|
||||
$tenantPackage = TenantPackage::where('tenant_id', $tenant->id)
|
||||
->where('package_id', $package->id)
|
||||
->first();
|
||||
|
||||
$this->assertNotNull($tenantPackage);
|
||||
$this->assertFalse($tenantPackage->active);
|
||||
$this->assertEquals('expired', $tenant->subscription_status);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{\App\Models\Tenant, \App\Models\Package, \App\Models\CheckoutSession}
|
||||
*/
|
||||
protected function prepareSession(): array
|
||||
{
|
||||
$user = User::factory()->create(['email_verified_at' => now()]);
|
||||
$tenant = Tenant::factory()->create(['user_id' => $user->id]);
|
||||
$user->forceFill(['tenant_id' => $tenant->id])->save();
|
||||
|
||||
$package = Package::factory()->create([
|
||||
'type' => 'reseller',
|
||||
'price' => 99,
|
||||
'paddle_price_id' => 'price_123',
|
||||
]);
|
||||
|
||||
/** @var CheckoutSessionService $sessions */
|
||||
$sessions = app(CheckoutSessionService::class);
|
||||
$session = $sessions->createOrResume($user, $package, ['tenant' => $tenant]);
|
||||
$sessions->selectProvider($session, CheckoutSession::PROVIDER_PADDLE);
|
||||
|
||||
return [$tenant, $package, $session];
|
||||
}
|
||||
}
|
||||
@@ -44,7 +44,7 @@ class ProfilePageTest extends TestCase
|
||||
'package_id' => $package->id,
|
||||
'price' => 199.00,
|
||||
'type' => 'reseller_subscription',
|
||||
'provider' => 'paddle',
|
||||
'provider' => 'lemonsqueezy',
|
||||
'purchased_at' => now()->subWeek(),
|
||||
]);
|
||||
|
||||
|
||||
@@ -36,12 +36,12 @@ class PurchaseConfirmationMailTest extends TestCase
|
||||
$purchase = PackagePurchase::factory()->create([
|
||||
'tenant_id' => $tenant->id,
|
||||
'package_id' => $package->id,
|
||||
'provider' => 'paddle',
|
||||
'provider_id' => 'txn_123',
|
||||
'provider' => 'lemonsqueezy',
|
||||
'provider_id' => 'ord_123',
|
||||
'price' => 59,
|
||||
'metadata' => [
|
||||
'payload' => [
|
||||
'invoice_url' => 'https://paddle.test/invoice/123',
|
||||
'receipt_url' => 'https://lemonsqueezy.test/receipt/123',
|
||||
],
|
||||
'currency' => 'EUR',
|
||||
],
|
||||
@@ -52,7 +52,7 @@ class PurchaseConfirmationMailTest extends TestCase
|
||||
|
||||
$this->assertStringContainsString('Die Fotospiel.App', $html);
|
||||
$this->assertStringContainsString('Classic', $html);
|
||||
$this->assertStringContainsString('txn_123', $html);
|
||||
$this->assertStringContainsString('https://paddle.test/invoice/123', $html);
|
||||
$this->assertStringContainsString('ord_123', $html);
|
||||
$this->assertStringContainsString('https://lemonsqueezy.test/receipt/123', $html);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ namespace Tests\Feature;
|
||||
use App\Models\Package;
|
||||
use App\Models\Tenant;
|
||||
use App\Models\User;
|
||||
use App\Services\Paddle\PaddleCheckoutService;
|
||||
use App\Services\LemonSqueezy\LemonSqueezyCheckoutService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Mockery;
|
||||
use Tests\TestCase;
|
||||
@@ -20,12 +20,12 @@ class PurchaseTest extends TestCase
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function test_create_paddle_checkout_requires_paddle_price(): void
|
||||
public function test_create_lemonsqueezy_checkout_requires_variant(): void
|
||||
{
|
||||
[$tenant, $package] = $this->seedTenantWithPackage(includePaddlePrice: false);
|
||||
[$tenant, $package] = $this->seedTenantWithPackage(includeVariant: false);
|
||||
$this->actingAs($tenant->user);
|
||||
|
||||
$response = $this->postJson('/paddle/create-checkout', [
|
||||
$response = $this->postJson('/lemonsqueezy/create-checkout', [
|
||||
'package_id' => $package->id,
|
||||
'accepted_terms' => true,
|
||||
]);
|
||||
@@ -34,12 +34,12 @@ class PurchaseTest extends TestCase
|
||||
->assertJsonValidationErrors('package_id');
|
||||
}
|
||||
|
||||
public function test_create_paddle_checkout_returns_checkout_url(): void
|
||||
public function test_create_lemonsqueezy_checkout_returns_checkout_url(): void
|
||||
{
|
||||
[$tenant, $package] = $this->seedTenantWithPackage(includePaddlePrice: true);
|
||||
[$tenant, $package] = $this->seedTenantWithPackage(includeVariant: true);
|
||||
$this->actingAs($tenant->user);
|
||||
|
||||
$service = Mockery::mock(PaddleCheckoutService::class);
|
||||
$service = Mockery::mock(LemonSqueezyCheckoutService::class);
|
||||
$service->shouldReceive('createCheckout')
|
||||
->once()
|
||||
->with(
|
||||
@@ -52,55 +52,41 @@ class PurchaseTest extends TestCase
|
||||
})
|
||||
)
|
||||
->andReturn([
|
||||
'checkout_url' => 'https://paddle.test/checkout/abc',
|
||||
'checkout_url' => 'https://checkout.lemonsqueezy.test/checkout/abc',
|
||||
]);
|
||||
|
||||
$this->app->instance(PaddleCheckoutService::class, $service);
|
||||
$this->app->instance(LemonSqueezyCheckoutService::class, $service);
|
||||
|
||||
$response = $this->postJson('/paddle/create-checkout', [
|
||||
$response = $this->postJson('/lemonsqueezy/create-checkout', [
|
||||
'package_id' => $package->id,
|
||||
'accepted_terms' => true,
|
||||
]);
|
||||
|
||||
$response->assertOk()
|
||||
->assertJson([
|
||||
'checkout_url' => 'https://paddle.test/checkout/abc',
|
||||
'checkout_url' => 'https://checkout.lemonsqueezy.test/checkout/abc',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_create_paddle_checkout_inline_returns_items(): void
|
||||
public function test_create_lemonsqueezy_checkout_requires_terms(): void
|
||||
{
|
||||
[$tenant, $package] = $this->seedTenantWithPackage(includePaddlePrice: true);
|
||||
[$tenant, $package] = $this->seedTenantWithPackage(includeVariant: true);
|
||||
$this->actingAs($tenant->user);
|
||||
|
||||
$service = Mockery::mock(PaddleCheckoutService::class);
|
||||
$service = Mockery::mock(LemonSqueezyCheckoutService::class);
|
||||
$service->shouldNotReceive('createCheckout');
|
||||
$this->app->instance(PaddleCheckoutService::class, $service);
|
||||
$this->app->instance(LemonSqueezyCheckoutService::class, $service);
|
||||
|
||||
$response = $this->postJson('/paddle/create-checkout', [
|
||||
$response = $this->postJson('/lemonsqueezy/create-checkout', [
|
||||
'package_id' => $package->id,
|
||||
'inline' => true,
|
||||
'accepted_terms' => true,
|
||||
'accepted_terms' => false,
|
||||
]);
|
||||
|
||||
$response->assertOk()
|
||||
->assertJson([
|
||||
'mode' => 'inline',
|
||||
])
|
||||
->assertJsonStructure([
|
||||
'mode',
|
||||
'items' => [
|
||||
['priceId', 'quantity'],
|
||||
],
|
||||
'custom_data' => ['tenant_id', 'package_id', 'checkout_session_id'],
|
||||
]);
|
||||
|
||||
$payload = $response->json();
|
||||
$this->assertSame($package->paddle_price_id, $payload['items'][0]['priceId']);
|
||||
$this->assertSame(1, $payload['items'][0]['quantity']);
|
||||
$response->assertStatus(422)
|
||||
->assertJsonValidationErrors('accepted_terms');
|
||||
}
|
||||
|
||||
private function seedTenantWithPackage(int $price = 10, string $type = 'endcustomer', bool $includePaddlePrice = true): array
|
||||
private function seedTenantWithPackage(int $price = 10, string $type = 'endcustomer', bool $includeVariant = true): array
|
||||
{
|
||||
$user = User::factory()->create(['email_verified_at' => now()]);
|
||||
$tenant = Tenant::factory()->create(['user_id' => $user->id]);
|
||||
@@ -110,7 +96,7 @@ class PurchaseTest extends TestCase
|
||||
$package = Package::factory()->create([
|
||||
'price' => $price,
|
||||
'type' => $type,
|
||||
'paddle_price_id' => $includePaddlePrice ? 'price_123' : null,
|
||||
'lemonsqueezy_variant_id' => $includeVariant ? 'variant_123' : null,
|
||||
]);
|
||||
|
||||
return [$tenant, $package];
|
||||
|
||||
@@ -54,7 +54,7 @@ class SuperAdminAuditLogMutationTest extends TestCase
|
||||
$this->bootSuperAdminPanel($user);
|
||||
|
||||
$this->mock(GiftVoucherService::class, function ($mock) use ($voucher): void {
|
||||
$mock->shouldReceive('issueFromPaddle')
|
||||
$mock->shouldReceive('issueFromLemonSqueezy')
|
||||
->once()
|
||||
->andReturn($voucher);
|
||||
});
|
||||
|
||||
@@ -2,14 +2,14 @@
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Jobs\SyncPackageToPaddle;
|
||||
use App\Jobs\SyncPackageToLemonSqueezy;
|
||||
use App\Models\Package;
|
||||
use App\Services\Paddle\PaddleCatalogService;
|
||||
use App\Services\LemonSqueezy\LemonSqueezyCatalogService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Mockery;
|
||||
use Tests\TestCase;
|
||||
|
||||
class SyncPackageToPaddleJobTest extends TestCase
|
||||
class SyncPackageToLemonSqueezyJobTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
@@ -23,13 +23,13 @@ class SyncPackageToPaddleJobTest extends TestCase
|
||||
public function test_job_creates_product_and_price_and_updates_package(): void
|
||||
{
|
||||
$package = Package::factory()->create([
|
||||
'paddle_product_id' => null,
|
||||
'paddle_price_id' => null,
|
||||
'lemonsqueezy_product_id' => null,
|
||||
'lemonsqueezy_variant_id' => null,
|
||||
'price' => 15.50,
|
||||
'slug' => 'silver-plan',
|
||||
]);
|
||||
|
||||
$service = Mockery::mock(PaddleCatalogService::class);
|
||||
$service = Mockery::mock(LemonSqueezyCatalogService::class);
|
||||
$service->shouldReceive('createProduct')
|
||||
->once()
|
||||
->withArgs(function ($pkg, $overrides) use ($package) {
|
||||
@@ -47,37 +47,37 @@ class SyncPackageToPaddleJobTest extends TestCase
|
||||
$service->shouldReceive('buildPricePayload')
|
||||
->andReturn(['payload' => 'price']);
|
||||
|
||||
$job = new SyncPackageToPaddle($package->id);
|
||||
$job = new SyncPackageToLemonSqueezy($package->id);
|
||||
$job->handle($service);
|
||||
|
||||
$package->refresh();
|
||||
|
||||
$this->assertSame('pro_123', $package->paddle_product_id);
|
||||
$this->assertSame('pri_123', $package->paddle_price_id);
|
||||
$this->assertSame('synced', $package->paddle_sync_status);
|
||||
$this->assertNotNull($package->paddle_synced_at);
|
||||
$this->assertSame(['payload' => 'product'], $package->paddle_snapshot['payload']['product']);
|
||||
$this->assertSame(['payload' => 'price'], $package->paddle_snapshot['payload']['price']);
|
||||
$this->assertSame('pro_123', $package->lemonsqueezy_product_id);
|
||||
$this->assertSame('pri_123', $package->lemonsqueezy_variant_id);
|
||||
$this->assertSame('synced', $package->lemonsqueezy_sync_status);
|
||||
$this->assertNotNull($package->lemonsqueezy_synced_at);
|
||||
$this->assertSame(['payload' => 'product'], $package->lemonsqueezy_snapshot['payload']['product']);
|
||||
$this->assertSame(['payload' => 'price'], $package->lemonsqueezy_snapshot['payload']['price']);
|
||||
}
|
||||
|
||||
public function test_dry_run_stores_snapshot_without_calling_paddle(): void
|
||||
public function test_dry_run_stores_snapshot_without_calling_lemonsqueezy(): void
|
||||
{
|
||||
$package = Package::factory()->create([
|
||||
'slug' => 'gold-plan',
|
||||
]);
|
||||
|
||||
$service = Mockery::mock(PaddleCatalogService::class);
|
||||
$service = Mockery::mock(LemonSqueezyCatalogService::class);
|
||||
$service->shouldReceive('buildProductPayload')->andReturn(['payload' => 'product']);
|
||||
$service->shouldReceive('buildPricePayload')->andReturn(['payload' => 'price']);
|
||||
|
||||
$job = new SyncPackageToPaddle($package->id, ['dry_run' => true]);
|
||||
$job = new SyncPackageToLemonSqueezy($package->id, ['dry_run' => true]);
|
||||
$job->handle($service);
|
||||
|
||||
$package->refresh();
|
||||
|
||||
$this->assertSame('dry-run', $package->paddle_sync_status);
|
||||
$this->assertTrue($package->paddle_snapshot['dry_run']);
|
||||
$this->assertSame(['payload' => 'product'], $package->paddle_snapshot['payload']['product']);
|
||||
$this->assertSame(['payload' => 'price'], $package->paddle_snapshot['payload']['price']);
|
||||
$this->assertSame('dry-run', $package->lemonsqueezy_sync_status);
|
||||
$this->assertTrue($package->lemonsqueezy_snapshot['dry_run']);
|
||||
$this->assertSame(['payload' => 'product'], $package->lemonsqueezy_snapshot['payload']['product']);
|
||||
$this->assertSame(['payload' => 'price'], $package->lemonsqueezy_snapshot['payload']['price']);
|
||||
}
|
||||
}
|
||||
@@ -17,26 +17,21 @@ class EventAddonCheckoutTest extends TenantTestCase
|
||||
|
||||
Config::set('package-addons.extra_photos_small', [
|
||||
'label' => 'Extra photos (500)',
|
||||
'price_id' => 'pri_addon_photos',
|
||||
'variant_id' => 'var_addon_photos',
|
||||
'increments' => ['extra_photos' => 500],
|
||||
]);
|
||||
|
||||
Config::set('paddle.api_key', 'test_key');
|
||||
Config::set('paddle.base_url', 'https://paddle.test');
|
||||
Config::set('paddle.environment', 'sandbox');
|
||||
Config::set('lemonsqueezy.api_key', 'test_key');
|
||||
Config::set('lemonsqueezy.base_url', 'https://lemonsqueezy.test');
|
||||
Config::set('lemonsqueezy.store_id', 'store_123');
|
||||
|
||||
// Fake Paddle response
|
||||
// Fake Lemon Squeezy response
|
||||
Http::fake([
|
||||
'*/customers' => Http::response([
|
||||
'https://lemonsqueezy.test/checkouts' => Http::response([
|
||||
'data' => [
|
||||
'id' => 'ctm_addon_123',
|
||||
],
|
||||
], 200),
|
||||
'*/transactions' => Http::response([
|
||||
'data' => [
|
||||
'id' => 'txn_addon_123',
|
||||
'checkout' => [
|
||||
'url' => 'https://checkout.paddle.test/abcd',
|
||||
'id' => 'chk_addon_123',
|
||||
'attributes' => [
|
||||
'url' => 'https://checkout.lemonsqueezy.test/abcd',
|
||||
],
|
||||
],
|
||||
], 200),
|
||||
@@ -73,14 +68,14 @@ class EventAddonCheckoutTest extends TenantTestCase
|
||||
]);
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJsonPath('checkout_id', 'txn_addon_123');
|
||||
$response->assertJsonPath('checkout_id', 'chk_addon_123');
|
||||
|
||||
$this->assertDatabaseHas('event_package_addons', [
|
||||
'event_package_id' => $eventPackage->id,
|
||||
'addon_key' => 'extra_photos_small',
|
||||
'status' => 'pending',
|
||||
'quantity' => 2,
|
||||
'transaction_id' => 'txn_addon_123',
|
||||
'checkout_id' => 'chk_addon_123',
|
||||
]);
|
||||
|
||||
$addon = EventPackageAddon::where('event_package_id', $eventPackage->id)->latest()->first();
|
||||
|
||||
@@ -2,9 +2,10 @@
|
||||
|
||||
namespace Tests\Feature\Tenant;
|
||||
|
||||
use App\Services\Paddle\Exceptions\PaddleException;
|
||||
use App\Services\Paddle\PaddleCustomerPortalService;
|
||||
use App\Services\Paddle\PaddleCustomerService;
|
||||
use App\Models\Package;
|
||||
use App\Models\TenantPackage;
|
||||
use App\Services\LemonSqueezy\Exceptions\LemonSqueezyException;
|
||||
use App\Services\LemonSqueezy\LemonSqueezySubscriptionService;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Mockery;
|
||||
|
||||
@@ -17,7 +18,7 @@ class TenantBillingPortalTest extends TenantTestCase
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function test_portal_logs_paddle_error_context(): void
|
||||
public function test_portal_logs_lemonsqueezy_error_context(): void
|
||||
{
|
||||
$logged = [];
|
||||
Log::listen(function ($event) use (&$logged): void {
|
||||
@@ -28,39 +29,44 @@ class TenantBillingPortalTest extends TenantTestCase
|
||||
];
|
||||
});
|
||||
|
||||
$customerService = Mockery::mock(PaddleCustomerService::class);
|
||||
$customerService->shouldReceive('ensureCustomerId')
|
||||
->once()
|
||||
->withArgs(fn ($tenant) => $tenant->is($this->tenant))
|
||||
->andReturn('ctm_test_123');
|
||||
$this->instance(PaddleCustomerService::class, $customerService);
|
||||
$package = Package::factory()->reseller()->create();
|
||||
TenantPackage::factory()->create([
|
||||
'tenant_id' => $this->tenant->id,
|
||||
'package_id' => $package->id,
|
||||
'lemonsqueezy_subscription_id' => 'sub_123',
|
||||
'active' => true,
|
||||
]);
|
||||
|
||||
$portalService = Mockery::mock(PaddleCustomerPortalService::class);
|
||||
$portalService->shouldReceive('createSession')
|
||||
$subscriptions = Mockery::mock(LemonSqueezySubscriptionService::class);
|
||||
$subscriptions->shouldReceive('retrieve')
|
||||
->once()
|
||||
->with('ctm_test_123')
|
||||
->andThrow(new PaddleException('Paddle request failed with status 404', 404, [
|
||||
'error' => [
|
||||
'code' => 'entity_not_found',
|
||||
'message' => 'Not found',
|
||||
->with('sub_123')
|
||||
->andThrow(new LemonSqueezyException('Not found', 404, [
|
||||
'errors' => [
|
||||
[
|
||||
'code' => 'entity_not_found',
|
||||
'detail' => 'Not found',
|
||||
],
|
||||
],
|
||||
'meta' => [
|
||||
'request_id' => 'req_123',
|
||||
],
|
||||
]));
|
||||
$this->instance(PaddleCustomerPortalService::class, $portalService);
|
||||
$this->instance(LemonSqueezySubscriptionService::class, $subscriptions);
|
||||
|
||||
$response = $this->authenticatedRequest('POST', '/api/v1/tenant/billing/portal');
|
||||
|
||||
$response->assertStatus(502)
|
||||
->assertJson([
|
||||
'message' => 'Failed to create Paddle customer portal session.',
|
||||
'message' => 'Failed to fetch Lemon Squeezy subscription portal URL.',
|
||||
]);
|
||||
|
||||
$matched = collect($logged)->contains(function (array $entry): bool {
|
||||
return $entry['level'] === 'warning'
|
||||
&& $entry['message'] === 'Failed to create Paddle customer portal session'
|
||||
&& $entry['message'] === 'Failed to fetch Lemon Squeezy subscription portal URL'
|
||||
&& ($entry['context']['tenant_id'] ?? null) === $this->tenant->id
|
||||
&& ($entry['context']['paddle_customer_id'] ?? null) === 'ctm_test_123'
|
||||
&& ($entry['context']['paddle_status'] ?? null) === 404
|
||||
&& ($entry['context']['paddle_error_code'] ?? null) === 'entity_not_found';
|
||||
&& ($entry['context']['lemonsqueezy_status'] ?? null) === 404
|
||||
&& (($entry['context']['lemonsqueezy_error']['code'] ?? null) === 'entity_not_found');
|
||||
});
|
||||
|
||||
$this->assertTrue($matched);
|
||||
|
||||
@@ -20,14 +20,14 @@ class TenantCheckoutSessionStatusTest extends TenantTestCase
|
||||
'tenant_id' => $this->tenant->id,
|
||||
'package_id' => $package->id,
|
||||
'status' => CheckoutSession::STATUS_FAILED,
|
||||
'provider' => CheckoutSession::PROVIDER_PADDLE,
|
||||
'provider' => CheckoutSession::PROVIDER_LEMONSQUEEZY,
|
||||
'provider_metadata' => [
|
||||
'paddle_checkout_url' => 'https://checkout.paddle.test/checkout/123',
|
||||
'lemonsqueezy_checkout_url' => 'https://checkout.lemonsqueezy.test/checkout/123',
|
||||
],
|
||||
'status_history' => [
|
||||
[
|
||||
'status' => CheckoutSession::STATUS_FAILED,
|
||||
'reason' => 'paddle_failed',
|
||||
'reason' => 'lemonsqueezy_failed',
|
||||
'at' => now()->toIso8601String(),
|
||||
],
|
||||
],
|
||||
@@ -40,7 +40,7 @@ class TenantCheckoutSessionStatusTest extends TenantTestCase
|
||||
|
||||
$response->assertOk()
|
||||
->assertJsonPath('status', CheckoutSession::STATUS_FAILED)
|
||||
->assertJsonPath('reason', 'paddle_failed')
|
||||
->assertJsonPath('checkout_url', 'https://checkout.paddle.test/checkout/123');
|
||||
->assertJsonPath('reason', 'lemonsqueezy_failed')
|
||||
->assertJsonPath('checkout_url', 'https://checkout.lemonsqueezy.test/checkout/123');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,10 +3,10 @@
|
||||
namespace Tests\Feature\Tenant;
|
||||
|
||||
use App\Models\Package;
|
||||
use App\Services\Paddle\PaddleCheckoutService;
|
||||
use App\Services\LemonSqueezy\LemonSqueezyCheckoutService;
|
||||
use Mockery;
|
||||
|
||||
class TenantPaddleCheckoutTest extends TenantTestCase
|
||||
class TenantLemonSqueezyCheckoutTest extends TenantTestCase
|
||||
{
|
||||
protected function tearDown(): void
|
||||
{
|
||||
@@ -15,14 +15,14 @@ class TenantPaddleCheckoutTest extends TenantTestCase
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function test_tenant_can_create_paddle_checkout(): void
|
||||
public function test_tenant_can_create_lemonsqueezy_checkout(): void
|
||||
{
|
||||
$package = Package::factory()->create([
|
||||
'paddle_price_id' => 'pri_test_123',
|
||||
'lemonsqueezy_variant_id' => 'pri_test_123',
|
||||
'price' => 129,
|
||||
]);
|
||||
|
||||
$checkoutService = Mockery::mock(PaddleCheckoutService::class);
|
||||
$checkoutService = Mockery::mock(LemonSqueezyCheckoutService::class);
|
||||
$checkoutService->shouldReceive('createCheckout')
|
||||
->once()
|
||||
->withArgs(function ($tenant, $payloadPackage, array $payload) use ($package) {
|
||||
@@ -35,28 +35,28 @@ class TenantPaddleCheckoutTest extends TenantTestCase
|
||||
&& ! empty($payload['metadata']['checkout_session_id']);
|
||||
})
|
||||
->andReturn([
|
||||
'checkout_url' => 'https://checkout.paddle.test/checkout/123',
|
||||
'checkout_url' => 'https://checkout.lemonsqueezy.test/checkout/123',
|
||||
'id' => 'chk_test_123',
|
||||
]);
|
||||
$this->instance(PaddleCheckoutService::class, $checkoutService);
|
||||
$this->instance(LemonSqueezyCheckoutService::class, $checkoutService);
|
||||
|
||||
$response = $this->authenticatedRequest('POST', '/api/v1/tenant/packages/paddle-checkout', [
|
||||
$response = $this->authenticatedRequest('POST', '/api/v1/tenant/packages/lemonsqueezy-checkout', [
|
||||
'package_id' => $package->id,
|
||||
]);
|
||||
|
||||
$response->assertOk()
|
||||
->assertJsonPath('checkout_url', 'https://checkout.paddle.test/checkout/123')
|
||||
->assertJsonPath('checkout_url', 'https://checkout.lemonsqueezy.test/checkout/123')
|
||||
->assertJsonStructure(['checkout_session_id']);
|
||||
}
|
||||
|
||||
public function test_paddle_checkout_requires_paddle_price_id(): void
|
||||
public function test_lemonsqueezy_checkout_requires_lemonsqueezy_variant_id(): void
|
||||
{
|
||||
$package = Package::factory()->create([
|
||||
'paddle_price_id' => null,
|
||||
'lemonsqueezy_variant_id' => null,
|
||||
'price' => 129,
|
||||
]);
|
||||
|
||||
$response = $this->authenticatedRequest('POST', '/api/v1/tenant/packages/paddle-checkout', [
|
||||
$response = $this->authenticatedRequest('POST', '/api/v1/tenant/packages/lemonsqueezy-checkout', [
|
||||
'package_id' => $package->id,
|
||||
]);
|
||||
|
||||
@@ -29,7 +29,7 @@ class CouponRedemptionServiceTest extends TestCase
|
||||
]);
|
||||
$coupon = Coupon::factory()->create([
|
||||
'code' => 'SAVE10',
|
||||
'paddle_discount_id' => 'dsc_123',
|
||||
'lemonsqueezy_discount_id' => 'dsc_123',
|
||||
]);
|
||||
$coupon->packages()->attach($package);
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
namespace Tests\Unit;
|
||||
|
||||
use App\Services\GiftVouchers\GiftVoucherCheckoutService;
|
||||
use App\Services\Paddle\PaddleClient;
|
||||
use App\Services\LemonSqueezy\LemonSqueezyCheckoutService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Mockery;
|
||||
use Tests\TestCase;
|
||||
@@ -15,8 +15,8 @@ class GiftVoucherCheckoutServiceTest extends TestCase
|
||||
public function test_it_lists_tiers_with_checkout_flag(): void
|
||||
{
|
||||
config()->set('gift-vouchers.tiers', [
|
||||
['key' => 'gift-a', 'label' => 'A', 'amount' => 10, 'currency' => 'EUR', 'paddle_price_id' => 'pri_a'],
|
||||
['key' => 'gift-b', 'label' => 'B', 'amount' => 20, 'currency' => 'EUR', 'paddle_price_id' => null],
|
||||
['key' => 'gift-a', 'label' => 'A', 'amount' => 10, 'currency' => 'EUR', 'lemonsqueezy_variant_id' => 'pri_a'],
|
||||
['key' => 'gift-b', 'label' => 'B', 'amount' => 20, 'currency' => 'EUR', 'lemonsqueezy_variant_id' => null],
|
||||
]);
|
||||
|
||||
$service = $this->app->make(GiftVoucherCheckoutService::class);
|
||||
@@ -31,26 +31,23 @@ class GiftVoucherCheckoutServiceTest extends TestCase
|
||||
public function test_it_creates_checkout_link_with_metadata(): void
|
||||
{
|
||||
config()->set('gift-vouchers.tiers', [
|
||||
['key' => 'gift-a', 'label' => 'A', 'amount' => 10, 'currency' => 'EUR', 'paddle_price_id' => 'pri_a'],
|
||||
['key' => 'gift-a', 'label' => 'A', 'amount' => 10, 'currency' => 'EUR', 'lemonsqueezy_variant_id' => 'pri_a'],
|
||||
]);
|
||||
|
||||
$client = Mockery::mock(PaddleClient::class);
|
||||
$client->shouldReceive('post')
|
||||
$checkoutService = Mockery::mock(LemonSqueezyCheckoutService::class);
|
||||
$checkoutService->shouldReceive('createVariantCheckout')
|
||||
->once()
|
||||
->with('/customers', Mockery::on(function ($payload) {
|
||||
return $payload['email'] === 'buyer@example.com';
|
||||
}))
|
||||
->andReturn(['data' => ['id' => 'ctm_123']]);
|
||||
$client->shouldReceive('post')
|
||||
->once()
|
||||
->with('/transactions', Mockery::on(function ($payload) {
|
||||
return $payload['items'][0]['price_id'] === 'pri_a'
|
||||
&& $payload['customer_id'] === 'ctm_123'
|
||||
&& $payload['custom_data']['type'] === 'gift_voucher';
|
||||
}))
|
||||
->andReturn(['data' => ['checkout' => ['url' => 'https://paddle.test/checkout/123'], 'id' => 'txn_123']]);
|
||||
->with('pri_a', Mockery::on(function (array $customData) {
|
||||
return ($customData['type'] ?? null) === 'gift_voucher'
|
||||
&& ($customData['tier_key'] ?? null) === 'gift-a'
|
||||
&& ($customData['purchaser_email'] ?? null) === 'buyer@example.com'
|
||||
&& ($customData['recipient_email'] ?? null) === 'friend@example.com'
|
||||
&& ($customData['recipient_name'] ?? null) === 'Friend'
|
||||
&& ($customData['message'] ?? null) === 'Hi';
|
||||
}), Mockery::type('array'))
|
||||
->andReturn(['checkout_url' => 'https://lemonsqueezy.test/checkout/123', 'id' => 'chk_123']);
|
||||
|
||||
$this->app->instance(PaddleClient::class, $client);
|
||||
$this->app->instance(LemonSqueezyCheckoutService::class, $checkoutService);
|
||||
|
||||
$service = $this->app->make(GiftVoucherCheckoutService::class);
|
||||
|
||||
@@ -62,7 +59,7 @@ class GiftVoucherCheckoutServiceTest extends TestCase
|
||||
'message' => 'Hi',
|
||||
]);
|
||||
|
||||
$this->assertSame('https://paddle.test/checkout/123', $checkout['checkout_url']);
|
||||
$this->assertSame('txn_123', $checkout['id']);
|
||||
$this->assertSame('https://lemonsqueezy.test/checkout/123', $checkout['checkout_url']);
|
||||
$this->assertSame('chk_123', $checkout['id']);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,14 +3,12 @@
|
||||
namespace Tests\Unit;
|
||||
|
||||
use App\Enums\CouponType;
|
||||
use App\Jobs\SyncCouponToPaddle;
|
||||
use App\Mail\GiftVoucherIssued;
|
||||
use App\Models\Coupon;
|
||||
use App\Models\GiftVoucher;
|
||||
use App\Models\Package;
|
||||
use App\Services\GiftVouchers\GiftVoucherService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Bus;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use Tests\TestCase;
|
||||
|
||||
@@ -18,37 +16,38 @@ class GiftVoucherServiceTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_it_issues_voucher_and_coupon_from_paddle_payload(): void
|
||||
public function test_it_issues_voucher_and_coupon_from_lemonsqueezy_payload(): void
|
||||
{
|
||||
$package = Package::factory()->create([
|
||||
'type' => 'endcustomer',
|
||||
'paddle_price_id' => 'pri_pkg_001',
|
||||
'lemonsqueezy_variant_id' => 'pri_pkg_001',
|
||||
'price' => 59,
|
||||
]);
|
||||
|
||||
$payload = [
|
||||
'id' => 'txn_123',
|
||||
'event_type' => 'transaction.completed',
|
||||
'currency_code' => 'EUR',
|
||||
'totals' => [
|
||||
'grand_total' => [
|
||||
'amount' => 5900,
|
||||
'data' => [
|
||||
'id' => 'ord_123',
|
||||
'attributes' => [
|
||||
'total' => 5900,
|
||||
'currency' => 'EUR',
|
||||
'checkout_id' => 'chk_abc',
|
||||
'variant_id' => 'pri_pkg_001',
|
||||
'user_email' => 'buyer@example.com',
|
||||
],
|
||||
],
|
||||
'custom_data' => [
|
||||
'type' => 'gift_card',
|
||||
'purchaser_email' => 'buyer@example.com',
|
||||
'recipient_email' => 'friend@example.com',
|
||||
'recipient_name' => 'Friend',
|
||||
'message' => 'Happy Day',
|
||||
'meta' => [
|
||||
'custom_data' => [
|
||||
'type' => 'gift_card',
|
||||
'purchaser_email' => 'buyer@example.com',
|
||||
'recipient_email' => 'friend@example.com',
|
||||
'recipient_name' => 'Friend',
|
||||
'message' => 'Happy Day',
|
||||
],
|
||||
],
|
||||
'checkout_id' => 'chk_abc',
|
||||
];
|
||||
|
||||
Bus::fake([SyncCouponToPaddle::class]);
|
||||
|
||||
$service = $this->app->make(GiftVoucherService::class);
|
||||
$voucher = $service->issueFromPaddle($payload);
|
||||
$voucher = $service->issueFromLemonSqueezy($payload);
|
||||
|
||||
$this->assertInstanceOf(GiftVoucher::class, $voucher);
|
||||
$this->assertSame(59.00, (float) $voucher->amount);
|
||||
@@ -56,7 +55,6 @@ class GiftVoucherServiceTest extends TestCase
|
||||
$this->assertSame($voucher->code, $voucher->coupon->code);
|
||||
$this->assertTrue($voucher->expires_at->greaterThan(now()->addYears(4)));
|
||||
$this->assertTrue($voucher->coupon->packages()->whereKey($package->id)->exists());
|
||||
Bus::assertDispatched(SyncCouponToPaddle::class);
|
||||
}
|
||||
|
||||
public function test_redeeming_coupon_marks_voucher_redeemed(): void
|
||||
@@ -71,7 +69,7 @@ class GiftVoucherServiceTest extends TestCase
|
||||
'type' => CouponType::FLAT,
|
||||
'amount' => 29,
|
||||
'currency' => 'EUR',
|
||||
'paddle_discount_id' => null,
|
||||
'lemonsqueezy_discount_id' => null,
|
||||
]);
|
||||
|
||||
$voucher->coupon()->associate($coupon)->save();
|
||||
@@ -86,45 +84,48 @@ class GiftVoucherServiceTest extends TestCase
|
||||
public function test_it_sends_notifications_to_purchaser_and_recipient_once(): void
|
||||
{
|
||||
Mail::fake();
|
||||
Bus::fake([SyncCouponToPaddle::class]);
|
||||
config()->set('gift-vouchers.reminder_days', 0);
|
||||
config()->set('gift-vouchers.expiry_reminder_days', 0);
|
||||
|
||||
Package::factory()->create([
|
||||
'type' => 'endcustomer',
|
||||
'paddle_price_id' => 'pri_pkg_001',
|
||||
'lemonsqueezy_variant_id' => 'pri_pkg_001',
|
||||
'price' => 29,
|
||||
]);
|
||||
|
||||
$payload = [
|
||||
'id' => 'txn_456',
|
||||
'currency_code' => 'EUR',
|
||||
'totals' => [
|
||||
'grand_total' => [
|
||||
'amount' => 2900,
|
||||
'data' => [
|
||||
'id' => 'ord_456',
|
||||
'attributes' => [
|
||||
'total' => 2900,
|
||||
'currency' => 'EUR',
|
||||
'checkout_id' => 'chk_notif',
|
||||
'variant_id' => 'pri_pkg_001',
|
||||
'user_email' => 'buyer@example.com',
|
||||
],
|
||||
],
|
||||
'custom_data' => [
|
||||
'type' => 'gift_voucher',
|
||||
'purchaser_email' => 'buyer@example.com',
|
||||
'recipient_email' => 'friend@example.com',
|
||||
'app_locale' => 'de',
|
||||
'meta' => [
|
||||
'custom_data' => [
|
||||
'type' => 'gift_voucher',
|
||||
'purchaser_email' => 'buyer@example.com',
|
||||
'recipient_email' => 'friend@example.com',
|
||||
'app_locale' => 'de',
|
||||
],
|
||||
],
|
||||
'checkout_id' => 'chk_notif',
|
||||
];
|
||||
|
||||
$service = $this->app->make(GiftVoucherService::class);
|
||||
$voucher = $service->issueFromPaddle($payload);
|
||||
$voucher = $service->issueFromLemonSqueezy($payload);
|
||||
|
||||
Mail::assertQueued(GiftVoucherIssued::class, 2);
|
||||
$this->assertTrue((bool) ($voucher->metadata['notifications_sent'] ?? false));
|
||||
|
||||
// Second call (duplicate webhook) should not resend
|
||||
$service->issueFromPaddle($payload);
|
||||
$service->issueFromLemonSqueezy($payload);
|
||||
Mail::assertQueued(GiftVoucherIssued::class, 2);
|
||||
}
|
||||
|
||||
public function test_it_resolves_amount_from_tier_by_price_id(): void
|
||||
public function test_it_resolves_amount_from_tier_by_variant_id(): void
|
||||
{
|
||||
config()->set('gift-vouchers.tiers', [
|
||||
[
|
||||
@@ -132,21 +133,25 @@ class GiftVoucherServiceTest extends TestCase
|
||||
'label' => 'Gift Classic (USD)',
|
||||
'amount' => 65.00,
|
||||
'currency' => 'USD',
|
||||
'paddle_price_id' => 'pri_usd_123',
|
||||
'lemonsqueezy_variant_id' => 'pri_usd_123',
|
||||
],
|
||||
]);
|
||||
|
||||
Bus::fake([SyncCouponToPaddle::class]);
|
||||
Mail::fake();
|
||||
|
||||
$payload = [
|
||||
'id' => 'txn_usd',
|
||||
'price_id' => 'pri_usd_123',
|
||||
'currency_code' => 'USD',
|
||||
'data' => [
|
||||
'id' => 'ord_usd',
|
||||
'attributes' => [
|
||||
'variant_id' => 'pri_usd_123',
|
||||
'currency' => 'USD',
|
||||
'total' => 6500,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$service = $this->app->make(GiftVoucherService::class);
|
||||
$voucher = $service->issueFromPaddle($payload);
|
||||
$voucher = $service->issueFromLemonSqueezy($payload);
|
||||
|
||||
$this->assertSame(65.00, (float) $voucher->amount);
|
||||
$this->assertSame('USD', $voucher->currency);
|
||||
|
||||
@@ -2,14 +2,14 @@
|
||||
|
||||
namespace Tests\Unit\Jobs;
|
||||
|
||||
use App\Jobs\SyncPackageAddonToPaddle;
|
||||
use App\Jobs\SyncPackageAddonToLemonSqueezy;
|
||||
use App\Models\PackageAddon;
|
||||
use App\Services\Paddle\PaddleAddonCatalogService;
|
||||
use App\Services\LemonSqueezy\LemonSqueezyAddonCatalogService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Mockery;
|
||||
use Tests\TestCase;
|
||||
|
||||
class SyncPackageAddonToPaddleTest extends TestCase
|
||||
class SyncPackageAddonToLemonSqueezyTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
@@ -22,7 +22,7 @@ class SyncPackageAddonToPaddleTest extends TestCase
|
||||
'metadata' => ['price_eur' => 5],
|
||||
]);
|
||||
|
||||
$service = Mockery::mock(PaddleAddonCatalogService::class);
|
||||
$service = Mockery::mock(LemonSqueezyAddonCatalogService::class);
|
||||
$service->shouldReceive('createProduct')
|
||||
->once()
|
||||
->andReturn(['id' => 'pro_addon_1']);
|
||||
@@ -30,13 +30,13 @@ class SyncPackageAddonToPaddleTest extends TestCase
|
||||
->once()
|
||||
->andReturn(['id' => 'pri_addon_1']);
|
||||
|
||||
$job = new SyncPackageAddonToPaddle($addon->id);
|
||||
$job = new SyncPackageAddonToLemonSqueezy($addon->id);
|
||||
$job->handle($service);
|
||||
|
||||
$addon->refresh();
|
||||
|
||||
$this->assertSame('pri_addon_1', $addon->price_id);
|
||||
$this->assertEquals('pro_addon_1', $addon->metadata['paddle_product_id']);
|
||||
$this->assertEquals('synced', $addon->metadata['paddle_sync_status']);
|
||||
$this->assertSame('pri_addon_1', $addon->variant_id);
|
||||
$this->assertEquals('pro_addon_1', $addon->metadata['lemonsqueezy_product_id']);
|
||||
$this->assertEquals('synced', $addon->metadata['lemonsqueezy_sync_status']);
|
||||
}
|
||||
}
|
||||
@@ -3,13 +3,13 @@
|
||||
namespace Tests\Unit;
|
||||
|
||||
use App\Models\Package;
|
||||
use App\Services\Paddle\PaddleCatalogService;
|
||||
use App\Services\Paddle\PaddleClient;
|
||||
use App\Services\LemonSqueezy\LemonSqueezyCatalogService;
|
||||
use App\Services\LemonSqueezy\LemonSqueezyClient;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Mockery;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PaddleCatalogServiceTest extends TestCase
|
||||
class LemonSqueezyCatalogServiceTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
@@ -40,7 +40,7 @@ class PaddleCatalogServiceTest extends TestCase
|
||||
],
|
||||
]);
|
||||
|
||||
$service = new PaddleCatalogService(Mockery::mock(PaddleClient::class));
|
||||
$service = new LemonSqueezyCatalogService(Mockery::mock(LemonSqueezyClient::class));
|
||||
|
||||
$payload = $service->buildProductPayload($package);
|
||||
|
||||
@@ -63,7 +63,7 @@ class PaddleCatalogServiceTest extends TestCase
|
||||
'name' => 'Silver Plan',
|
||||
]);
|
||||
|
||||
$service = new PaddleCatalogService(Mockery::mock(PaddleClient::class));
|
||||
$service = new LemonSqueezyCatalogService(Mockery::mock(LemonSqueezyClient::class));
|
||||
|
||||
$payload = $service->buildPricePayload($package, 'pro_123');
|
||||
|
||||
61
tests/Unit/LemonSqueezyCheckoutServiceTest.php
Normal file
61
tests/Unit/LemonSqueezyCheckoutServiceTest.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use App\Models\Package;
|
||||
use App\Models\Tenant;
|
||||
use App\Services\LemonSqueezy\LemonSqueezyCheckoutService;
|
||||
use App\Services\LemonSqueezy\LemonSqueezyClient;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Mockery;
|
||||
use Tests\TestCase;
|
||||
|
||||
class LemonSqueezyCheckoutServiceTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_create_checkout_sends_custom_data_payload(): void
|
||||
{
|
||||
$tenant = Tenant::factory()->create([
|
||||
'contact_email' => 'buyer@example.com',
|
||||
]);
|
||||
|
||||
$package = Package::factory()->create([
|
||||
'lemonsqueezy_variant_id' => 'pri_123',
|
||||
]);
|
||||
|
||||
config()->set('lemonsqueezy.store_id', 'store_123');
|
||||
|
||||
$client = Mockery::mock(LemonSqueezyClient::class);
|
||||
$client->shouldReceive('post')
|
||||
->once()
|
||||
->with('/checkouts', Mockery::on(function (array $payload) use ($tenant, $package) {
|
||||
$data = $payload['data'] ?? [];
|
||||
$attributes = $data['attributes'] ?? [];
|
||||
$custom = $attributes['checkout_data']['custom'] ?? [];
|
||||
|
||||
return ($data['type'] ?? null) === 'checkouts'
|
||||
&& ($data['relationships']['variant']['data']['id'] ?? null) === 'pri_123'
|
||||
&& ($data['relationships']['store']['data']['id'] ?? null) === 'store_123'
|
||||
&& ($custom['tenant_id'] ?? null) === (string) $tenant->id
|
||||
&& ($custom['package_id'] ?? null) === (string) $package->id
|
||||
&& ($custom['source'] ?? null) === 'test'
|
||||
&& ($custom['success_url'] ?? null) === 'https://example.test/success'
|
||||
&& ($custom['return_url'] ?? null) === 'https://example.test/cancel';
|
||||
}))
|
||||
->andReturn(['data' => ['attributes' => ['url' => 'https://lemonsqueezy.test/checkout/123'], 'id' => 'chk_123']]);
|
||||
|
||||
$this->app->instance(LemonSqueezyClient::class, $client);
|
||||
|
||||
$service = $this->app->make(LemonSqueezyCheckoutService::class);
|
||||
|
||||
$checkout = $service->createCheckout($tenant, $package, [
|
||||
'success_url' => 'https://example.test/success',
|
||||
'return_url' => 'https://example.test/cancel',
|
||||
'metadata' => ['source' => 'test'],
|
||||
]);
|
||||
|
||||
$this->assertSame('https://lemonsqueezy.test/checkout/123', $checkout['checkout_url']);
|
||||
$this->assertSame('chk_123', $checkout['id']);
|
||||
}
|
||||
}
|
||||
@@ -4,13 +4,13 @@ namespace Tests\Unit;
|
||||
|
||||
use App\Enums\CouponType;
|
||||
use App\Models\Coupon;
|
||||
use App\Services\Paddle\PaddleClient;
|
||||
use App\Services\Paddle\PaddleDiscountService;
|
||||
use App\Services\LemonSqueezy\LemonSqueezyClient;
|
||||
use App\Services\LemonSqueezy\LemonSqueezyDiscountService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Mockery;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PaddleDiscountServiceTest extends TestCase
|
||||
class LemonSqueezyDiscountServiceTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
@@ -32,7 +32,7 @@ class PaddleDiscountServiceTest extends TestCase
|
||||
'amount' => 10,
|
||||
]);
|
||||
|
||||
$service = new TestablePaddleDiscountService(Mockery::mock(PaddleClient::class));
|
||||
$service = new TestableLemonSqueezyDiscountService(Mockery::mock(LemonSqueezyClient::class));
|
||||
|
||||
$payload = $service->buildPayload($coupon);
|
||||
|
||||
@@ -50,7 +50,7 @@ class PaddleDiscountServiceTest extends TestCase
|
||||
'description' => 'Flat discount',
|
||||
]);
|
||||
|
||||
$service = new TestablePaddleDiscountService(Mockery::mock(PaddleClient::class));
|
||||
$service = new TestableLemonSqueezyDiscountService(Mockery::mock(LemonSqueezyClient::class));
|
||||
|
||||
$payload = $service->buildPayload($coupon);
|
||||
|
||||
@@ -67,7 +67,7 @@ class PaddleDiscountServiceTest extends TestCase
|
||||
'description' => 'Percent discount',
|
||||
]);
|
||||
|
||||
$service = new TestablePaddleDiscountService(Mockery::mock(PaddleClient::class));
|
||||
$service = new TestableLemonSqueezyDiscountService(Mockery::mock(LemonSqueezyClient::class));
|
||||
|
||||
$payload = $service->buildPayload($coupon);
|
||||
|
||||
@@ -76,7 +76,7 @@ class PaddleDiscountServiceTest extends TestCase
|
||||
}
|
||||
}
|
||||
|
||||
class TestablePaddleDiscountService extends PaddleDiscountService
|
||||
class TestableLemonSqueezyDiscountService extends LemonSqueezyDiscountService
|
||||
{
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
52
tests/Unit/LemonSqueezyOrderServiceTest.php
Normal file
52
tests/Unit/LemonSqueezyOrderServiceTest.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use App\Services\LemonSqueezy\LemonSqueezyClient;
|
||||
use App\Services\LemonSqueezy\LemonSqueezyOrderService;
|
||||
use Mockery;
|
||||
use Tests\TestCase;
|
||||
|
||||
class LemonSqueezyOrderServiceTest extends TestCase
|
||||
{
|
||||
public function test_list_for_customer_uses_expected_filters(): void
|
||||
{
|
||||
$client = Mockery::mock(LemonSqueezyClient::class);
|
||||
$client->shouldReceive('get')
|
||||
->once()
|
||||
->with('/orders', Mockery::on(function (array $payload) {
|
||||
return $payload['filter[customer_id]'] === 'ctm_123'
|
||||
&& $payload['sort'] === '-created_at';
|
||||
}))
|
||||
->andReturn(['data' => [], 'meta' => []]);
|
||||
|
||||
$this->app->instance(LemonSqueezyClient::class, $client);
|
||||
|
||||
$service = $this->app->make(LemonSqueezyOrderService::class);
|
||||
$service->listForCustomer('ctm_123');
|
||||
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
|
||||
public function test_find_by_checkout_id_uses_checkout_then_order_lookup(): void
|
||||
{
|
||||
$client = Mockery::mock(LemonSqueezyClient::class);
|
||||
$client->shouldReceive('get')
|
||||
->once()
|
||||
->with('/checkouts/chk_123', [])
|
||||
->andReturn(['data' => ['attributes' => ['order_id' => 'ord_123']]]);
|
||||
|
||||
$client->shouldReceive('get')
|
||||
->once()
|
||||
->with('/orders/ord_123', [])
|
||||
->andReturn(['data' => ['id' => 'ord_123', 'attributes' => ['status' => 'paid']]]);
|
||||
|
||||
$this->app->instance(LemonSqueezyClient::class, $client);
|
||||
|
||||
$service = $this->app->make(LemonSqueezyOrderService::class);
|
||||
$order = $service->findByCheckoutId('chk_123');
|
||||
|
||||
$this->assertIsArray($order);
|
||||
$this->assertSame('ord_123', $order['id'] ?? null);
|
||||
}
|
||||
}
|
||||
26
tests/Unit/LemonSqueezySyncLoggingConfigTest.php
Normal file
26
tests/Unit/LemonSqueezySyncLoggingConfigTest.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use Tests\TestCase;
|
||||
|
||||
class LemonSqueezySyncLoggingConfigTest extends TestCase
|
||||
{
|
||||
public function test_lemonsqueezy_sync_channel_is_configured(): void
|
||||
{
|
||||
$channel = config('logging.channels.lemonsqueezy-sync');
|
||||
|
||||
$this->assertIsArray($channel);
|
||||
$this->assertSame('stack', $channel['driver'] ?? null);
|
||||
$this->assertNotEmpty($channel['channels'] ?? null);
|
||||
}
|
||||
|
||||
public function test_lemonsqueezy_sync_file_channel_is_configured(): void
|
||||
{
|
||||
$channel = config('logging.channels.lemonsqueezy-sync-file');
|
||||
|
||||
$this->assertIsArray($channel);
|
||||
$this->assertSame('daily', $channel['driver'] ?? null);
|
||||
$this->assertSame('lemonsqueezy-sync.log', basename((string) ($channel['path'] ?? '')));
|
||||
}
|
||||
}
|
||||
31
tests/Unit/PackageLemonSqueezyLinkTest.php
Normal file
31
tests/Unit/PackageLemonSqueezyLinkTest.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use App\Models\Package;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PackageLemonSqueezyLinkTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_link_lemonsqueezy_ids_updates_fields(): void
|
||||
{
|
||||
$package = Package::factory()->create([
|
||||
'lemonsqueezy_product_id' => null,
|
||||
'lemonsqueezy_variant_id' => null,
|
||||
'lemonsqueezy_sync_status' => null,
|
||||
'lemonsqueezy_synced_at' => null,
|
||||
]);
|
||||
|
||||
$package->linkLemonSqueezyIds('pro_123', 'pri_123');
|
||||
|
||||
$package->refresh();
|
||||
|
||||
$this->assertSame('pro_123', $package->lemonsqueezy_product_id);
|
||||
$this->assertSame('pri_123', $package->lemonsqueezy_variant_id);
|
||||
$this->assertSame('linked', $package->lemonsqueezy_sync_status);
|
||||
$this->assertNotNull($package->lemonsqueezy_synced_at);
|
||||
}
|
||||
}
|
||||
@@ -6,33 +6,33 @@ use App\Models\Package;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PackagePaddleSyncErrorTest extends TestCase
|
||||
class PackageLemonSqueezySyncErrorTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_paddle_sync_error_message_returns_message_when_present(): void
|
||||
public function test_lemonsqueezy_sync_error_message_returns_message_when_present(): void
|
||||
{
|
||||
$package = Package::factory()->create([
|
||||
'paddle_snapshot' => [
|
||||
'lemonsqueezy_snapshot' => [
|
||||
'error' => [
|
||||
'message' => 'Sync failed.',
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
$this->assertSame('Sync failed.', $package->paddle_sync_error_message);
|
||||
$this->assertSame('Sync failed.', $package->lemonsqueezy_sync_error_message);
|
||||
}
|
||||
|
||||
public function test_paddle_sync_error_message_returns_null_when_missing(): void
|
||||
public function test_lemonsqueezy_sync_error_message_returns_null_when_missing(): void
|
||||
{
|
||||
$package = Package::factory()->create([
|
||||
'paddle_snapshot' => [
|
||||
'lemonsqueezy_snapshot' => [
|
||||
'product' => [
|
||||
'id' => 'pro_123',
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
$this->assertNull($package->paddle_sync_error_message);
|
||||
$this->assertNull($package->lemonsqueezy_sync_error_message);
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use App\Models\Package;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PackagePaddleLinkTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_link_paddle_ids_updates_fields(): void
|
||||
{
|
||||
$package = Package::factory()->create([
|
||||
'paddle_product_id' => null,
|
||||
'paddle_price_id' => null,
|
||||
'paddle_sync_status' => null,
|
||||
'paddle_synced_at' => null,
|
||||
]);
|
||||
|
||||
$package->linkPaddleIds('pro_123', 'pri_123');
|
||||
|
||||
$package->refresh();
|
||||
|
||||
$this->assertSame('pro_123', $package->paddle_product_id);
|
||||
$this->assertSame('pri_123', $package->paddle_price_id);
|
||||
$this->assertSame('linked', $package->paddle_sync_status);
|
||||
$this->assertNotNull($package->paddle_synced_at);
|
||||
}
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use App\Models\Package;
|
||||
use App\Models\Tenant;
|
||||
use App\Services\Paddle\PaddleCheckoutService;
|
||||
use App\Services\Paddle\PaddleClient;
|
||||
use App\Services\Paddle\PaddleCustomerService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Mockery;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PaddleCheckoutServiceTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_create_checkout_sends_custom_data_payload(): void
|
||||
{
|
||||
$tenant = Tenant::factory()->create([
|
||||
'contact_email' => 'buyer@example.com',
|
||||
]);
|
||||
|
||||
$package = Package::factory()->create([
|
||||
'paddle_price_id' => 'pri_123',
|
||||
]);
|
||||
|
||||
$client = Mockery::mock(PaddleClient::class);
|
||||
$customers = Mockery::mock(PaddleCustomerService::class);
|
||||
|
||||
$customers->shouldReceive('ensureCustomerId')
|
||||
->once()
|
||||
->with($tenant)
|
||||
->andReturn('ctm_123');
|
||||
|
||||
$client->shouldReceive('post')
|
||||
->once()
|
||||
->with('/transactions', Mockery::on(function (array $payload) use ($tenant, $package) {
|
||||
return $payload['items'][0]['price_id'] === 'pri_123'
|
||||
&& $payload['customer_id'] === 'ctm_123'
|
||||
&& ($payload['custom_data']['tenant_id'] ?? null) === (string) $tenant->id
|
||||
&& ($payload['custom_data']['package_id'] ?? null) === (string) $package->id
|
||||
&& ($payload['custom_data']['source'] ?? null) === 'test'
|
||||
&& ($payload['custom_data']['success_url'] ?? null) === 'https://example.test/success'
|
||||
&& ($payload['custom_data']['cancel_url'] ?? null) === 'https://example.test/cancel'
|
||||
&& ! isset($payload['metadata'])
|
||||
&& ! isset($payload['success_url'])
|
||||
&& ! isset($payload['cancel_url'])
|
||||
&& ! isset($payload['customer_email']);
|
||||
}))
|
||||
->andReturn(['data' => ['checkout' => ['url' => 'https://paddle.test/checkout/123'], 'id' => 'txn_123']]);
|
||||
|
||||
$this->app->instance(PaddleClient::class, $client);
|
||||
$this->app->instance(PaddleCustomerService::class, $customers);
|
||||
|
||||
$service = $this->app->make(PaddleCheckoutService::class);
|
||||
|
||||
$checkout = $service->createCheckout($tenant, $package, [
|
||||
'success_url' => 'https://example.test/success',
|
||||
'return_url' => 'https://example.test/cancel',
|
||||
'metadata' => ['source' => 'test'],
|
||||
]);
|
||||
|
||||
$this->assertSame('https://paddle.test/checkout/123', $checkout['checkout_url']);
|
||||
$this->assertSame('txn_123', $checkout['id']);
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use Tests\TestCase;
|
||||
|
||||
class PaddleSyncLoggingConfigTest extends TestCase
|
||||
{
|
||||
public function test_paddle_sync_channel_is_configured(): void
|
||||
{
|
||||
$channel = config('logging.channels.paddle-sync');
|
||||
|
||||
$this->assertIsArray($channel);
|
||||
$this->assertSame('stack', $channel['driver'] ?? null);
|
||||
$this->assertNotEmpty($channel['channels'] ?? null);
|
||||
}
|
||||
|
||||
public function test_paddle_sync_file_channel_is_configured(): void
|
||||
{
|
||||
$channel = config('logging.channels.paddle-sync-file');
|
||||
|
||||
$this->assertIsArray($channel);
|
||||
$this->assertSame('daily', $channel['driver'] ?? null);
|
||||
$this->assertSame('paddle-sync.log', basename((string) ($channel['path'] ?? '')));
|
||||
}
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use App\Services\Paddle\PaddleClient;
|
||||
use App\Services\Paddle\PaddleTransactionService;
|
||||
use Mockery;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PaddleTransactionServiceTest extends TestCase
|
||||
{
|
||||
public function test_list_for_customer_uses_expected_order_by_format(): void
|
||||
{
|
||||
$client = Mockery::mock(PaddleClient::class);
|
||||
$client->shouldReceive('get')
|
||||
->once()
|
||||
->with('/transactions', Mockery::on(function (array $payload) {
|
||||
return $payload['customer_id'] === 'ctm_123'
|
||||
&& $payload['order_by'] === 'created_at[desc]';
|
||||
}))
|
||||
->andReturn(['data' => [], 'meta' => ['pagination' => []]]);
|
||||
|
||||
$this->app->instance(PaddleClient::class, $client);
|
||||
|
||||
$service = $this->app->make(PaddleTransactionService::class);
|
||||
$service->listForCustomer('ctm_123');
|
||||
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
|
||||
public function test_find_by_checkout_id_uses_expected_order_by_format(): void
|
||||
{
|
||||
$client = Mockery::mock(PaddleClient::class);
|
||||
$client->shouldReceive('get')
|
||||
->once()
|
||||
->with('/transactions', Mockery::on(function (array $payload) {
|
||||
return $payload['checkout_id'] === 'chk_123'
|
||||
&& $payload['order_by'] === 'created_at[desc]';
|
||||
}))
|
||||
->andReturn(['data' => []]);
|
||||
|
||||
$this->app->instance(PaddleClient::class, $client);
|
||||
|
||||
$service = $this->app->make(PaddleTransactionService::class);
|
||||
$this->assertNull($service->findByCheckoutId('chk_123'));
|
||||
}
|
||||
|
||||
public function test_find_by_custom_data_uses_expected_order_by_format(): void
|
||||
{
|
||||
$client = Mockery::mock(PaddleClient::class);
|
||||
$client->shouldReceive('get')
|
||||
->once()
|
||||
->with('/transactions', Mockery::on(function (array $payload) {
|
||||
return $payload['order_by'] === 'created_at[desc]'
|
||||
&& $payload['per_page'] === 20;
|
||||
}))
|
||||
->andReturn(['data' => []]);
|
||||
|
||||
$this->app->instance(PaddleClient::class, $client);
|
||||
|
||||
$service = $this->app->make(PaddleTransactionService::class);
|
||||
$this->assertNull($service->findByCustomData([
|
||||
'checkout_session_id' => 'sess_123',
|
||||
]));
|
||||
}
|
||||
}
|
||||
@@ -33,7 +33,7 @@ class SuperAdminNavigationGroupsTest extends TestCase
|
||||
\App\Filament\Resources\PackageResource::class => 'admin.nav.commercial',
|
||||
\App\Filament\Resources\PhotoboothSettings\PhotoboothSettingResource::class => 'admin.nav.storage',
|
||||
\App\Filament\Resources\PurchaseResource::class => 'admin.nav.billing',
|
||||
\App\Filament\Clusters\DailyOps\Resources\TenantPaddleHealths\TenantPaddleHealthResource::class => 'admin.nav.billing',
|
||||
\App\Filament\Clusters\DailyOps\Resources\TenantLemonSqueezyHealths\TenantLemonSqueezyHealthResource::class => 'admin.nav.billing',
|
||||
\App\Filament\Resources\PurchaseHistoryResource::class => 'admin.nav.commercial',
|
||||
\App\Filament\Resources\EventPurchaseResource::class => 'admin.nav.commercial',
|
||||
\App\Filament\Resources\TenantPackageResource::class => 'admin.nav.commercial',
|
||||
@@ -57,7 +57,7 @@ class SuperAdminNavigationGroupsTest extends TestCase
|
||||
\App\Filament\Resources\PhotoResource::class => DailyOpsCluster::class,
|
||||
\App\Filament\Resources\TenantResource::class => DailyOpsCluster::class,
|
||||
\App\Filament\Resources\PurchaseResource::class => DailyOpsCluster::class,
|
||||
\App\Filament\Clusters\DailyOps\Resources\TenantPaddleHealths\TenantPaddleHealthResource::class => DailyOpsCluster::class,
|
||||
\App\Filament\Clusters\DailyOps\Resources\TenantLemonSqueezyHealths\TenantLemonSqueezyHealthResource::class => DailyOpsCluster::class,
|
||||
\App\Filament\Resources\TenantFeedbackResource::class => DailyOpsCluster::class,
|
||||
\App\Filament\SuperAdmin\Pages\OpsHealthDashboard::class => DailyOpsCluster::class,
|
||||
\App\Filament\SuperAdmin\Pages\IntegrationsHealthDashboard::class => DailyOpsCluster::class,
|
||||
|
||||
@@ -6,7 +6,7 @@ import { test, expectFixture as expect } from '../helpers/test-fixtures';
|
||||
* This suite is currently skipped until we have stable seed data and
|
||||
* authentication helpers for Playwright. Once those are in place we can
|
||||
* remove the skip and let the flow exercise the welcome -> packages -> summary
|
||||
* steps with mocked Paddle APIs.
|
||||
* steps with mocked Lemon Squeezy APIs.
|
||||
*/
|
||||
test.describe('Tenant Onboarding Welcome Flow', () => {
|
||||
test('redirects unauthenticated users to login', async ({ page }) => {
|
||||
@@ -47,8 +47,8 @@ test.describe('Tenant Onboarding Welcome Flow', () => {
|
||||
await expect(page).toHaveURL(/\/event-admin\/mobile\/welcome\/summary/);
|
||||
await expect(page.getByRole('heading', { name: /Bestellübersicht/i })).toBeVisible();
|
||||
|
||||
// Validate Paddle payment section.
|
||||
await expect(page.getByRole('heading', { name: /^Paddle$/i })).toBeVisible();
|
||||
// Validate billing CTA is present on summary.
|
||||
await expect(page.getByRole('button', { name: /Go to billing|Zum Billing|Zu Billing/i })).toBeVisible();
|
||||
|
||||
// Continue to the setup step without completing a purchase.
|
||||
await page.getByRole('button', { name: /Weiter zum Setup/i }).click();
|
||||
|
||||
@@ -41,7 +41,7 @@ export type TestingApiFixtures = {
|
||||
getTestMailbox: () => Promise<MailboxEntry[]>;
|
||||
seedTestCoupons: (definitions?: CouponSeedDefinition[]) => Promise<Array<{ id: number; code: string }>>;
|
||||
getLatestCheckoutSession: (filters?: { email?: string; tenantId?: number; status?: string }) => Promise<CheckoutSessionSummary | null>;
|
||||
simulatePaddleCompletion: (sessionId: string, overrides?: Partial<PaddleSimulationOverrides>) => Promise<void>;
|
||||
simulateLemonSqueezyCompletion: (sessionId: string, overrides?: Partial<LemonSqueezySimulationOverrides>) => Promise<void>;
|
||||
fetchJoinToken: (params: { eventId?: number; slug?: string; ensureActive?: boolean }) => Promise<JoinTokenPayload>;
|
||||
};
|
||||
|
||||
@@ -58,9 +58,9 @@ export type CheckoutSessionSummary = {
|
||||
created_at: string | null;
|
||||
};
|
||||
|
||||
export type PaddleSimulationOverrides = {
|
||||
export type LemonSqueezySimulationOverrides = {
|
||||
event_type: string;
|
||||
transaction_id?: string;
|
||||
order_id?: string;
|
||||
status?: string;
|
||||
checkout_id?: string;
|
||||
metadata?: Record<string, unknown>;
|
||||
@@ -156,10 +156,10 @@ export const test = base.extend<TenantAdminFixtures & TestingApiFixtures>({
|
||||
});
|
||||
},
|
||||
|
||||
simulatePaddleCompletion: async ({ request }, use) => {
|
||||
await use(async (sessionId: string, overrides?: Partial<PaddleSimulationOverrides>) => {
|
||||
simulateLemonSqueezyCompletion: async ({ request }, use) => {
|
||||
await use(async (sessionId: string, overrides?: Partial<LemonSqueezySimulationOverrides>) => {
|
||||
await expectApiSuccess(
|
||||
request.post(`/api/_testing/checkout/sessions/${sessionId}/simulate-paddle`, {
|
||||
request.post(`/api/_testing/checkout/sessions/${sessionId}/simulate-lemonsqueezy`, {
|
||||
data: overrides,
|
||||
})
|
||||
);
|
||||
|
||||
@@ -5,29 +5,29 @@ const demoTenantCredentials = {
|
||||
password: process.env.E2E_DEMO_TENANT_PASSWORD ?? 'Demo1234!',
|
||||
};
|
||||
|
||||
test.describe('Checkout Payment Step – Paddle flow', () => {
|
||||
test('opens Paddle checkout and shows success notice', async ({ page }) => {
|
||||
await page.route('**/paddle/create-checkout', async (route) => {
|
||||
test.describe('Checkout Payment Step – Lemon Squeezy flow', () => {
|
||||
test('opens Lemon Squeezy checkout and shows success notice', async ({ page }) => {
|
||||
await page.route('**/lemonsqueezy/create-checkout', async (route) => {
|
||||
await route.fulfill({
|
||||
status: 200,
|
||||
contentType: 'application/json',
|
||||
body: JSON.stringify({
|
||||
checkout_url: 'https://paddle.test/checkout/success',
|
||||
checkout_url: 'https://fotospiel.lemonsqueezy.com/checkout/success',
|
||||
}),
|
||||
});
|
||||
});
|
||||
|
||||
await page.route('https://cdn.paddle.com/paddle/v2/paddle.js', async (route) => {
|
||||
await page.route('https://app.lemonsqueezy.com/js/lemon.js', async (route) => {
|
||||
await route.fulfill({
|
||||
status: 200,
|
||||
contentType: 'application/javascript',
|
||||
body: `
|
||||
window.Paddle = {
|
||||
Environment: { set: function(env) { window.__paddleEnv = env; } },
|
||||
Initialize: function(opts) { window.__paddleInit = opts; },
|
||||
Checkout: {
|
||||
open: function(config) {
|
||||
window.__paddleOpenConfig = config;
|
||||
window.createLemonSqueezy = function() {};
|
||||
window.LemonSqueezy = {
|
||||
Setup: function(options) { window.__lemonEventHandler = options?.eventHandler || null; },
|
||||
Url: {
|
||||
Open: function(url) {
|
||||
window.__lemonOpenedUrl = url;
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -46,7 +46,7 @@ test.describe('Checkout Payment Step – Paddle flow', () => {
|
||||
};
|
||||
});
|
||||
|
||||
await page.getByRole('button', { name: /Continue with Paddle|Weiter mit Paddle/ }).first().click();
|
||||
await page.getByRole('button', { name: /Continue with Lemon Squeezy|Weiter mit Lemon Squeezy/ }).first().click();
|
||||
|
||||
await expect(
|
||||
page.locator(
|
||||
@@ -57,7 +57,7 @@ test.describe('Checkout Payment Step – Paddle flow', () => {
|
||||
let mode: 'inline' | 'hosted' | null = null;
|
||||
for (let i = 0; i < 8; i++) {
|
||||
const state = await page.evaluate(() => ({
|
||||
inline: Boolean(window.__paddleOpenConfig),
|
||||
inline: Boolean(window.__lemonOpenedUrl),
|
||||
opened: window.__openedUrls?.length ?? 0,
|
||||
}));
|
||||
|
||||
@@ -77,19 +77,19 @@ test.describe('Checkout Payment Step – Paddle flow', () => {
|
||||
expect(mode).not.toBeNull();
|
||||
|
||||
if (mode === 'inline') {
|
||||
const inlineConfig = await page.evaluate(() => window.__paddleOpenConfig ?? null);
|
||||
expect(inlineConfig).not.toBeNull();
|
||||
const inlineUrl = await page.evaluate(() => window.__lemonOpenedUrl ?? null);
|
||||
expect(inlineUrl).not.toBeNull();
|
||||
}
|
||||
|
||||
if (mode === 'hosted') {
|
||||
await expect.poll(async () => {
|
||||
return page.evaluate(() => window.__openedUrls?.[0]?.url ?? null);
|
||||
}).toContain('paddle');
|
||||
}).toContain('lemonsqueezy');
|
||||
}
|
||||
});
|
||||
|
||||
test('shows error state when Paddle checkout creation fails', async ({ page }) => {
|
||||
await page.route('**/paddle/create-checkout', async (route) => {
|
||||
test('shows error state when Lemon Squeezy checkout creation fails', async ({ page }) => {
|
||||
await page.route('**/lemonsqueezy/create-checkout', async (route) => {
|
||||
await route.fulfill({
|
||||
status: 500,
|
||||
contentType: 'application/json',
|
||||
@@ -97,17 +97,17 @@ test.describe('Checkout Payment Step – Paddle flow', () => {
|
||||
});
|
||||
});
|
||||
|
||||
await page.route('https://cdn.paddle.com/paddle/v2/paddle.js', async (route) => {
|
||||
await page.route('https://app.lemonsqueezy.com/js/lemon.js', async (route) => {
|
||||
await route.fulfill({
|
||||
status: 200,
|
||||
contentType: 'application/javascript',
|
||||
body: `
|
||||
window.Paddle = {
|
||||
Environment: { set: function(env) { window.__paddleEnv = env; } },
|
||||
Initialize: function(opts) { window.__paddleInit = opts; },
|
||||
Checkout: {
|
||||
open: function() {
|
||||
throw new Error('forced paddle failure');
|
||||
window.createLemonSqueezy = function() {};
|
||||
window.LemonSqueezy = {
|
||||
Setup: function(options) { window.__lemonEventHandler = options?.eventHandler || null; },
|
||||
Url: {
|
||||
Open: function() {
|
||||
throw new Error('forced Lemon Squeezy failure');
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -118,10 +118,10 @@ test.describe('Checkout Payment Step – Paddle flow', () => {
|
||||
await openCheckoutPaymentStep(page, demoTenantCredentials);
|
||||
await acceptCheckoutTerms(page);
|
||||
|
||||
await page.getByRole('button', { name: /Continue with Paddle|Weiter mit Paddle/ }).first().click();
|
||||
await page.getByRole('button', { name: /Continue with Lemon Squeezy|Weiter mit Lemon Squeezy/ }).first().click();
|
||||
|
||||
await expect(
|
||||
page.locator('text=/Paddle-Checkout konnte nicht gestartet werden|Paddle checkout could not be started/i')
|
||||
page.locator('text=/Lemon Squeezy-Checkout konnte nicht gestartet werden|Lemon Squeezy checkout could not be started/i')
|
||||
).toBeVisible();
|
||||
});
|
||||
});
|
||||
@@ -169,8 +169,7 @@ async function acceptCheckoutTerms(page: import('@playwright/test').Page) {
|
||||
declare global {
|
||||
interface Window {
|
||||
__openedUrls?: Array<{ url: string; target?: string | null; features?: string | null }>;
|
||||
__paddleOpenConfig?: { url?: string; items?: Array<{ priceId: string; quantity: number }>; settings?: { displayMode?: string } };
|
||||
__paddleEnv?: string;
|
||||
__paddleInit?: Record<string, unknown>;
|
||||
__lemonOpenedUrl?: string | null;
|
||||
__lemonEventHandler?: ((event: { event: string; data?: unknown }) => void) | null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { expect, test } from '@playwright/test';
|
||||
|
||||
const shouldRun = process.env.E2E_PADDLE_SANDBOX === '1';
|
||||
const shouldRun = process.env.E2E_LEMONSQUEEZY_SANDBOX === '1' || process.env.E2E_PADDLE_SANDBOX === '1';
|
||||
|
||||
test.describe('Paddle sandbox checkout (staging)', () => {
|
||||
test.skip(!shouldRun, 'Set E2E_PADDLE_SANDBOX=1 to run live sandbox checkout on staging.');
|
||||
test.describe('Lemon Squeezy sandbox checkout (staging)', () => {
|
||||
test.skip(!shouldRun, 'Set E2E_LEMONSQUEEZY_SANDBOX=1 to run live sandbox checkout on staging.');
|
||||
|
||||
test('creates Paddle checkout session from packages page', async ({ page }) => {
|
||||
test('creates Lemon Squeezy checkout session from packages page', async ({ page }) => {
|
||||
const base = process.env.E2E_BASE_URL ?? 'https://test-y0k0.fotospiel.app';
|
||||
|
||||
await page.goto(`${base}/packages`);
|
||||
@@ -23,16 +23,16 @@ test.describe('Paddle sandbox checkout (staging)', () => {
|
||||
}
|
||||
|
||||
const [requestPromise] = await Promise.all([
|
||||
page.waitForRequest('**/paddle/create-checkout'),
|
||||
page.waitForRequest('**/lemonsqueezy/create-checkout'),
|
||||
checkoutButtons.first().click(),
|
||||
]);
|
||||
|
||||
const checkoutRequest = await requestPromise.response();
|
||||
expect(checkoutRequest, 'Expected paddle/create-checkout request to resolve').toBeTruthy();
|
||||
expect(checkoutRequest, 'Expected lemonsqueezy/create-checkout request to resolve').toBeTruthy();
|
||||
expect(checkoutRequest!.status()).toBeLessThan(400);
|
||||
|
||||
const body = await checkoutRequest!.json();
|
||||
const checkoutUrl = body.checkout_url ?? body.url ?? '';
|
||||
expect(checkoutUrl).toContain('paddle');
|
||||
expect(checkoutUrl).toContain('lemonsqueezy');
|
||||
});
|
||||
});
|
||||
@@ -3,18 +3,18 @@ import fs from 'node:fs/promises';
|
||||
|
||||
import { dismissConsentBanner, expectFixture as expect, test } from '../helpers/test-fixtures';
|
||||
|
||||
const shouldRun = process.env.E2E_PADDLE_SANDBOX === '1';
|
||||
const shouldRun = process.env.E2E_LEMONSQUEEZY_SANDBOX === '1' || process.env.E2E_PADDLE_SANDBOX === '1';
|
||||
const baseUrl = process.env.E2E_BASE_URL ?? 'https://test-y0k0.fotospiel.app';
|
||||
const locale = process.env.E2E_LOCALE ?? 'de';
|
||||
const checkoutSlug = locale === 'en' ? 'checkout' : 'bestellen';
|
||||
const tenantEmail = buildTenantEmail();
|
||||
const tenantPassword = process.env.E2E_TENANT_PASSWORD ?? null;
|
||||
const sandboxCard = {
|
||||
number: process.env.E2E_PADDLE_CARD_NUMBER ?? '4242 4242 4242 4242',
|
||||
expiry: process.env.E2E_PADDLE_CARD_EXPIRY ?? '12/34',
|
||||
cvc: process.env.E2E_PADDLE_CARD_CVC ?? '123',
|
||||
name: process.env.E2E_PADDLE_CARD_NAME ?? 'Playwright Tester',
|
||||
postal: process.env.E2E_PADDLE_CARD_POSTAL ?? '10115',
|
||||
number: process.env.E2E_LEMONSQUEEZY_CARD_NUMBER ?? process.env.E2E_PADDLE_CARD_NUMBER ?? '4242 4242 4242 4242',
|
||||
expiry: process.env.E2E_LEMONSQUEEZY_CARD_EXPIRY ?? process.env.E2E_PADDLE_CARD_EXPIRY ?? '12/34',
|
||||
cvc: process.env.E2E_LEMONSQUEEZY_CARD_CVC ?? process.env.E2E_PADDLE_CARD_CVC ?? '123',
|
||||
name: process.env.E2E_LEMONSQUEEZY_CARD_NAME ?? process.env.E2E_PADDLE_CARD_NAME ?? 'Playwright Tester',
|
||||
postal: process.env.E2E_LEMONSQUEEZY_CARD_POSTAL ?? process.env.E2E_PADDLE_CARD_POSTAL ?? '10115',
|
||||
};
|
||||
|
||||
test.use({
|
||||
@@ -27,16 +27,16 @@ test.use({
|
||||
},
|
||||
});
|
||||
|
||||
test.describe('Paddle sandbox full flow (staging)', () => {
|
||||
test.skip(!shouldRun, 'Set E2E_PADDLE_SANDBOX=1 to run live sandbox checkout on staging.');
|
||||
test.describe('Lemon Squeezy sandbox full flow (staging)', () => {
|
||||
test.skip(!shouldRun, 'Set E2E_LEMONSQUEEZY_SANDBOX=1 to run live sandbox checkout on staging.');
|
||||
test.skip(!tenantEmail || !tenantPassword, 'Set E2E_TENANT_EMAIL and E2E_TENANT_PASSWORD for sandbox flow.');
|
||||
|
||||
test('register, pay via Paddle sandbox, and login to event admin', async ({ page, request }, testInfo) => {
|
||||
const paddleNetworkLog: string[] = [];
|
||||
test('register, pay via Lemon Squeezy sandbox, and login to event admin', async ({ page, request }, testInfo) => {
|
||||
const lemonsqueezyNetworkLog: string[] = [];
|
||||
|
||||
page.on('response', async (response) => {
|
||||
const url = response.url();
|
||||
if (!/paddle/i.test(url)) {
|
||||
if (!/lemonsqueezy|lemon/i.test(url)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -52,9 +52,9 @@ test.describe('Paddle sandbox full flow (staging)', () => {
|
||||
}
|
||||
|
||||
const entry = [`[${status}] ${url}`, bodySnippet].filter(Boolean).join('\n');
|
||||
paddleNetworkLog.push(entry);
|
||||
if (paddleNetworkLog.length > 40) {
|
||||
paddleNetworkLog.shift();
|
||||
lemonsqueezyNetworkLog.push(entry);
|
||||
if (lemonsqueezyNetworkLog.length > 40) {
|
||||
lemonsqueezyNetworkLog.shift();
|
||||
}
|
||||
});
|
||||
|
||||
@@ -81,11 +81,11 @@ test.describe('Paddle sandbox full flow (staging)', () => {
|
||||
await expect(termsCheckbox).toBeVisible();
|
||||
await termsCheckbox.click();
|
||||
|
||||
const checkoutCta = page.getByRole('button', { name: /Weiter mit Paddle|Continue with Paddle/i }).first();
|
||||
const checkoutCta = page.getByRole('button', { name: /Weiter mit Lemon Squeezy|Continue with Lemon Squeezy/i }).first();
|
||||
await expect(checkoutCta).toBeVisible({ timeout: 20000 });
|
||||
|
||||
const [apiResponse] = await Promise.all([
|
||||
page.waitForResponse((resp) => resp.url().includes('/paddle/create-checkout') && resp.status() < 500),
|
||||
page.waitForResponse((resp) => resp.url().includes('/lemonsqueezy/create-checkout') && resp.status() < 500),
|
||||
checkoutCta.click(),
|
||||
]);
|
||||
|
||||
@@ -101,23 +101,23 @@ test.describe('Paddle sandbox full flow (staging)', () => {
|
||||
const checkoutUrl = extractCheckoutUrl(checkoutPayload, rawBody);
|
||||
|
||||
if (!inlineMode) {
|
||||
expect(checkoutUrl).toContain('paddle');
|
||||
expect(checkoutUrl).toContain('lemonsqueezy');
|
||||
}
|
||||
|
||||
// Navigate to Paddle hosted checkout and complete payment.
|
||||
// Navigate to Lemon Squeezy hosted checkout and complete payment.
|
||||
if (inlineMode) {
|
||||
await expect(
|
||||
page.getByText(/Checkout geöffnet|Checkout opened|Paddle-Checkout/i).first()
|
||||
page.getByText(/Lemon Squeezy|Checkout geöffnet|Checkout opened/i).first()
|
||||
).toBeVisible({ timeout: 20_000 });
|
||||
await waitForPaddleCardInputs(page, ['input[autocomplete="cc-number"]', 'input[name="cardnumber"]', 'input[name="card_number"]']);
|
||||
await waitForLemonCardInputs(page, ['input[autocomplete="cc-number"]', 'input[name="cardnumber"]', 'input[name="card_number"]']);
|
||||
} else if (checkoutUrl) {
|
||||
await page.goto(checkoutUrl);
|
||||
await expect(page).toHaveURL(/paddle/);
|
||||
await expect(page).toHaveURL(/lemonsqueezy/);
|
||||
} else {
|
||||
throw new Error(`Missing Paddle checkout URL. Response: ${rawBody}`);
|
||||
throw new Error(`Missing Lemon Squeezy checkout URL. Response: ${rawBody}`);
|
||||
}
|
||||
|
||||
await completeHostedPaddleCheckout(page, sandboxCard);
|
||||
await completeHostedLemonSqueezyCheckout(page, sandboxCard);
|
||||
|
||||
await expect
|
||||
.poll(
|
||||
@@ -146,10 +146,10 @@ test.describe('Paddle sandbox full flow (staging)', () => {
|
||||
await expect(page).toHaveURL(/\/event-admin\/mobile\/(dashboard|welcome)/i, { timeout: 30_000 });
|
||||
await expect(page.getByText(/Dashboard|Willkommen/i)).toBeVisible();
|
||||
} finally {
|
||||
if (paddleNetworkLog.length > 0) {
|
||||
const logPath = testInfo.outputPath('paddle-network-log.txt');
|
||||
await fs.writeFile(logPath, paddleNetworkLog.join('\n\n'), 'utf8');
|
||||
await testInfo.attach('paddle-network-log', {
|
||||
if (lemonsqueezyNetworkLog.length > 0) {
|
||||
const logPath = testInfo.outputPath('lemonsqueezy-network-log.txt');
|
||||
await fs.writeFile(logPath, lemonsqueezyNetworkLog.join('\n\n'), 'utf8');
|
||||
await testInfo.attach('lemonsqueezy-network-log', {
|
||||
path: logPath,
|
||||
contentType: 'text/plain',
|
||||
});
|
||||
@@ -232,7 +232,7 @@ async function proceedToAccountStep(page: Page, timeoutMs = 30_000): Promise<voi
|
||||
throw new Error('Account step did not load in time.');
|
||||
}
|
||||
|
||||
async function completeHostedPaddleCheckout(
|
||||
async function completeHostedLemonSqueezyCheckout(
|
||||
page: Page,
|
||||
card: { number: string; expiry: string; cvc: string; name: string; postal: string }
|
||||
): Promise<void> {
|
||||
@@ -279,7 +279,7 @@ async function completeHostedPaddleCheckout(
|
||||
await payButton.click();
|
||||
}
|
||||
|
||||
async function waitForPaddleCardInputs(page: Page, selectors: string[], timeoutMs = 30_000): Promise<void> {
|
||||
async function waitForLemonCardInputs(page: Page, selectors: string[], timeoutMs = 30_000): Promise<void> {
|
||||
const deadline = Date.now() + timeoutMs;
|
||||
|
||||
while (Date.now() < deadline) {
|
||||
@@ -288,13 +288,13 @@ async function waitForPaddleCardInputs(page: Page, selectors: string[], timeoutM
|
||||
}
|
||||
|
||||
if (await hasAnyText(page, /Something went wrong|try again later/i)) {
|
||||
throw new Error('Paddle inline checkout returned an error in the iframe.');
|
||||
throw new Error('Lemon Squeezy inline checkout returned an error in the iframe.');
|
||||
}
|
||||
|
||||
await page.waitForTimeout(500);
|
||||
}
|
||||
|
||||
throw new Error('Paddle card inputs did not appear in time.');
|
||||
throw new Error('Lemon Squeezy card inputs did not appear in time.');
|
||||
}
|
||||
|
||||
async function hasAnySelector(page: Page, selectors: string[]): Promise<boolean> {
|
||||
@@ -359,7 +359,7 @@ function extractCheckoutUrl(payload: Record<string, unknown> | null, rawBody: st
|
||||
}
|
||||
|
||||
function buildTenantEmail(): string | null {
|
||||
const rawEmail = process.env.E2E_TENANT_EMAIL ?? process.env.E2E_PADDLE_EMAIL ?? null;
|
||||
const rawEmail = process.env.E2E_TENANT_EMAIL ?? process.env.E2E_LEMONSQUEEZY_EMAIL ?? process.env.E2E_PADDLE_EMAIL ?? null;
|
||||
if (!rawEmail) {
|
||||
return null;
|
||||
}
|
||||
@@ -2,14 +2,14 @@ import { test, expectFixture as expect } from '../helpers/test-fixtures';
|
||||
|
||||
const shouldRun = process.env.E2E_TESTING_API === '1';
|
||||
|
||||
test.describe('Classic package checkout with Paddle completion', () => {
|
||||
test.describe('Classic package checkout with Lemon Squeezy completion', () => {
|
||||
test.skip(!shouldRun, 'Set E2E_TESTING_API=1 to enable checkout tests that use /api/_testing endpoints.');
|
||||
test('registers, applies coupon, and reaches confirmation', async ({
|
||||
page,
|
||||
clearTestMailbox,
|
||||
seedTestCoupons,
|
||||
getLatestCheckoutSession,
|
||||
simulatePaddleCompletion,
|
||||
simulateLemonSqueezyCompletion,
|
||||
getTestMailbox,
|
||||
}) => {
|
||||
await clearTestMailbox();
|
||||
@@ -27,23 +27,20 @@ test.describe('Classic package checkout with Paddle completion', () => {
|
||||
};
|
||||
});
|
||||
|
||||
await page.route('https://cdn.paddle.com/paddle/v2/paddle.js', async (route) => {
|
||||
await page.route('https://app.lemonsqueezy.com/js/lemon.js', async (route) => {
|
||||
await route.fulfill({
|
||||
status: 200,
|
||||
contentType: 'application/javascript',
|
||||
body: `
|
||||
window.__paddleEventCallback = null;
|
||||
window.__paddleInitOptions = null;
|
||||
window.__paddleCheckoutConfig = null;
|
||||
window.Paddle = {
|
||||
Environment: { set() {} },
|
||||
Initialize(options) {
|
||||
window.__paddleInitOptions = options;
|
||||
window.__paddleEventCallback = options?.eventCallback || null;
|
||||
window.__lemonEventHandler = null;
|
||||
window.__lemonOpenedUrl = null;
|
||||
window.LemonSqueezy = {
|
||||
Setup(options) {
|
||||
window.__lemonEventHandler = options?.eventHandler || null;
|
||||
},
|
||||
Checkout: {
|
||||
open(config) {
|
||||
window.__paddleCheckoutConfig = config;
|
||||
Url: {
|
||||
Open(url) {
|
||||
window.__lemonOpenedUrl = url;
|
||||
},
|
||||
},
|
||||
};
|
||||
@@ -51,14 +48,14 @@ test.describe('Classic package checkout with Paddle completion', () => {
|
||||
});
|
||||
});
|
||||
|
||||
let paddleRequestPayload: Record<string, unknown> | null = null;
|
||||
await page.route('**/paddle/create-checkout', async (route) => {
|
||||
paddleRequestPayload = route.request().postDataJSON() as Record<string, unknown>;
|
||||
let lemonsqueezyRequestPayload: Record<string, unknown> | null = null;
|
||||
await page.route('**/lemonsqueezy/create-checkout', async (route) => {
|
||||
lemonsqueezyRequestPayload = route.request().postDataJSON() as Record<string, unknown>;
|
||||
await route.fulfill({
|
||||
status: 200,
|
||||
contentType: 'application/json',
|
||||
body: JSON.stringify({
|
||||
checkout_url: 'https://sandbox.paddle.test/checkout/abc123',
|
||||
checkout_url: 'https://fotospiel.lemonsqueezy.com/checkout/abc123',
|
||||
}),
|
||||
});
|
||||
});
|
||||
@@ -116,12 +113,12 @@ test.describe('Classic package checkout with Paddle completion', () => {
|
||||
await expect(termsCheckbox).toBeVisible();
|
||||
await termsCheckbox.click();
|
||||
|
||||
await page.getByRole('button', { name: /Weiter mit Paddle|Continue with Paddle/i }).first().click();
|
||||
await page.getByRole('button', { name: /Weiter mit Lemon Squeezy|Continue with Lemon Squeezy/i }).first().click();
|
||||
|
||||
let checkoutMode: 'inline' | 'hosted' | null = null;
|
||||
for (let i = 0; i < 8; i++) {
|
||||
const state = await page.evaluate(() => ({
|
||||
inline: Boolean(window.__paddleCheckoutConfig),
|
||||
inline: Boolean(window.__lemonOpenedUrl),
|
||||
opened: window.__openedWindows?.length ?? 0,
|
||||
}));
|
||||
|
||||
@@ -143,11 +140,14 @@ test.describe('Classic package checkout with Paddle completion', () => {
|
||||
if (checkoutMode === 'hosted') {
|
||||
await expect.poll(async () => {
|
||||
return page.evaluate(() => window.__openedWindows?.[0]?.[0] ?? null);
|
||||
}).toContain('https://sandbox.paddle.test/checkout/abc123');
|
||||
}).toContain('https://fotospiel.lemonsqueezy.com/checkout/abc123');
|
||||
}
|
||||
|
||||
await page.evaluate(() => {
|
||||
window.__paddleEventCallback?.({ name: 'checkout.completed' });
|
||||
window.__lemonEventHandler?.({
|
||||
event: 'Checkout.Success',
|
||||
data: { id: 'ord_test', attributes: { checkout_id: 'chk_123' } },
|
||||
});
|
||||
});
|
||||
|
||||
let session = null;
|
||||
@@ -160,7 +160,7 @@ test.describe('Classic package checkout with Paddle completion', () => {
|
||||
}
|
||||
|
||||
if (session) {
|
||||
await simulatePaddleCompletion(session.id);
|
||||
await simulateLemonSqueezyCompletion(session.id);
|
||||
|
||||
for (let i = 0; i < 6; i++) {
|
||||
const refreshed = await getLatestCheckoutSession({ email });
|
||||
@@ -179,8 +179,8 @@ test.describe('Classic package checkout with Paddle completion', () => {
|
||||
page.getByRole('button', { name: /Zum Admin-Bereich|To Admin Area/i })
|
||||
).toBeVisible();
|
||||
|
||||
if (paddleRequestPayload) {
|
||||
expect(paddleRequestPayload['coupon_code']).toBe('PERCENT10');
|
||||
if (lemonsqueezyRequestPayload) {
|
||||
expect(lemonsqueezyRequestPayload['coupon_code']).toBe('PERCENT10');
|
||||
}
|
||||
|
||||
const messages = await getTestMailbox();
|
||||
@@ -191,8 +191,7 @@ test.describe('Classic package checkout with Paddle completion', () => {
|
||||
declare global {
|
||||
interface Window {
|
||||
__openedWindows?: unknown[];
|
||||
__paddleEventCallback?: ((event: { name: string }) => void) | null;
|
||||
__paddleInitOptions?: unknown;
|
||||
__paddleCheckoutConfig?: unknown;
|
||||
__lemonEventHandler?: ((event: { event: string; data?: unknown }) => void) | null;
|
||||
__lemonOpenedUrl?: string | null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user