class ActiveMerchant::Billing::ElementGateway

Constants

SERVICE_LIVE_URL
SERVICE_TEST_URL

Public Class Methods

new(options={}) click to toggle source
Calls superclass method ActiveMerchant::Billing::Gateway::new
# File lib/active_merchant/billing/gateways/element.rb, line 20
def initialize(options={})
  requires!(options, :account_id, :account_token, :application_id, :acceptor_id, :application_name, :application_version)
  super
end

Public Instance Methods

authorize(money, payment, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/element.rb, line 41
def authorize(money, payment, options={})
  request = build_soap_request do |xml|
    xml.CreditCardAuthorization(xmlns: 'https://transaction.elementexpress.com') do
      add_credentials(xml)
      add_payment_method(xml, payment)
      add_transaction(xml, money, options)
      add_terminal(xml, options)
      add_address(xml, options)
    end
  end

  commit('CreditCardAuthorization', request, money)
end
capture(money, authorization, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/element.rb, line 55
def capture(money, authorization, options={})
  trans_id, _ = split_authorization(authorization)
  options[:trans_id] = trans_id

  request = build_soap_request do |xml|
    xml.CreditCardAuthorizationCompletion(xmlns: 'https://transaction.elementexpress.com') do
      add_credentials(xml)
      add_transaction(xml, money, options)
      add_terminal(xml, options)
    end
  end

  commit('CreditCardAuthorizationCompletion', request, money)
end
purchase(money, payment, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/element.rb, line 25
def purchase(money, payment, options={})
  action = payment.is_a?(Check) ? 'CheckSale' : 'CreditCardSale'

  request = build_soap_request do |xml|
    xml.send(action, xmlns: 'https://transaction.elementexpress.com') do
      add_credentials(xml)
      add_payment_method(xml, payment)
      add_transaction(xml, money, options)
      add_terminal(xml, options)
      add_address(xml, options)
    end
  end

  commit(action, request, money)
end
refund(money, authorization, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/element.rb, line 70
def refund(money, authorization, options={})
  trans_id, _ = split_authorization(authorization)
  options[:trans_id] = trans_id

  request = build_soap_request do |xml|
    xml.CreditCardReturn(xmlns: 'https://transaction.elementexpress.com') do
      add_credentials(xml)
      add_transaction(xml, money, options)
      add_terminal(xml, options)
    end
  end

  commit('CreditCardReturn', request, money)
end
scrub(transcript) click to toggle source
# File lib/active_merchant/billing/gateways/element.rb, line 124
def scrub(transcript)
  transcript.
    gsub(%r((<AccountToken>).+?(</AccountToken>))i, '\1[FILTERED]\2').
    gsub(%r((<CardNumber>).+?(</CardNumber>))i, '\1[FILTERED]\2').
    gsub(%r((<CVV>).+?(</CVV>))i, '\1[FILTERED]\2').
    gsub(%r((<AccountNumber>).+?(</AccountNumber>))i, '\1[FILTERED]\2').
    gsub(%r((<RoutingNumber>).+?(</RoutingNumber>))i, '\1[FILTERED]\2')
end
store(payment, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/element.rb, line 100
def store(payment, options = {})
  request = build_soap_request do |xml|
    xml.PaymentAccountCreate(xmlns: 'https://services.elementexpress.com') do
      add_credentials(xml)
      add_payment_method(xml, payment)
      add_payment_account(xml, payment, options[:payment_account_reference_number] || SecureRandom.hex(20))
      add_address(xml, options)
    end
  end

  commit('PaymentAccountCreate', request, nil)
end
supports_scrubbing?() click to toggle source
# File lib/active_merchant/billing/gateways/element.rb, line 120
def supports_scrubbing?
  true
end
verify(credit_card, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/element.rb, line 113
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(authorization, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/element.rb, line 85
def void(authorization, options={})
  trans_id, trans_amount = split_authorization(authorization)
  options.merge!({trans_id: trans_id, trans_amount: trans_amount, reversal_type: 'Full'})

  request = build_soap_request do |xml|
    xml.CreditCardReversal(xmlns: 'https://transaction.elementexpress.com') do
      add_credentials(xml)
      add_transaction(xml, trans_amount, options)
      add_terminal(xml, options)
    end
  end

  commit('CreditCardReversal', request, trans_amount)
end

Private Instance Methods

add_address(xml, options) click to toggle source
# File lib/active_merchant/billing/gateways/element.rb, line 217
def add_address(xml, options)
  if address = options[:billing_address] || options[:address]
    xml.address do
      xml.BillingAddress1 address[:address1] if address[:address1]
      xml.BillingAddress2 address[:address2] if address[:address2]
      xml.BillingCity address[:city] if address[:city]
      xml.BillingState address[:state] if address[:state]
      xml.BillingZipcode address[:zip] if address[:zip]
      xml.BillingEmail address[:email] if address[:email]
      xml.BillingPhone address[:phone_number] if address[:phone_number]
    end
  end
  if shipping_address = options[:shipping_address]
    xml.address do
      xml.ShippingAddress1 shipping_address[:address1] if shipping_address[:address1]
      xml.ShippingAddress2 shipping_address[:address2] if shipping_address[:address2]
      xml.ShippingCity shipping_address[:city] if shipping_address[:city]
      xml.ShippingState shipping_address[:state] if shipping_address[:state]
      xml.ShippingZipcode shipping_address[:zip] if shipping_address[:zip]
      xml.ShippingEmail shipping_address[:email] if shipping_address[:email]
      xml.ShippingPhone shipping_address[:phone_number] if shipping_address[:phone_number]
    end
  end
end
add_credentials(xml) click to toggle source
# File lib/active_merchant/billing/gateways/element.rb, line 135
def add_credentials(xml)
  xml.credentials do
    xml.AccountID @options[:account_id]
    xml.AccountToken @options[:account_token]
    xml.AcceptorID @options[:acceptor_id]
  end
  xml.application do
    xml.ApplicationID @options[:application_id]
    xml.ApplicationName @options[:application_name]
    xml.ApplicationVersion @options[:application_version]
  end
end
add_credit_card(xml, payment) click to toggle source
# File lib/active_merchant/billing/gateways/element.rb, line 199
def add_credit_card(xml, payment)
  xml.card do
    xml.CardNumber payment.number
    xml.ExpirationMonth format(payment.month, :two_digits)
    xml.ExpirationYear format(payment.year, :two_digits)
    xml.CardholderName payment.first_name + ' ' + payment.last_name
    xml.CVV payment.verification_value
  end
end
add_echeck(xml, payment) click to toggle source
# File lib/active_merchant/billing/gateways/element.rb, line 209
def add_echeck(xml, payment)
  xml.demandDepositAccount do
    xml.AccountNumber payment.account_number
    xml.RoutingNumber payment.routing_number
    xml.DDAAccountType payment.account_type.capitalize
  end
end
add_payment_account(xml, payment, payment_account_reference_number) click to toggle source
# File lib/active_merchant/billing/gateways/element.rb, line 158
def add_payment_account(xml, payment, payment_account_reference_number)
  xml.paymentAccount do
    xml.PaymentAccountType payment_account_type(payment)
    xml.PaymentAccountReferenceNumber payment_account_reference_number
  end
end
add_payment_account_id(xml, payment) click to toggle source
# File lib/active_merchant/billing/gateways/element.rb, line 165
def add_payment_account_id(xml, payment)
  xml.extendedParameters do
    xml.ExtendedParameters do
      xml.Key 'PaymentAccount'
      xml.Value('xsi:type' => 'PaymentAccount') do
        xml.PaymentAccountID payment
      end
    end
  end
end
add_payment_method(xml, payment) click to toggle source
# File lib/active_merchant/billing/gateways/element.rb, line 148
def add_payment_method(xml, payment)
  if payment.is_a?(String)
    add_payment_account_id(xml, payment)
  elsif payment.is_a?(Check)
    add_echeck(xml, payment)
  else
    add_credit_card(xml, payment)
  end
end
add_terminal(xml, options) click to toggle source
# File lib/active_merchant/billing/gateways/element.rb, line 186
def add_terminal(xml, options)
  xml.terminal do
    xml.TerminalID '01'
    xml.CardPresentCode 'UseDefault'
    xml.CardholderPresentCode 'UseDefault'
    xml.CardInputCode 'UseDefault'
    xml.CVVPresenceCode 'UseDefault'
    xml.TerminalCapabilityCode 'UseDefault'
    xml.TerminalEnvironmentCode 'UseDefault'
    xml.MotoECICode 'NonAuthenticatedSecureECommerceTransaction'
  end
end
add_transaction(xml, money, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/element.rb, line 176
def add_transaction(xml, money, options = {})
  xml.transaction do
    xml.ReversalType options[:reversal_type] if options[:reversal_type]
    xml.TransactionID options[:trans_id] if options[:trans_id]
    xml.TransactionAmount amount(money.to_i) if money
    xml.MarketCode 'Default' if money
    xml.ReferenceNumber options[:order_id] || SecureRandom.hex(20)
  end
end
authorization_from(action, response, amount) click to toggle source
# File lib/active_merchant/billing/gateways/element.rb, line 283
def authorization_from(action, response, amount)
  if action == 'PaymentAccountCreate'
    response['paymentaccount']['paymentaccountid']
  else
    "#{response['transaction']['transactionid']}|#{amount}" if response['transaction']
  end
end
avs_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/element.rb, line 299
def avs_from(response)
  AVSResult.new(code: response['card']['avsresponsecode']) if response['card']
end
build_soap_request() { |xml| ... } click to toggle source
# File lib/active_merchant/billing/gateways/element.rb, line 311
def build_soap_request
  builder = Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml|
    xml['soap'].Envelope('xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
                         'xmlns:xsd' => 'http://www.w3.org/2001/XMLSchema',
                         'xmlns:soap' => 'http://schemas.xmlsoap.org/soap/envelope/') do

      xml['soap'].Body do
        yield(xml)
      end
    end
  end

  builder.to_xml
end
commit(action, xml, amount) click to toggle source
# File lib/active_merchant/billing/gateways/element.rb, line 269
def commit(action, xml, amount)
  response = parse(ssl_post(url(action), xml, headers(action)))

  Response.new(
    success_from(response),
    message_from(response),
    response,
    authorization: authorization_from(action, response, amount),
    avs_result: success_from(response) ? avs_from(response) : nil,
    cvv_result: success_from(response) ? cvv_from(response) : nil,
    test: test?
  )
end
cvv_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/element.rb, line 303
def cvv_from(response)
  CVVResult.new(response['card']['cvvresponsecode']) if response['card']
end
headers(action) click to toggle source
# File lib/active_merchant/billing/gateways/element.rb, line 348
def headers(action)
  {
    'Content-Type' => 'text/xml; charset=utf-8',
    'SOAPAction' => "https://#{interface(action)}.elementexpress.com/#{action}"
  }
end
interface(action) click to toggle source
# File lib/active_merchant/billing/gateways/element.rb, line 343
def interface(action)
  return 'transaction' if action != 'PaymentAccountCreate'
  return 'services' if action == 'PaymentAccountCreate'
end
message_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/element.rb, line 295
def message_from(response)
  response['expressresponsemessage']
end
parse(xml) click to toggle source
# File lib/active_merchant/billing/gateways/element.rb, line 242
def parse(xml)
  response = {}

  doc = Nokogiri::XML(xml)
  doc.remove_namespaces!
  root = doc.root.xpath('//response/*')

  if root.empty?
    root = doc.root.xpath('//Response/*')
  end

  root.each do |node|
    if node.elements.empty?
      response[node.name.downcase] = node.text
    else
      node_name = node.name.downcase
      response[node_name] = Hash.new

      node.elements.each do |childnode|
        response[node_name][childnode.name.downcase] = childnode.text
      end
    end
  end

  response
end
payment_account_type(payment) click to toggle source
# File lib/active_merchant/billing/gateways/element.rb, line 326
def payment_account_type(payment)
  if payment.is_a?(Check)
    payment_account_type = payment.account_type
  else
    payment_account_type = 'CreditCard'
  end
  payment_account_type
end
split_authorization(authorization) click to toggle source
# File lib/active_merchant/billing/gateways/element.rb, line 307
def split_authorization(authorization)
  authorization.split('|')
end
success_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/element.rb, line 291
def success_from(response)
  response['expressresponsecode'] == '0'
end
url(action) click to toggle source
# File lib/active_merchant/billing/gateways/element.rb, line 335
def url(action)
  if action == 'PaymentAccountCreate'
    test? ? SERVICE_TEST_URL : SERVICE_LIVE_URL
  else
    test? ? test_url : live_url
  end
end