【问题标题】:Node.js Paypal rest sdkNode.js 贝宝休息 sdk
【发布时间】:2020-04-09 12:23:26
【问题描述】:

我是 Node.js 的新手,正在部署 Paypal rest SDK,我被困在这一点上,我想将一个参数从 paypal API 调用的“success”api 传递给由 paypal API 调用的原始 API角度

//paypal
paypal.configure({
    'mode': 'sandbox', //sandbox or live
    'client_id': '',
    'client_secret': ''
});

app.post('/pay-online', (req, res) => {
    console.log('request came')
    console.log(req.body.fare.toString())

    var create_payment_json = {
        "intent": "sale",
        "payer": {
            "payment_method": "paypal"
        },
        "redirect_urls": {
            "return_url": "localhost:3000/success",
            "cancel_url": "localhost:3000/cancel"
        },
        "transactions": [{
            "item_list": {
                "items": [{
                    "name": "item",
                    "sku": "item",
                    "price": req.body.fare,
                    "currency": "USD",
                    "quantity": 1
                }]
            },
            "amount": {
                "currency": "USD",
                "total": req.body.fare
            },
            "description": "This is the payment description."
        }]
    };


    paypal.payment.create(create_payment_json, function (error, payment) {
        if (error) {
            throw error;
        } else {
            console.log("Create Payment Response");
            console.log(payment);
            res.send(payment.links);
        }

    });

});

var getSuccess = app.get('/success', (req, res) => {   
    var paymentId = req.query.paymentId;
    var payerId = { payer_id: req.query.PayerID };

    paypal.payment.execute(paymentId, payerId, function (error, payment) {
        console.log(payment.state)
        if (error) {
            console.error(JSON.stringify(error));
        } else {
            if (payment.state == 'approved') {
                console.log('payment completed successfully');
            } else {
                console.log('payment not successful');
            }
        }
    });
});

我需要将 (payment.state) 传递到调用“在线支付”API 的页面的问题,如何将参数传递回页面?

【问题讨论】:

    标签: javascript node.js angular paypal


    【解决方案1】:

    如果您可以从“paypal.payment.create”方法中获取状态,您可以将 JSON 对象返回到调用“在线支付”API 的页面,如下所示。

    app.post('/pay-online', (req, res) => {
        console.log('request came')
        console.log(req.body.fare.toString())
    
        var create_payment_json = {
            "intent": "sale",
            "payer": {
                "payment_method": "paypal"
            },
            "redirect_urls": {
                "return_url": "localhost:3000/success",
                "cancel_url": "localhost:3000/cancel"
            },
            "transactions": [{
                "item_list": {
                    "items": [{
                        "name": "item",
                        "sku": "item",
                        "price": req.body.fare,
                        "currency": "USD",
                        "quantity": 1
                    }]
                },
                "amount": {
                    "currency": "USD",
                    "total": req.body.fare
                },
                "description": "This is the payment description."
            }]
        };
    
    
        paypal.payment.create(create_payment_json, function (error, payment) {
            if (error) {
                throw error;
            } else {
                console.log("Create Payment Response");
                console.log(payment);
                res.send({
                          payment:payment.links, 
                          state:payment.state .     <----JSON Property
                         });
            }
    
        });
    
    });
    

    如果您需要调用“paypal.payment.execute”方法来获取付款状态,那么您必须在“/pay-online”方法的成功块内调用“paypal.payment.execute”方法。

    【讨论】:

      猜你喜欢
      • 2014-02-15
      • 2016-04-25
      • 2015-10-16
      • 2013-10-18
      • 2017-02-01
      • 2017-03-01
      • 2018-10-05
      • 2015-10-29
      • 2014-11-02
      相关资源
      最近更新 更多