php - 如何使用 checkout.js 在 paypal 快速结帐中成功全额付款后获取 IPN 状态

标签 php paypal-sandbox paypal-ipn paypal

我已经实现了新的 Paypal 快速结账。我想将我的 success_ipn.php URL 和 cancelled.php URL 传递给 checkout.js。

我已经做了很多谷歌和官方文档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。

在对此进行更多谷歌搜索后,我只能得到下面的变量,而不是交易 ID 等:

paymentID: data.paymentID,
payerID:   data.payerID

请帮帮我这些家伙。

最佳答案

在尝试过并对此进行谷歌搜索后,我找到了解决方案。

Paypal 在 checckout.js 最新版本后弃用了 Express Checkout - NVP/SOAP。因此,GetExpressCheckoutDetailsDoExpressCheckoutPayment 也已弃用。

现在,所有工作都将在 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+'&currency='+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');
?>

希望这对其他开发者有所帮助。

关于php - 如何使用 checkout.js 在 paypal 快速结帐中成功全额付款后获取 IPN 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44906045/

相关文章:

php - 确定变量是否是 PHP 中的有效闭包

Paypal 开发我的应用程序 - 如何删除它们?

paypal - 如果我的 Paypal IPNs 监听器出现故障会怎样?

php - 如何在wordpress的简码函数中包含一个文件

php(ssh2,tunnel),我无法连接我的远程mysql服务器

php - codeigniter 3.x 中的 MongoDB\Driver\Exception\InvalidArgumentException

php - 使用 PayPal IPN 时的客户信息

安卓 : Mobile Express Checkout Library

paypal - 在本地主机上运行的网站上使用 Paypal IPN

php - Paypal 实现 : Hidden Elements to PHP