2

私は Authorize.net で ActiveMerchant を使用しており、既に 2 つのモデル Order と Transaction をセットアップしています。( http://railscasts.com/episodes/145-integrating-active-merchant )

エラーが発生した場合、トランザクションからエラー メッセージを取得し、フラッシュ メッセージに出力したいと考えています。これが私のコードです:

#order_controller.rb

  def create
    @order = Order.new(params[:order])

    respond_to do |format|
      if @order.save
        if @order.purchase
          format.html { redirect_to(@order, :notice => 'Order was successfully created.') }
          format.xml  { render :xml => @order, :status => :created, :location => @order }
        else  
          flash[:error] = @order.transactions.response
          format.html { render :action => "new" }
        end 
      else  
        format.html { render :action => "new" }
        format.xml  { render :xml => @order.errors, :status => :unprocessable_entity }
      end
    end
  end

そして、ここに私のトランザクションモデルがあります:

#models/order_transaction.rb
class OrderTransaction < ActiveRecord::Base
  belongs_to :order
  serialize :params

  def response=(response)
    self.success       = response.success?
    self.authorization = response.authorization
    self.message       = response.message
    self.params        = response.params
  rescue ActiveMerchant::ActiveMerchantError => e
    self.success       = false
    self.authorization = nil
    self.message       = e.message
    self.params        = {}
  end
end

トランザクション データは、次のようにデータベースに保存されています。

#models/order.rb
class Order < ActiveRecord::Base
  has_many :transactions, :class_name => "OrderTransaction"
  attr_accessor :card_type, :card_number, :card_month, :card_year, :card_verification

  def purchase
      response = GATEWAY.purchase(charge_amount, credit_card, purchase_options)
      transactions.create!(:action => "purchase", :amount => charge_amount, :response => response)
      response.success?
  end


...
end

トランザクションが失敗したときに、このトランザクション エラー メッセージを表示したいと考えています。1 つの注文に対して 2 つのトランザクションがある場合、これらのエラーが発生するにはどうすればよいですか?

どんな助けでも大歓迎です。

ありがとう!

4

0 に答える 0