【问题标题】:How to disable coffeescript for Stripe if no charge in Rails 3 app?如果 Rails 3 应用程序不收费,如何禁用 Stripe 的咖啡脚本?
【发布时间】:2012-08-12 02:02:49
【问题描述】:

我正在使用 Stripe 在我的 Rails 3 应用中接受付款。 Stripe 需要这个咖啡脚本:

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)

在我的应用程序中,如果向客户收取的费用为 0,则 Stripe 信用卡字段是隐藏的,并且不需要成功提交表单。我的控制器中的这句话说明了这一点:

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

这是我遇到的问题:当费用为 0 并且我的表单中不需要付款时,我点击提交按钮提交表单,但没有任何反应。我可以单击并单击,但没有任何反应。但是,当我重新加载页面时,表单已成功提交。

我删除了 Stripe 的咖啡脚本文件(上图),然后提交按钮在这个实例中起作用。那么如何在这个例子中禁用咖啡脚本呢? “if”语句会进入我的控制器还是咖啡脚本?

【问题讨论】:

    标签: jquery ruby-on-rails ruby-on-rails-3 coffeescript stripe-payments


    【解决方案1】:

    在您的提交事件中,返回 false 会阻止表单提交。如果您不想要这种行为,请返回 false 以外的任何值。在这种情况下,如果您的费用为 0,听起来您只需要一起绕过处理,这就是您需要的:

    $('#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
    

    【讨论】:

      猜你喜欢
      • 2018-05-26
      • 2012-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-08
      • 2015-01-02
      • 2013-01-09
      • 1970-01-01
      相关资源
      最近更新 更多