Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

connecteev's avatar

Laravel Cashier: Cannot redirect to Stripe Billing Portal

https://laravel.com/docs/10.x/billing#billing-portal

When I execute this laravel code:

        Route::get('/stripe-billing-portal', function (Request $request) {
            $stripeCustomer = $request->user()->createOrGetStripeCustomer();
            $url = $request->user()->billingPortalUrl();
            return redirect()->away($url);
//            return $request->user()->redirectToBillingPortal(route('test.payments.index'));
        })->name('stripe-billing-portal');

I see this error in the console:

Access to XMLHttpRequest at 'https://billing.stripe.com/p/session/test_YWNjdF8wbnJCRFhHRjljcWg1cEV4SldKcixfT01UQ2lrRHlvYmFpR2lpUm9SODM2SThEcjdoUlphSg0100Q5ftkFoj' (redirected from 'http://codecourse_build_a_forum_with_inertia.test/test/payments/stripe-billing-portal') from origin 'http://codecourse_build_a_forum_with_inertia.test' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

And the redirect does not happen. How can I fix this?

0 likes
3 replies
connecteev's avatar
connecteev
OP
Best Answer
Level 11

Found a solution thanks to Robert Boes on Discord.

You shouldn't call that route through XHR, just a regular anchor would suffice (my href was using Inertia's <Link>).

When I replace it with a regular <a :href it works. Or, if I use <Link :href with return Inertia::location($url); in the controller (make it an external redirect that Inertia understands; https://inertiajs.com/redirects#external-redirects), that also works.

7 likes
AdnanTemur's avatar

@connecteev Thanks man. I was wondering why my requests were failing despite using ngrok for https. At the end it was an XHR issue. I was using useForm from Inertia for the post request as I had payload. For anyone else wondering, here's a sample controller code:

		$checkout = $user->checkoutCharge(5000, 'Form Title', 1, [
            'success_url' => route('payment.success'),
            'cancel_url' => route('payment.failed'),
        ]);

        return Inertia::location($checkout->url); 

Please or to participate in this conversation.