如何使用 checkout.js 在 paypal 快速结帐中成功全额付款后获取 IPN 状态
How to get IPN status after success full payment in paypal express checkout using checkout.js
我已经实施了新的贝宝快速结账。我想把我的 success_ipn.php
URL 和 cancelled.php
URL 传给 checkout.js.
我已经完成了很多 google 以及官方文档 Paypal DOC。
下面是我的文件代码:
<!DOCTYPE html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<script src="https://www.paypalobjects.com/api/checkout.js"></script>
</head>
<body>
<div id="paypal-button-container"></div>
<script>
var EXECUTE_PAYMENT_URL = 'http://example.com/success_ipn.php';
paypal.Button.render({
env: 'sandbox', // sandbox | production
// PayPal Client IDs - replace with your own
// Create a PayPal app: https://developer.paypal.com/developer/applications/create
client: {
sandbox: 'My Sandbox Client ID here.',
//production: '<insert production client id>'
},
// Show the buyer a 'Pay Now' button in the checkout flow
commit: true,
// payment() is called when the button is clicked
payment: function(data, actions) {
// Make a call to the REST api to create the payment
return actions.payment.create({
payment: {
transactions: [
{
amount: { total: '0.01', currency: 'USD' }
}
]
}
});
},
// onAuthorize() is called when the buyer approves the payment
onAuthorize: function(data, actions) {
return paypal.request.post(EXECUTE_PAYMENT_URL, {
paymentID: data.paymentID,
payerID: data.payerID
}).then(function() {
window.alert('Payment Complete!');
});
}
}, '#paypal-button-container');
</script>
</body>
</html>
它工作正常,但我如何传递我的 IPN 文件 URL 并取消 URL。
再做一些 google 之后,我只能得到以下变量而不是交易 ID 等:
paymentID: data.paymentID,
payerID: data.payerID
请大家帮帮我。
在尝试过之后google我找到了解决方案。
Paypal 在 checckout.js
最新版本之后弃用了 Express Checkout - NVP/SOAP。因此,GetExpressCheckoutDetails
和 DoExpressCheckoutPayment
也已弃用。
现在,所有工作都将在 Rest 上完成 API。
下面是我们如何获取 json 格式的付款人信息和交易详情的更新代码。在获得 json 响应后,我们也可以使用 aJax.
更新我们的数据库记录
<script src="https://www.paypalobjects.com/api/checkout.js"></script>
<div id="paypal-button-container"></div>
<script>
var EXECUTE_PAYMENT_URL = 'http://example.com/success_checkout.php';
paypal.Button.render({
env: 'sandbox', // sandbox | production
// PayPal Client IDs - replace with your own
// Create a PayPal app: https://developer.paypal.com/developer/applications/create
client: {
sandbox: '',
//production: '<insert production client id>'
},
// Show the buyer a 'Pay Now' button in the checkout flow
commit: true,
// payment() is called when the button is clicked
payment: function(data, actions) {
// Make a call to the REST api to create the payment
return actions.payment.create({
payment: {
transactions: [
{
amount: { total: '10', currency: 'USD' },
custom: 'custom value here'
}
]
}
});
},
onAuthorize: function(data, actions) {
return actions.payment.get().then(function(payment) {
//debugger;
console.log(payment);
var txn_id = payment.cart;
var bookID = payment.transactions[0].custom;
var currency = payment.transactions[0].amount["currency"];
var amount = payment.transactions[0].amount["total"];
var payerID = payment.payer.payer_info["payer_id"];
var pstatus = payment.payer.status;
var successUrl = EXECUTE_PAYMENT_URL+'?txn_id='+txn_id+'&bookID='+bookID+'¤cy='+currency+'&amount='+amount+'&payerID='+payerID+'&pstatus='+pstatus;
//console.log(newUrl);
window.location.replace(successUrl);
});
},
onCancel: function(data, actions) {
var cancelUrl = "http://example.com/cancelled.php";
//console.log(newUrl);
window.location.replace(cancelUrl);
}
}, '#paypal-button-container');
</script>
您将在 success_checkout.php 中收到回复,如下所示:
<?php
echo '<pre>' print_r($_REQUEST);
// do your stuff here
header('Location: thanks.php');
?>
希望这对其他开发人员有所帮助。
我已经实施了新的贝宝快速结账。我想把我的 success_ipn.php
URL 和 cancelled.php
URL 传给 checkout.js.
我已经完成了很多 google 以及官方文档 Paypal DOC。
下面是我的文件代码:
<!DOCTYPE html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<script src="https://www.paypalobjects.com/api/checkout.js"></script>
</head>
<body>
<div id="paypal-button-container"></div>
<script>
var EXECUTE_PAYMENT_URL = 'http://example.com/success_ipn.php';
paypal.Button.render({
env: 'sandbox', // sandbox | production
// PayPal Client IDs - replace with your own
// Create a PayPal app: https://developer.paypal.com/developer/applications/create
client: {
sandbox: 'My Sandbox Client ID here.',
//production: '<insert production client id>'
},
// Show the buyer a 'Pay Now' button in the checkout flow
commit: true,
// payment() is called when the button is clicked
payment: function(data, actions) {
// Make a call to the REST api to create the payment
return actions.payment.create({
payment: {
transactions: [
{
amount: { total: '0.01', currency: 'USD' }
}
]
}
});
},
// onAuthorize() is called when the buyer approves the payment
onAuthorize: function(data, actions) {
return paypal.request.post(EXECUTE_PAYMENT_URL, {
paymentID: data.paymentID,
payerID: data.payerID
}).then(function() {
window.alert('Payment Complete!');
});
}
}, '#paypal-button-container');
</script>
</body>
</html>
它工作正常,但我如何传递我的 IPN 文件 URL 并取消 URL。
再做一些 google 之后,我只能得到以下变量而不是交易 ID 等:
paymentID: data.paymentID,
payerID: data.payerID
请大家帮帮我。
在尝试过之后google我找到了解决方案。
Paypal 在 checckout.js
最新版本之后弃用了 Express Checkout - NVP/SOAP。因此,GetExpressCheckoutDetails
和 DoExpressCheckoutPayment
也已弃用。
现在,所有工作都将在 Rest 上完成 API。
下面是我们如何获取 json 格式的付款人信息和交易详情的更新代码。在获得 json 响应后,我们也可以使用 aJax.
更新我们的数据库记录<script src="https://www.paypalobjects.com/api/checkout.js"></script>
<div id="paypal-button-container"></div>
<script>
var EXECUTE_PAYMENT_URL = 'http://example.com/success_checkout.php';
paypal.Button.render({
env: 'sandbox', // sandbox | production
// PayPal Client IDs - replace with your own
// Create a PayPal app: https://developer.paypal.com/developer/applications/create
client: {
sandbox: '',
//production: '<insert production client id>'
},
// Show the buyer a 'Pay Now' button in the checkout flow
commit: true,
// payment() is called when the button is clicked
payment: function(data, actions) {
// Make a call to the REST api to create the payment
return actions.payment.create({
payment: {
transactions: [
{
amount: { total: '10', currency: 'USD' },
custom: 'custom value here'
}
]
}
});
},
onAuthorize: function(data, actions) {
return actions.payment.get().then(function(payment) {
//debugger;
console.log(payment);
var txn_id = payment.cart;
var bookID = payment.transactions[0].custom;
var currency = payment.transactions[0].amount["currency"];
var amount = payment.transactions[0].amount["total"];
var payerID = payment.payer.payer_info["payer_id"];
var pstatus = payment.payer.status;
var successUrl = EXECUTE_PAYMENT_URL+'?txn_id='+txn_id+'&bookID='+bookID+'¤cy='+currency+'&amount='+amount+'&payerID='+payerID+'&pstatus='+pstatus;
//console.log(newUrl);
window.location.replace(successUrl);
});
},
onCancel: function(data, actions) {
var cancelUrl = "http://example.com/cancelled.php";
//console.log(newUrl);
window.location.replace(cancelUrl);
}
}, '#paypal-button-container');
</script>
您将在 success_checkout.php 中收到回复,如下所示:
<?php
echo '<pre>' print_r($_REQUEST);
// do your stuff here
header('Location: thanks.php');
?>
希望这对其他开发人员有所帮助。