英文:
Get response from PayPal buttton in django
问题
我已经将PayPal按钮集成到我的项目中。以下是我用于PayPal按钮的代码。
<!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false -->
<!-- 语言: lang-js -->
<script src="https://www.paypal.com/sdk/js?client-id=MyClientID¤cy=USD" data-sdk-integration-source="button-factory"></script>
<script>
function initPayPalButton() {
paypal
.Buttons({
style: {
shape: "rect",
color: "blue",
layout: "horizontal",
label: "buynow",
},
createOrder: function (data, actions) {
return actions.order.create({
purchase_units: [{amount: {currency_code: "USD", value: totalAmount}}],
});
},
onApprove: function (data, actions) {
return actions.order.capture().then(function (orderData) {
// 完整的可用详细信息
console.log("Capture result", orderData, JSON.stringify(orderData, null, 2));
// 在此页面内显示成功消息,例如
const element = document.getElementById("paypal-button-container");
element.innerHTML = "";
element.innerHTML = "<h3>感谢您的付款!</h3>";
// 或者跳转到另一个URL:actions.redirect('thank_you.html');
});
},
onError: function (err) {
console.log(err);
},
})
.render("#paypal-button-container");
}
initPayPalButton();
</script>
<!-- 语言: lang-html -->
<div class="paypalButton-mainDiv">
<div id="smart-button-container">
<div style="text-align: center">
<div id="paypal-button-container"></div>
</div>
</div>
</div>
<!-- 结束代码片段 -->
但是我无法在付款后从此代码中获取响应。我需要获取响应并希望将其存储在我的数据库中。
英文:
I have integrated PayPal buttons into my project. I am sharing my code here for PayPal Buttons.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
<script src="https://www.paypal.com/sdk/js?client-id=MyClientID&currency=USD" data-sdk-integration-source="button-factory"></script>
<script>
function initPayPalButton() {
paypal
.Buttons({
style: {
shape: "rect",
color: "blue",
layout: "horizontal",
label: "buynow",
},
createOrder: function (data, actions) {
return actions.order.create({
purchase_units: [{amount: {currency_code: "USD", value: totalAmount}}],
});
},
onApprove: function (data, actions) {
return actions.order.capture().then(function (orderData) {
// Full available details
console.log("Capture result", orderData, JSON.stringify(orderData, null, 2));
// 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');
});
},
onError: function (err) {
console.log(err);
},
})
.render("#paypal-button-container");
}
initPayPalButton();
</script>
<!-- language: lang-html -->
<div class="paypalButton-mainDiv">
<div id="smart-button-container">
<div style="text-align: center">
<div id="paypal-button-container"></div>
</div>
</div>
</div>
<!-- end snippet -->
But I am not able to get the response back from this code after Payment. I need to get the response and want to store it in my database.
答案1
得分: 0
看起来在成功支付时,没有发生数据持久化。
你可以使用 onApprove
来在付款后获取响应,然后使用 AJAX 调用或表单提交将其发送到你的服务器。代码如下:
onApprove: function (data, actions) {
return actions.order.capture().then(function (orderData) {
// [anything before]
var xhr = new XMLHttpRequest();
xhr.open('POST', '/<your-server-endpoint>');
// optional: set header
// xhr.setRequestHeader(/*<headers you need>*/);
xhr.onload = function() {
if (xhr.status === 200) {
console.log('Response received: ' + xhr.responseText);
}
};
xhr.send(JSON.stringify(orderData));
// [anything after, e.g. element.innerHTML = <html> OR go to another URL ]
});
},
更理想的情况是,你可以使用表单而不是 AJAX;在这种情况下,你需要创建一个存储响应的隐藏 <input>
元素,然后使用 JavaScript 提交表单。这些的确切规范由你决定,并且可以根据标准的 JS 文档完成。
英文:
It seems that no data persistence happens when you have a successful payment.
You can use onApprove
to get the response after payment and then send it to your server using an AJAX call or a form submit. That would look something like this:
onApprove: function (data, actions) {
return actions.order.capture().then(function (orderData) {
// [anything before]
var xhr = new XMLHttpRequest();
xhr.open('POST', '/<your-server-endpoint>');
// optional: set header
// xhr.setRequestHeader(/*<headers you need>*/);
xhr.onload = function() {
if (xhr.status === 200) {
console.log('Response received: ' + xhr.responseText);
}
};
xhr.send(JSON.stringify(orderData));
// [anything after, e.g. element.innerHTML = <html> OR go to another URL ]
});
},
Even more ideally, you can use a form, rather than AJAX; in that case, you need to create a hidden <input>
element that stores the response and then submit the form using JavaScript.
The exact specifications of these are up to you, and are doable as per the standard JS documentation.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论