【问题标题】:How to redirect after an execution of a paypal payment?执行贝宝付款后如何重定向?
【发布时间】:2020-02-05 01:26:20
【问题描述】:

我按照说明如何使用 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');

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

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() 之类的操作,但它不会重定向买家。

【问题讨论】:

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


    【解决方案1】:

    回答我自己的问题:在服务器端得到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');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-22
      • 2012-11-28
      • 2011-07-25
      • 2010-12-22
      • 2016-06-17
      相关资源
      最近更新 更多