我查看了有关错误的 Stripe 文档,但在正确处理/重定向这些错误时仍然遇到一些问题。基本上无论发生什么,我都希望他们回到edit
行动(通过edit_profile_path
)并向他们显示一条消息(无论成功与否)。
我有一个关于edit
POST 到该update
操作的操作的表单。这可以使用有效的信用卡正常工作(费用在 Stripe 仪表板中)。我正在使用 Stripe.js。
class ExtrasController < ApplicationController
def edit
@extras = current_user.extras
end
def update
Stripe.api_key = "hidden"
token = params[:stripeToken]
begin
charge = Stripe::Charge.create(
:amount => 5000, # amount in cents
:currency => "usd",
:card => token,
:description => current_user.email
)
rescue Stripe::CardError => e
# redirect_to edit_extras_path, notice: e.message
# What I'm trying to do, but obviously results in AbstractController::DoubleRenderError
rescue => e
# Something else happened, completely unrelated to Stripe
# Display a generic error message
end
redirect_to edit_extras_path, notice: "Card charged successfully."
end
end