2

编辑: 当我在表单的非模态版本上使用我的 transactions.js.coffee 时,一切正常。关于处理表单的一些事情xhr是防止Stripe.createToken访问f.hidden_field :stripe_card_token. 或者该jQuery对象可能是在模态存在之前创建的,所以会因为该元素在模态中而setupForm()无法找到而静默失败,因此在脚本运行时被隐藏?$("#new_transaction_button")也许:attr_accessor :stripe_card_token在模态中不起作用?

我试图允许用户在模式窗口中输入信用卡详细信息。我对此进行了梳理,无法弄清楚为什么 Stripe 一直将令牌设置为空字符串Stripe error while creating customer: Empty string given for card.

我怀疑这是 jquery 传递表单的方式的问题。

这是我的transactions.js.coffee文件:

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

transaction =
    setupForm: ->
        $("#new_transaction_button").click ->
            $('input[type=submit]').attr('disabled', true)
            transaction.processCard()
            valuesToSubmit = $("#new_transaction").serialize()
            $("#new_transaction_modal").modal "hide"
            .ajax(
                url: "/posts"
                data: valuesToSubmit
                type: "POST"
            ).success (data, status) ->

            false

    processCard: ->
        card =
            number: $('#card_number').val()
            cvc: $('#card_code').val()
            exp_month: $('#card_month').val()
            exp_year: $('#card_year').val()
        Stripe.createToken(card, transaction.handleStripeResponse)

    handleStripeResponse: (status, response) ->
        if status == 200
            $('#transaction_stripe_card_token').val("test")
            $('#new_transaction')[0].submit()
        else
            alert(response.error.message)

在上面的函数中handleStripeResponse,我将. 这个对吗?如果是,为什么不被调用?是因为我的控制器处理模态表单的方式吗?#transaction_stripe_card_token.val()handleStripeResponseStripe error while creating customer: string "test" given for card.handleStripeResponse

交易控制器.rb

 def create
    if @post = Post.where(:id => flash[:the_post_id]).first
    #@price = @post.price
      @tier_id = @post.tier_id
      @amount = @tier_id*100
    else
      @amount = 1000
    end
    respond_to do |format|
      #non-user encountered form and entered credit details AND password- signing up and paying
      if !current_user && params[:password].present?        
        @user = User.new(:email => params[:transaction][:email], :password => params[:password])
        @transaction = @user.transactions.new(params[:transaction].merge(:price => @amount))
        @transaction.save_customer(@user)
        if @transaction.save && @user.save
          @user.charge_as_customer(@amount)
          #sign_in @user
          format.js { render }
          format.html { redirect_to @transaction, notice: 'Transaction was successfully created.' }
          format.json { render json: @transaction, status: :created, location: @transaction }
        else
          format.html { render action: "new" }
          format.json { render json: @transaction.errors, status: :unprocessable_entity }
        end

模态信用卡表格:(空白/缩进在生产中是正确的。复制/粘贴有问题)

= simple_form_for @transaction, :validate => true, :remote => true, :form => { "data-type" => "json" } do |f|
  %div.modal-header
    %button.close{"aria-hidden" => "true", "data-dismiss" => "modal", :type => "button"} ×
    - if @transaction.errors.any?
      #error_explanation
        %h2= "#{pluralize(@transaction.errors.count, "error")} prohibited this transaction from being saved:"
        %ul
          - @transaction.errors.full_messages.each do |msg|
            %li= msg

  %div.modal-body
    =hidden_field_tag :post_id

    = f.hidden_field :stripe_card_token

%div.row-fluid
  %div
    = label_tag :credit_card_number, nil, :id => "credit_label"
    = text_field_tag :credit_card_number,nil, :class=>"credit-number span12", :id => "card_number"
%div.row-fluid
  %div.span4
    = 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.span3
  %div.span2.credit-cvv
    = label_tag :cvv, "CVV"
    = text_field_tag :cvv, nil, :class=>"credit-cvv input-block-level", :id => "card_code"
%div.row-fluid
  =f.label :email
  =f.text_field :email, :validate => true
-unless current_user
  %div.row-fluid
    =label_tag :password
    =text_field_tag :password, params[:password]
  %div.modal-footer
    = f.button :submit, name: 'Get It', class: "btn-primary", id: "new_transaction_button"

而且,为了彻底,这是我save_customer的交易方法:

  attr_accessor :stripe_card_token

  def save_customer(user)
    if valid?
      customer = Stripe::Customer.create( :description => email, :card => stripe_card_token)
      user.stripe_customer_id = customer.id
      save!
    end
  rescue Stripe::InvalidRequestError => e
    logger.error "Egads, Stripe error while creating customer: #{e.message}"
    errors.add :base, "There was a problem with your credit card."
    false
  end

无法为我的生活弄清楚问题是什么。任何帮助将不胜感激。谢谢朋友。

4

0 回答 0