node.js - 如何在执行 Paypal 付款后重定向?

标签 node.js express paypal-sandbox paypal-rest-sdk

我按照有关如何使用 SDK API 从 PayPal ( https://developer.paypal.com/docs/checkout/integrate/ ) 添加智能支付按钮的说明进行操作。一切正常,除了在执行付款后我无法重定向买家。

HTML 页面中的 JS 代码如下所示:

paypal.Buttons({
        createOrder: function (data, actions) {
            return actions.order.create({
                purchase_units: [{
                    amount: {
                        value: price
                    }
                }]
            });
        },
        onApprove: async function (data, actions) {
            return actions.order.capture().then(function (details) {
                alert('success!');
                return fetch('/paypal-transaction-complete', {
                    method: 'post',
                    headers: {
                        'content-type': 'application/json'
                    },
                    body: JSON.stringify({
                        orderID: data.orderID,
                    })
                });
            });
        }
    }).render('#paypal-button-container');

在服务器端,我使用异步函数执行它并使用箭头函数等待 promise :
app.post('/paypal-transaction-complete', function (req, res) {
    paypalRequestHandler.handleRequest(req, res)
        .then(() => {
            res.redirect('/'); // not working
        }).catch(err => {
        console.log(err);
        res.sendStatus(500);
    });
});


我想知道为什么它不重定向,我可以执行 console.log() 之类的操作,但它不会重定向买家。

最佳答案

回答我自己的问题:在服务器端得到promise后,应该向客户端返回一个响应码,然后在客户端可以更改页面的位置。所以就我而言,它在服务器端看起来像这样:

app.post('/paypal-transaction-complete', function (req, res) {
    paypalRequestHandler.handleRequest(req, res)
        .then(() => {
            res.sendStatus(200);
        }).catch(err => {
        console.log(err);
        res.sendStatus(500);
    });
});

在客户端:
paypal.Buttons({
        createOrder: function (data, actions) {
            return actions.order.create({
                purchase_units: [{
                    amount: {
                        value: price
                    }
                }]
            });
        },
        onApprove: async function (data, actions) {
            return actions.order.capture().then(function (details) {
                alert('success!');
                const responsePromise = fetch('/paypal-transaction-complete', {
                    method: 'post',
                    headers: {
                        'content-type': 'application/json'
                    },
                    body: JSON.stringify({
                        orderID: data.orderID,
                    })
                });
                responsePromise.then(function (responseFromServer) {
                    if(responseFromServer.status === 200) {
                        location.href = 'success_page';
                    } else {
                        alert('smth went wrong');
                         location.href = '/';
                        })
                    }

                });
            });
        }
    }).render('#paypal-button-container');

关于node.js - 如何在执行 Paypal 付款后重定向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58274818/

相关文章:

node.js - 使用先前数据的条件对 upsert 进行 Sequelize

javascript - 哪个 rsa 库推荐用于 "react native"平台?

sql-server - 在 Linux 上使用 Node.js 访问 SQL Server

node.js - 如何使用 nodejs 和 express 在 REST API 中实现搜索和过滤

尝试使用付款人 ID 和金额调用 Refund API 方法时,PayPal SOAP API 错误 13606

javascript - Promise 链接错误处理

javascript - 在 Google Cloud Functions 中使用 process.exit

javascript - Neo4j JavaScript 驱动程序(bolt)Cypher 通过 id 查询不起作用 => 错误 : Expected entity id to be an integral value

Paypal MPL iOS 错误 : internal server error in iOS

paypal-sandbox - paypal payment direct pro sandbox 仅适用于美国账户?