Sha256: 40821c4ddda5fedb9d9b620776dd07dc353da26d7851ec982b92e92547be5a70

Contents?: true

Size: 1.53 KB

Versions: 11

Compression:

Stored size: 1.53 KB

Contents

class OrderTransaction < ActiveRecord::Base
  belongs_to :order
  serialize :params

  # open up everything for mass assignment
  attr_protected

  class << self
    def authorize(amount, credit_card, options = {})
      process('authorization', amount) do |gw|
        gw.authorize(amount, credit_card, options)
      end
    end

    def capture(amount, authorization, options = {})
      process('capture', amount) do |gw|
        gw.capture(amount, authorization, options)
      end
    end

    private

    def process(action, amount = nil)
      result = OrderTransaction.new
      result.amount = amount
      result.action = action

      begin
        # TODO: is eval appropriate in this instance?  I tried Object.const_get but had problems.
        # OPTIMIZE: this is a bit crufty
        am_setting = Forge.config.ecommerce.active_merchant
        gateway_class = eval("ActiveMerchant::Billing::" + am_setting[:gateway])
        gateway = gateway_class.new(:login => am_setting[:login], :password => am_setting[:password])
        response = yield gateway

        result.success   = response.success?
        result.reference = response.authorization
        result.message   = response.message
        result.params    = response.params
        result.test      = response.test?
      rescue ActiveMerchant::ActiveMerchantError => e
        result.success   = false
        result.reference = nil
        result.message   = e.message
        result.params    = {}
        result.test      = gateway.test?
      end

      result
    end
  end

end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
forge-cli-0.1.10 lib/forge/app/models/order_transaction.rb
forge-cli-0.1.9 lib/forge/app/models/order_transaction.rb
forge-cli-0.1.8 lib/forge/app/models/order_transaction.rb
forge-cli-0.1.7 lib/forge/app/models/order_transaction.rb
forge-cli-0.1.6 lib/forge/app/models/order_transaction.rb
forge-cli-0.1.5 lib/forge/app/models/order_transaction.rb
forge-cli-0.1.4 lib/forge/app/models/order_transaction.rb
forge-cli-0.1.3 lib/forge/app/models/order_transaction.rb
forge-cli-0.1.2 lib/forge/app/models/order_transaction.rb
forge-cli-0.1.1 lib/forge/app/models/order_transaction.rb
forge-cli-0.1.0 lib/forge/app/models/order_transaction.rb