【问题标题】:Unable to charge customer using stripe无法使用条纹向客户收费
【发布时间】:2016-05-05 02:31:32
【问题描述】:

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

到目前为止,我能够做到以下几点:

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

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

当我尝试向客户收费时,我收到以下错误:

Stripe::CardError(无法向没有活动卡的客户收费)

也许卡令牌没有正确分配给客户?我该如何解决?也许这是我缺少的一些简单的东西,但我一直在努力寻找解决方案。

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 %>

【问题讨论】:

  • 你连充电对象都没有))
  • 感谢您的回复。充电发生在控制器中的充电动作中。
  • Charging 对象在控制器中但没有充电成功。
  • 您是否尝试删除此after_action :charge, only: [:create] 并将charge 添加到您要调用的创建方法中?因为,在调用更新方法后,我看到 yr 正在删除 yr 对象。
  • 我已尝试按照您所说的放入 create 方法并在 after_create 后删除费用,但我得到了同样的错误。

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


【解决方案1】:

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

【讨论】:

  • 迈克尔。就这么简单。感谢您的建议!
  • 那个总是让我着迷!现在,当事情不正常时,这是我最先看到的地方之一。 :D 祝你好运!
猜你喜欢
  • 2020-05-29
  • 2016-04-14
  • 1970-01-01
  • 2018-11-17
  • 2022-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多