首页 > 解决方案 > 为什么条带客户令牌没有保存在我的数据库中?

问题描述

我目前正在学习提升技能教程“基本 Web 开发人员课程”(目前在第 162 课上),我尝试将 Stripe 与付费会员联系起来。本教程正在使用旧版本的 gems 和库等,这就是我告诉你的原因。

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

开发控制台显示:“未捕获的类型错误:无法读取未定义的属性‘提交’”消息。

我需要改变什么?有没有。与我的 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找到

标签: javascriptrubydatabasestripe-paymentsruby-on-rails-2

解决方案


推荐阅读