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,
|
||||
]);
|
||||
|
||||
Reference in New Issue
Block a user