我正在尝试使用Stripe处理信用卡付款,但我正在努力解决基本的铁轨问题,我是新手。我不能让控制器使用方法“save_with_payment”,它跳过并重定向错误通知。表单在事件显示页面上,我有一个注册模型来处理付款。对于不正确的缩进道歉我仍然对此持怀疑态度。另一个错误是重定向转到事件索引页面而不是显示页面。很困惑。
class Registration
include Mongoid::Document
field :address
field :email
field :job_title
field :company
field :first_name
field :last_name
field :postcode
field :stripe_card_token
field :stripe_customer_token
def save_with_payment
if valid?
charge = Stripe::Charge.create({
amount: 40000,
currency: "gbp",
card: stripe_card_token,
description: email
})
save
end
rescue Stripe::CardError => e
end
end
class EventsController < ApplicationController
def index
@event = Event.all
end
def show
@event = Event.where(:slug => params[:slug]).first
@registration = Registration.new
end
def payment
@registration = Registration.new(params[:registration])
if @registration.save_with_payment(@event)
redirect_to events_path(@event), :notice => "Thank you for registering for"
else
redirect_to events_path(@event), :notice => "We're sorry, something went wrong"
end
end
end
match 'registration' => 'events#show', :via => :get
match 'registration' => 'events#payment', :via => :post
match '/:slug' => "events#show"
<%= simple_form_for [@registration], :method => :post, :url => registration_path do |f| %>
<%= f.input :first_name %>
<%= f.input :last_name %>
<%= f.input :email %>
<%= f.input :job_title %>
<%= f.input :company %>
<%= f.input :stripe_card_token, as: :hidden %>
<div class="input card_number">
<label for="card_number">Card number</label>
<%= text_field_tag :card_number %>
</div>
<div class="input card_cvc">
<label for="card_code">Security code (CVC)</label>
<%= text_field_tag :card_cvc %>
</div>
<div class="input card_dates">
<label>Card expiration date</label>
<%= select_month nil, { add_month_number: true }, { id: "card_month"} %>
<%= select_year nil, { add_year: Date.today.year, end_year: Date.today.year + 15 }, { id: "card_year"} %>
</div>
<%= f.button :submit, "Register", class: "button" %>
<% end %>
答案 0 :(得分:0)
你怎么知道它会跳过你的方法?可能有例外。提升救援区中的例外情况。
def save_with_payment
if valid?
charge = Stripe::Charge.create({
amount: 400.00,
currency: "gbp",
card: stripe_card_token,
description: email
})
save
end
rescue Exception => e
raise e
false
end