首页 文章

让Coffeescript找到正确的Div // Stripe Error

提问于
浏览
1

在Ryan Bates的教程的帮助下,我能够设置Stripe . 现在,我正在尝试允许用户更新他们的信用卡信息 .

现在,我已经挂断了在单击操作按钮时能够调用条带 . 可能的原因是我通过基本上复制Ryan的代码来制作新的信用卡,一起攻击了咖啡 .

这是我用来输入新信用卡信息的表格

<%= form_tag("/users/update_card", :method => "put", :class => "edit_user", :id => "change_card" ) do %>
  <%= hidden_field_tag :stripe_card_token %>

  <div id="stripe_error" class="alert">   
      <noscript>JavaScript is not enabled and is required for this form. First enable it in your web browser settings.</noscript>
  </div>

    <div class="field">
      <%= label_tag :card_number, "Credit Card Number" %>
      <%= text_field_tag :card_number, nil, name: nil %>
    </div>
    <div class="field">
      <%= label_tag :card_code, "Security Code on Card (CVV)" %>
      <%= text_field_tag :card_code, nil, name: nil %>
    </div>
    <div class="field">
      <%= label_tag :card_month, "Card Expiration" %>
      <%= select_month nil, {add_month_numbers: true}, {name: nil, id: "card_month"} %>
      <%= select_year nil, {start_year: Date.today.year, end_year: Date.today.year+15}, {name: nil, id: "card_year"} %>
    </div>


  <%= submit_tag("Update My Credit Card", :class => "button") %>

这是我正在使用的Coffeescript . 上半部分包括新用户(有效)的注册,下半部分从changecard.setupForm()开始,没有

# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/

jQuery ->
  Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content'))
  subscription.setupForm()

subscription =
  setupForm: ->
    $('#new_user').submit ->
      $('input[type=submit]').attr('disabled', true)
      if $('#card_number').length
        subscription.processCard()
        false
      else
        true

  processCard: ->
    card =
      number: $('#card_number').val()
      cvc: $('#card_code').val()
      expMonth: $('#card_month').val()
      expYear: $('#card_year').val()
    Stripe.createToken(card, subscription.handleStripeResponse)

  handleStripeResponse: (status, response) ->
    if status == 200
      $('#user_stripe_card_token').val(response.id)
      $('#new_user')[0].submit()
    else
      $('#stripe_error').text(response.error.message)
      $('input[type=submit]').attr('disabled', false)

changecard.setupForm()

changecard =
  setupForm: ->
    $('#change_card').submit ->
      $('input[type=submit]').attr('disabled', true)
      if $('#card_number').length
        subscription.processCard()
        false
      else
        true

  processCard: ->
    card =
      number: $('#card_number').val()
      cvc: $('#card_code').val()
      expMonth: $('#card_month').val()
      expYear: $('#card_year').val()
    Stripe.createToken(card, subscription.handleStripeResponse)

  handleStripeResponse: (status, response) ->
    if status == 200
      $('#user_stripe_card_token').val(response.id)
      $('#change_card')[0].submit()
    else
      $('#stripe_error').text(response.error.message)
      $('input[type=submit]').attr('disabled', false)

这是我的update_card动作

def update_card
  @user = current_user
  cu = Stripe::Customer.retrieve(@user.stripe_customer_token)
  cu.card = params[:stripe_customer_token] # obtained with Stripe.js
  cu.save
  redirect_to edit_user_path(@user)
  end

感谢您坚持到目前为止!非常感谢您的想法 .

Edit

在单步执行javascript之后,它暂停了

changecard.setupForm()

那是我开始的路线:P . 错误是

uncaught TypeError: Cannot call method 'setupForm' of undefined

似乎使用我上面提到的coffeescript没有正确定义更改卡 . 我不知道如何改变它,任何帮助非常感谢!

Edit 2

使用javascript调试,这是最终代码,它似乎提供了stripe_card_token

**# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
jQuery ->
  Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content'))
  subscription.setupForm()
  changecard.setupForm()
subscription =
  setupForm: ->
    $('#new_user').submit ->
      $('input[type=submit]').attr('disabled', true)
      if $('#card_number').length
        subscription.processCard()
        false
      else
        true

  processCard: ->
    card =
      number: $('#card_number').val()
      cvc: $('#card_code').val()
      expMonth: $('#card_month').val()
      expYear: $('#card_year').val()
    Stripe.createToken(card, subscription.handleStripeResponse)

  handleStripeResponse: (status, response) ->
    if status == 200
      $('#user_stripe_card_token').val(response.id)
      $('#new_user')[0].submit()
    else
      $('#stripe_error').text(response.error.message)
      $('input[type=submit]').attr('disabled', false)
changecard =
  setupForm: ->
    $('#change_card').submit ->
      $('input[type=submit]').attr('disabled', true)
      if $('#card_number').length
        changecard.processCard()
        false
      else
        true

  processCard: ->
    card =
      number: $('#card_number').val()
      cvc: $('#card_code').val()
      expMonth: $('#card_month').val()
      expYear: $('#card_year').val()
    Stripe.createToken(card, changecard.handleStripeResponse)

  handleStripeResponse: (status, response) ->
    if status == 200
      $('#stripe_card_token').val(response.id)
      $('#change_card')[0].submit()
    else
      $('#stripe_error').text(response.error.message)
      $('input[type=submit]').attr('disabled', false)**

1 回答

  • 0

    编辑2

    使用javascript调试,这是最终代码,它似乎提供了stripe_card_token

    **# Place all the behaviors and hooks related to the matching controller here.
    # All this logic will automatically be available in application.js.
    # You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
    jQuery ->
      Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content'))
      subscription.setupForm()
      changecard.setupForm()
    subscription =
      setupForm: ->
        $('#new_user').submit ->
          $('input[type=submit]').attr('disabled', true)
          if $('#card_number').length
            subscription.processCard()
            false
          else
            true
    
      processCard: ->
        card =
          number: $('#card_number').val()
          cvc: $('#card_code').val()
          expMonth: $('#card_month').val()
          expYear: $('#card_year').val()
        Stripe.createToken(card, subscription.handleStripeResponse)
    
      handleStripeResponse: (status, response) ->
        if status == 200
          $('#user_stripe_card_token').val(response.id)
          $('#new_user')[0].submit()
        else
          $('#stripe_error').text(response.error.message)
          $('input[type=submit]').attr('disabled', false)
    changecard =
      setupForm: ->
        $('#change_card').submit ->
          $('input[type=submit]').attr('disabled', true)
          if $('#card_number').length
            changecard.processCard()
            false
          else
            true
    
      processCard: ->
        card =
          number: $('#card_number').val()
          cvc: $('#card_code').val()
          expMonth: $('#card_month').val()
          expYear: $('#card_year').val()
        Stripe.createToken(card, changecard.handleStripeResponse)
    
      handleStripeResponse: (status, response) ->
        if status == 200
          $('#stripe_card_token').val(response.id)
          $('#change_card')[0].submit()
        else
          $('#stripe_error').text(response.error.message)
          $('input[type=submit]').attr('disabled', false)**
    

相关问题