Laravel Paypal POST 请求被拒绝

时间:2021-06-25 05:12:26

标签: php laravel post paypal paypal-ipn

我正在尝试在我的 Laravel 项目中实施 IPN,由于“csrf”,我网站的帖子请求正在起作用,

我尝试像这里写的那样实现它:

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

在我的路由/web.php:

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

在 IController 中写的是什么:

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');............

error_log 不显示,意味着该函数从未进入。

问题:我如何强制 Laravel 排除来自 Paypal 的帖子?

1 个答案:

答案 0 :(得分:3)

你可以在 laravel 中从 CSRF 保护中排除 URI。

转到 App\Http\Middleware\VerifyCsrfToken 并添加要排除 csrf 令牌的网址。

<?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',
       
    ];
}

正如文件所说

<块引用>

有时您可能希望从 CSRF 保护中排除一组 URI。 例如,如果您使用 Stripe 处理付款并且 使用他们的 webhook 系统,您将需要排除您的 Stripe 由于 Stripe 不知道,来自 CSRF 保护的 webhook 处理程序路由 将什么 CSRF 令牌发送到您的路由。

通常,您应该将这些类型的路由放置在网络之外 App\Providers\RouteServiceProvider 应用的中间件组 到 routes/web.php 文件中的所有路由。但是,您也可以 通过将路由的 URI 添加到 $except 属性来排除路由 VerifyCsrfToken 中间件:

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

相关问题