class ActiveMerchant::Billing::EwayRapidGateway

Constants

MESSAGES

Public Class Methods

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

Public Instance Methods

authorize(amount, payment_method, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/eway_rapid.rb, line 58
def authorize(amount, payment_method, options={})
  params = {}
  add_metadata(params, options)
  add_invoice(params, amount, options)
  add_customer_data(params, options)
  add_credit_card(params, payment_method, options)
  params['Method'] = 'Authorise'
  commit(url_for('Authorisation'), params)
end
capture(amount, identification, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/eway_rapid.rb, line 68
def capture(amount, identification, options = {})
  params = {}
  add_metadata(params, options)
  add_invoice(params, amount, options)
  add_reference(params, identification)
  commit(url_for("CapturePayment"), params)
end
purchase(amount, payment_method, options={}) click to toggle source

Public: Run a purchase transaction.

amount - The monetary amount of the transaction in cents. payment_method - The payment method or authorization token returned from store. options - A standard ActiveMerchant options hash:

:transaction_type - One of: Purchase (default), MOTO
                    or Recurring.  For stored card payments (aka - TokenPayments),
                    this must be either MOTO or Recurring.
:order_id         - A merchant-supplied identifier for the
                    transaction (optional).
:description      - A merchant-supplied description of the
                    transaction (optional).
:currency         - Three letter currency code for the
                    transaction (default: "AUD")
:billing_address  - Standard ActiveMerchant address hash
                    (optional).
:shipping_address - Standard ActiveMerchant address hash
                    (optional).
:ip               - The ip of the consumer initiating the
                    transaction (optional).
:application_id   - A string identifying the application
                    submitting the transaction
                    (default: "https://github.com/Shopify/active_merchant")

Returns an ActiveMerchant::Billing::Response object where authorization is the Transaction ID on success

# File lib/active_merchant/billing/gateways/eway_rapid.rb, line 48
def purchase(amount, payment_method, options={})
  params = {}
  add_metadata(params, options)
  add_invoice(params, amount, options)
  add_customer_data(params, options)
  add_credit_card(params, payment_method, options)
  params['Method'] = payment_method.respond_to?(:number) ? 'ProcessPayment' : 'TokenPayment'
  commit(url_for('Transaction'), params)
end
refund(amount, identification, options = {}) click to toggle source

Public: Refund a transaction.

amount - The monetary amount of the transaction in cents identification - The transaction id which is returned in the

authorization of the successful purchase transaction

options - A standard ActiveMerchant options hash:

:order_id         - A merchant-supplied identifier for the
                    transaction (optional).
:description      - A merchant-supplied description of the
                    transaction (optional).
:currency         - Three letter currency code for the
                    transaction (default: "AUD")
:billing_address  - Standard ActiveMerchant address hash
                    (optional).
:shipping_address - Standard ActiveMerchant address hash
                    (optional).
:ip               - The ip of the consumer initiating the
                    transaction (optional).
:application_id   - A string identifying the application
                    submitting the transaction
                    (default: "https://github.com/Shopify/active_merchant")

Returns an ActiveMerchant::Billing::Response object

# File lib/active_merchant/billing/gateways/eway_rapid.rb, line 105
def refund(amount, identification, options = {})
  params = {}
  add_metadata(params, options)
  add_invoice(params, amount, options, "Refund")
  add_reference(params["Refund"], identification)
  add_customer_data(params, options)
  commit(url_for("Transaction/#{identification}/Refund"), params)
end
store(payment_method, options = {}) click to toggle source

Public: Store card details and return a valid token

payment_method - The payment method or nil if :customer_token is provided options - A supplemented ActiveMerchant options hash:

:order_id         - A merchant-supplied identifier for the
                    transaction (optional).
:description      - A merchant-supplied description of the
                    transaction (optional).
:billing_address  - Standard ActiveMerchant address hash
                    (required).
:ip               - The ip of the consumer initiating the
                    transaction (optional).
:application_id   - A string identifying the application
                    submitting the transaction
                    (default: "https://github.com/Shopify/active_merchant")

Returns an ActiveMerchant::Billing::Response object where the authorization is the customer_token on success

# File lib/active_merchant/billing/gateways/eway_rapid.rb, line 131
def store(payment_method, options = {})
  requires!(options, :billing_address)
  params = {}
  add_metadata(params, options)
  add_invoice(params, 0, options)
  add_customer_data(params, options)
  add_credit_card(params, payment_method, options)
  params['Method'] = 'CreateTokenCustomer'
  commit(url_for("Transaction"), params)
end
update(customer_token, payment_method, options = {}) click to toggle source

Public: Update a customer’s data

customer_token - The customer token returned in the authorization of

a successful store transaction.

payment_method - The payment method or nil if :customer_token is provided options - A supplemented ActiveMerchant options hash:

:order_id         - A merchant-supplied identifier for the
                    transaction (optional).
:description      - A merchant-supplied description of the
                    transaction (optional).
:billing_address  - Standard ActiveMerchant address hash
                    (optional).
:ip               - The ip of the consumer initiating the
                    transaction (optional).
:application_id   - A string identifying the application
                    submitting the transaction
                    (default: "https://github.com/Shopify/active_merchant")

Returns an ActiveMerchant::Billing::Response object where the authorization is the customer_token on success

# File lib/active_merchant/billing/gateways/eway_rapid.rb, line 161
def update(customer_token, payment_method, options = {})
  params = {}
  add_metadata(params, options)
  add_invoice(params, 0, options)
  add_customer_data(params, options)
  add_credit_card(params, payment_method, options)
  add_customer_token(params, customer_token)
  params['Method'] = 'UpdateTokenCustomer'
  commit(url_for("Transaction"), params)
end
void(identification, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/eway_rapid.rb, line 76
def void(identification, options = {})
  params = {}
  add_reference(params, identification)
  commit(url_for("CancelAuthorisation"), params)
end

Private Instance Methods

add_address(params, address, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/eway_rapid.rb, line 206
def add_address(params, address, options={})
  return unless address

  if address[:name]
    parts = address[:name].split(/\s+/)
    params['FirstName'] = parts.shift if parts.size > 1
    params['LastName'] = parts.join(" ")
  end
  params['Title'] = address[:title]
  params['CompanyName'] = address[:company] unless options[:skip_company]
  params['Street1'] = truncate(address[:address1])
  params['Street2'] = truncate(address[:address2])
  params['City'] = truncate(address[:city])
  params['State'] = address[:state]
  params['PostalCode'] = address[:zip]
  params['Country'] = address[:country].to_s.downcase
  params['Phone'] = address[:phone]
  params['Fax'] = address[:fax]
  params['Email'] = options[:email]
end
add_credit_card(params, credit_card, options) click to toggle source
# File lib/active_merchant/billing/gateways/eway_rapid.rb, line 227
def add_credit_card(params, credit_card, options)
  return unless credit_card
  params['Customer'] ||= {}
  if credit_card.respond_to? :number
    card_details = params['Customer']['CardDetails'] = {}
    card_details['Name'] = truncate(credit_card.name)
    card_details['Number'] = credit_card.number
    card_details['ExpiryMonth'] = "%02d" % (credit_card.month || 0)
    card_details['ExpiryYear'] = "%02d" % (credit_card.year || 0)
    card_details['CVN'] = credit_card.verification_value
  else
    add_customer_token(params, credit_card)
  end
end
add_customer_data(params, options) click to toggle source
# File lib/active_merchant/billing/gateways/eway_rapid.rb, line 199
def add_customer_data(params, options)
  params['Customer'] ||= {}
  add_address(params['Customer'], (options[:billing_address] || options[:address]), {:email => options[:email]})
  params['ShippingAddress'] = {}
  add_address(params['ShippingAddress'], options[:shipping_address], {:skip_company => true})
end
add_customer_token(params, token) click to toggle source
# File lib/active_merchant/billing/gateways/eway_rapid.rb, line 242
def add_customer_token(params, token)
  params['Customer'] ||= {}
  params['Customer']['TokenCustomerID'] = token
end
add_invoice(params, money, options, key = "Payment") click to toggle source
# File lib/active_merchant/billing/gateways/eway_rapid.rb, line 185
def add_invoice(params, money, options, key = "Payment")
  currency_code = options[:currency] || currency(money)
  params[key] = {
    'TotalAmount' => localized_amount(money, currency_code),
    'InvoiceReference' => truncate(options[:order_id]),
    'InvoiceDescription' => truncate(options[:description], 64),
    'CurrencyCode' => currency_code,
  }
end
add_metadata(params, options) click to toggle source
# File lib/active_merchant/billing/gateways/eway_rapid.rb, line 174
def add_metadata(params, options)
  params['RedirectUrl'] = options[:redirect_url] || 'http://example.com'
  params['CustomerIP'] = options[:ip] if options[:ip]
  params['TransactionType'] = options[:transaction_type] || 'Purchase'
  params['DeviceID'] = options[:application_id] || application_id
  if partner = options[:partner_id] || partner_id
    params['PartnerID'] = truncate(partner, 50)
  end
  params
end
add_reference(params, reference) click to toggle source
# File lib/active_merchant/billing/gateways/eway_rapid.rb, line 195
def add_reference(params, reference)
  params['TransactionID'] = reference
end
authorization_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/eway_rapid.rb, line 308
def authorization_from(response)
  # Note: TransactionID is always null for store requests, but TokenCustomerID is also sent back for purchase from
  # stored card transactions so we give precendence to TransactionID
  response['TransactionID'] || response['Customer']['TokenCustomerID']
end
avs_result_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/eway_rapid.rb, line 314
def avs_result_from(response)
  verification = response['Verification'] || {}
  code = case verification['Address']
  when "Valid"
    "M"
  when "Invalid"
    "N"
  else
    "I"
  end
  {:code => code}
end
commit(url, params) click to toggle source
# File lib/active_merchant/billing/gateways/eway_rapid.rb, line 251
def commit(url, params)
  headers = {
    "Authorization" => ("Basic " + Base64.strict_encode64(@options[:login].to_s + ":" + @options[:password].to_s).chomp),
    "Content-Type" => "application/json"
  }
  request = params.to_json
  raw = parse(ssl_post(url, request, headers))

  succeeded = success?(raw)
  ActiveMerchant::Billing::Response.new(
    succeeded,
    message_from(succeeded, raw),
    raw,
    :authorization => authorization_from(raw),
    :test => test?,
    :avs_result => avs_result_from(raw),
    :cvv_result => cvv_result_from(raw)
  )
rescue ActiveMerchant::ResponseError => e
  return ActiveMerchant::Billing::Response.new(false, e.response.message, {:status_code => e.response.code}, :test => test?)
end
cvv_result_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/eway_rapid.rb, line 327
def cvv_result_from(response)
  verification = response['Verification'] || {}
  case verification['CVN']
  when "Valid"
    "M"
  when "Invalid"
    "N"
  else
    "P"
  end
end
message_from(succeeded, response) click to toggle source
# File lib/active_merchant/billing/gateways/eway_rapid.rb, line 294
def message_from(succeeded, response)
  if response['Errors']
    parse_errors(response['Errors'])
  elsif response['ResponseMessage']
    parse_errors(response['ResponseMessage'])
  elsif response['ResponseCode']
    ActiveMerchant::Billing::EwayGateway::MESSAGES[response['ResponseCode']]
  elsif succeeded
    "Succeeded"
  else
    "Failed"
  end
end
parse(data) click to toggle source
# File lib/active_merchant/billing/gateways/eway_rapid.rb, line 273
def parse(data)
  JSON.parse(data)
end
parse_errors(message) click to toggle source
# File lib/active_merchant/billing/gateways/eway_rapid.rb, line 289
def parse_errors(message)
  errors = message.split(',').collect{|code| MESSAGES[code.strip]}.flatten.join(',')
  errors.presence || message
end
success?(response) click to toggle source
# File lib/active_merchant/billing/gateways/eway_rapid.rb, line 277
def success?(response)
  if response['ResponseCode'] == "00"
    true
  elsif response['TransactionStatus']
    (response['TransactionStatus'] == true)
  elsif response["Succeeded"]
    (response["Succeeded"] == true)
  else
    false
  end
end
truncate(value, max_size = 50) click to toggle source
# File lib/active_merchant/billing/gateways/eway_rapid.rb, line 339
def truncate(value, max_size = 50)
  return nil unless value
  value.to_s[0, max_size]
end
url_for(action) click to toggle source
# File lib/active_merchant/billing/gateways/eway_rapid.rb, line 247
def url_for(action)
  (test? ? test_url : live_url) + action
end