Laravel Paypal POST request is rejected

Viewed 186

I'm trying to implement IPN in my Laravel project, a post request from my website is working thanks to "csrf",

i try to implement it like written here:

https://developer.paypal.com/docs/api-basics/notifications/ipn/ht-ipn/

in my routes/web.php:

Route::post('i', [IController::class, 'y'])->name('i');

and in IController whats written on the side:

public function y()
    {
        error_log('function y called');
        // STEP 1: read POST data

// Reading POSTed data directly from $_POST causes serialization issues with array data in the POST.

// Instead, read raw POST data from the input stream.

        $raw_post_data = file_get_contents('php://input');............

The error_log doesn't show up, means the function is never entered.

Question: How can i force Laravel to except POSTS From Paypal?

1 Answers

You can Exclude URIs From CSRF Protection in laravel.

Goto App\Http\Middleware\VerifyCsrfToken and add url which you want to exclude csrf token.

<?php

namespace App\Http\Middleware;

use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;

class VerifyCsrfToken extends Middleware
{
    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array
     */
    protected $except = [
        'i',
       
    ];
}

As document says

Sometimes you may wish to exclude a set of URIs from CSRF protection. For example, if you are using Stripe to process payments and are utilizing their webhook system, you will need to exclude your Stripe webhook handler route from CSRF protection since Stripe will not know what CSRF token to send to your routes.

Typically, you should place these kinds of routes outside of the web middleware group that the App\Providers\RouteServiceProvider applies to all routes in the routes/web.php file. However, you may also exclude the routes by adding their URIs to the $except property of the VerifyCsrfToken middleware:

Ref:https://laravel.com/docs/8.x/csrf#csrf-excluding-uris

Related