Ruby on rails 无法使用条带向客户收费
我正试图用信用卡的详细信息保存一个客户,然后再收费 到目前为止,我能够做到以下几点:Ruby on rails 无法使用条带向客户收费,ruby-on-rails,ruby,ruby-on-rails-4,coffeescript,stripe-payments,Ruby On Rails,Ruby,Ruby On Rails 4,Coffeescript,Stripe Payments,我正试图用信用卡的详细信息保存一个客户,然后再收费 到目前为止,我能够做到以下几点: 将卡令牌设置为Stripe(我在Stripe中的日志和Rails控制台中看到它) 创建一个客户,将其发送到Stripe并将客户id保存在我的数据库中 当我试图向客户收费时,出现以下错误: Stripe::CardError(无法向没有活动卡的客户收费) 可能卡令牌没有正确分配给客户?我怎样才能解决它?也许我遗漏了一些简单的问题,但我一直在努力寻找解决方案 application.rb: class Applic
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
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)
应用程序\u 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_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 %>
class ApplicationController“感谢您的申请!”
结束
其他的
重定向到确认路径(@application.listing\u id,@application.id),:notice=>“感谢您的申请!”
结束
结束
结束
def费用
Stripe::Charge.create(
:金额=>1500,
:货币=>“美元”,
:customer=>@application.stripe\u customer\u令牌
)
结束
私有的
def set_应用程序
@application=application.find(参数[:id])
结束
def应用程序参数
参数要求(:应用)。许可(
:_销毁,
:用户id,
:列表号,
:条带\u客户\u令牌,
:st_地址,
:单位:,
:城市,
:陈述
)
结束
结束
form.html.erb
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_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 %>
已提供信用卡。
“数字”%>
“cvc”%>
“exp month”}%>
“exp year”}%>
未启用JavaScript,此表单需要JavaScript。首先在web浏览器设置中启用它。
返回
您的应用程序参数()
筛选方法似乎不允许从表单提交的条带卡令牌。我相信,如果您将其添加到您的permit()
过滤器列表中,您应该能够将值传递到控制器,以便在需要时使用它。您甚至没有chargin object())谢谢您的回复。在充电操作中,充电发生在控制器中。充电对象在控制器中,但未成功充电。您是否尝试在\u操作:充电后删除此,仅:[:创建]
,并将其添加到您要调用的创建方法中?因为,我看到你在调用update方法后删除了你的对象。我试着像你说的那样放入create方法并在创建后删除费用,但我得到了相同的错误。Michael。就这么简单。谢谢你的建议!那一个一直让我着迷!现在,当事情不正常时,这是我首先要看的地方之一祝你好运!