检查Stripe是否已按月收费

时间:2018-05-14 07:51:45

标签: javascript node.js express stripe-payments

我正在建立一个网站应用,它有一个地方,只有高级用户才能到达。如何检查用户是否支付并且是否付费?现在我这样做:

var time = req.params.time;
console.log(time);
if (time === '1') {
    var amount = 1499;
    var days = 30;
}
else if (time === '3') {
    amount = 999 * 3;
    days = 90;
}
else res.render('payment/error', {title: 'Error', user: req.user});
//^^^^ error/security handling ^^^^//

console.log(req.user.username);
stripe.charges.create({
    amount: amount,
    currency: "cad",
    source: req.body.stripeToken,
    description: "Charge for DigitLead website evaluation tool",
    receipt_email: req.user.username,
}).then(ch => {
    console.log(ch);
    Account.findById(req.user._id).then(u => {
        var date = new Date().getTime();
        console.log(date);
        u.lastPaid = date;
        date += (days * 24 * 60 * 60 * 1000);
        console.log(date);
        u.premiumEnds = date;
        console.log(u);
        u.save();
        res.render('payment/summary', {
            title: 'Payment Summary',
            user: req.user,
            date: u.premiumEnds,
        })
    });
}).catch(e => console.log(e));

但我不喜欢这样,因为如果我将来需要对支付系统做出更多更改,我会永远带我这样做。我可以使用Stripe API执行此操作还是必须使用自己的代码?我也很感激其他建议。

这是我的模特。我知道我需要改变它

var mongoose = require('mongoose');

var plm = require('passport-local-mongoose');

var accountSchema = new mongoose.Schema({
    accType: String,
    websites: [],
    keys: [],
    creditCardNo: Number,
    alexaCalls: Number,
    stripeId: String,
    lastPaid: Date,
    premiumEnds: Date,
});

accountSchema.plugin(plm);

module.exports = mongoose.model('Account', accountSchema);

0 个答案:

没有答案