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 61
def authorize(amount, payment_method, options = {})
  params = {}
  add_metadata(params, options)
  add_invoice(params, amount, options)
  add_customer_data(params, options, payment_method)
  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 71
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.
:invoice          - The merchant’s invoice number for this
                    transaction (optional).
: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/activemerchant/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 50
def purchase(amount, payment_method, options = {})
  params = {}
  add_metadata(params, options)
  add_invoice(params, amount, options)
  add_customer_data(params, options, payment_method)
  add_credit_card(params, payment_method, options)
  add_3ds_authenticated_data(params, options) if options[:three_d_secure]
  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:

:invoice          - The merchant’s invoice number for this
                    transaction (optional).
: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/activemerchant/active_merchant")

Returns an ActiveMerchant::Billing::Response object

# File lib/active_merchant/billing/gateways/eway_rapid.rb, line 110
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
scrub(transcript) click to toggle source
# File lib/active_merchant/billing/gateways/eway_rapid.rb, line 181
def scrub(transcript)
  transcript.
    gsub(%r((Authorization: Basic )\w+), '\1[FILTERED]').
    gsub(%r(("Number\\?":\\?")[^"]*)i, '\1[FILTERED]').
    gsub(%r(("CVN\\?":\\?"?)[^",]*)i, '\1[FILTERED]')
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/activemerchant/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 136
def store(payment_method, options = {})
  requires!(options, :billing_address)
  params = {}
  add_metadata(params, options)
  add_invoice(params, 0, options)
  add_customer_data(params, options, payment_method)
  add_credit_card(params, payment_method, options)
  params['Method'] = 'CreateTokenCustomer'
  commit(url_for('Transaction'), params)
end
supports_scrubbing() click to toggle source
# File lib/active_merchant/billing/gateways/eway_rapid.rb, line 177
def supports_scrubbing
  true
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/activemerchant/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 166
def update(customer_token, payment_method, options = {})
  params = {}
  add_metadata(params, options)
  add_invoice(params, 0, options)
  add_customer_data(params, options, payment_method)
  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 79
def void(identification, options = {})
  params = {}
  add_reference(params, identification)
  commit(url_for('CancelAuthorisation'), params)
end

Private Instance Methods

add_3ds_authenticated_data(params, options) click to toggle source
# File lib/active_merchant/billing/gateways/eway_rapid.rb, line 201
def add_3ds_authenticated_data(params, options)
  three_d_secure_options = options[:three_d_secure]
  params['PaymentInstrument'] ||= {} if params['PaymentInstrument'].nil?
  threed_secure_auth = params['PaymentInstrument']['ThreeDSecureAuth'] = {}
  threed_secure_auth['Cryptogram'] = three_d_secure_options[:cavv]
  threed_secure_auth['ECI'] = three_d_secure_options[:eci]
  threed_secure_auth['XID'] = three_d_secure_options[:xid]
  threed_secure_auth['AuthStatus'] = three_d_secure_options[:authentication_response_status]
  threed_secure_auth['dsTransactionId'] = three_d_secure_options[:ds_transaction_id]
  threed_secure_auth['Version'] = three_d_secure_options[:version]
end
add_address(params, address, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/eway_rapid.rb, line 267
def add_address(params, address, options = {})
  return unless address

  params['Title'] = address[:title]
  params['CompanyName'] = address[:company] unless options[:skip_company]
  params['Street1'] = truncate(address[:address1], 50)
  params['Street2'] = truncate(address[:address2], 50)
  params['City'] = truncate(address[:city], 50)
  params['State'] = address[:state]
  params['PostalCode'] = address[:zip]
  params['Country'] = address[:country].to_s.downcase
  params['Phone'] = address[:phone] || address[:phone_number]
  params['Fax'] = address[:fax]
end
add_credit_card(params, credit_card, options) click to toggle source
# File lib/active_merchant/billing/gateways/eway_rapid.rb, line 282
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, 50)
    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, payment_method = nil) click to toggle source
# File lib/active_merchant/billing/gateways/eway_rapid.rb, line 228
def add_customer_data(params, options, payment_method = nil)
  add_customer_fields(params, options, payment_method)
  add_shipping_fields(params, options)
end
add_customer_fields(params, options, payment_method) click to toggle source
# File lib/active_merchant/billing/gateways/eway_rapid.rb, line 233
def add_customer_fields(params, options, payment_method)
  key = 'Customer'
  params[key] ||= {}

  customer_address = options[:billing_address] || options[:address]

  add_name_and_email(params[key], customer_address, options[:email], payment_method)
  add_address(params[key], customer_address)
end
add_customer_token(params, token) click to toggle source
# File lib/active_merchant/billing/gateways/eway_rapid.rb, line 298
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 213
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], 50),
    'InvoiceNumber' => truncate(options[:invoice] || options[:order_id], 12),
    '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 190
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_name_and_email(params, address, email, payment_method = nil) click to toggle source
# File lib/active_merchant/billing/gateways/eway_rapid.rb, line 251
def add_name_and_email(params, address, email, payment_method = nil)
  if address.present?
    params['FirstName'], params['LastName'] = split_names(address[:name])
  elsif payment_method_name_available?(payment_method)
    params['FirstName'] = payment_method.first_name
    params['LastName'] = payment_method.last_name
  end

  params['Email'] = email
end
add_reference(params, reference) click to toggle source
# File lib/active_merchant/billing/gateways/eway_rapid.rb, line 224
def add_reference(params, reference)
  params['TransactionID'] = reference
end
add_shipping_fields(params, options) click to toggle source
# File lib/active_merchant/billing/gateways/eway_rapid.rb, line 243
def add_shipping_fields(params, options)
  key = 'ShippingAddress'
  params[key] = {}

  add_name_and_email(params[key], options[:shipping_address], options[:email])
  add_address(params[key], options[:shipping_address], { skip_company: true })
end
authorization_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/eway_rapid.rb, line 364
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 precedence 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 370
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 307
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 384
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 350
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 329
def parse(data)
  JSON.parse(data)
end
parse_errors(message) click to toggle source
# File lib/active_merchant/billing/gateways/eway_rapid.rb, line 345
def parse_errors(message)
  errors = message.split(',').collect { |code| MESSAGES[code.strip] }.flatten.join(',')
  errors.presence || message
end
payment_method_name_available?(payment_method) click to toggle source
# File lib/active_merchant/billing/gateways/eway_rapid.rb, line 262
def payment_method_name_available?(payment_method)
  payment_method.respond_to?(:first_name) && payment_method.respond_to?(:last_name) &&
    payment_method.first_name.present? && payment_method.last_name.present?
end
success?(response) click to toggle source
# File lib/active_merchant/billing/gateways/eway_rapid.rb, line 333
def success?(response)
  if response['ResponseCode'] == '00'
    true
  elsif response['TransactionStatus']
    (response['TransactionStatus'] == true)
  elsif response['Succeeded']
    (response['Succeeded'] == true)
  else
    false
  end
end
url_for(action) click to toggle source
# File lib/active_merchant/billing/gateways/eway_rapid.rb, line 303
def url_for(action)
  (test? ? test_url : live_url) + action
end