我正在尝试使用信用卡详细信息保存客户并在以后收费。
到目前为止,我能够做到以下几点:
获取Stripe的卡片令牌(我在条带和我的Rails控制台的日志中看到它)
创建客户,将其发送给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 %>
答案 0 :(得分:2)
您的application_params()
过滤器方法似乎不允许从您的表单提交stripe_card_token
。我相信如果您将其添加到permit()
过滤器列表中,您应该能够将值传递给控制器,以便在需要时可以使用它。