无法改变计划|条纹

时间:2017-10-06 22:35:42

标签: node.js express stripe-payments

编辑 Project repo

我正在使用锅炉板并且已经与创建者记录了这个问题,但希望尽快得到帮助。我的问题是,当我去改变计划时,它告诉我请在选择计划之前添加一张卡片。该页面的代码如下:

表格:

`<form {% if !user.stripe.last4 %}id="cardForm"{% endif %} 
action="/user/plan" method="POST" class="form-horizontal">
  <div class="form-group">
    <label for="plan" class="col-sm-3 control-label">Plan</label>
    <div class="col-sm-4">
      {% for plan in plans %}
        <div class="radio">
          <label>
            <input type="radio" {% if user.stripe.plan == loop.key 
%}checked{% endif %} name="plan" value="{{loop.key}}" data-toggle="radio">
            <span>{{plan.name}} - ${{plan.price}}</span>
          </label>
        </div>
      {% endfor %}
    </div>
  </div>
  {% if !user.stripe.last4 %}
  <div id="cardWrapper">
    <div class="form-group">
      <label class="col-sm-3 control-label">Test Card Info</label>
      <div class="col-sm-7">
        <div class="form-control-static">4242424242424242, 11/19, 123 - <a 
href="https://stripe.com/docs/testing">additional test info</a></div>
      </div>
    </div>
    <div class="form-group">
      <label class="col-sm-3 control-label">Card Number</label>
      <div class="col-sm-4">
        <input id="card-num" type="text" class="form-control">
      </div>
    </div>
    <div class="form-group">
      <label class="col-sm-3 control-label">Card Details</label>
      <div class="col-sm-4">
        <div class="row">
          <div class="col-xs-4">
            <input id="card-month" type="text" size="2" maxlength="2" 
class="form-control" placeholder="MM">
          </div>
          <div class="col-xs-4">
            <input id="card-year"  type="text" size="2" maxlength="2" 
class="form-control" placeholder="YY">
          </div>
          <div class="col-xs-4">
            <input id="card-cvc" type="text" size="3" maxlength="3" 
class="form-control" placeholder="CVC">
          </div>
        </div>
      </div>
    </div>
    <div class="form-group">
      <div class="col-sm-6">
        <div id="cardFormError" class="alert alert-danger hidden" 
role="alert">
          <p>{{error}}</p>
        </div>
      </div>
    </div>
  </div>
  {% endif %}
  <div class="form-group">
    <div class="col-sm-offset-3 col-sm-4">
      <button type="submit" class="btn btn-primary">Update Plan</button>
    </div>
  </div>
</form>`

调用路线/用户/计划

app.post('/user/plan',
    setRedirect({auth: '/', success: '/billing', failure: '/billing'}),
    isAuthenticated,
    users.postPlan);

users.postPlan是我认为的问题:

exports.postPlan = function(req, res, next){
  var 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);
    });
  });
};

这是我觉得某个地方搞砸了的代码。如果有更多我可以包括让我知道。我不确定问题所在的代码部分,但我认为是users.postPlan,但我对节点的了解还不够。

1 个答案:

答案 0 :(得分:0)

我很想确认user.stripe.last4中有什么东西;这似乎就是为什么它将你引导到卡片形式。