我正在使用 Stripe 和 Ruby on Rails 3.2 构建一个小型概念证明。到目前为止,我已经观看了有关如何在 RoR 应用程序中实现 Stripe 的 Railscast,它运行得非常好。
我按照RailsCast #288 Billing with Stripe构建了我的应用程序。现在,我的用户可以添加和编辑他们的信用卡,甚至可以注册课程并在完成后对他们的信用卡收费。
现在我一直在使用 Stripe 的众多测试信用卡进行测试,我希望在出现异常时捕获尽可能多的异常。我在我的注册模型中使用了 Stripe 的示例错误,如下所示:
class Registration < ActiveRecord::Base
  belongs_to :user
  belongs_to :session
  attr_accessible :session_id, :user_id, :session, :user, :stripe_payment_id
  validates :user_id, :uniqueness => {:scope => :session_id}
  def save_with_payment(user, stripe_card_token)
    if valid?
      if user.stripe_customer_id.present?
        charge = Stripe::Charge.create(
            :customer => user.stripe_customer_id,
            :amount => self.session.price.to_i * 100,
            :description => "Registration for #{self.session.name} (Id:#{self.session.id})",
            :currency => 'cad'
        )
      else
        customer = Stripe::Customer.create(
            :email => user.email,
            :card => stripe_card_token,
            :description => user.name
        )
        charge = Stripe::Charge.create(
            :customer => customer.id,
            :amount => self.session.price.to_i * 100,
            :description => "Registration for #{self.session.name} (Id:#{self.session.id})",
            :currency => 'cad'
        )
        user.update_attribute(:stripe_customer_id, customer.id)
      end
      self.stripe_payment_id = charge.id
      save!
    end
  rescue Stripe::CardError => e
    body = e.json_body
    err  = body[:error]
    logger.debug "Status is: #{e.http_status}"
    logger.debug "Type is: #{err[:type]}"
    logger.debug "Code is: #{err[:code]}"
    logger.debug "Param is: #{err[:param]}"
    logger.debug "Message is: #{err[:message]}"
  rescue Stripe::InvalidRequestError => e
    # Invalid parameters were supplied to Stripe's API
  rescue Stripe::AuthenticationError => e
    # Authentication with Stripe's API failed
    # (maybe you changed API keys recently)
  rescue Stripe::APIConnectionError => e
    # Network communication with Stripe failed
  rescue Stripe::StripeError => e
    # Display a very generic error to the user, and maybe send
    # yourself an email
  rescue => e
    # Something else happened, completely unrelated to Stripe
  end
end
我现在只是从错误中解救出来,并没有在一个被提出后真正采取行动,最终我想阻止当前的类注册发生并重定向一个带有 flash 错误的用户。
我已经阅读了rescure_from但我不确定处理所有可能的 Stripe 错误的最佳方法是什么。我知道无法从模型重定向,您的专家将如何处理?
这是我的注册控制器:
class Classroom::RegistrationsController < ApplicationController
  before_filter :authenticate_user!
  def new
    if params[:session_id]
      @session = Session.find(params[:session_id])
      @registration = Registration.new(user: current_user, session: @session)
    else
      flash[:error] = "Course session is required"
    end
    rescue ActiveRecord::RecordNotFound
      render file: 'public/404', status: :not_found
  end
  def create
    if params[:session_id]
      @session = Session.find(params[:session_id])
      @registration = Registration.new(user: current_user, session: @session)
      if @registration.save_with_payment(current_user, params[:stripe_card_token])
        flash[:notice] = "Course registration saved with success."
        logger.debug "Course registration saved with success."
        mixpanel.track 'Registered to a session', { :distinct_id => current_user.id,
                                           :id => @session.id,
                                           'Name' => @session.name,
                                           'Description' => @session.description,
                                           'Course' => @session.course.name
        }
        mixpanel.increment current_user.id, { :'Sessions Registered' => 1}
        mixpanel.track_charge(current_user.id, @session.price.to_i)
      else
        flash[:error] = "There was a problem saving the registration."
        logger.debug "There was a problem saving the registration."
      end
      redirect_to root_path
    else
      flash[:error] = "Session required."
      redirect_to root_path
    end
  end
end
感谢您花时间回复,非常感谢!
弗朗西斯