[英]Stripe payment express js :transaction details is missing
I am following official stripe documentation for setting up the payment 我正在遵循官方的条纹文档来设置付款
https://stripe.com/docs/checkout/express ,everything works expected but couple of things is missing https://stripe.com/docs/checkout/express ,一切正常,但缺少一些内容
1)Transaction details ie id,payment status etc 1)交易明细,例如身份证,付款状态等
stripe.customers.create(body)
.then(customer =>
{
console.log("customer",customer);
stripe.charges.create({
amount,
description: "Sample Charge",
currency: "usd",
customer: customer.id
}
)
} )
.then(charge =>{
console.log("charge",charge);
res.render("charge.pug")
}).catch(error=>{
console.log("Error",error);
});
});
console.log("charge",charge);
gives undefined
给出
undefined
2) Do i need to protect the POST
api ?? 2)我需要保护
POST
api吗?
app.post("/charge", (req, res) => {
let amount = 500;
let body = {
email: req.body.stripeEmail,
source: req.body.stripeToken
};
stripe.customers.create(body)
.then(customer =>
{
console.log("customer",customer);
stripe.charges.create({
amount,
description: "Sample Charge",
currency: "usd",
customer: customer.id
}
)
} )
.then(charge =>{
console.log("charge",charge);
res.render("charge.pug")
}).catch(error=>{
console.log("Error",error);
});
});
You need to return
: 您需要
return
:
app.post("/charge", (req, res) => {
let amount = 500;
let body = {
email: req.body.stripeEmail,
source: req.body.stripeToken
};
stripe.customers.create(body)
.then(customer => {
console.log("customer",customer);
return stripe.charges.create({ // <-- return the Promise
amount,
description: "Sample Charge",
currency: "usd",
customer: customer.id
})
})
.then(charge =>{
console.log("charge",charge);
res.render("charge.pug")
})
.catch(error => console.log("Error",error));
});
And no you don't need to protect the route. 而且,您不需要保护路线。 You don't see online merchants protect their routes, however, typically they have some sort of "Checkout as Guest" option. 您不会看到在线商家保护其路线,但是,通常来说,他们有某种“以客人结帐”选项。
transaction details is missing
交易明细丢失
You are resolving promise of stripe.customers.create(body)
twice and not on stripe.charges.create
So to solve your first problem resolve promise( .then()
) to stripe.charges.create
as follows 您正在两次解决
stripe.customers.create(body)
promise,而不是在stripe.charges.create
因此,要解决您的第一个问题,将promise( stripe.charges.create
.then()
)解决为stripe.charges.create
,如下所示
stripe.customers.create(body)
.then(customer => {
console.log("customer", customer);
stripe.charges.create({
amount,
description: "Sample Charge",
currency: "usd",
customer: customer.id
}
).then(charge => {
console.log("charge", charge); //Here you will get transaction info now
res.render("charge.pug")
}).catch(error => {
console.log("Error", error);
})
}).catch(error => {
console.log("Error", error);
});
})`
Do i need to protect the POST api ??
我需要保护POST API吗?
Yes. 是。 You should protect your each and every public API.
您应该保护每个公共API。 You can use JWT or any other oAUTH to protect your routes.
您可以使用JWT或任何其他oAUTH保护您的路由。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.