【问题标题】:Laravel Paypal POST request is rejectedLaravel Paypal POST 请求被拒绝
【发布时间】:2021-09-08 12:57:56
【问题描述】:

我正在尝试在我的 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 的 POSTS?

【问题讨论】:

    标签: php laravel post paypal paypal-ipn


    【解决方案1】:

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

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

    <?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 来自 CSRF 保护的 webhook 处理程序路由,因为 Stripe 不会知道 将什么 CSRF 令牌发送到您的路线。

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

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

    【讨论】:

    • 是的!谢谢。我不能投票 :-( StOv 为什么?
    • @Strella 。您现在只能接受答案,因为投票需要至少 15 个信誉点 stackoverflow.com/help/privileges/vote-up
    • 现在我知道了,另一个聪明的举动是你不能评论低于 50 并且必须提交答案。
    猜你喜欢
    • 2017-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-16
    • 2020-05-28
    • 2020-06-19
    • 2013-11-14
    • 2018-11-30
    相关资源
    最近更新 更多