从PayPal按钮中获取Django的响应

huangapple go评论129阅读模式
英文:

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&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) {
            // 完整的可用详细信息
            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 -->

&lt;script src=&quot;https://www.paypal.com/sdk/js?client-id=MyClientID&amp;currency=USD&quot; data-sdk-integration-source=&quot;button-factory&quot;&gt;&lt;/script&gt;
&lt;script&gt;
function initPayPalButton() {
paypal
.Buttons({
style: {
shape: &quot;rect&quot;,
color: &quot;blue&quot;,
layout: &quot;horizontal&quot;,
label: &quot;buynow&quot;,
},
createOrder: function (data, actions) {
return actions.order.create({
purchase_units: [{amount: {currency_code: &quot;USD&quot;, value: totalAmount}}],
});
},
onApprove: function (data, actions) {
return actions.order.capture().then(function (orderData) {
// Full available details
console.log(&quot;Capture result&quot;, orderData, JSON.stringify(orderData, null, 2));
// Show a success message within this page, e.g.
const element = document.getElementById(&quot;paypal-button-container&quot;);
element.innerHTML = &quot;&quot;;
element.innerHTML = &quot;&lt;h3&gt;Thank you for your payment!&lt;/h3&gt;&quot;;
// Or go to another URL:  actions.redirect(&#39;thank_you.html&#39;);
});
},
onError: function (err) {
console.log(err);
},
})
.render(&quot;#paypal-button-container&quot;);
}
initPayPalButton();
&lt;/script&gt;

<!-- language: lang-html -->

&lt;div class=&quot;paypalButton-mainDiv&quot;&gt;
&lt;div id=&quot;smart-button-container&quot;&gt;
&lt;div style=&quot;text-align: center&quot;&gt;
&lt;div id=&quot;paypal-button-container&quot;&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

<!-- 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(&#39;POST&#39;, &#39;/&lt;your-server-endpoint&gt;&#39;);
// optional: set header
// xhr.setRequestHeader(/*&lt;headers you need&gt;*/);
xhr.onload = function() {
if (xhr.status === 200) {
console.log(&#39;Response received: &#39; + xhr.responseText);
}
};
xhr.send(JSON.stringify(orderData));
// [anything after, e.g. element.innerHTML = &lt;html&gt; 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 &lt;input&gt; 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.

huangapple
  • 本文由 发表于 2023年3月7日 21:10:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75662405.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定