我想在我的网站上使用Paypal,因此我正在遵循教程here。到目前为止,一切正常:通过composer下载了SDK。创建了类PayPalClient
和CreateOrder
。
在本节的最后,需要使用javascript将CreateOrder
的结果链接到按钮:
<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; // Use the same key name for order ID on the client and server
});
},
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-Request已正确完成。按钮旁边还有一个XHR-Response。但是似乎我使用了错误的格式或错误的数据部分,这些数据被返回到fetch()
-Function。
有人有想法吗?
感谢您的帮助!
Tim
好,我找到了解决方法。