希望在 Stripe Checkout 付款成功后才提交 Rails 表格
Looking to Submit Rails Form Only After Successful Stripe Checkout Payment
我正在使用 Rails 4.2,并尝试将 Stripe Checkout (https://stripe.com/docs/checkout/guides/rails) 集成到我的 rails 应用程序中,并且有一个我在任何地方都没有看到的场景。注意:我尝试从许多在线资源中进行自定义表单集成,但无法正常工作,因此选择了结帐。
在我的 rails 应用中,我有一个订单 table,我想要完成的主要任务是有一个表单,用户可以在其中提交他们的个人信息(非付款) 下订单。然后,stripe checkout 集成将允许他们支付;但是,如果没有记录条带费用,则不会在数据库中创建订单记录。我一直无法通过使用 stripe 建议的单独 "charges" 控制器来完成此操作,并且还尝试将 stripe 代码合并到我的订单控制器中(见下文)。
我应该注意,我已经能够获得结帐按钮以提交到条带并处理费用,但还没有能够在我的数据库中创建订单记录。
我已经广泛搜索了这个问题的答案(目前正在等待 stripe 支持的回复)。如有任何建议,我们将不胜感激!
orders_controller.rb
(这是我尝试将他们建议的收费控制器中的条纹代码组合到我自己的订单控制器中的示例。我现在不确定在处理收费后要做什么它提交表格)
def create
@order = Order.new(order_params)
customer = Stripe::Customer.create(
:email => 'example@stripe.com',
:card => params[:stripeToken]
)
charge = Stripe::Charge.create(
:customer => customer.id,
:amount => 5000,
:description => 'Rails Stripe customer',
:currency => 'usd'
)
rescue Stripe::CardError => e
flash[:error] = e.message
render 'new'
end
orders/new.html.erb
(我省略了表单中所有其他字段的代码,这只显示了我的表单提交按钮和条纹结帐按钮。理想情况下,我可以将这些操作合并到一个按钮中,或者只当付款通过 stripe 成功处理时提交通过)
<%= form_for @order do |f| %>
// lots of form fields
<%= f.submit %>
<script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
data-description="A month's subscription"
data-amount="500"></script>
通常你会做...
def create
@order = Order.new(order_params)
charge_error = nil
if @order.valid?
begin
customer = Stripe::Customer.create(
:email => 'example@stripe.com',
:card => params[:stripeToken])
charge = Stripe::Charge.create(
:customer => customer.id,
:amount => 5000,
:description => 'Rails Stripe customer',
:currency => 'usd')
rescue Stripe::CardError => e
charge_error = e.message
end
if charge_error
flash[:error] = charge_error
render :new
else
@order.save
redirect_to (successful page)
end
else
flash[:error] = 'one or more errors in your order'
render :new
end
end
这样,除非@order 被验证,否则不会进行收费,除非收费成功,否则@order 不会被保存。
我正在使用 Rails 4.2,并尝试将 Stripe Checkout (https://stripe.com/docs/checkout/guides/rails) 集成到我的 rails 应用程序中,并且有一个我在任何地方都没有看到的场景。注意:我尝试从许多在线资源中进行自定义表单集成,但无法正常工作,因此选择了结帐。
在我的 rails 应用中,我有一个订单 table,我想要完成的主要任务是有一个表单,用户可以在其中提交他们的个人信息(非付款) 下订单。然后,stripe checkout 集成将允许他们支付;但是,如果没有记录条带费用,则不会在数据库中创建订单记录。我一直无法通过使用 stripe 建议的单独 "charges" 控制器来完成此操作,并且还尝试将 stripe 代码合并到我的订单控制器中(见下文)。
我应该注意,我已经能够获得结帐按钮以提交到条带并处理费用,但还没有能够在我的数据库中创建订单记录。
我已经广泛搜索了这个问题的答案(目前正在等待 stripe 支持的回复)。如有任何建议,我们将不胜感激!
orders_controller.rb
(这是我尝试将他们建议的收费控制器中的条纹代码组合到我自己的订单控制器中的示例。我现在不确定在处理收费后要做什么它提交表格)
def create
@order = Order.new(order_params)
customer = Stripe::Customer.create(
:email => 'example@stripe.com',
:card => params[:stripeToken]
)
charge = Stripe::Charge.create(
:customer => customer.id,
:amount => 5000,
:description => 'Rails Stripe customer',
:currency => 'usd'
)
rescue Stripe::CardError => e
flash[:error] = e.message
render 'new'
end
orders/new.html.erb
(我省略了表单中所有其他字段的代码,这只显示了我的表单提交按钮和条纹结帐按钮。理想情况下,我可以将这些操作合并到一个按钮中,或者只当付款通过 stripe 成功处理时提交通过)
<%= form_for @order do |f| %>
// lots of form fields
<%= f.submit %>
<script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
data-description="A month's subscription"
data-amount="500"></script>
通常你会做...
def create
@order = Order.new(order_params)
charge_error = nil
if @order.valid?
begin
customer = Stripe::Customer.create(
:email => 'example@stripe.com',
:card => params[:stripeToken])
charge = Stripe::Charge.create(
:customer => customer.id,
:amount => 5000,
:description => 'Rails Stripe customer',
:currency => 'usd')
rescue Stripe::CardError => e
charge_error = e.message
end
if charge_error
flash[:error] = charge_error
render :new
else
@order.save
redirect_to (successful page)
end
else
flash[:error] = 'one or more errors in your order'
render :new
end
end
这样,除非@order 被验证,否则不会进行收费,除非收费成功,否则@order 不会被保存。