【问题标题】:How to declare variable in paypal.paypent.create and use it outside如何在 paypal.payment.create 中声明变量并在外部使用
【发布时间】:2019-12-13 14:20:03
【问题描述】:

您好,我正在研究 paypal 函数

async openOrder({request, response }) {



 const paypalResponse = await paypal.payment.create(create_payment_json, function(error, payment) {
    if (error) {
        throw error;
    } else {
        console.log(payment);
        for (let i = 0; i < payment.links.length; i++) {
            console.log(payment.links.length);
            if (payment.links[i].rel === 'approval_url') {
               // I declare varialbe here
                var paymentLink = payment.links[i].href;
                // response.redirect('payment.links[i].href');
            }
        }
    }
});
 console.log(paymentLink) // return undefined
}

我不能使用 response.redirect 的原因是因为我正在处理只返回 json api 的后端 [ 使用前端运行不同的端口] 所以我想在 json 中返回 Link url paypal。

如何在异步函数中声明我的可变支付链接

【问题讨论】:

    标签: javascript node.js paypal


    【解决方案1】:

    好吧,您误解了 javascript 的异步特性。 console.log 甚至会在付款完成之前执行。无论您需要对结果做什么,都应该在回调中给出。

    因此,将用户重定向到链接是在回调方法中完成的。我添加了return 语句,这样就不会发送多个http响应了。

    async openOrder({
        request,
        response
    }) {
    
        const payment = await paypal.payment.create(create_payment_jsonfunction(error, payment) {
            if (error) {
                throw error;
            } else {
                console.log(payment);
                // do you operations with result here
                for (let i = 0; i < payment.links.length; i++) {
                    console.log(payment.links.length);
                    if (payment.links[i].rel === 'approval_url') {
                        // I declare varialbe here
                        var paymentLink = payment.links[i].href;
    
                        return response.redirect('payment.links[i].href');
                    }
                }
            }
        });
    
    }
    

    【讨论】:

    • 我不能做这样的事情,因为在 paypal-rest-sdk 它错误 cb is not a function 我认为它需要回调函数
    猜你喜欢
    • 1970-01-01
    • 2021-05-03
    • 1970-01-01
    • 2016-05-19
    • 2011-06-29
    • 2012-02-06
    • 2011-09-29
    • 2011-03-10
    相关资源
    最近更新 更多