diff --git a/routes/web.php b/routes/web.php index 0fe44d82..952e3726 100644 --- a/routes/web.php +++ b/routes/web.php @@ -106,7 +106,7 @@ ], 'quantity' => 1, ]], - 'success_url' => route('course').'?purchased=1', + 'success_url' => route('cart.success').'?session_id={CHECKOUT_SESSION_ID}', 'cancel_url' => route('course'), 'customer' => $user->stripe_id, 'metadata' => $metadata, diff --git a/tests/Feature/CoursePageTest.php b/tests/Feature/CoursePageTest.php index 8eb19c19..5d3c1ee7 100644 --- a/tests/Feature/CoursePageTest.php +++ b/tests/Feature/CoursePageTest.php @@ -2,6 +2,7 @@ namespace Tests\Feature; +use App\Models\User; use Illuminate\Foundation\Testing\RefreshDatabase; use PHPUnit\Framework\Attributes\Test; use Tests\TestCase; @@ -48,4 +49,61 @@ public function course_checkout_redirects_guests_to_login(): void ->post(route('course.checkout')) ->assertRedirect(route('customer.login')); } + + #[Test] + public function course_checkout_redirects_to_stripe_with_cart_success_url(): void + { + $user = User::factory()->create(['stripe_id' => 'cus_test123']); + + $stripeSessionUrl = 'https://checkout.stripe.com/test-session'; + $capturedParams = null; + + $mockCheckoutSessions = new class($stripeSessionUrl, $capturedParams) + { + public function __construct( + private string $url, + private &$capturedParams, + ) {} + + public function create(array $params): object + { + $this->capturedParams = $params; + + return (object) [ + 'id' => 'cs_test123', + 'url' => $this->url, + ]; + } + }; + + $mockCheckout = new \stdClass; + $mockCheckout->sessions = $mockCheckoutSessions; + + $mockCustomers = new class + { + public function retrieve(): \Stripe\Customer + { + return \Stripe\Customer::constructFrom([ + 'id' => 'cus_test123', + 'name' => 'Test User', + 'email' => 'test@example.com', + ]); + } + }; + + $mockStripeClient = $this->createMock(\Stripe\StripeClient::class); + $mockStripeClient->checkout = $mockCheckout; + $mockStripeClient->customers = $mockCustomers; + + $this->app->bind(\Stripe\StripeClient::class, fn () => $mockStripeClient); + + $this + ->actingAs($user) + ->post(route('course.checkout')) + ->assertRedirect($stripeSessionUrl); + + $this->assertNotNull($capturedParams, 'Stripe checkout session should have been created'); + $this->assertStringContainsString(route('cart.success'), $capturedParams['success_url']); + $this->assertStringContainsString('{CHECKOUT_SESSION_ID}', $capturedParams['success_url']); + } }