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

smacy's avatar
Level 1

No 'Access-Control-Allow-Origin' header srmklive/laravel-paypal

i am using the package https://github.com/srmklive/laravel-paypal for paypal integration with inertia vue laravel route

Route::post('/', [FrontendController::class, 'membershipStore'])->name('membershipStore')->middleware('cors');
//my controller
 public function membershipStore(Request $request)
    {
        $data =  $this->validate($request, [
...         
        ]);
        session()->put('membershipData', $data);
        $provider = new PayPalClient;
        $provider->setApiCredentials(config('paypal'));
        $paypalToken = $provider->getAccessToken();

        $response = $provider->createOrder([
            "intent" => "CAPTURE",
            "application_context" => [
                "return_url" => route('processSuccess'),
                "cancel_url" => route('processCancel'),
            ],
            "purchase_units" => [
                0 => [
                    "amount" => [
                        "currency_code" => "USD",
                        "value" => 1
                    ]
                ]
            ]
        ]);

        if (isset($response['id']) && $response['id'] != null) {

            // redirect to approve href
            foreach ($response['links'] as $links) {
                if ($links['rel'] == 'approve') {
                    return redirect()->away($links['href']);
                }
            }

            return redirect()
                ->route('front.index')
                ->with('error', 'Something went wrong.');
        } else {

            return redirect()
                ->route('front.index')
                ->with('error', $response['message'] ?? 'Something went wrong.');
        }
    }

error getting in console as

Access to XMLHttpRequest at 'https://www.sandbox.paypal.com/checkoutnow?token=0SF52410KT659903M' (redirected from 'http://nrnamembership.test/') from origin 'http://nrnamembership.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.

to resolve this i created middleware as

class CorMiddleware
{    
    public function handle(Request $request, Closure $next)
    {
        $response = $next($request);
        $response->header('Access-Control-Allow-Methods', 'HEAD, GET, POST, PUT, DELETE');
        $response->header('Access-Control-Allow-Headers', $request->header('Access-Control-Request-Headers'));
        $response->header('Access-Control-Allow-Origin', '*');
        return $response;
    }
}

how can it be resolved

0 likes
6 replies
martinbean's avatar

@MooseSaid You bumped at least three topics.

If you have a question, create your own thread. Don‘t flood the Laracasts front page with ancient threads you’ve dug up.

Please or to participate in this conversation.