Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions resources/views/course.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<x-layout title="The NativePHP Masterclass">
<div class="mx-auto max-w-5xl">
@if (session('error'))
<div class="mt-6 rounded-2xl border border-red-200 bg-red-50 p-4 text-center text-red-700 dark:border-red-800 dark:bg-red-950/50 dark:text-red-300">
{{ session('error') }}
</div>
@endif

{{-- Hero Section --}}
<section
class="mt-12 md:mt-20"
Expand Down
2 changes: 1 addition & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
58 changes: 58 additions & 0 deletions tests/Feature/CoursePageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tests\Feature;

use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
Expand Down Expand Up @@ -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']);
}
}