class ActiveMerchant::Billing::UsaEpayTransactionGateway

Constants

STANDARD_ERROR_CODE_MAPPING
TRANSACTIONS

Public Class Methods

new(options = {}) click to toggle source
Calls superclass method ActiveMerchant::Billing::Gateway::new
# File lib/active_merchant/billing/gateways/usa_epay_transaction.rb, line 41
def initialize(options = {})
  requires!(options, :login)
  super
end

Public Instance Methods

authorize(money, credit_card, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/usa_epay_transaction.rb, line 46
def authorize(money, credit_card, options = {})
  post = {}

  add_amount(post, money)
  add_invoice(post, options)
  add_credit_card(post, credit_card)
  unless credit_card.track_data.present?
    add_address(post, credit_card, options)
    add_customer_data(post, options)
  end
  add_split_payments(post, options)

  commit(:authorization, post)
end
capture(money, authorization, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/usa_epay_transaction.rb, line 76
def capture(money, authorization, options = {})
  post = { :refNum => authorization }

  add_amount(post, money)
  commit(:capture, post)
end
purchase(money, credit_card, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/usa_epay_transaction.rb, line 61
def purchase(money, credit_card, options = {})
  post = {}

  add_amount(post, money)
  add_invoice(post, options)
  add_credit_card(post, credit_card)
  unless credit_card.track_data.present?
    add_address(post, credit_card, options)
    add_customer_data(post, options)
  end
  add_split_payments(post, options)

  commit(:purchase, post)
end
refund(money, authorization, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/usa_epay_transaction.rb, line 83
def refund(money, authorization, options = {})
  post = { :refNum => authorization }

  add_amount(post, money)
  commit(:refund, post)
end
verify(creditcard, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/usa_epay_transaction.rb, line 90
def verify(creditcard, options = {})
  MultiResponse.run(:use_first_response) do |r|
    r.process { authorize(1, creditcard, options) }
    r.process(:ignore_result) { void(r.authorization, options) }
  end
end
void(authorization, options = {}) click to toggle source

Pass `no_release: true` to keep the void from immediately settling

# File lib/active_merchant/billing/gateways/usa_epay_transaction.rb, line 98
def void(authorization, options = {})
  command = (options[:no_release] ? :void : :void_release)
  commit(command, refNum: authorization)
end

Private Instance Methods

add_address(post, credit_card, options) click to toggle source
# File lib/active_merchant/billing/gateways/usa_epay_transaction.rb, line 135
def add_address(post, credit_card, options)
  billing_address = options[:billing_address] || options[:address]

  add_address_for_type(:billing, post, credit_card, billing_address) if billing_address
  add_address_for_type(:shipping, post, credit_card, options[:shipping_address]) if options[:shipping_address]
end
add_address_for_type(type, post, credit_card, address) click to toggle source
# File lib/active_merchant/billing/gateways/usa_epay_transaction.rb, line 142
def add_address_for_type(type, post, credit_card, address)
  prefix = address_key_prefix(type)

  post[address_key(prefix, 'fname')]    = credit_card.first_name
  post[address_key(prefix, 'lname')]    = credit_card.last_name
  post[address_key(prefix, 'company')]  = address[:company]   unless address[:company].blank?
  post[address_key(prefix, 'street')]   = address[:address1]  unless address[:address1].blank?
  post[address_key(prefix, 'street2')]  = address[:address2]  unless address[:address2].blank?
  post[address_key(prefix, 'city')]     = address[:city]      unless address[:city].blank?
  post[address_key(prefix, 'state')]    = address[:state]     unless address[:state].blank?
  post[address_key(prefix, 'zip')]      = address[:zip]       unless address[:zip].blank?
  post[address_key(prefix, 'country')]  = address[:country]   unless address[:country].blank?
  post[address_key(prefix, 'phone')]    = address[:phone]     unless address[:phone].blank?
end
add_amount(post, money) click to toggle source
# File lib/active_merchant/billing/gateways/usa_epay_transaction.rb, line 105
def add_amount(post, money)
  post[:amount] = amount(money)
end
add_credit_card(post, credit_card) click to toggle source
# File lib/active_merchant/billing/gateways/usa_epay_transaction.rb, line 173
def add_credit_card(post, credit_card)
  if credit_card.track_data.present?
    post[:magstripe] = credit_card.track_data
    post[:cardpresent] = true
  else
    post[:card]   = credit_card.number
    post[:cvv2]   = credit_card.verification_value if credit_card.verification_value?
    post[:expir]  = expdate(credit_card)
    post[:name]   = credit_card.name unless credit_card.name.blank?
    post[:cardpresent] = true if credit_card.manual_entry
  end
end
add_customer_data(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/usa_epay_transaction.rb, line 116
def add_customer_data(post, options)
  address = options[:billing_address] || options[:address] || {}
  post[:street] = address[:address1]
  post[:zip] = address[:zip]

  if options.has_key? :email
    post[:custemail] = options[:email]
    post[:custreceipt] = 'No'
  end

  if options.has_key? :customer
    post[:custid] = options[:customer]
  end

  if options.has_key? :ip
    post[:ip] = options[:ip]
  end
end
add_invoice(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/usa_epay_transaction.rb, line 168
def add_invoice(post, options)
  post[:invoice]      = options[:order_id]
  post[:description]  = options[:description]
end
add_split_payments(post, options) click to toggle source

see: wiki.usaepay.com/developer/transactionapi#split_payments

# File lib/active_merchant/billing/gateways/usa_epay_transaction.rb, line 187
def add_split_payments(post, options)
  return unless options[:split_payments].is_a?(Array)
  options[:split_payments].each_with_index do |payment, index|
    prefix = '%02d' % (index + 2)
    post["#{prefix}key"]         = payment[:key]
    post["#{prefix}amount"]      = amount(payment[:amount])
    post["#{prefix}description"] = payment[:description]
  end

  # When blank it's 'Stop'. 'Continue' is another one
  post['onError'] = options[:on_error] || 'Void'
end
address_key(prefix, key) click to toggle source
# File lib/active_merchant/billing/gateways/usa_epay_transaction.rb, line 164
def address_key(prefix, key)
  "#{prefix}#{key}".to_sym
end
address_key_prefix(type) click to toggle source
# File lib/active_merchant/billing/gateways/usa_epay_transaction.rb, line 157
def address_key_prefix(type)
  case type
  when :shipping then 'ship'
  when :billing then 'bill'
  end
end
commit(action, parameters) click to toggle source
# File lib/active_merchant/billing/gateways/usa_epay_transaction.rb, line 225
def commit(action, parameters)
  url = (test? ? self.test_url : self.live_url)
  response = parse(ssl_post(url, post_data(action, parameters)))
  Response.new(response[:status] == 'Approved', message_from(response), response,
    :test           => test?,
    :authorization  => response[:ref_num],
    :cvv_result     => response[:cvv2_result_code],
    :avs_result     => { :code => response[:avs_result_code] },
    :error_code     => STANDARD_ERROR_CODE_MAPPING[response[:error_code]]
  )
end
expdate(credit_card) click to toggle source
# File lib/active_merchant/billing/gateways/usa_epay_transaction.rb, line 109
def expdate(credit_card)
  year  = format(credit_card.year, :two_digits)
  month = format(credit_card.month, :two_digits)

  "#{month}#{year}"
end
message_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/usa_epay_transaction.rb, line 237
def message_from(response)
  if response[:status] == "Approved"
    return 'Success'
  else
    return 'Unspecified error' if response[:error].blank?
    return response[:error]
  end
end
parse(body) click to toggle source
# File lib/active_merchant/billing/gateways/usa_epay_transaction.rb, line 200
def parse(body)
  fields = {}
  for line in body.split('&')
    key, value = *line.scan( %r{^(\w+)\=(.*)$} ).flatten
    fields[key] = CGI.unescape(value.to_s)
  end

  {
    :status           => fields['UMstatus'],
    :auth_code        => fields['UMauthCode'],
    :ref_num          => fields['UMrefNum'],
    :batch            => fields['UMbatch'],
    :avs_result       => fields['UMavsResult'],
    :avs_result_code  => fields['UMavsResultCode'],
    :cvv2_result      => fields['UMcvv2Result'],
    :cvv2_result_code => fields['UMcvv2ResultCode'],
    :vpas_result_code => fields['UMvpasResultCode'],
    :result           => fields['UMresult'],
    :error            => fields['UMerror'],
    :error_code       => fields['UMerrorcode'],
    :acs_url          => fields['UMacsurl'],
    :payload          => fields['UMpayload']
  }.delete_if{|k, v| v.nil?}
end
post_data(action, parameters = {}) click to toggle source
# File lib/active_merchant/billing/gateways/usa_epay_transaction.rb, line 246
def post_data(action, parameters = {})
  parameters[:command]  = TRANSACTIONS[action]
  parameters[:key]      = @options[:login]
  parameters[:software] = 'Active Merchant'
  parameters[:testmode] = (@options[:test] ? 1 : 0)
  seed = SecureRandom.hex(32).upcase
  hash = Digest::SHA1.hexdigest("#{parameters[:command]}:#{@options[:password]}:#{parameters[:amount]}:#{parameters[:invoice]}:#{seed}")
  parameters[:hash] = "s/#{seed}/#{hash}/n"

  parameters.collect { |key, value| "UM#{key}=#{CGI.escape(value.to_s)}" }.join("&")
end