简体   繁体   English

Node.js Paypal rest sdk

[英]Node.js Paypal rest sdk

I'm new to Node.js and was deploying Paypal rest SDK, I'm stuck at this point, I want to pass a parameter from 'success' api that called by the paypal API, to the original API called by angular我是 Node.js 的新手并且正在部署 Paypal rest SDK,我被困在这一点上,我想将一个参数从由 Paypal API 调用的“成功”api 传递给由 angular 调用的原始 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');
            }
        }
    });
});

My question that I need to pass (payment.state) to the page that called 'pay-online' API, how can I pass the parameter back to the page?我的问题是,我需要将 (payment.state) 传递给名为“pay-online” API 的页面,如何将参数传递回页面?

You can return a JSON object to the page which calls the 'pay-online' API as follows if you can get the state from "paypal.payment.create" method.如果您可以从“paypal.payment.create”方法获取状态,您可以将一个 JSON 对象返回到调用“pay-online”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
                     });
        }

    });

});

If you need to call the "paypal.payment.execute" method to get the payment state then you have to call "paypal.payment.execute" method inside the '/pay-online' method's success block.如果您需要调用“paypal.payment.execute”方法来获取付款状态,那么您必须在“/pay-online”方法的成功块中调用“paypal.payment.execute”方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM