Javascript 如何使用PayPal Express Checkout Integration(客户端REST)获取交易ID
我想得到交易ID一旦贝宝支付成功 我正在使用 请在下面找到我的付款按钮代码 我的目标是能够更新MySQL数据库,以便(1)确认订单,(2)在其旁边包含Paypal事务ID。这就是orderconfirm.php文件调用后的目标。为此,我需要能够在付款成功时获取TransactionID变量Javascript 如何使用PayPal Express Checkout Integration(客户端REST)获取交易ID,javascript,php,rest,paypal,client-side,Javascript,Php,Rest,Paypal,Client Side,我想得到交易ID一旦贝宝支付成功 我正在使用 请在下面找到我的付款按钮代码 我的目标是能够更新MySQL数据库,以便(1)确认订单,(2)在其旁边包含Paypal事务ID。这就是orderconfirm.php文件调用后的目标。为此,我需要能够在付款成功时获取TransactionID变量 <script> paypal.Button.render({ env: 'sandbox', // Or 'sandbox'
<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>
paypal.Button.render({
env:'沙盒',//或'沙盒'
客户:{
沙盒:“ABCD”,
制作:“ABCD”
},
提交:true,//显示“立即付款”按钮
支付:功能(数据、操作){
返回操作.payment.create({
付款:{
交易:[
{
金额:{总额:'0.01',币种:'USD'},
项目清单:{
项目:[
{
姓名:'帽子',
描述:'棕色帽子',
数量:“1”,
价格:'0.01',
货币:美元
}
]
}
}
]
},
});
},
onAuthorize:函数(数据、操作){
返回actions.payment.execute().then(函数(付款){
document.querySelector(“#confirmmessage”).innerText=“谢谢-您的付款已处理完毕”;
document.querySelector(“#paypal按钮”).style.display='none';
document.querySelector('#confirmdiv').style.display
='块';
window.location.href='orderconfirm.php?transactionID=如何获取此字段';
});
}
}“#贝宝按钮”);
因此,以下是我找到的解决问题的方法
1-要获得PaymentID,您只需执行以下操作(正如inarilo正确建议的那样-谢谢!)
注意:PaymentID与TransactionID不同:请参阅
要获取事务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';
你读了吗?是的,我读了,但我不明白这与我有什么关系。我正在使用客户端集成。如果我的理解是正确的,您提到的代码是服务器上创建支付对象的curl命令。我不能这样做,因为我使用的是共享托管服务。我遗漏了什么吗?你应该可以通过JS做同样的事情。但是共享主机并不意味着你不能使用curl。数据包含什么,它有销售id吗?只要使用Authorize返回的数据,paypal就会返回paymentID和orderID窗口。location.href=“+data.paymentID+”&order=“+data.orderID;支付对象是什么?你能给这行代码添加这么多上下文吗?感谢payment.transactions[0]。相关_资源[0]。sale.idUPDATE:上述内容对checkout.js有效。对于与SDK的新集成(从2019年2月开始),可以在以下位置找到交易ID…purchase_units[0]。payments.captures[0]。ID
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';