我正在尝试使用 nodejs 进行条带支付,但出现以下错误。一个在控制台中,一个在浏览器中的 Web 应用程序 UI 中。原因是什么?此外,我正在尝试使用保存在 mongoDB 中的 req.user 属性详细信息。我收到控制台错误:(node:6344) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'subscriptionPlan' of undefined
和浏览器=>
Error: You cannot use a Stripe token more than once: tok_1IJggu*******************.
下面是我的代码:
router.post('/processPayment/:priceID', (req, res) => {
console.log("plan Token = "+ req.body.stripeToken);
console.log("plan Email = "+ req.body.stripeEmail);
var dateFormated, dateFormated2;
return stripe2.customers.create({
source: req.body.stripeToken,
email: req.body.stripeEmail
}).then(customer => {
stripe2.subscriptions.create({
customer: customer.id,
items: [
{
plan: req.params.priceID
}
]
}).then(subscription => {
console.log("subscription.status = "+ subscription.status);
dateFormated = new Date();
dateFormated2 = new Date();
console.log(dateFormated2.getMinutes() +' '+dateFormated2.getHours()+' '+dateFormated2.getDate() + ' * *');
var j = schedule.scheduleJob(dateFormated2.getMinutes() +' '+dateFormated2.getHours()+' '+dateFormated2.getDate() + ' * *',function(){
var snm = new Date();
return stripe2.subscriptions.retrieve(req.user.subsID)
.then(subsData => {
console.log("Status = "+ subsData.status);
if(subsData.status == 'active')
{
User.findOneAndUpdate({_id:req.user.id},
{$set: {"subscriptionDuplicateDate":req.user.subscriptionNextMonth }},
function (error, data) {
if (error) {
console.log(error);
return res.render("error",{error:error});
}
else if(req.user.subscriptionPlan == '$299' || req.user.subscriptionPlan == '$499' || req.user.subscriptionPlan == '$999')
{
snm = snm.setMonth(snm.getMonth() + 1);
console.log("snm = "+ snm);
User.findOneAndUpdate({_id:req.user.id},
{$set: {"subscriptionNextMonth" : new Date(snm.toLocaleString())}},
function (error, data) {
if (error) {
console.log(error);
return res.render("error",{error:error});
}
})
console.log('Your scheduled job every month');
}
else if(req.user.subscriptionPlan == '$2990' || req.user.subscriptionPlan == '$4990' || req.user.subscriptionPlan == '$9990')
{
snm = snm.setFullYear(snm.getFullYear() + 1);
console.log("snm = "+ snm);
User.findOneAndUpdate({_id:req.user.id},
{$set: {"subscriptionNextMonth" : new Date(snm.toLocaleString())}},
function (error, data) {
if (error) {
console.log(error);
return res.render("error",{error:error});
}
})
console.log('Your scheduled job every year');
}
})
}
else {
User.findOneAndUpdate({_id:req.user.id},
{$set: {"subscriptionTotalCount" : 0}},
function (error, data) {
if (error) {
console.log(error);
return res.render("error",{error:error});
}
})
}
}).catch(err => {
res.render('error', {message: err});
});
});
if(req.user.subscriptionPlan == '$299' || req.user.subscriptionPlan == '$499' || req.user.subscriptionPlan == '$999')
{
User.findOneAndUpdate({_id:req.user.id},
{$set: {"subscriptionDate":dateFormated,
"subscriptionDuplicateDate":dateFormated,
"subscriptionNextMonth":new Date(dateFormated.setMonth(dateFormated.getMonth() + 1))}},
function (error, data) {
if(data)
console.log("subscription date set");
})
}
else if(req.user.subscriptionPlan == '$2990' || req.user.subscriptionPlan == '$4990' || req.user.subscriptionPlan == '$9990')
{
User.findOneAndUpdate({_id:req.user.id},
{$set: {"subscriptionDate":dateFormated,
"subscriptionDuplicateDate":dateFormated,
"subscriptionNextMonth":new Date(dateFormated.setFullYear(dateFormated.getFullYear() + 1))}},
function (error, data) {
if(data)
console.log("subscription date set");
})
}
// console.log("subscription.items = "+ JSON.stringify(subscription.items));
console.dir(subscription);
res.redirect('/thankyou/0d4a3bc9cd71c4500584f01e7ce74cc6f2a4967ca266f3b7578ff29bd640dbbc/'+customer.id+'/'+subscription.id);
})
}).catch(err => {
res.render('error', {message: err});
});
});