0

我正在尝试在Node.Js环境中执行PayPal 支付,但我似乎无法理解它。付款没有完成。

我认为我没有正确设置参数,或者我的代码可能需要重新组织。

请帮我把头绕过去,并最终开始朝着正确的方向前进?

谢谢大家。

本地客户端浏览器:

<script>
  paypal.Button.render({
    env: 'sandbox', // Or 'production'
    // Set up the payment:
    // 1. Add a payment callback
    payment: function(data, actions) {
      // 2. Make a request to your server
      return actions.request.post('/my-api/create-payment/')
        .then(function(res) {
          // 3. Return res.id from the response
          return res.id;
        });
    },
    // Execute the payment:
    // 1. Add an onAuthorize callback
    onAuthorize: function(data, actions) {
      // 2. Make a request to your server
      return actions.request.post('/my-api/execute-payment/', {
        paymentID: data.paymentID,
        payerID:   data.payerID
      })
        .then(function(res) {
          // 3. Show the buyer a confirmation message.
        });
    }
  }, '#paypal-button');
</script>  

节点环境

const path = require("path");

var express = require("express");
var cors = require("cors");
var app = express();

var request = require("request");

const port = process.env.PORT || 3000;

app.use(cors());

var bodyParser = require("body-parser");
app.use(
    bodyParser.urlencoded({
        extended: true,
    })
);
app.use(bodyParser.json());

app.get("/", function (req, res) {
    res.sendFile(path.join(__dirname + "/index.html"));
});

/** - - - - - - - */
var requestBody = {
    "sender_batch_header": {
        "sender_batch_id": "Payouts_2018_100007",
        "email_subject": "You have a payout!",
        "email_message": "You have received a payout! Thanks for using our service!",
    },
    "items": [
        {
            "note": "Your 1$ Payout!",
            "amount": {
                "currency": "USD",
                "value": "100.00",
            },
            "receiver": "john-doe@personal.example.com",
            "sender_item_id": "Test_txn_1",
        },
        {
            "note": "Your 1$ Payout!",
            "amount": {
                "currency": "USD",
                "value": "200.00",
            },
            "receiver": "business.example.com",
            "sender_item_id": "Test_txn_2",
        },
    ],
};

// Add your credentials:
// Add your client ID and secret
var CLIENT = "abc-def-ZAFupw-kq6-ghi";
var SECRET = "xyz-ns0xjKT0dkbpJrkvnkMG4ZUZEHd-mnop567";
var PAYPAL_API = "https://api-m.sandbox.paypal.com";

var PAYOUTS_URL = "https://api-m.sandbox.paypal.com/v1/payments/payouts?";

// Set up the payment:
// 1. Set up a URL to handle requests from the PayPal button
app.post("/my-api/create-payment/", function (req, res) {
    // 2. Call /v1/payments/payment to set up the payment
    request.post(
        PAYPAL_API + "/v1/payments/payment",
        {
            auth: {
                user: CLIENT,
                pass: SECRET,
            },
            body: {
                intent: "sale",
                payer: {
                    payment_method: "paypal",
                },
                transactions: [
                    {
                        amount: {
                            total: "300.00",
                            currency: "USD",
                        },
                    },
                ],
                redirect_urls: {
                    return_url: "https://mighty-oasis-92039.herokuapp.com/",
                    cancel_url: "https://mighty-oasis-92039.herokuapp.com/",
                },
            },
            json: true,
        },
        function (err, response) {
            if (err) {
                console.error(err);
                return res.sendStatus(500);
            }
            // 3. Return the payment ID to the client
            res.json({
                id: response.body.id,
            });
        }
    );
});

/** START PAYOUTS */

app.post("/my-api/execute-payment/", function (req, res) {
    // 2. Get the payment ID and the payer ID from the request body.
    var paymentID = req.body.paymentID;
    var payerID = req.body.payerID;
    // 3. Call /v1/payments/payment/PAY-XXX/execute to finalize the payment.
    request.post(
        `${PAYOUTS_URL}paymentID`,
        {
            auth: {
                user: CLIENT,
                pass: SECRET,
            },
            requestBody,
        },
        function (err, response) {
            if (err) {
                console.error(err);
                return res.sendStatus(500);
            }
            // 4. Return a success response to the client
            res.json({
                status: "success",
            });
        }
    );
});
/** END PAYOUTS */

app.listen(port, () => console.log(`listening on port ${port}!`));
4

1 回答 1

0

您问题中的按钮代码适用于 checkout.js 和 v1/payments,它们都是已弃用的集成,用于接收付款。

您的服务器端代码用于付款,用于从您自己的帐户发送付款。这些是完全不同的事情。


如果您希望买家能够直接支付另一个帐户,请先集成当前版本的 PayPal Checkout。

服务器端“创建订单”和“捕获订单”记录在这里:https ://developer.paypal.com/docs/business/checkout/server-side-api-calls/#server-side-api-calls

将返回 JSON 数据的这两个路由与以下批准流程配对:https ://developer.paypal.com/demo/checkout/#/pattern/server

并在创建调用中,配置payee谁将收到付款:https ://developer.paypal.com/docs/checkout/integration-features/pay-another-account/

于 2021-03-25T18:05:55.273 回答