【发布时间】:2020-08-17 14:41:39
【问题描述】:
我想在我的网站上使用 Paypal,所以我按照教程 here。
到目前为止一切正常:使用 composer 下载了 SDK。创建了 PayPalClient 和 CreateOrder 类。
在本节教程的最后,CreateOrder 的结果需要使用 javascript 链接到 Buttons:
<script>
paypal.Buttons({
createOrder: function() {
return fetch('test_paypal_create_paypal_transaction.php', {
method: 'post',
headers: {
'content-type': 'application/json'
}
}).then(function(res) {
return res.json();
}).then(function(data) {
return data.orderID;
});
},
onApprove: function(data, actions) {
// This function captures the funds from the transaction.
return actions.order.capture().then(function(details) {
// This function shows a transaction success message to your buyer.
alert('Transaction completed by ' + details.payer.name.given_name);
});
}
}).render('#paypal-button-container');
//This function displays Smart Payment Buttons on your web page.
</script>
这是 php 文件 (test_paypal_create_paypal_transaction.php):
require __DIR__ . '/../../vendor/autoload.php';
use MyProject\PayPay\CreateOrder;
$response = CreateOrder::createOrder(false);
echo json_encode($response);
CreateOrder 类工作正常。我可以从贝宝发出回应。这个问题似乎不存在。
当我按下 Paypal 按钮时,我还可以在 Firefox-Dev-Tools 中看到 XHR 请求已正确完成。还有一个 XHR 响应返回到按钮。但似乎我使用了错误的格式或错误的数据部分,即返回到fetch()-Function。
有人有想法吗?
谢谢你的帮助!!!
提姆
【问题讨论】:
标签: php ajax paypal xmlhttprequest paypal-rest-sdk