Skip to content

Instantly share code, notes, and snippets.

@janx
Created July 8, 2009 10:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janx/142722 to your computer and use it in GitHub Desktop.
Save janx/142722 to your computer and use it in GitHub Desktop.
class OrderTransaction < ActiveRecord::Base
belongs_to :order
serialize :params
cattr_accessor :gateway
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
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment