【问题标题】:Laravel 5.1 csrftoken curl from paypal来自贝宝的 Laravel 5.1 csrftoken curl
【发布时间】:2015-12-22 03:10:21
【问题描述】:

当贝宝在我的网站上发送帖子时,我如何添加或使用 csrftoken。 我的错误代码: VerifyCsrfToken.php 第 53 行中的 TokenMismatchException。

这里代码:

    public function getPaypal(Request $request)
   {

    $uri = $request->all();

    if(isset($uri['tx']))
    {

      $pp_hostname = "www.sandbox.paypal.com"; // Change to www.sandbox.paypal.com to test against sandbox
      // read the post from PayPal system and add 'cmd'
      $req = 'cmd=_notify-synch';

      $tx_token = $uri['tx'];
      $auth_token = "EHNebv....e";
      $req .= "&tx=$tx_token&at=$auth_token";

      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, "https://$pp_hostname/cgi-bin/webscr");
      curl_setopt($ch, CURLOPT_POST, 1);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
      //set cacert.pem verisign certificate path in curl using 'CURLOPT_CAINFO' field here,
      //if your server does not bundled with default verisign certificates.
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
      curl_setopt($ch, CURLOPT_HTTPHEADER, array("Host: $pp_hostname"));
      $res = curl_exec($ch);
      curl_close($ch);

   }

【问题讨论】:

    标签: php laravel curl paypal


    【解决方案1】:

    根据 Laravel 文档:http://laravel.com/docs/5.1/routing#csrf-protection

    从 CSRF 保护中排除 URI

    有时您可能希望从 CSRF 保护中排除一组 URI。 例如,如果您使用 Stripe 处理付款并且 使用他们的 webhook 系统,您需要排除您的 webhook 来自 Laravel 的 CSRF 保护的处理程序路由。

    您可以通过将 URI 添加到 VerifyCsrfToken 中间件:

    <?php
    
    namespace App\Http\Middleware;
    
    use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier;
    
    class VerifyCsrfToken extends BaseVerifier
    {
        /**
         * The URIs that should be excluded from CSRF verification.
         *
         * @var array
         */
        protected $except = [
            'paypal/*',
        ];
    }
    

    如果还不清楚,请告诉我。

    【讨论】:

      【解决方案2】:

      您不需要完全禁用中间件,只需转到 app\Http\Middle 中的 VerifyCrsfToken 文件,然后编辑受保护的数组 $except 并包含和输入 paypal 发布到的路由。

      protected $except = [
          /paypal/data,
      
      
      ];
      

      【讨论】:

        【解决方案3】:

        TokenMismatchException 是 Laravel 错误,而不是 PayPal。对于每个 POST 请求,您需要通过它发送一个 _token 值。

        如果您通过表单发送,只需在表单模板中回显csrf_field()

        如果你从 Laravel 以外的其他地方发送请求,你可以禁用该路由上的 CSRF 保护。在此处阅读有关中间件的更多信息:http://laravel.com/docs/5.1/middleware

        在此处了解更多信息:http://laravel.com/docs/5.1/routing#csrf-protection

        【讨论】:

          猜你喜欢
          • 2020-11-07
          • 2011-09-10
          • 2019-06-25
          • 2014-12-22
          • 2022-01-19
          • 2016-06-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多