create(); $user = User::factory()->for($tenant)->create(); $package = Package::factory()->create(); /** @var CheckoutSessionService $sessions */ $sessions = app(CheckoutSessionService::class); $session = $sessions->createOrResume($user, $package, [ 'tenant' => $tenant, ]); $sessions->markCompleted($session, now()); $this->actingAs($user); $response = $this->getJson(route('checkout.session.status', $session)); $response->assertOk() ->assertJsonPath('status', CheckoutSession::STATUS_COMPLETED); } public function test_user_cannot_fetch_other_users_checkout_session_status(): void { $tenant = Tenant::factory()->create(); $owner = User::factory()->for($tenant)->create(); $otherUser = User::factory()->create(); $package = Package::factory()->create(); /** @var CheckoutSessionService $sessions */ $sessions = app(CheckoutSessionService::class); $session = $sessions->createOrResume($owner, $package, [ 'tenant' => $tenant, ]); $this->actingAs($otherUser); $response = $this->getJson(route('checkout.session.status', $session)); $response->assertForbidden(); } public function test_session_status_recovers_completed_lemonsqueezy_order(): void { $tenant = Tenant::factory()->create(); $user = User::factory()->for($tenant)->create([ 'pending_purchase' => true, ]); $package = Package::factory()->create([ 'type' => 'endcustomer', 'price' => 99, ]); /** @var CheckoutSessionService $sessions */ $sessions = app(CheckoutSessionService::class); $session = $sessions->createOrResume($user, $package, [ 'tenant' => $tenant, ]); $sessions->selectProvider($session, CheckoutSession::PROVIDER_LEMONSQUEEZY); $session->forceFill([ 'provider_metadata' => [ 'lemonsqueezy_checkout_id' => 'chk_123', ], ])->save(); config()->set([ 'lemonsqueezy.api_key' => 'test-key', 'lemonsqueezy.base_url' => 'https://lemonsqueezy.test', ]); Http::fake([ 'https://lemonsqueezy.test/checkouts/chk_123' => Http::response([ 'data' => [ '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), ]); Mail::fake(); Notification::fake(); $this->actingAs($user); $response = $this->getJson(route('checkout.session.status', $session)); $response->assertOk() ->assertJsonPath('status', CheckoutSession::STATUS_COMPLETED); $this->assertDatabaseHas('checkout_sessions', [ 'id' => $session->id, 'status' => CheckoutSession::STATUS_COMPLETED, ]); $this->assertDatabaseHas('package_purchases', [ 'tenant_id' => $tenant->id, 'package_id' => $package->id, ]); } public function test_session_confirm_recovers_completed_lemonsqueezy_order(): void { $tenant = Tenant::factory()->create(); $user = User::factory()->for($tenant)->create([ 'pending_purchase' => true, ]); $package = Package::factory()->create([ 'type' => 'endcustomer', 'price' => 79, ]); /** @var CheckoutSessionService $sessions */ $sessions = app(CheckoutSessionService::class); $session = $sessions->createOrResume($user, $package, [ 'tenant' => $tenant, ]); $sessions->selectProvider($session, CheckoutSession::PROVIDER_LEMONSQUEEZY); config()->set([ 'lemonsqueezy.api_key' => 'test-key', 'lemonsqueezy.base_url' => 'https://lemonsqueezy.test', ]); Http::fake([ 'https://lemonsqueezy.test/orders/ord_987' => Http::response([ 'data' => [ 'id' => 'ord_987', 'attributes' => [ 'status' => 'paid', 'currency' => 'EUR', 'total' => 7900, ], ], ], 200), ]); Mail::fake(); Notification::fake(); $this->actingAs($user); $response = $this->postJson(route('checkout.session.confirm', $session), [ 'order_id' => 'ord_987', 'checkout_id' => 'chk_987', ]); $response->assertOk() ->assertJsonPath('status', CheckoutSession::STATUS_COMPLETED); $this->assertDatabaseHas('checkout_sessions', [ 'id' => $session->id, 'status' => CheckoutSession::STATUS_COMPLETED, ]); } }