繁体   English   中英

PayPal 服务器端集成未将付款详细信息传递给客户端

[英]PayPal Server Side integration not passing payment details to the client

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

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

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

当我登录沙箱 paypal 时,在个人帐户上,我可以看到交易正在成功发送(它们正在等待,等待商家确认)。 当我登录沙盒商家帐户时,没有可用的交易。 当我从智能按钮获取订单 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

在服务器端,不要将“详细信息”指定为键。

res.json(captureDetails);


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


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

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

暂无
暂无

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

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