【发布时间】:2015-01-15 03:33:03
【问题描述】:
上下文:
我正在使用 Stripe 结账来接受 Rails 中的一次性付款。 我有一个收费控制器,如下所示。 我最初使用 stripe webhook 来收听 charge.succeeded,但由于 webhook 的异步性质而遇到了一些问题。 我已将业务逻辑移至控制器。 如果客户收费成功,那么我将客户和其他一些详细信息保存到数据库中。
我的问题:
这项检查是否足以确保收费成功?
if charge["paid"] == true
Stripe::Charge.create 的 Stripe 文档指出,“ 如果收费成功,则返回一个收费对象。如果出现问题,则引发错误。常见的错误来源是无效或过期的卡,或可用余额不足的有效卡。”
我的 ChargesController:
class ChargesController < ApplicationController
def new
end
def create
# Amount in cents
@amount = 100
temp_job_id = cookies[:temp_job_id]
customer_email = TempJobPost.find_by(id: temp_job_id).company[:email]
customer = Stripe::Customer.create(
:email => customer_email,
:card => params[:stripeToken]
)
charge = Stripe::Charge.create(
:customer => customer.id,
:amount => @amount,
:description => 'Rails Stripe customer',
:currency => 'usd',
:metadata => {"job_id"=> temp_job_id}
)
# TODO: charge.paid or charge["paid"]
if charge["paid"] == true
#Save customer to the db
end
# need to test this and refactor this using begin-->rescue--->end
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to charges_path
end
end
【问题讨论】:
标签: ruby-on-rails ruby stripe-payments payment-processing webhooks