如何在Paypal上将资金从一个帐户转移到另一个帐户

时间:2021-03-27 04:37:49

标签: javascript html node.js paypal payment

我已经尝试了三天,但没有成功。当我 Capture payment for order 时什么也没有发生。没有资金从买方/收款人账户转移到卖方账户。没有交易得到反映。完全没有。

您如何在支付订单时转移资金?

先谢谢大家。

客户端:index.html

<!DOCTYPE html>
<html lang="en">

<head>
    <!-- Add meta tags for mobile and IE -->
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <title> PayPal Smart Payment Buttons Integration | Server Demo </title>
</head>

<body>
    <!-- Set up a container element for the button -->
    <div id="paypal-button-container"></div>

    <!-- Include the PayPal JavaScript SDK -->
    <script src="https://www.paypal.com/sdk/js?client-id=AehnkBdF5YqJJP-50mQSYBJpg_OWguVkwn0qCOztGt7G-ZAFupw-kq6-k_O1havNvHuhT0R5SJjf4anE&currency=USD"></script>

    <script>
        // Render the PayPal button into #paypal-button-container
        paypal.Buttons({

            // Call your server to set up the transaction
            createOrder: function(data, actions) {
                return fetch('/demo/checkout/api/paypal/order/create/', {
                    method: 'post'
                }).catch(error => {
                    console.error('ERR -> fetch : ' + error.message);
                }).then(function(res) {
                    return res.json();
                }).catch(error => {
                    console.error('ERR -> res : ' + error.message);
                }).then(function(orderData) {
                    return orderData.id;
                }).catch(error => {
                    console.error('ERR -> orderData : ' + error.message);
                });
            },

            // Call your server to finalize the transaction
            onApprove: function(data, actions) {
                console.log('onApprove : data : ' + JSON.stringify(data));

                return fetch('/demo/checkout/api/paypal/order', {
                    method: 'POST',
                    headers: {
                    'Accept': 'application/json',
                    'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({'orderID': data.orderID})
                }).then(function(res) {
                    return res.json();
                }).then(function(orderData) {
                    console.log('onApprove : orderData : ' + JSON.stringify(orderData));

                    // Three cases to handle:
                    //   (1) Recoverable INSTRUMENT_DECLINED -> call actions.restart()
                    //   (2) Other non-recoverable errors -> Show a failure message
                    //   (3) Successful transaction -> Show confirmation or thank you

                    // This example reads a v2/checkout/orders capture response, propagated from the server
                    // You could use a different API or structure for your 'orderData'
                    var errorDetail = Array.isArray(orderData.details) && orderData.details[0];

                    if (errorDetail && errorDetail.issue === 'INSTRUMENT_DECLINED') {
                        return actions.restart(); // Recoverable state, per:
                        // https://developer.paypal.com/docs/checkout/integration-features/funding-failure/
                    }

                    if (errorDetail) {
                        var msg = 'Sorry, your transaction could not be processed.';
                        if (errorDetail.description) msg += '\n\n' + errorDetail.description;
                        if (orderData.debug_id) msg += ' (' + orderData.debug_id + ')';
                        return alert(msg); // Show a failure message
                    }

                    // Show a success message
                    alert('Transaction completed by ' + JSON.stringify(orderData));
                });
            }

        }).render('#paypal-button-container');
    </script>
</body>

</html>

服务器端:NODE.JS:app.js

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());

// Add your credentials:
// Add your client ID and secret
var CLIENT = "AehnkBdF5YqJJP-50mQSYBJpg_OWguVkwn0qCOztGt7G-ZAFupw-kq6-k_O1havNvHuhT0R5SJjf4anE";
var SECRET = "ED6FI5upB-ns0xjKT0dkbpJrkvnkMG4ZUZEHd-0GbPrTe9cQXCVzRXMiciJqgv3Th1RrqYBqIjhl9Tdm";
var PAYPAL_API = "https://api-m.sandbox.paypal.com";

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

// Set up the payment:
// 1. Set up a URL to handle requests from the PayPal button
app.post("/demo/checkout/api/paypal/order/create/", function (req, res) {
    // 2. Call /v2/checkout/orders to set up the Order
    request.post(
        PAYPAL_API + "/v2/checkout/orders",
        {
            auth: {
                user: CLIENT,
                pass: SECRET,
            },
            body: {
                intent: "CAPTURE",
                payer: {
                    payment_method: "paypal",
                },
                purchase_units: [
                    {
                        amount: {
                            currency_code: "USD",
                            value: "19.17",
                        },
                        payee: {
                            email_address: "payments@business.example.com",
                        },
                    },
                ],
                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 EXECUTE PAYMENT */
// 1. Set up a URL to handle requests from the PayPal button.
app.post("/demo/checkout/api/paypal/order/", async function (req, res) {
    // 2. Get the payment ID and the payer ID from the request query.
    var orderID = req.body.orderID;

    // 3. Call /v2/checkout/orders/{id}/capture to Capture payment for order.
    request.post(
        PAYPAL_API + "/v2/checkout/orders/" + orderID + "/capture",
        {
            auth: {
                user: CLIENT,
                pass: SECRET,
            },
            "PayPal-Request-Id": new Date().getTime() + "",
            json: true,
        },
        function (err, response) {
            if (err) {
                console.log("authorization >>> " + err);
                return res.sendStatus(500);
            }

            // 4. Return a success response to the client
            res.send(response);
        }
    );
});
/** END EXECUTE PAYMENT */

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

0 个答案:

没有答案
相关问题