我有一个注册模型。我收到Stripe费用时会创建新的注册。
出于某种原因,我的控制器每次收取条纹费用时都会创建2 x Stripe费用和2 x注册记录。这是我的注册#create action:
def create
# Amount in cents
amount = params[:stripeAmount].to_i * 100
# Create the customer in Stripe
customer = Stripe::Customer.create(
email: current_user.email,
card: params[:stripeToken]
)
# Create the charge using the customer data returned by Stripe API
charge = Stripe::Charge.create(
customer: customer.id,
amount: amount,
description: 'Rails Stripe customer',
currency: 'usd'
)
if charge["paid"] == true
@workshop = params[:workshop]
@enrollment = Enrollment.new(user_id: current_user.id, workshop_id: @workshop)
if @enrollment.save
redirect_to thank_you_path + "?workshop=" + @workshop
else
flash[:notice] = "Please try again"
end
end
# place more code upon successfully creating the charge
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to charges_path
flash[:notice] = "Please try again"
end
我哪里错了?
编辑:我在代码中放了一堆put来帮助调试。第二次收费是在我去“if @ enrollment.save”后立即创建的