如何使用PayPal快速结帐集成(客户端 REST)获取事务 ID



我想在PayPal付款成功后获取交易ID。

我正在使用PayPal快速结帐(客户端 REST(。

请在下面找到我的付款按钮代码。

我的目标是能够更新MySQL数据库以(1(确认订单和(2(在其旁边包含PayPal事务ID。这就是 orderconset .php 文件在调用后的目标。为此,我需要能够在付款成功时获取 TransactionID 变量

<script>
paypal.Button.render({
env: 'sandbox', // Or 'sandbox'
client: {
sandbox:    'ABCD',
production: 'ABCD'
},
commit: true, // Show a 'Pay Now' button
payment: function(data, actions) {
return actions.payment.create({
payment: {
transactions: [
{
amount: { total: '0.01', currency: 'USD' },
item_list: {
items: [
{
name: 'hat',
description: 'Brown hat',
quantity: '1',
price: '0.01',
currency: 'USD'
}
]
}
}
]
},    
});
},
onAuthorize: function(data, actions) {
return actions.payment.execute().then(function(payment) {
document.querySelector('#confirmmessage').innerText = 'Thank you - Your payment has been processed';
document.querySelector('#paypal-button').style.display = 'none';
document.querySelector('#confirmdiv').style.display 
= 'block';
window.location.href = 'orderconfirmed.php?transactionID=HOW_TO_GET_THIS_FIELD';
});
}
}, '#paypal-button');
</script>

所以这是我找到的问题的解决方案

1-要获得付款ID,您可以简单地做(正如inarilo正确建议的那样 - 谢谢!

data.paymentID

注意:付款 ID 与交易 ID 不同:请参阅付款 ID 和交易 ID 之间的区别

要获取交易 ID,我使用:

payment.transactions[0].related_resources[0].sale.id

2-然后要使用这些信息并通过PHP更新MySQL数据库,我使用AJAX。

所以把代码放在一起看起来像这样:

document.querySelector('#confirmmessage').innerText = payment.transactions[0].related_resources[0].sale.id;

if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
xmlhttp.open("GET","testajax.php",true);
xmlhttp.send();

document.querySelector('#confirmdiv').style.display = 'block';

最新更新