条带订阅未更新数量

时间:2018-09-10 22:43:30

标签: node.js mongodb express stripe-payments

我有一个相当简单的应用程序,它只允许您创建一个管理员用户,该用户可以在其帐户下创建子用户。管理员创建另一个用户时,我希望他们使用其当前用户数(按存储在user.company.subUserCount上)更新带区上的订阅。但是,当我这样做时,不会用正确的值更新用户模型,并且条带也不会更新。

希望有人可以看看我的代码片段,看看它有什么问题导致它不更新Stripe。

路线

// POST USER/NEW
    app.post('/user/new', 
    isAuthenticated,
    company.postNewUserPlan,
    sessions.postSignupSub,
    (req, res) => {
        User.findById(req.user.id, function(err, user) {
            user.company.subUserCount = req.user.company.subUserCount + 1;
            user.save();
        });
    });

company.postNewUserPlan

exports.postNewUserPlan = function(req, res, next){
  var plan = req.user.company.stripe.plan;
  var coupon = null;
  var stripeToken = null;
  plan = plan.toLowerCase();

  if(req.body.stripeToken){
    stripeToken = req.body.stripeToken;
  }

  User.findById(req.user.id, function(err, user) {
    if (err) return next(err);
    var quantity = user.company.subUserCount + 1;
    user.setPlan(plan, coupon, quantity, stripeToken, function (err) {
      var msg;

      if (err) {
        if(err.code && err.code == 'card_declined'){
          msg = 'Your card was declined. Please provide a valid card.';
        } else if(err && err.message) {
          msg = err.message;
        } else {
          msg = 'An unexpected error occurred.';
        }
        req.flash('errors', msg);
        return res.redirect('/user/create');
      }
    });
  });
  next();
};

sessions.postSignupSub护照代码

passport.use('signup-sub', new LocalStrategy({
      usernameField: 'email',
      passReqToCallback : true
    },
    function(req, email, password, done) {
        User.findOne({ email: req.body.email }, function(err, existingUser) {
          if(err){
            console.log(err);
          }
          if (existingUser) {
            req.flash('form', {
              email: req.body.email
            });
            return done(null, false, req.flash('error', 'An account with that email address already exists.'));
          }
          var preRole = req.body.role;
          var role = ''
          if (preRole === undefined) {
            role = 'manager';
          } else if (preRole === 'on')  {
            role = 'employee';
          }
          // edit this portion to accept other properties when creating a user.
          var user = new User({
            email: req.body.email,
            password: req.body.password, // user schema pre save task hashes this password
            role: role,
            companyID: req.user.companyID,
            isVerified: true
          });

          user.save(function(err) {
            if (err) return done(err, false, req.flash('error', 'Error saving user.'));

            var time = 14 * 24 * 3600000;
            req.session.cookie.maxAge = time; //2 weeks
            req.session.cookie.expires = new Date(Date.now() + time);
            req.session.touch();
            return done(null, user, req.flash('success', `Your new ${role} has been created`));
          });
        });

    })
  );

1 个答案:

答案 0 :(得分:0)

经过一些测试,我设法解决了与OP中未包含的代码有关的问题,我发现订阅不是用数量进行更新,而是仅用其创建。

代码

schema.methods.setPlan = function(plan, coupon, quantity, stripe_token, cb) {
    var user = this;

    var subscriptionHandler = function(err, subscription) {
      if(err) return cb(err);

      user.company.stripe.plan = plan;
      user.company.stripe.subscriptionId = subscription.id;
      user.company.subUserCount = quantity;
      user.save(function(err){
        if (err) return cb(err);
        return cb(null);
      });
    };

    var createSubscription = function(){
      stripe.customers.createSubscription(
        user.company.stripe.customerId,
        {plan: plan, coupon: coupon, quantity: quantity},
        subscriptionHandler
      );
    };

    if(stripe_token) {
      user.setCard(stripe_token, function(err){
        if (err) return cb(err);
        createSubscription();
      });
    } else {
      if (user.company.stripe.subscriptionId){
        // update subscription
        stripe.customers.updateSubscription(
          user.company.stripe.customerId,
          user.company.stripe.subscriptionId,
          { plan: plan, coupon: coupon, ***quantity: quantity*** }, <-- Part I had to change
          subscriptionHandler
        );
      } else {
        createSubscription();
      }
    }
  };