简体   繁体   English

无法更改计划| 条纹

[英]Can't change plan | Stripe

EDIT Project repo 编辑 项目仓库

I am using a boiler plate and have logged the issue with the creator but was hoping for help sooner. 我使用的是样板,并已与创建者一起记录了该问题,但希望能尽快获得帮助。 My issue is that when I go to change plans it tells me to please add a card before choosing a plan. 我的问题是,当我去更改计划时,它告诉我在选择计划之前请先添加一张卡。 The code for the page is as follows: 该页面的代码如下:

The form: 表格:

`<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>`

Calls the route /user/plan 调用路线/用户/计划

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

users.postPlan is the issue I think: 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);
    });
  });
};

This is the code I think that somewhere is messed up. 这是我认为某个地方混乱的代码。 If there appear to be more I can include let me know. 如果还有更多,我可以告诉我。 I am not sure which section of code the issue lays in but I think it is the users.postPlan but I don't know enough about node to tell. 我不确定该问题位于代码的哪一部分,但我认为它是users.postPlan,但我对节点的了解还不够。

I'd be tempted to confirm that user.stripe.last4 has something in it; 我很想确认user.stripe.last4是否包含某些内容。 that seems to be why it's routing you to the card form. 这似乎就是为什么将您转至卡片形式的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM