lmsqueezy / laravel

A package to easily integrate your Laravel application with Lemon Squeezy.
https://lemonsqueezy.com
MIT License
521 stars 51 forks source link

Redirecting to checkout is throwing a CORS error #38

Closed WendellAdriel closed 8 months ago

WendellAdriel commented 1 year ago

Lemon Squeezy for Laravel Version

1.1.1

Laravel Version

10.18.0

PHP Version

8.2.5

Description

I created a route to open the checkout as described in the docs (example removing specific stuff for my app):

public function __invoke(): mixed
    {
        $user = Auth::user();
        return $user->subscribe('VARIANT_ID')
            ->withoutSubscriptionPreview()
            ->withName($user->name)
            ->withEmail($user->email);
    }

This was working last week without issues, but since yesterday I'm getting this error:

Access to fetch at '{CHECKOUT_URL}' (redirected from 'http://konf.test/dashboard/subscribe') from origin 'http://konf.test' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

And I didn't change the code above, so IDK what could be happening.

Steps To Reproduce

Create a home route with a button to subscribe.

Create a route that will have this logic to be the subscribe route:

public function __invoke(): mixed
    {
        $user = Auth::user();

        return $user->subscribe('VARIANT_ID')
            ->withoutSubscriptionPreview()
            ->withName($user->name)
            ->withEmail($user->email);
    }

Go to the home route and click the link to the subscribe route, and check the error pop-up in the browser console in dev tools.

WendellAdriel commented 1 year ago

Just made some more tests and it seems that this error is happening only if I use the wire:navigate from Livewire v3 in the link, so it could be related to some issue regarding this feature of Livewire v3.

WendellAdriel commented 1 year ago

I was able to get the same error using $this->redirectRoute() from Livewire v3 as well to redirect to the route that loads the checkout.

koossaayy commented 1 year ago

I will try to reproduce the error, but which version of Livewire exactly you are using ?

WendellAdriel commented 1 year ago

I will try to reproduce the error, but which version of Livewire exactly you are using ?

I'm using v3, latest beta release. But I'll downgrade to v2 now to test

WendellAdriel commented 1 year ago

@koossaayy I did some more tests and on v3 and v2 the issue is fixed if I use the code below instead of the one shown in the docs:

public function __invoke(): mixed
    {
        $user = Auth::user();

        return redirect()->away(
            $user->subscribe('VARIANT_ID')
                ->withoutSubscriptionPreview()
                ->withName($user->name)
                ->withEmail($user->email)
                ->url()
        );
    }
koossaayy commented 1 year ago

That's good, and the code is working on V3 ?

WendellAdriel commented 1 year ago

@koossaayy yes, this is working for both v2 and v3 without any issues. The only catch in v3 is that you can’t use wire:navigate $this->redirect() with the navigate true because it could still cause some issues because of how it works (replace the html to simulate a SPA behaviour)

heyjorgedev commented 8 months ago

Is this issue still relevant or can it be closed? Seems to have a solution cc @driesvints

driesvints commented 8 months ago

Yeah can be closed. Thanks all.