Node.js 贝宝休息 sdk

Posted

技术标签:

【中文标题】Node.js 贝宝休息 sdk【英文标题】:Node.js Paypal rest 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 的页面的问题,如何将参数传递回页面?

【问题讨论】:

【参考方案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”方法。

【讨论】:

以上是关于Node.js 贝宝休息 sdk的主要内容,如果未能解决你的问题,请参考以下文章

贝宝休息api

贝宝休息访问令牌调用的主体是啥格式和内容类型?

使用休息的贝宝交易

有没有办法设置参考交易贝宝的休息 API

PHP |贝宝休息 API | java.lang.NullPointerException

在 post、put 和 delete 上使用 mongoose 和 node.js 休息 api