【问题标题】:Laravel paypal server integrationLaravel 贝宝服务器集成
【发布时间】:2018-02-02 06:54:22
【问题描述】:

我正在尝试在 Laravel 5.4 中集成 paypal 支付,但遇到以下问题: 我从他们的文档中复制了这段代码:

<div id="paypal-button"></div>

    <script>
        var CREATE_PAYMENT_URL  = '{{ route('pay') }}';
        var EXECUTE_PAYMENT_URL = '{{ route('execute-payment') }}';

        paypal.Button.render({

            env: 'sandbox', // Or 'sandbox'

            commit: true, // Show a 'Pay Now' button

            payment: function() {
                 return paypal.request.post(CREATE_PAYMENT_URL).then(function(data) {
                       return data.id;
                 });
            },

            onAuthorize: function(data) {
                return paypal.request.post(EXECUTE_PAYMENT_URL, {
                       paymentID: data.paymentID,
                       payerID:   data.payerID
                }).then(function() {

                // The payment is complete!
               // You can now show a confirmation message to the customer
               });
            }

        }, '#paypal-button');
    </script>

但是当我按下 paypal 按钮时,它会在控制台中显示内部服务器错误。我想这是因为缺少 csrf 令牌。如果是这种情况,我该如何将令牌添加到请求中?

【问题讨论】:

    标签: laravel paypal


    【解决方案1】:

    如果您的错误是由 TokenMismatchException 引起的,您需要将其添加到请求中,如下所示:

    payment: function() {
        return paypal.request.post(CREATE_PAYMENT_URL, {
            _token:    {{csrf_token()}}
        }).then(function(data) {
            return data.id;
        });
    },
    onAuthorize: function(data) {
        return paypal.request.post(EXECUTE_PAYMENT_URL, {
            paymentID: data.paymentID,
            payerID:   data.payerID,
            _token:    {{csrf_token()}}
       }).then(function() {
           // The payment is complete!
           // You can now show a confirmation message to the customer
       });
    }
    

    希望对你有所帮助。

    【讨论】:

    • 支付完成时调用该函数我需要在支付函数中传递token
    【解决方案2】:

    Laravel 开箱即用,带有 csrf-token meta tag。因此,您可以这样做,而不是直接调用paypal.request.post:

    <div id="paypal-button"></div>
    
    <script>
      var CREATE_PAYMENT_URL  = '{{ route('pay') }}';
      var EXECUTE_PAYMENT_URL = '{{ route('execute-payment') }}';
      var laravelCsrfToken = document.head.querySelector('meta[name="csrf-token"]')
    
      paypal.Button.render({
    
        env: 'sandbox', // Or 'sandbox'
    
        commit: true, // Show a 'Pay Now' button
    
        payment: function() {
          return paypal.request({
            method: 'post',
            url: CREATE_PAYMENT_URL,
            headers: {
              'x-csrf-token': laravelCsrfToken
            }
          }).then(function(data) {
            return data.id;
          });
        },
    
        onAuthorize: function(data) {
          return paypal.request({
            method: 'post',
            url: EXECUTE_PAYMENT_URL,
            data: {
              paymentID: data.paymentID,
              payerID:   data.payerID
            },
            headers: {
              'x-csrf-token': laravelCsrfToken
            }
          }).then(function() {
            // The payment is complete!
            // You can now show a confirmation message to the customer
          });
        }
      }, '#paypal-button');
    </script>
    

    【讨论】:

    • 我试过了,但我再次收到此错误 checkout.js:20324 POST localhost:8000/pay 500 (Internal Server Error)
    • 转到 Google Chrome 开发工具中的 network 选项卡,看看发生了什么。或者去 Laravel 日志获取错误信息。
    • token是否在header中传递?签入开发工具
    • 1.验证您的 html 中是否存在 csrf-token 元标记。 2. 在你的 Javascript 中使用console.log(laravelCsrfToken)。
    • 我设法通过了令牌并且不再收到错误但现在贝宝说“目前似乎没有工作。请稍后再试。”我希望这只是他们的临时问题,他们会尽快解决
    猜你喜欢
    • 2021-02-07
    • 2021-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-21
    • 2017-10-18
    • 2014-01-12
    • 2011-08-01
    相关资源
    最近更新 更多