无法使用条纹向客户收费

Unable to charge customer using stripe

我正在尝试使用信用卡详细信息保存客户并稍后收费。

到目前为止,我能够执行以下操作:

  1. 获取 Stripe 的卡片令牌(我在 stripe 的日志和我的 Rails 控制台中看到它)

  2. 创建一个客户,将其发送到 Stripe 并将客户 ID 保存在我的数据库中。

当我尝试向客户收费时出现以下错误:

Stripe::CardError (Cannot charge a customer that has no active card)

可能卡令牌没有正确分配给客户?我该如何解决?也许我遗漏了一些简单的东西,但我已经尝试了一段时间的解决方案。

application.rb:

class Application < ActiveRecord::Base

  attr_accessor :stripe_card_token

  def save_with_payment
    if valid?
      customer = Stripe::Customer.create(
        description: id, 
        email: self.user.email,
        source: self.stripe_card_token
      )
      self.stripe_customer_token = customer.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 

applications.js.coffee:

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

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

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

  handleStripeResponse: (status, response) ->
    if status == 200
      $('#application_stripe_card_token').val(response.id)
      $('#new_application')[0].submit() ->
       return true if($form.find('.application_stripe_card_token').val())

    else
      $('#stripe_error').text(response.error.message)
      $('input[type=submit]').attr('disabled', false)

applications_controller.rb

class ApplicationsController < ApplicationController

  before_action :set_application, only: [:show, :edit, :update, :confirmation, :charge]
  after_action :charge, only: [:create]

  def new 
    @listing = Listing.find(params[:listing_id])
    @application = Application.new

    if Guarantor.where(application_id: @application.id).first.blank?
      @guarantor = Guarantor.new(params[:guarantor])
    end
  end 

  def create
    @listing = Listing.find(params[:listing_id])
    @application = current_user.applications.create(application_params)       

    if params[:btnSubmit]
      redirect_to confirmation_listing_application_path(@application.listing_id, @application.id)
    elsif @application.save_with_payment
      if params[:application][:roommates_attributes].present?
        params[:application][:roommates_attributes].values.each do |a|
          @email = a[:email]
        end
        @user = User.where(email: @email).first
        if @user.blank?
          flash[:error] = "The email address doesn't exist in our records"
          redirect_to new_listing_application_path(@application.listing_id)
          @application.destroy
        else          
          redirect_to confirmation_listing_application_path(@application.listing_id, @application.id), :notice => "Thank you for applying!"
        end
      else
        redirect_to confirmation_listing_application_path(@application.listing_id, @application.id), :notice => "Thank you for applying!"
      end
    end   
  end

  def charge
    Stripe::Charge.create(
      :amount   => 1500, 
      :currency => "usd",
      :customer => @application.stripe_customer_token 
    )
  end

  private

  def set_application
    @application = Application.find(params[:id])
  end

  def application_params
    params.require(:application).permit(
      :_destroy,
      :user_id,
      :listing_id, 
      :stripe_customer_token,
      :st_address,
      :unit,
      :city,
      :state
    )
  end
end

form.html.erb

<%= form_for [@listing, @application], html: {id: "new_application", multipart: true} do |f| %>
  <%= f.hidden_field :stripe_card_token %>

  <% if @application.stripe_customer_token.present? %>
    Credit Card has been provided.
  <% else %>
    <div class="row">
      <div class="col-md-6">
        <%= label_tag :card_number, "Credit Card Number" %>
        <%= text_field_tag :card_number, nil, name: nil, "data-stripe" => "number" %>
      </div>
    </div>

    <div class="row">
      <div class="col-md-6">
        <%= label_tag :card_code, "Security Code (CVC)" %>
        <%= text_field_tag :card_code, nil, name: nil, "data-stripe" => "cvc" %>
      </div>
    </div>

    <div class="row">
      <div class="col-md-6">
        <%= label_tag :card_month, "Card Expiration" %>
        <%= select_month nil, {add_month_numbers: true},    {name: nil, id: "card_month", "data-stripe" => "exp-month" } %>
        <%= select_year nil, {start_year: Date.today.year, end_year: Date.today.year+15}, {name: nil, id: "card_year", "data-stripe" => "exp-year"} %>
      </div>
    </div>
  <% end %>

  <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 class="nextBackBtns">
    <a herf="#" class="btn btn-primary-custom btnBack" data-content="details">Back</a>
    <%= f.submit "Save", class: "btn btn-primary" %>
  </div> 
<% end %>

您的 application_params() 过滤方法似乎不允许从您的表单提交的 stripe_card_token。我相信,如果您将其添加到 permit() 过滤器列表,您应该能够将值传递给控制器​​,以便在需要时使用它。