Question

I'm using Stripe to accept payments in my Rails 3 app. Stripe requires this coffeescript:

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

video =
  setupForm: ->
    $('#new_video input:submit').click ->
      $(this).addClass('wasclicked')

    $('#new_video').submit ->
      clickedBtn = $(this).find('.wasclicked')

      if clickedBtn.attr('name') != 'back_button' && $('#card_number').length
        video.processCard()
        clickedBtn.removeClass('wasclicked')
        false
      else
        true

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

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

In my app, if the charge to the customer is 0, then the Stripe credit card fields are hidden, and not required to successfully submit the form. This statement in my controller accounts for that:

@video.save if @video.all_valid? && ((@total > 0 && @video.save_with_payment(@total)) || @total == 0)

Here's the problem I'm having: when the charge is 0, and payment is not required in my form, I hit the submit button to submit the form, and nothing happens. I can just click and click and there's no response whatsoever. However, when I reload the page, then the form is successfully submitted.

I deleted the coffeescript file (above) for Stripe, and then the submit button worked in this instance. So how do I disable the coffeescript in this example? Would an "if" statement go into my controller, or the coffeescript?

Was it helpful?

Solution

In your submit event, returning false prevents the form from submitting. If you don't want this behavior, return any value other than false. In this case it sounds like you just need to bypass processing all together if your charge is 0, this is what you would need:

$('#new_video').submit ->
  clickedBtn = $(this).find('.wasclicked')

  if clickedBtn.attr('name') != 'back_button' && $('#card_number').length && $('#charge').val() > 0
    video.processCard()
    clickedBtn.removeClass('wasclicked')
    false
  else
    true
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top