Laravel 5.1 csrftoken curl from paypal


Laravel 5.1 csrftoken curl from paypal

当paypal在我的网站上发送帖子时,我如何添加或使用csrftoken。我的错误代码:VerifyCsrfToken.php中的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);
   }

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

从CSRF保护中排除uri

有时您可能希望从CSRF保护中排除一组uri。例如,如果您正在使用Stripe来处理付款利用他们的网络钩子系统,你需要排除你的网络钩子从Laravel的CSRF保护的handler路由。

可以通过将uri添加到属性的$except属性来排除它们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/*',
    ];
}

您不需要完全禁用中间件,只需在app'Http'Middle中转到VerifyCrsfToken文件,然后编辑受保护数组$except和include以及paypal正在发送的路由条目。

protected $except = [
    /paypal/data,

];

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