Node.js 在Paypal支付网关的IPN服务中,通知请求的主体未定义
我正在尝试使用“Paypal rest sdk”和“Paypal IPN”在node js中集成Paypal支付网关的IPN服务。除IPN服务外,一切正常。当IPNAPI击中我的服务器时,请求主体似乎未定义,它抛出req.body is undefined错误。我刚刚在我的沙箱商户帐户中创建了IPN请求url 我遵循的步骤 1) 登录到 2) 单击菜单选项卡中的配置文件和设置 3) 然后销售工具->即时付款通知更新-> 4) 单击选择IPN设置按钮 5) 启用IPN消息并输入url 6) 点击保存按钮,仅此而已 我想这就是我们需要做的一切。对吧? 如上所述,我正在使用“paypal ipn”来处理ipn服务。 当我使用expressjs时,我遵循了下面url中提到的配置。 代码Node.js 在Paypal支付网关的IPN服务中,通知请求的主体未定义,node.js,express,paypal,paypal-sandbox,paypal-ipn,Node.js,Express,Paypal,Paypal Sandbox,Paypal Ipn,我正在尝试使用“Paypal rest sdk”和“Paypal IPN”在node js中集成Paypal支付网关的IPN服务。除IPN服务外,一切正常。当IPNAPI击中我的服务器时,请求主体似乎未定义,它抛出req.body is undefined错误。我刚刚在我的沙箱商户帐户中创建了IPN请求url 我遵循的步骤 1) 登录到 2) 单击菜单选项卡中的配置文件和设置 3) 然后销售工具->即时付款通知更新-> 4) 单击选择IPN设置按钮 5) 启用IPN消息并输入url 6) 点击保
const express=require('express');
const ejs=require('ejs');
const paypal=require('paypal-rest-sdk');
常量ipn=要求('paypal-ipn');
paypal.configure({
'mode':'sandbox',//沙盒或live
'客户id':'XXXXXXXXXXXXXXXXXXXXXXXXXX',
“客户机密”:“XXXXXXXXXXXX”
});
常量app=express();
应用程序集(“查看引擎”、“ejs”);
app.get(“/”,(req,res)=>res.render('index_page');
应用程序发布(“/pay”,(请求、回复)=>{
const create\u payment\u json={
“意图”:“出售”,
“付款人”:{
“付款方式”:“贝宝”
},
“重定向URL”:{
“返回url”:http://bef4cbb1.ngrok.io/success",
“取消url”:http://bef4cbb1.ngrok.io/cancel",
},
“交易”:[{
“项目清单”:{
“项目”:[{
“姓名”:“衬衫”,
“数量”:“001”,
“价格”:“50.00”,
“货币”:“美元”,
“数量”:1
}]
},
“金额”:{
“货币”:“美元”,
“总计”:“50.00”
},
“描述”:“为有史以来最好的团队戴上帽子”
}]
};
paypal.payment.create(创建付款,函数(错误,付款){
如果(错误){
投掷误差;
}否则{
控制台日志(支付);
for(设i=0;i{
const payerId=req.query.payerId;
const paymentId=req.query.paymentId;
const execute\u payment\u json={
“付款人id”:付款人id,
“交易”:[{
“金额”:{
“货币”:“美元”,
“总计”:“50.00”
}
}]
};
paypal.payment.execute(paymentId,execute\u payment\u json,函数(error,payment){
如果(错误){
console.log(error.response);
投掷误差;
}否则{
log(“----响应----”+JSON.stringify(支付));
res.redirect(“/payment_success”);
}
});
});
app.post(“/paymnet\u details\u notification”,(请求、回复)=>{
log(“----success\u notification--start---”;
ipn.验证(需求主体、功能(错误、付款){
如果(错误){
log(“----成功\通知---错误---”;
console.log(error.response);
投掷误差;
}否则{
log(“----成功\-通知-成功----”;
}
});
返回“收到数据通知”
});
app.get('/payment\u success',(req,res)=>res.render('payment\u success');
app.get('/payment\u failed',(req,res)=>res.render('payment\u failed');
app.get('/cancel',(req,res)=>res.render('cancel');
app.listen(3000,()=>console.log('Server Started');
我非常确定您需要与REST API而不是IPN一起使用。好的,我将切换到Webhook。在Webhook中,我也面临同样的问题,如何解析请求主体。它现在似乎没有定义它的工作方式,忘记添加“主体解析器”,即y。。
const express = require('express');
const ejs = require('ejs');
const paypal = require('paypal-rest-sdk');
const ipn = require('paypal-ipn');
paypal.configure({
'mode': 'sandbox', //sandbox or live
'client_id': 'XXXXXXXXXXXXXXXXXXXXXXXXXX',
'client_secret': 'XXXXXXXXXXXXX'
});
const app = express();
app.set('view engine', 'ejs');
app.get('/', (req, res) => res.render('index_page'));
app.post('/pay', (req, res) => {
const create_payment_json = {
"intent": "sale",
"payer": {
"payment_method": "paypal"
},
"redirect_urls": {
"return_url": "http://bef4cbb1.ngrok.io/success",
"cancel_url": "http://bef4cbb1.ngrok.io/cancel",
},
"transactions": [{
"item_list": {
"items": [{
"name": "Shirt",
"quantity": "001",
"price": "50.00",
"currency": "USD",
"quantity": 1
}]
},
"amount": {
"currency": "USD",
"total": "50.00"
},
"description": "Hat for the best team ever"
}]
};
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++){
if(payment.links[i].rel === 'approval_url'){
res.redirect(payment.links[i].href);
}
}
}
});
});
app.get('/success', (req, res) => {
const payerId = req.query.PayerID;
const paymentId = req.query.paymentId;
const execute_payment_json = {
"payer_id": payerId,
"transactions": [{
"amount": {
"currency": "USD",
"total": "50.00"
}
}]
};
paypal.payment.execute(paymentId, execute_payment_json, function (error, payment) {
if (error) {
console.log(error.response);
throw error;
} else {
console.log("-------Response--------"+JSON.stringify(payment));
res.redirect("/payment_success");
}
});
});
app.post('/paymnet_details_notification', (req, res) => {
console.log("-----success_notification-- start---");
ipn.verify(req.body, function(error,payment){
if (error) {
console.log("-----success_notification-- error---");
console.log(error.response);
throw error;
} else {
console.log("-----success_notification- success----");
}
});
return "Data notification received"
});
app.get('/payment_success', (req, res) => res.render('payment_success'));
app.get('/payment_failed', (req, res) => res.render('payment_failed'));
app.get('/cancel', (req, res) => res.render('cancel'));
app.listen(3000, () => console.log('Server Started'));