【发布时间】:2018-03-19 18:22:37
【问题描述】:
我正在使用 Laravel 5.4 创建一个需要 PayPal 进行支付的应用程序,当我研究 PayPal 集成文档时,我发现 REST API 需要服务器端名称中的 2 个控制器“/demo/checkout/api/paypal/payment/ create/" 和 "/demo/checkout/api/paypal/payment/execute/",但是 PayPal 的文档很模糊,有谁知道它是什么以及它的例子吗?
这是我在前端的代码:
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $("meta[name='csrf-token']").attr("content")
}
});
paypal.Button.render({
env: 'sandbox', // sandbox | production
// Show the buyer a 'Pay Now' button in the checkout flow
commit: true,
// payment() is called when the button is clicked
payment: function() {
// Set up a url on your server to create the payment
var CREATE_URL = '{{route('paypal.createpayment')}}';
// Make a call to your server to set up the payment
return paypal.request({
method: 'post',
url: CREATE_URL,
headers: {
'x-csrf-token': $("meta[name='csrf-token']").attr("content")
}
}).then(function(res) {
return res.paymentID;
});
},
// onAuthorize() is called when the buyer approves the payment
onAuthorize: function(data, actions) {
// Set up a url on your server to execute the payment
var EXECUTE_URL = '/demo/checkout/api/paypal/payment/execute/';
// Set up the data you need to pass to your server
var data = {
paymentID: data.paymentID,
payerID: data.payerID
};
// Make a call to your server to execute the payment
return paypal.request.post(EXECUTE_URL, data)
.then(function (res) {
window.alert('Payment Complete!');
});
}
}, '#paypal-button-container');
【问题讨论】: