简体   繁体   English

Paypal 智能支付按钮 — 如何在服务器端创建订单并将订单 ID 传递给 Paypal 按钮设置?

[英]Paypal Smart Payment Buttons — how to create order server-side and pass order ID to Paypal Buttons setup?

I'm integrating Paypal Checkout and following in their documentation for how to set up the client SDK here.我正在集成 Paypal Checkout 并在他们的文档中关注如何在此处设置客户端 SDK。

I would like to not set up the order items and sum on the client side, but instead use the server side setup for orders mentioned at the end of step 4 .我不想在客户端设置订单项和总和,而是对步骤 4末尾提到的订单使用服务器端设置。 Using their PHP SDK I can manage to setup the order no problem, but where I am struggling to get things to work is how to pass this created order to the client.使用他们的 PHP SDK 我可以设法设置订单没有问题,但我努力让事情工作的地方是如何将这个创建的订单传递给客户端。 The return object for the created order has the ID and URLs to initiate capture it, but following the links actually provides a different UX from clicking the Paypal Button, so I would like to use the Button still, but with the created order.创建的订单的返回对象具有用于启动捕获它的 ID 和 URL,但跟随链接实际上提供了与单击 Paypal 按钮不同的 UX,因此我想仍然使用该按钮,但使用创建的订单。

Using their example code:使用他们的示例代码:

  paypal.Buttons({
    createOrder: function(data, actions) {
      // This function sets up the details of the transaction, including the amount and line item details.
      return actions.order.create({
        purchase_units: [{
          amount: {
            value: '0.01'
          }
        }]
      });
    },
    onApprove: function(data, actions) {
      // This function captures the funds from the transaction.
      return actions.order.capture().then(function(details) {
        // This function shows a transaction success message to your buyer.
        alert('Transaction completed by ' + details.payer.name.given_name);
      });
    }
  }).render('#paypal-button-container');
  //This function displays Smart Payment Buttons on your web page.

Essentially I have an order ID from my server side call and would not need to use the "createOrder" call, but how can I pass my order ID to the paypal.Buttons() setup so that when the user clicks on the rendered Paypal Button they are sent to approve the order I created?本质上,我从我的服务器端调用有一个订单 ID,不需要使用“createOrder”调用,但是如何将我的订单 ID 传递给paypal.Buttons()设置,以便当用户单击呈现的 Paypal 按钮时他们被发送以批准我创建的订单?

PS: I get that you verify and capture the order after, but why would anybody ever want to set up the order charge in their client side Javascript? PS:我知道您之后验证并捕获订单,但是为什么有人想在他们的客户端 Javascript 中设置订单费用? To me that just seems like a fundamentally wrong idea.对我来说,这似乎是一个根本错误的想法。 Am I missing something here?我在这里错过了什么吗?

The solution was fairly obvious, but somehow I didn't stumble upon it when looking earlier.解决方案相当明显,但不知何故,我在早些时候查看时并没有偶然发现它。 createOrder needs to return the order ID, this can be done as documented : createOrder需要返回订单 ID,这可以按照文档进行

method: 'post',
    headers: {
      'content-type': 'application/json'
    }
  }).then(function(res) {
    return res.json();
  }).then(function(data) {
    return data.orderID; // Use the same key name for order ID on the client and server
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM