class ActiveMerchant::Billing::EwayGateway

Public: For more information on the Eway Gateway please visit their Developers Area

Constants

MESSAGES

Public Class Methods

new(options = {}) click to toggle source

Public: Create a new Eway Gateway. options - A hash of options:

:login     - Your Customer ID.
:password  - Your XML Refund Password that you
             specified on the Eway site. (optional)
Calls superclass method ActiveMerchant::Billing::Gateway::new
# File lib/active_merchant/billing/gateways/eway.rb, line 21
def initialize(options = {})
  requires!(options, :login)
  super
end

Public Instance Methods

purchase(money, creditcard, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/eway.rb, line 26
def purchase(money, creditcard, options = {})
  requires_address!(options)

  post = {}
  add_creditcard(post, creditcard)
  add_address(post, options)
  add_customer_id(post)
  add_invoice_data(post, options)
  add_non_optional_data(post)
  add_amount(post, money)
  post[:CustomerEmail] = options[:email]

  commit(purchase_url(post[:CVN]), money, post)
end
refund(money, authorization, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/eway.rb, line 41
def refund(money, authorization, options={})
  post = {}

  add_customer_id(post)
  add_amount(post, money)
  add_non_optional_data(post)
  post[:OriginalTrxnNumber] = authorization
  post[:RefundPassword] = @options[:password]
  post[:CardExpiryMonth] = nil
  post[:CardExpiryYear] = nil

  commit(refund_url, money, post)
end

Private Instance Methods

add_address(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/eway.rb, line 71
def add_address(post, options)
  if address = options[:billing_address] || options[:address]
    post[:CustomerAddress]    = [ address[:address1], address[:address2], address[:city], address[:state], address[:country] ].compact.join(', ')
    post[:CustomerPostcode]   = address[:zip]
  end
end
add_amount(post, money) click to toggle source
# File lib/active_merchant/billing/gateways/eway.rb, line 87
def add_amount(post, money)
  post[:TotalAmount] = amount(money)
end
add_creditcard(post, creditcard) click to toggle source
# File lib/active_merchant/billing/gateways/eway.rb, line 60
def add_creditcard(post, creditcard)
  post[:CardNumber]  = creditcard.number
  post[:CardExpiryMonth]  = sprintf("%.2i", creditcard.month)
  post[:CardExpiryYear] = sprintf("%.4i", creditcard.year)[-2..-1]
  post[:CustomerFirstName] = creditcard.first_name
  post[:CustomerLastName]  = creditcard.last_name
  post[:CardHoldersName] = creditcard.name

  post[:CVN] = creditcard.verification_value if creditcard.verification_value?
end
add_customer_id(post) click to toggle source
# File lib/active_merchant/billing/gateways/eway.rb, line 78
def add_customer_id(post)
  post[:CustomerID] = @options[:login]
end
add_invoice_data(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/eway.rb, line 82
def add_invoice_data(post, options)
  post[:CustomerInvoiceRef] = options[:order_id]
  post[:CustomerInvoiceDescription] = options[:description]
end
add_non_optional_data(post) click to toggle source
# File lib/active_merchant/billing/gateways/eway.rb, line 91
def add_non_optional_data(post)
  post[:Option1] = nil
  post[:Option2] = nil
  post[:Option3] = nil
  post[:TrxnNumber] = nil
end
commit(url, money, parameters) click to toggle source
# File lib/active_merchant/billing/gateways/eway.rb, line 98
def commit(url, money, parameters)
  raw_response = ssl_post(url, post_data(parameters))
  response = parse(raw_response)

  Response.new(success?(response),
    message_from(response[:ewaytrxnerror]),
    response,
    :authorization => response[:ewaytrxnnumber],
    :test => test?
  )
end
message_from(message) click to toggle source
# File lib/active_merchant/billing/gateways/eway.rb, line 134
def message_from(message)
  return '' if message.blank?
  MESSAGES[message[0,2]] || message
end
parse(xml) click to toggle source
# File lib/active_merchant/billing/gateways/eway.rb, line 114
def parse(xml)
  response = {}
  xml = REXML::Document.new(xml)
  xml.elements.each('//ewayResponse/*') do |node|
    response[node.name.downcase.to_sym] = normalize(node.text)
  end unless xml.root.nil?

  response
end
post_data(parameters = {}) click to toggle source
# File lib/active_merchant/billing/gateways/eway.rb, line 124
def post_data(parameters = {})
  xml   = REXML::Document.new
  root  = xml.add_element("ewaygateway")

  parameters.each do |key, value|
    root.add_element("eway#{key}").text = value
  end
  xml.to_s
end
purchase_url(cvn) click to toggle source
# File lib/active_merchant/billing/gateways/eway.rb, line 139
def purchase_url(cvn)
  suffix = test? ? 'xmltest/testpage.asp' : 'xmlpayment.asp'
  gateway_part = cvn ? 'gateway_cvn' : 'gateway'
  "#{live_url}/#{gateway_part}/#{suffix}"
end
refund_url() click to toggle source
# File lib/active_merchant/billing/gateways/eway.rb, line 145
def refund_url
  suffix = test? ? 'xmltest/refund_test.asp' : 'xmlpaymentrefund.asp'
  "#{live_url}/gateway/#{suffix}"
end
requires_address!(options) click to toggle source
# File lib/active_merchant/billing/gateways/eway.rb, line 56
def requires_address!(options)
  raise ArgumentError.new("Missing eWay required parameters: address or billing_address") unless (options.has_key?(:address) or options.has_key?(:billing_address))
end
success?(response) click to toggle source
# File lib/active_merchant/billing/gateways/eway.rb, line 110
def success?(response)
  response[:ewaytrxnstatus] == "True"
end