【问题标题】:How to get the Transaction ID using PayPal Express Checkout Integration (Client-side REST)如何使用 PayPal Express Checkout 集成(客户端 REST)获取交易 ID
【发布时间】:2018-01-05 02:05:48
【问题描述】:

我想在 Paypal 支付成功后获取交易 ID。

我正在使用Paypal Express Checkout (Client-side REST)

请在下面找到我的付款按钮代码。

我的目标是能够更新 MySQL 数据库以 (1) 确认订单并 (2) 在其旁边包含 Paypal 交易 ID。这就是 orderconfirmed.php 文件在调用后的目标。为此,我需要能够在付款成功时获取 TransactionID 变量

<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>

【问题讨论】:

  • 是的,但我不明白这与我有什么关系。我正在使用客户端集成。如果我的理解是正确的,那么您所指的代码是服务器上用于创建支付对象的 curl 命令。我不能这样做,因为我正在使用共享托管服务。我错过了什么吗?
  • 你应该可以通过 JS 做同样的事情。但是共享主机并不意味着你不能使用 curl。
  • 数据包含什么,有销售ID吗?
  • 只用onAuthorize返回的数据,paypal返回paymentID和orderID window.location.href = "yourwebsite.com/orderconfirmed.php?payment=" + data.paymentID + "&order=" + data.orderID;

标签: javascript php rest paypal client-side


【解决方案1】:

所以这是我发现的问题的解决方案

1- 获取 PaymentID 您可以简单地执行(正如 inarilo 正确建议的那样 - 谢谢!)

data.paymentID

注意:PaymentID 与 TransactionID 不同:见Difference between paymentId and 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';

【讨论】:

  • 支付对象是什么?你能在这行代码中添加这样的上下文吗?谢谢payment.transactions[0].related_resources[0].sale.id
  • 更新:以上内容对 checkout.js 有效。对于与 SDK 的新集成(从 2019 年 2 月开始),可以在 ...purchase_units[0].payments 找到交易 ID .captures[0].id
猜你喜欢
  • 2020-08-06
  • 2014-03-03
  • 2015-03-28
  • 2012-11-14
  • 2015-03-28
  • 2012-04-25
  • 2013-03-15
  • 2013-03-18
  • 2018-02-27
相关资源
最近更新 更多