2017-08-24 51 views
0

我想支付寶付款,在Laravel 5.4整合和我有以下問題: 我從他們的文件複製此代碼:Laravel的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> 

但是,當我按下貝寶按鈕,它會在控制檯中給我內部服務器錯誤。我想這是因爲csrf令牌丟失。如果是這種情況,我該如何將令牌添加到reqeust中?

回答

0

如果你的錯誤是原因由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 
    }); 
} 

希望這有助於你。

+0

這個函數被調用時,支付完成後 我需要通過令牌的支付功能 – TheAngelM97

+0

@ TheAngelM97我已經更新了我的答案 – Asur

0

Laravel開箱即用csrf-tokenmeta 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> 
+0

我嘗試它但我再次收到此錯誤 checkout.js:20324 POST http:// localhost:8000/pay 500(內部服務器錯誤) – TheAngelM97

+0

轉到谷歌瀏覽器開發工具中的「網絡」選項卡,看看發生了什麼。或者轉到Laravel日誌以獲取錯誤。 –

+0

它會拋出無效的csrf標記錯誤 – TheAngelM97