为什么条纹客户令牌没有保存在我的数据库中?

时间:2019-02-07 17:02:22

标签: javascript ruby database stripe-payments ruby-on-rails-2

我目前正在学习upskill教程“ The Essential Web Developer Course”(当前在第162课上),在该教程中,我尝试将Stripe与高级会员的费用联系起来。本教程使用的是旧版的gem和库等,这就是为什么我要告诉你的原因。

问题是,在Stripe将我的客户令牌发回给我之后。它不会存储在我的数据库中,也不会登录到成员身份。相反,我的提交按钮将保持禁用状态,并显示字符串“ Processing”。

开发控制台显示以下消息:“未捕获的TypeError:无法读取未定义的属性'submit'”。

我需要更改什么?有……。与我的save_with_subscription方法有关?

  attr_accessor :stripe_card_token
  def save_with_subscription
   if valid?
    customer = Stripe::Customer.create(description: email, plan: plan_id, card: stripe_card_token)
    self.stripe_customer_token = customer.id
    save!
   end

它的js文件:

/* global $, Stripe */
//Document ready.
$(document).on('turbolinks:load', function(){
  var theForm = $('#pro_form');
  var submitBtn = $('#form-signup-btn');
  //Set Stripe public key.
  Stripe.setPublishableKey( $('meta[name="stripe-key"]').attr('content') );
  //When user clicks form submit btn,
  submitBtn.click(function(event){
    //prevent default submission behavior.
    event.preventDefault();
    submitBtn.val("Processing").prop('disabled', true);
    //Collect the credit card fields.
    var ccNum = $('#card_number').val(),
        cvcNum = $('#card_code').val(),
        expMonth = $('#card_month').val(),
        expYear = $('#card_year').val();
    //Use Stripe JS library to check for card errors.
    var error = false;
    //Validate card number.
    if(!Stripe.card.validateCardNumber(ccNum)) {
      error = true;
      alert('The credit card number appears to be invalid');
    }
    //Validate CVC number.
    if(!Stripe.card.validateCVC(cvcNum)) {
      error = true;
      alert('The CVC number appears to be invalid');
    }
    //Validate expiration date.
    if(!Stripe.card.validateExpiry(expMonth, expYear)) {
      error = true;
      alert('The expiration date appears to be invalid');
    }
    if (error) {
      //If there are card errors, don't send to Stripe.
      submitBtn.prop('disabled', false).val("Sign Up");
    } else {
      //Send the card info to Stripe.
      Stripe.createToken({
        number: ccNum,
        cvc: cvcNum,
        exp_month: expMonth,
        exp_year: expYear
      }, stripeResponseHandler);
    }
    return false;
  });
  //Stripe will return a card token.
  function stripeResponseHandler(status, response) {
    //Get the token from the response.
    var token = response.id;
    //Inject the card token in a hidden field.
    theForm.append( $('<input type="hidden" name="user[stripe_card_token]">').val(token) );
    //Submit form to our Rails app.
    theForm.get(0).submit();
  }
});

我的所有代码都可以在https://github.com/JakkSwords/upskill_saas_tutorial/tree/user_memberships

上找到

0 个答案:

没有答案