【问题标题】:PayPal Server Side integration not passing payment details to the clientPayPal 服务器端集成未将付款详细信息传递给客户端
【发布时间】:2020-12-02 18:42:33
【问题描述】:

为什么我没有在客户端收到包含捕获详细信息的响应?

我正在尝试为 PayPal 的智能按钮实现服务器端集成。我尝试了几种不同的方法,这是我最成功的方法。

但是,它似乎仍然没有 100% 工作。 Atm,单击按钮打开支付窗口,我可以使用沙盒个人帐户登录,通过结帐流程,然后我收到标准警报,但由于某种原因,我没有从服务器获得所需的响应。

当我在个人帐户上登录沙盒贝宝时,我可以看到交易正在成功发送(它们正在等待,等待商家确认)。当我登录沙盒商家帐户时,没有可用的交易。当我从智能按钮中获取订单 ID 并将其发送到 PayPal 的 api 路由以获取订单详细信息时,它会以捕获并完成的方式返回。

是否有其他人遇到过类似的付款未显示在商家沙盒帐户中的情况?如果我登录开发者帐户并查看 API 日志,我可以看到已成功创建和捕获订单,但它们仍然没有显示在商家帐户中。

这是我的服务器端代码:

const express = require("express");
const router = express.Router();

// 1. Set up your server to make calls to PayPal

// 1a. Import the SDK package
const paypal = require("@paypal/checkout-server-sdk");

// 1b. Import the PayPal SDK client that was created in `Set up Server-Side SDK`.
/**
 *
 * PayPal HTTP client dependency
 */
const payPalClient = require("./PayPalConfig");

// route to set up a transaction
router.post("/orders/create", async (req, res) => {
  // 3. Call PayPal to set up a transaction
  const request = new paypal.orders.OrdersCreateRequest();
  request.prefer("return=representation");
  request.requestBody({
    intent: "CAPTURE",
    purchase_units: [
      {
        amount: {
          currency_code: "USD",
          value: "4.20",
        },
      },
    ],
  });

  let order;
  try {
    order = await payPalClient.client().execute(request);
  } catch (err) {
    // 4. Handle any errors from the call
    console.error(err);
    return res.sendStatus(500);
  }

  // 5. Return a successful response to the client with the order ID
  res.json({
    orderID: order.result.id,
  });
  console.log(order.result.id);
});

// route to handle capturing of orders
router.post("/orders/capture", async (req, res) => {
  // const captureDetails
  let captureDetails = "";
  // 2a. Get the order ID from the request body
  const orderID = req.body.orderID;
  // 3. Call PayPal to capture the order
  const request = new paypal.orders.OrdersCaptureRequest(orderID);
  request.requestBody({});

  try {
    const capture = await payPalClient.client().execute(request);

    // 4. Save the capture ID to your database. Implement logic to save capture to your database for future reference.
    const captureID = capture.result.purchase_units[0].payments.captures[0].id;
    captureDetails = capture.result;
    // await database.saveCaptureID(captureID);
    res.json(captureDetails);
  } catch (err) {
    // 5. Handle any errors from the call
    console.error(err);
    return res.sendStatus(500);
  }

  // 6. Return a successful response to the client
  // res.sendStatus(200).json({ details: captureDetails });
  res.json({ details: captureDetails });
});

module.exports = router;

这是我的客户端代码:

// Render the PayPal button into #paypal-button-container
paypal
  .Buttons({
    // Call your server to set up the transaction
    createOrder: function (data, actions) {
      return fetch("http://localhost:3000/payment/paypal/orders/create", {
        method: "post",
      })
        .then(function (res) {
          return res.json();
        })
        .then(function (orderData) {
          return orderData.orderID;
          console.log(orderData.orderID);
        });
    },

    // Call your server to finalize the transaction
    onApprove: function (data) {
      return fetch("http://localhost:3000/payment/paypal/orders/capture", {
        method: "post",
        headers: {
          "content-type": "application/json",
        },
        body: JSON.stringify({
          orderID: data.orderID,
        }),
      })
        .then(function (res) {
          return res;
        })
        .then(function (details) {
          console.log(details);
          alert("Transaction funds captured from " + details.payer_given_name);
        });
    },
  })
  .render("#paypal-button-container");

这是从客户端记录的详细信息

Response {type: "cors", url: "http://localhost:3000/payment/paypal/orders/capture", redirected: false, status: 200, ok: true, …}
body: (...)
bodyUsed: false
headers: Headers {}
ok: true
redirected: false
status: 200
statusText: "OK"
type: "cors"
url: "http://localhost:3000/payment/paypal/orders/capture"
__proto__: Response

【问题讨论】:

    标签: javascript node.js paypal paypal-sandbox paypal-rest-sdk


    【解决方案1】:

    在服务器端,不要将 'details' 指定为键。

    res.json(captureDetails);


    您需要在客户端返回 res.json()。它没有解析json对象。


    当我登录沙盒商家帐户时,没有 可用的交易。当我从智能获取订单 ID 时 按钮,并将其发送到 PayPal 的 api 路由以获取订单详细信息,它 捕获并完成后返回。

    您登录了错误的沙盒商家帐户。正确的将取决于您使用的沙盒 clientId。

    【讨论】:

    • 感谢您的回复。但是,我没有登录错误的沙盒商家帐户。我只有两个沙盒账户,一个商家,一个客户。 paypal 生成的商家电子邮件是我用来登录的电子邮件,它与客户帐户的待付款项上显示的电子邮件相匹配。
    • 如果您认为您登录的是正确的帐户但付款不存在,那么付款会发送到沙盒中未经确认的电子邮件,因此实际上不在该帐户中。它们正在等待无人认领,如果在该电子邮件中无人认领,它们将在 30 天内退回。通过sandbox.paypal.com/businessprofile/settings/emaildeveloper.paypal.com/developer/notifications 确认沙盒帐户中的电子邮件
    猜你喜欢
    • 1970-01-01
    • 2015-08-05
    • 2010-11-26
    • 2021-03-22
    • 1970-01-01
    • 2018-08-30
    • 2014-02-23
    • 2014-07-03
    • 1970-01-01
    相关资源
    最近更新 更多