英文:
PayPal Checkout popup not showing up
问题
在尝试将PayPal集成到我的网站时,我在控制台日志中收到了一个错误。当我按下结账按钮时,应该出现一个小的PayPal弹出窗口,以便我进行结账。但实际上,弹出窗口出现了0.1秒,然后又关闭了。
Payments.html
<script>
// 将PayPal按钮呈现到 #paypal-button-container 中
paypal.Buttons({
// 调用您的服务器以设置交易
createOrder: function (data, actions) {
return fetch('/demo/checkout/api/paypal/order/create/', {
method: 'post'
}).then(function (res) {
return res.json();
}).then(function (orderData) {
return orderData.id;
});
},
// 调用您的服务器以完成交易
onApprove: function (data, actions) {
return fetch('/demo/checkout/api/paypal/order/' + data.orderID + '/capture/', {
method: 'post'
}).then(function (res) {
return res.json();
}).then(function (orderData) {
// 处理三种情况:
// (1) 可恢复的INSTRUMENT_DECLINED -> 调用 actions.restart()
// (2) 其他不可恢复的错误 -> 显示失败消息
// (3) 成功的交易 -> 显示确认或感谢
// 此示例读取从服务器传播的v2/checkout/orders捕获响应
// 您可以为您的'orderData'使用不同的API或结构
var errorDetail = Array.isArray(orderData.details) && orderData.details[0];
if (errorDetail && errorDetail.issue === 'INSTRUMENT_DECLINED') {
return actions.restart(); // 可恢复状态,详见:
// https://developer.paypal.com/docs/checkout/integration-features/funding-failure/
}
if (errorDetail) {
var msg = '抱歉,无法处理您的交易。';
if (errorDetail.description) msg += '\n\n' + errorDetail.description;
if (orderData.debug_id) msg += '(' + orderData.debug_id + ')';
return alert(
msg); // 显示失败消息(在生产环境中尽量避免使用警报)
}
// 成功的捕获!仅供演示用途:
console.log('捕获结果', orderData, JSON.stringify(orderData, null, 2));
var transaction = orderData.purchase_units[0].payments.captures[0];
alert('交易 ' + transaction.status + ': ' + transaction.id +
'\n\n请查看控制台以获取所有可用的详细信息');
// 用成功消息替换上述代码,例如在此页面内显示成功消息:
// const element = document.getElementById('paypal-button-container');
// element.innerHTML = '';
// element.innerHTML = '<h3>感谢您的付款!</h3>';
// 或转到另一个URL:actions.redirect('thank_you.html');
});
}
}).render('#paypal-button-container');
</script>
base.html
<!-- PayPal脚本 -->
<script src="https://www.paypal.com/sdk/js?client-id=test¤cy=USD"></script>
我期望PayPal按钮会打开一个小页面以确认我的结账。
英文:
When trying to integrate paypal to my website i get an error in the console log.
Ive never seen this error before. So when i press the checkout button it a small paypal popup should show up so that i can checkout. Instead the popup shows up for 0.1s then closes again.
Payments.html
<script>
// Render the PayPal button into #paypal-button-container
paypal.Buttons({
// Call your server to set up the transaction
createOrder: function (data, actions) {
return fetch('/demo/checkout/api/paypal/order/create/', {
method: 'post'
}).then(function (res) {
return res.json();
}).then(function (orderData) {
return orderData.id;
});
},
// Call your server to finalize the transaction
onApprove: function (data, actions) {
return fetch('/demo/checkout/api/paypal/order/' + data.orderID + '/capture/', {
method: 'post'
}).then(function (res) {
return res.json();
}).then(function (orderData) {
// Three cases to handle:
// (1) Recoverable INSTRUMENT_DECLINED -> call actions.restart()
// (2) Other non-recoverable errors -> Show a failure message
// (3) Successful transaction -> Show confirmation or thank you
// This example reads a v2/checkout/orders capture response, propagated from the server
// You could use a different API or structure for your 'orderData'
var errorDetail = Array.isArray(orderData.details) && orderData.details[0];
if (errorDetail && errorDetail.issue === 'INSTRUMENT_DECLINED') {
return actions.restart(); // Recoverable state, per:
// https://developer.paypal.com/docs/checkout/integration-features/funding-failure/
}
if (errorDetail) {
var msg = 'Sorry, your transaction could not be processed.';
if (errorDetail.description) msg += '\n\n' + errorDetail.description;
if (orderData.debug_id) msg += ' (' + orderData.debug_id + ')';
return alert(
msg); // Show a failure message (try to avoid alerts in production environments)
}
// Successful capture! For demo purposes:
console.log('Capture result', orderData, JSON.stringify(orderData, null, 2));
var transaction = orderData.purchase_units[0].payments.captures[0];
alert('Transaction ' + transaction.status + ': ' + transaction.id +
'\n\nSee console for all available details');
// Replace the above to show a success message within this page, e.g.
// const element = document.getElementById('paypal-button-container');
// element.innerHTML = '';
// element.innerHTML = '<h3>Thank you for your payment!</h3>';
// Or go to another URL: actions.redirect('thank_you.html');
});
}
}).render('#paypal-button-container');
</script>
base.html
<!-- Paypal Script -->
<script src="https://www.paypal.com/sdk/js?client-id=test&currency=USD"></script>
I was expecting the paypalbutton to open up a little page to confirm my chechout.
答案1
得分: 1
/demo/checkout/api/paypal/order/create/
或者你用其他路径替换它,必须是你服务器上的有效路由,调用PayPal v2/checkout/orders API 并且仅返回/输出该API的JSON响应。如果返回任何不能解析为有效JSON的内容,比如HTML 404页面,那么结果就是你问题中的错误。
你还必须实现一个捕获路由,可以从onApprove
函数中调用。
有关Node.js的完整示例,请参阅文档(当然,你也可以使用其他后端环境来实现所需的路由)。
英文:
/demo/checkout/api/paypal/order/create/
, or whatever you replace this path with, needs to be a valid route on your server that calls the PayPal v2/checkout/orders API and returns/outputs only the JSON response of that. If anything else is returned that does not parse as valid JSON, such as an HTML 404 page, the result is the error in you question.
You must also implement a capture route to be called from the onApprove function.
For a full sample in node.js , see the documentation (you can of course use any other backend environment to implement the required routes)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论