[英]Link Paypal-Buttons (client) to Server-Response (PHP) with ajax
我想在我的網站上使用 Paypal,所以我按照這里的教程進行操作。 到目前為止一切正常:使用作曲家下載了 SDK。 創建了 class PayPalClient
和CreateOrder
。
在本節教程的最后,需要使用 javascript 將CreateOrder
的結果鏈接到 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);
class CreateOrder
工作正常。 我可以輸出來自 paypal 的響應。 這個問題似乎不存在。 當我按下 Paypal 按鈕時,我還可以在 Firefox-Dev-Tools 中看到 XHR 請求已正確完成。 還有一個 XHR 響應返回到按鈕。 但似乎我使用了錯誤的格式或錯誤的數據部分,即返回到fetch()
-Function。
有人有想法嗎?
感謝幫助!!!
蒂姆
好的,我找到了解決方案。
這個:
[...]
}).then(function(data) {
return data.orderID;
});
[...]
需要替換為:
[...]
}).then(function(data) {
return data.result.id;
});
[...]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.