执行 paypal 付款后如何重定向?

M_e*_*ght 5 paypal-sandbox node.js express 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');
Run Code Online (Sandbox Code Playgroud)

在服务器端,我使用异步函数执行它,并使用箭头函数等待承诺:

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

Run Code Online (Sandbox Code Playgroud)

我想知道为什么它不重定向,我可以做像 console.log() 这样的事情,但它只是不会重定向买家。

M_e*_*ght 3

回答我自己的问题:在服务器端获得承诺后,应将响应代码返回给客户端,然后在客户端可以更改页面的位置。所以就我而言,它在服务器端看起来像这样:

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

在客户端:

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');
Run Code Online (Sandbox Code Playgroud)