无法创建/生成stripe_card_token

时间:2014-06-11 08:14:07

标签: ruby-on-rails ruby ruby-on-rails-4 erb stripe-payments

我正在创建一个带有:

的stripe_card_token
Stripe.createToken(card, charge.handleStripeResponse)

在我的charges.js.coffee文件中

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

charge =
  setupForm: ->
    $('#new_charge').submit ->
      $('input[type=submit]').attr('disabled', true)
      if $('#card_number').length
        charge.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, charge.handleStripeResponse)

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

这是我的收费模式:

class Charge < ActiveRecord::Base

    attr_accessor :stripe_card_token

    belongs_to :contest
    belongs_to :user

    def save_with_payment(charge)
      if valid?
        charge = Stripe::Charge.create(
          :amount => (charge.amount.to_i)*100,
          :currency => "usd",
          :card => stripe_card_token)
        self.stripe_charge_token = charge_id
        save!
      end
    rescue Stripe::InvalidRequestError => e
      logger.error "Stripe error while creating customer: #{e.message}"
      errors.add :base, "There was a problem with your credit card."
      false
    end

end

但是,当我点击捐赠按钮时,我会收到通知(&amp;重定向到root_path),如我的控制器中所述:"Transaction was not able to be recorded"

这是我的收费控制器:

class ChargesController < ApplicationController

    def new
      @contest = Contest.find(params[:id])
      @charge = Charge.new(amount: params[:amount])
    end

    def create
      @charge = Charge.new(charges_params)
      if @charge.save_with_payment(@charge)
        redirect_to root_path, :notice => "Contribution was recorded succesfully!"
      else
        redirect_to root_path, :notice => "Transaction was not able to be recorded"
      end
    end

    def charges_params
        params.require(:charge).permit(:stripe_card_token, :contest_id, :user_id, :amount)
    end

end

当我查看我的日志时,我收到此错误:

Started POST "/charges" for 127.0.0.1 at 2014-06-11 01:10:29 -0700
Processing by ChargesController#create as HTML
  Parameters: {"utf8"=>"√", "authenticity_token"=>"9bqk2Q6HBM1ehL4JPgncNQ3gnP0X1nQK2y6ENm8Yb8g=", "c
harge"=>{"contest_id"=>"31", "user_id"=>"", "amount"=>"40", "stripe_card_token"=>""}, "card_number"=
>"4242424242424242", "card_code"=>"419", "button"=>""}
Stripe error while creating customer: You passed an empty string for 'card'. We assume empty values
are an attempt to unset a parameter; however 'card' cannot be unset. You should remove 'card' from y
our request or supply a non-empty value
Redirected to http://localhost:3000/
Completed 302 Found in 502ms (ActiveRecord: 0.0ms)


Started GET "/" for 127.0.0.1 at 2014-06-11 01:10:30 -0700

由于某种原因,条带卡令牌未生成......

这是我的收费/新表格:

<% content_for :head do %>
    <%= tag :meta, :name => "stripe-key", :content => STRIPE_PUBLIC_KEY %>
<% end %>
<%= form_for @charge, :html => { :class => 'form' } do |f| %>
    <div class="form-inputs">
        <div class="row">
            <%= f.hidden_field :contest_id, value: @contest.id  %>
            <%= f.hidden_field :user_id %>
            <%= f.hidden_field :amount %>
            <%= f.hidden_field :stripe_card_token %>
            <div class="small-12 medium-6 large-2 columns">
                <%= f.label "Amount:", class: "text-right" %>
            </div>
            <div class="small-12 medium-6 large-10 columns" style="margin-bottom: 0.5rem; margin-top: 0.5rem;">
                <%= number_to_currency(@charge.amount, :unit => "$") %>
            </div>
              <div class="small-12 medium-6 large-2 columns">
                <%= label_tag :card_number, "Credit Card Number:", class: "text-right" %>
              </div>
              <div class="small-12 medium-6 large-10 columns">
                <%= text_field_tag :card_number %>
              </div>
              <div class="small-12 medium-6 large-2 columns">
                <%= label_tag :card_code, "Security Code on Card (CVV):", class: "text-right" %>
              </div>
              <div class="small-12 medium-6 large-10 columns end">
                <%= text_field_tag :card_code %>
              </div>
              <div class="small-12 medium-6 large-2 columns">
                <%= label_tag :card_month, "Card Expiration:", class: "text-right" %>
              </div>
              <div class="small-6 medium-3 columns">
                <%= select_month nil, {add_month_numbers: true}, {name: nil, id: "card_month"} %>
              </div>
              <div class="small-6 medium-3 columns">
                <%= select_year nil, {start_year: Date.today.year, end_year: Date.today.year+15}, {name: nil, id: "card_year"} %>
              </div>

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

        </div>
        <div class="row">
            <div class="form-actions small-9 small-offset-2 columns">
                <%= button_tag :type => "button", :class => "radius" do  %>
                    <%= link_to "Back", @contest, :style => "color: white" %>
                <% end %>
                <%= f.button :Donate, :class => "radius" %>
            </div>
        </div>
    </div>
<% end %>
<% content_for :js do %>
    <%= javascript_include_tag "https://js.stripe.com/v1", "application" %> 
    <%= javascript_include_tag asset_path("stripe/stripe.js"), "application" %>
<% end %>

我的布局的主要部分有以下内容:

 <%= javascript_include_tag "https://js.stripe.com/v1/", "application", "data-turbolinks-track" => true %>
    <%= csrf_meta_tags %>

    <%= tag :meta, :name => "stripe-key", :content => STRIPE_PUBLIC_KEY %>

这是我的收费表:

  create_table "charges", force: true do |t|
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "stripe_charge_token"
    t.integer  "contest_id"
    t.integer  "user_id"
    t.decimal  "amount"
  end

在我的config / initializers / stripe.rb中,我有:

Stripe.api_key = "sk_testdfkdlfjgkdlsuEvpipr2umoKu9f"
STRIPE_PUBLIC_KEY = "pk_testdfgkdfgpud6JHfCvuRHnvpuLPo"

我做错了什么?

1 个答案:

答案 0 :(得分:0)

因此,首先,如前所述,您需要确保您的表单没有信用卡详细信息的名称属性,以便该信息不会影响您的服务器。您只需要服务器上的令牌,而不是信用卡号,CVC或过期。

其次,为了调试这个,我将停止提交表单(在Stripe令牌请求之后)并检查隐藏的输入是否正在使用Stripe中的令牌填充。然后我确认令牌正在进入服务器。

希望有所帮助! 拉里

PS我在Stripe的支持工作。