node.js - Node 和 Paypal : Retrieve User Info after Payment

标签 node.js paypal

我正在使用本教程: https://www.nodejsera.com/paypal-payment-integration-using-nodejs-part2.html

它实现PayPal的方式是:

// start payment process 
app.get('/buy' , ( req , res ) => {
    // create payment object 
    var payment = {
            "intent": "authorize",
    "payer": {
        "payment_method": "paypal"
    },
    "redirect_urls": {
        "return_url": "http://127.0.0.1:3000/success",
        "cancel_url": "http://127.0.0.1:3000/err"
    },
    "transactions": [{
        "amount": {
            "total": 39.00,
            "currency": "USD"
        },
        "description": " a book on mean stack "
    }]
    }   

    // call the create Pay method 
    createPay( payment ) 
        .then( ( transaction ) => {
            var id = transaction.id; 
            var links = transaction.links;
            var counter = links.length; 
            while( counter -- ) {
                if ( links[counter].method == 'REDIRECT') {
                    // redirect to paypal where user approves the transaction 
                    return res.redirect( links[counter].href )
                }
            }
        })
        .catch( ( err ) => { 
            console.log( err ); 
            res.redirect('/err');
        });
}); 
// helper functions 
var createPay = ( payment ) => {
    return new Promise( ( resolve , reject ) => {
        paypal.payment.create( payment , function( err , payment ) {
         if ( err ) {
             reject(err); 
         }
        else {
            resolve(payment); 
        }
        }); 
    });
}       

然后用户被相应地重定向:

app.get('/success' , (req ,res ) => {
    res.redirect('/success.html');
    console.log(req.query);
})

// error page
app.get('/err' , (req , res) => {
    console.log(req.query);
    res.redirect('/err.html');
})

我正在使用 Sandbox,虚拟支付成功。我想在付款完成后向买家(在本例中为我自己)发送一封电子邮件。我如何检索该信息(如果付款成功、用户电子邮件和姓名等)?

还有关于在 node.js 中使用库来实现自动邮件的任何建议(nodemailer 是一个选项,还是已弃用?)?

谢谢

最佳答案

我不熟悉 paypal api。但我认为有两点需要牢记。

  • 创建一个 webhook 来接收通知(对于处理那里的状态变化很重要)

  • 在重定向端点捕获即时结果

请仔细检查您的重定向端点已从 paypal 收到的内容。

app.get('/success' , (req ,res ) => {
    console.log(req.query); // should include payer id
    res.redirect('/success.html');
})

关于node.js - Node 和 Paypal : Retrieve User Info after Payment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49105049/

相关文章:

mysql - Nodejs 12 回调不适用于 mysql 连接?

javascript - 在 PayPal API returnURL 上访问 $_POST 数据

ios - Xcode 5.1 和架构 x86_64 的编译错误

google-chrome - Chrome 中无法使用 PayPal 网页

android - paypal android sdk 货币错误错误

node.js - 如何从 Node.js/Express 应用程序的 Mongoose 预 Hook 中查询?

javascript - 不兑现的 promise

node.js - 如何安装express框架

node.js - 如何连接到moto独立服务器?

jquery - 文本字段的递增名称属性