class ActiveMerchant::Billing::ElavonGateway

Elavon Virtual Merchant Gateway

Example use:

gateway = ActiveMerchant::Billing::ElavonGateway.new(
            :login     => "my_virtual_merchant_id",
            :password  => "my_virtual_merchant_pin",
            :user      => "my_virtual_merchant_user_id" # optional
         )

# set up credit card obj as in main ActiveMerchant example
creditcard = ActiveMerchant::Billing::CreditCard.new(
  :type       => 'visa',
  :number     => '41111111111111111',
  :month      => 10,
  :year       => 2011,
  :first_name => 'Bob',
  :last_name  => 'Bobsen'
)

# run request
response = gateway.purchase(1000, creditcard) # authorize and capture 10 USD

puts response.success?      # Check whether the transaction was successful
puts response.message       # Retrieve the message returned by Elavon
puts response.authorization # Retrieve the unique transaction ID returned by Elavon

Public Class Methods

new(options = {}) click to toggle source

Initialize the Gateway

The gateway requires that a valid login and password be passed in the options hash.

Options

  • :login – Merchant ID

  • :password – PIN

  • :user – Specify a subuser of the account (optional)

  • :test => true or false – Force test transactions

Calls superclass method ActiveMerchant::Billing::Gateway::new
# File lib/active_merchant/billing/gateways/elavon.rb, line 66
def initialize(options = {})
  requires!(options, :login, :password)
  super
end

Public Instance Methods

authorize(money, creditcard, options = {}) click to toggle source

Authorize a credit card for a given amount.

Parameters

  • money - The amount to be authorized as an Integer value in cents.

  • credit_card - The CreditCard details for the transaction.

  • options

    • :billing_address - The billing address for the cardholder.

# File lib/active_merchant/billing/gateways/elavon.rb, line 94
def authorize(money, creditcard, options = {})
  form = {}
  add_salestax(form, options)
  add_invoice(form, options)
  add_creditcard(form, creditcard)
  add_address(form, options)
  add_customer_data(form, options)
  add_test_mode(form, options)
  commit(:authorize, money, form)
end
capture(money, authorization, options = {}) click to toggle source

Capture authorized funds from a credit card.

Parameters

  • money - The amount to be captured as an Integer value in cents.

  • authorization - The approval code returned from the initial authorization.

  • options

    • :credit_card - The CreditCard details from the initial transaction (required).

# File lib/active_merchant/billing/gateways/elavon.rb, line 112
def capture(money, authorization, options = {})
  requires!(options, :credit_card)

  form = {}
  add_salestax(form, options)
  add_approval_code(form, authorization)
  add_invoice(form, options)
  add_creditcard(form, options[:credit_card])
  add_customer_data(form, options)
  add_test_mode(form, options)
  commit(:capture, money, form)
end
credit(money, creditcard, options = {}) click to toggle source

Make a credit to a card. Use the refund method if you’d like to credit using previous transaction

Parameters

  • money - The amount to be credited as an Integer value in cents.

  • creditcard - The credit card to be credited.

  • options

# File lib/active_merchant/billing/gateways/elavon.rb, line 161
def credit(money, creditcard, options = {})
  if creditcard.is_a?(String)
    raise ArgumentError, "Reference credits are not supported. Please supply the original credit card or use the #refund method."
  end

  form = {}
  add_invoice(form, options)
  add_creditcard(form, creditcard)
  add_address(form, options)
  add_customer_data(form, options)
  add_test_mode(form, options)
  commit(:credit, money, form)
end
purchase(money, payment_method, options = {}) click to toggle source

Make a purchase

# File lib/active_merchant/billing/gateways/elavon.rb, line 72
def purchase(money, payment_method, options = {})
  form = {}
  add_salestax(form, options)
  add_invoice(form, options)
  if payment_method.is_a?(String)
    add_token(form, payment_method)
  else
    add_creditcard(form, payment_method)
  end
  add_address(form, options)
  add_customer_data(form, options)
  add_test_mode(form, options)
  commit(:purchase, money, form)
end
refund(money, identification, options = {}) click to toggle source

Refund a transaction.

This transaction indicates to the gateway that money should flow from the merchant to the customer.

Parameters

  • money – The amount to be credited to the customer as an Integer value in cents.

  • identification – The ID of the original transaction against which the refund is being issued.

  • options – A hash of parameters.

# File lib/active_merchant/billing/gateways/elavon.rb, line 135
def refund(money, identification, options = {})
  form = {}
  add_txn_id(form, identification)
  add_test_mode(form, options)
  commit(:refund, money, form)
end
store(creditcard, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/elavon.rb, line 182
def store(creditcard, options = {})
  form = {}
  add_creditcard(form, creditcard)
  add_address(form, options)
  add_customer_data(form, options)
  add_test_mode(form, options)
  add_verification(form, options)
  form[:add_token] = 'Y'
  commit(:store, nil, form)
end
update(token, creditcard, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/elavon.rb, line 193
def update(token, creditcard, options = {})
  form = {}
  add_token(form, token)
  add_creditcard(form, creditcard)
  add_address(form, options)
  add_customer_data(form, options)
  add_test_mode(form, options)
  commit(:update, nil, form)
end
verify(credit_card, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/elavon.rb, line 175
def verify(credit_card, options = {})
  MultiResponse.run(:use_first_response) do |r|
    r.process { authorize(100, credit_card, options) }
    r.process(:ignore_result) { void(r.authorization, options) }
  end
end
void(identification, options = {}) click to toggle source

Void a previous transaction

Parameters

  • authorization - The authorization returned from the previous request.

# File lib/active_merchant/billing/gateways/elavon.rb, line 147
def void(identification, options = {})
  form = {}
  add_txn_id(form, identification)
  add_test_mode(form, options)
  commit(:void, nil, form)
end

Private Instance Methods

add_address(form, options) click to toggle source
# File lib/active_merchant/billing/gateways/elavon.rb, line 252
def add_address(form, options)
  billing_address = options[:billing_address] || options[:address]

  if billing_address
    form[:avs_address]    = billing_address[:address1].to_s.slice(0, 30)
    form[:address2]       = billing_address[:address2].to_s.slice(0, 30)
    form[:avs_zip]        = billing_address[:zip].to_s.gsub(/[^a-zA-Z0-9]/, '').slice(0, 9)
    form[:city]           = billing_address[:city].to_s.slice(0, 30)
    form[:state]          = billing_address[:state].to_s.slice(0, 10)
    form[:company]        = billing_address[:company].to_s.slice(0, 50)
    form[:phone]          = billing_address[:phone].to_s.slice(0, 20)
    form[:country]        = billing_address[:country].to_s.slice(0, 50)
  end

  if shipping_address = options[:shipping_address]
    first_name, last_name = parse_first_and_last_name(shipping_address[:name])
    form[:ship_to_first_name]     = first_name.to_s.slice(0, 20)
    form[:ship_to_last_name]      = last_name.to_s.slice(0, 30)
    form[:ship_to_address1]       = shipping_address[:address1].to_s.slice(0, 30)
    form[:ship_to_address2]       = shipping_address[:address2].to_s.slice(0, 30)
    form[:ship_to_city]           = shipping_address[:city].to_s.slice(0, 30)
    form[:ship_to_state]          = shipping_address[:state].to_s.slice(0, 10)
    form[:ship_to_company]        = shipping_address[:company].to_s.slice(0, 50)
    form[:ship_to_country]        = shipping_address[:country].to_s.slice(0, 50)
    form[:ship_to_zip]            = shipping_address[:zip].to_s.slice(0, 10)
  end
end
add_approval_code(form, authorization) click to toggle source
# File lib/active_merchant/billing/gateways/elavon.rb, line 210
def add_approval_code(form, authorization)
  form[:approval_code] = authorization.split(';').first
end
add_creditcard(form, creditcard) click to toggle source
# File lib/active_merchant/billing/gateways/elavon.rb, line 222
def add_creditcard(form, creditcard)
  form[:card_number] = creditcard.number
  form[:exp_date] = expdate(creditcard)

  if creditcard.verification_value?
    add_verification_value(form, creditcard)
  end

  form[:first_name] = creditcard.first_name.to_s.slice(0, 20)
  form[:last_name] = creditcard.last_name.to_s.slice(0, 30)
end
add_customer_data(form, options) click to toggle source
# File lib/active_merchant/billing/gateways/elavon.rb, line 243
def add_customer_data(form, options)
  form[:email] = options[:email].to_s.slice(0, 100) unless options[:email].blank?
  form[:customer_code] = options[:customer].to_s.slice(0, 10) unless options[:customer].blank?
end
add_invoice(form,options) click to toggle source
# File lib/active_merchant/billing/gateways/elavon.rb, line 205
def add_invoice(form,options)
  form[:invoice_number] = (options[:order_id] || options[:invoice]).to_s.slice(0, 10)
  form[:description] = options[:description].to_s.slice(0, 255)
end
add_salestax(form, options) click to toggle source
# File lib/active_merchant/billing/gateways/elavon.rb, line 248
def add_salestax(form, options)
  form[:salestax] = options[:tax] if options[:tax].present?
end
add_test_mode(form, options) click to toggle source
# File lib/active_merchant/billing/gateways/elavon.rb, line 292
def add_test_mode(form, options)
  form[:test_mode] = 'TRUE' if options[:test_mode]
end
add_token(form, token) click to toggle source
# File lib/active_merchant/billing/gateways/elavon.rb, line 234
def add_token(form, token)
  form[:token] = token
end
add_txn_id(form, authorization) click to toggle source
# File lib/active_merchant/billing/gateways/elavon.rb, line 214
def add_txn_id(form, authorization)
  form[:txn_id] = authorization.split(';').last
end
add_verification(form, options) click to toggle source
# File lib/active_merchant/billing/gateways/elavon.rb, line 280
def add_verification(form, options)
  form[:verify] = 'Y' if options[:verify]
end
add_verification_value(form, creditcard) click to toggle source
# File lib/active_merchant/billing/gateways/elavon.rb, line 238
def add_verification_value(form, creditcard)
  form[:cvv2cvc2] = creditcard.verification_value
  form[:cvv2cvc2_indicator] = '1'
end
authorization_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/elavon.rb, line 218
def authorization_from(response)
  [response['approval_code'], response['txn_id']].join(';')
end
commit(action, money, parameters) click to toggle source
# File lib/active_merchant/billing/gateways/elavon.rb, line 304
def commit(action, money, parameters)
  parameters[:amount] = amount(money)
  parameters[:transaction_type] = self.actions[action]

  response = parse( ssl_post(test? ? self.test_url : self.live_url, post_data(parameters)) )

  Response.new(response['result'] == '0', message_from(response), response,
    :test => @options[:test] || test?,
    :authorization => authorization_from(response),
    :avs_result => { :code => response['avs_response'] },
    :cvv_result => response['cvv2_response']
  )
end
message_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/elavon.rb, line 296
def message_from(response)
  success?(response) ? response['result_message'] : response['errorMessage']
end
parse(msg) click to toggle source
# File lib/active_merchant/billing/gateways/elavon.rb, line 336
def parse(msg)
  resp = {}
  msg.split(self.delimiter).collect{|li|
      key, value = li.split("=")
      resp[key.to_s.strip.gsub(/^ssl_/, '')] = value.to_s.strip
    }
  resp
end
parse_first_and_last_name(value) click to toggle source
# File lib/active_merchant/billing/gateways/elavon.rb, line 284
def parse_first_and_last_name(value)
  name = value.to_s.split(' ')

  last_name = name.pop || ''
  first_name = name.join(' ')
  [ first_name, last_name ]
end
post_data(parameters) click to toggle source
# File lib/active_merchant/billing/gateways/elavon.rb, line 318
def post_data(parameters)
  result = preamble
  result.merge!(parameters)
  result.collect { |key, value| "ssl_#{key}=#{CGI.escape(value.to_s)}" }.join("&")
end
preamble() click to toggle source
# File lib/active_merchant/billing/gateways/elavon.rb, line 324
def preamble
  result = {
    'merchant_id'   => @options[:login],
    'pin'           => @options[:password],
    'show_form'     => 'false',
    'result_format' => 'ASCII'
  }

  result['user_id'] = @options[:user] unless @options[:user].blank?
  result
end
success?(response) click to toggle source
# File lib/active_merchant/billing/gateways/elavon.rb, line 300
def success?(response)
  !response.has_key?('errorMessage')
end