将两个函数合并为一个Stripe和Passport

时间:2017-11-13 23:32:24

标签: javascript node.js express stripe-payments passport.js

我目前已设置,因此用户只需使用他们的电子邮件和密码即可注册。简单易行,我希望他们能够从select元素中选择一个计划。如果选择的req.body.planname,我知道如何使用plan访问我想要的部分请求。

我目前有两个独立的控制器功能以不同的方式执行此操作。我有它所以postSignup使用passport.js签署用户,一切都很好。我还有postPlan当用户在/billing并选择一个计划时,然后提交他们已分配给他们的计划表单。我坚持的部分是User.findById(req.user.id, function(err, user) {部分,更具体地说是req.user.id部分。如何使用下面的两个函数并将它们组合在一起,以便根据注册表单中的select元素设置用户的计划。

postPlan

exports.postPlan = function(req, res, next){
  var plan = req.body.plan;
  console.log("plan: ",req.body.plan);
  var stripeToken = null;

  if(plan){
    plan = plan.toLowerCase();
  }

  if(req.user.stripe.plan == plan){
    req.flash('info', {msg: 'The selected plan is the same as the current plan.'});
    return res.redirect(req.redirect.success);
  }

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

  if(!req.user.stripe.last4 && !req.body.stripeToken){

    req.flash('errors', {msg: 'Please add a card to your account before choosing a plan.'});
    return res.redirect(req.redirect.failure);
  }

  User.findById(req.user.id, function(err, user) {
    if (err) return next(err);

    user.setPlan(plan, 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:  msg});
        return res.redirect(req.redirect.failure);
      }
      req.flash('success', { msg: 'Plan has been updated.' });
      res.redirect(req.redirect.success);
    });
  });
};

postSignup

    exports.postSignup = function(req, res, next){
      req.assert('email', 'Please sign up with a valid email.').isEmail();
      req.assert('password', 'Password must be at least 6 characters long').len(6);

      var errors = req.validationErrors();

      if (errors) {
        req.flash('errors', errors);
        req.flash('form', {
          email: req.body.email
        });
        return res.redirect('/signup');
      }
      // calls next middleware to authenticate with passport
      passport.authenticate('signup', {
        successRedirect: '/dashboard', // Select redirect for post signup
        failureRedirect: '/signup',
        failureFlash : true
      })(req, res, next);
        next();
    };

当前的想法

    exports.postSignup = function(req, res, next){
  req.assert('email', 'Please sign up with a valid email.').isEmail();
  req.assert('password', 'Password must be at least 6 characters long').len(6);

  var errors = req.validationErrors();

  if (errors) {
    req.flash('errors', errors);
    req.flash('form', {
      email: req.body.email
    });
    return res.redirect('/signup');
  }
  // calls next middleware to authenticate with passport
  passport.authenticate('signup', {
    successRedirect: '/dashboard', // Select redirect for post signup
    failureRedirect: '/signup',
    failureFlash : true
  });
  // var plan = req.body.plan;
  var plan = 'silver';
  var stripeToken = null;
  if(req.body.stripeToken){
    stripeToken = req.body.stripeToken;
  }
  User.findById({"email":req.body.email}, function(err, user) {
    if (err) return next(err);
    console.log("Ran here");
    user.setPlan(plan, stripeToken, function (err) {
      var msg;
      if (err) {
        if(err && err.message) {
          msg = err.message;
        } else {
          msg = 'An unexpected error occurred.';
        }
        req.flash('errors', { msg:  msg});
        return res.redirect(req.redirect.failure);
      }
      req.flash('success', { msg: 'Thanks for signing up! ' });
      res.redirect(req.redirect.success);
    });
  });

  (req, res, next);
    next();
};

上方当前尝试的错误消息

{
  "message": "Cast to ObjectId failed for value \"{ email: 'snappierjaguar@gmail.com' }\" at path \"_id\" for model \"User\"",
  "error": {
    "message": "Cast to ObjectId failed for value \"{ email: 'snappierjaguar@gmail.com' }\" at path \"_id\" for model \"User\"",
    "name": "CastError",
    "stringValue": "\"{ email: 'snappierjaguar@gmail.com' }\"",
    "kind": "ObjectId",
    "value": {
      "email": "snappierjaguar@gmail.com"
    },
    "path": "_id"
  }
}

0 个答案:

没有答案