class ActiveMerchant::Billing::EbanxGateway

Constants

CARD_BRAND
HTTP_METHOD
URL_MAP

Public Class Methods

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

Public Instance Methods

authorize(money, payment, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/ebanx.rb, line 58
def authorize(money, payment, options={})
  post = { payment: {} }
  add_integration_key(post)
  add_operation(post)
  add_invoice(post, money, options)
  add_customer_data(post, payment, options)
  add_card_or_token(post, payment)
  add_address(post, options)
  add_customer_responsible_person(post, payment, options)
  post[:payment][:creditcard][:auto_capture] = false

  commit(:authorize, post)
end
capture(money, authorization, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/ebanx.rb, line 72
def capture(money, authorization, options={})
  post = {}
  add_integration_key(post)
  post[:hash] = authorization
  post[:amount] = amount(money)

  commit(:capture, post)
end
purchase(money, payment, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/ebanx.rb, line 45
def purchase(money, payment, options={})
  post = { payment: {} }
  add_integration_key(post)
  add_operation(post)
  add_invoice(post, money, options)
  add_customer_data(post, payment, options)
  add_card_or_token(post, payment)
  add_address(post, options)
  add_customer_responsible_person(post, payment, options)

  commit(:purchase, post)
end
refund(money, authorization, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/ebanx.rb, line 81
def refund(money, authorization, options={})
  post = {}
  add_integration_key(post)
  add_operation(post)
  add_authorization(post, authorization)
  post[:amount] = amount(money)
  post[:description] = options[:description]

  commit(:refund, post)
end
scrub(transcript) click to toggle source
# File lib/active_merchant/billing/gateways/ebanx.rb, line 120
def scrub(transcript)
  transcript.
    gsub(/(integration_key\\?":\\?")(\d*)/, '\1[FILTERED]').
    gsub(/(card_number\\?":\\?")(\d*)/, '\1[FILTERED]').
    gsub(/(card_cvv\\?":\\?")(\d*)/, '\1[FILTERED]')
end
store(credit_card, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/ebanx.rb, line 100
def store(credit_card, options={})
  post = {}
  add_integration_key(post)
  add_payment_details(post, credit_card)
  post[:country] = customer_country(options)

  commit(:store, post)
end
supports_scrubbing?() click to toggle source
# File lib/active_merchant/billing/gateways/ebanx.rb, line 116
def supports_scrubbing?
  true
end
verify(credit_card, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/ebanx.rb, line 109
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/ebanx.rb, line 92
def void(authorization, options={})
  post = {}
  add_integration_key(post)
  add_authorization(post, authorization)

  commit(:void, post)
end

Private Instance Methods

add_address(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/ebanx.rb, line 158
def add_address(post, options)
  if address = options[:billing_address] || options[:address]
    post[:payment][:address] = address[:address1].split[1..-1].join(' ') if address[:address1]
    post[:payment][:street_number] = address[:address1].split.first if address[:address1]
    post[:payment][:city] = address[:city]
    post[:payment][:state] = address[:state]
    post[:payment][:zipcode] = address[:zip]
    post[:payment][:country] = address[:country].downcase
    post[:payment][:phone_number] = address[:phone]
  end
end
add_authorization(post, authorization) click to toggle source
# File lib/active_merchant/billing/gateways/ebanx.rb, line 137
def add_authorization(post, authorization)
  post[:hash] = authorization
end
add_card_or_token(post, payment) click to toggle source
# File lib/active_merchant/billing/gateways/ebanx.rb, line 177
def add_card_or_token(post, payment)
  if payment.is_a?(String)
    payment, brand = payment.split('|')
  end
  post[:payment][:payment_type_code] = payment.is_a?(String) ? brand : CARD_BRAND[payment.brand.to_sym]
  post[:payment][:creditcard] = payment_details(payment)
end
add_customer_data(post, payment, options) click to toggle source
# File lib/active_merchant/billing/gateways/ebanx.rb, line 141
def add_customer_data(post, payment, options)
  post[:payment][:name] = customer_name(payment, options)
  post[:payment][:email] = options[:email] || 'unspecified@example.com'
  post[:payment][:document] = options[:document]
  post[:payment][:birth_date] = options[:birth_date] if options[:birth_date]
end
add_customer_responsible_person(post, payment, options) click to toggle source
# File lib/active_merchant/billing/gateways/ebanx.rb, line 148
def add_customer_responsible_person(post, payment, options)
  post[:payment][:person_type] = options[:person_type] if options[:person_type]
  if options[:person_type]&.casecmp('business')&.zero?
    post[:payment][:responsible] = {}
    post[:payment][:responsible][:name] = options[:responsible_name] if options[:responsible_name]
    post[:payment][:responsible][:document] = options[:responsible_document] if options[:responsible_document]
    post[:payment][:responsible][:birth_date] = options[:responsible_birth_date] if options[:responsible_birth_date]
  end
end
add_integration_key(post) click to toggle source
# File lib/active_merchant/billing/gateways/ebanx.rb, line 129
def add_integration_key(post)
  post[:integration_key] = @options[:integration_key].to_s
end
add_invoice(post, money, options) click to toggle source
# File lib/active_merchant/billing/gateways/ebanx.rb, line 170
def add_invoice(post, money, options)
  post[:payment][:amount_total] = amount(money)
  post[:payment][:currency_code] = (options[:currency] || currency(money))
  post[:payment][:merchant_payment_code] = options[:order_id]
  post[:payment][:instalments] = options[:instalments] || 1
end
add_operation(post) click to toggle source
# File lib/active_merchant/billing/gateways/ebanx.rb, line 133
def add_operation(post)
  post[:operation] = 'request'
end
add_payment_details(post, payment) click to toggle source
# File lib/active_merchant/billing/gateways/ebanx.rb, line 185
def add_payment_details(post, payment)
  post[:payment_type_code] = CARD_BRAND[payment.brand.to_sym]
  post[:creditcard] = payment_details(payment)
end
authorization_from(action, parameters, response) click to toggle source
# File lib/active_merchant/billing/gateways/ebanx.rb, line 242
def authorization_from(action, parameters, response)
  if action == :store
    "#{response.try(:[], "token")}|#{CARD_BRAND[parameters[:payment_type_code].to_sym]}"
  else
    response.try(:[], 'payment').try(:[], 'hash')
  end
end
commit(action, parameters) click to toggle source
# File lib/active_merchant/billing/gateways/ebanx.rb, line 207
def commit(action, parameters)
  url = url_for((test? ? test_url : live_url), action, parameters)
  response = parse(ssl_request(HTTP_METHOD[action], url, post_data(action, parameters), {}))

  success = success_from(action, response)

  Response.new(
    success,
    message_from(response),
    response,
    authorization: authorization_from(action, parameters, response),
    test: test?,
    error_code: error_code_from(response, success)
  )
end
convert_to_url_form_encoded(parameters) click to toggle source
# File lib/active_merchant/billing/gateways/ebanx.rb, line 266
def convert_to_url_form_encoded(parameters)
  parameters.map do |key, value|
    next if value != false && value.blank?
    "#{key}=#{value}"
  end.compact.join('&')
end
customer_country(options) click to toggle source
# File lib/active_merchant/billing/gateways/ebanx.rb, line 280
def customer_country(options)
  if country = options[:country] || (options[:billing_address][:country] if options[:billing_address])
    country.downcase
  end
end
customer_name(payment, options) click to toggle source
# File lib/active_merchant/billing/gateways/ebanx.rb, line 286
def customer_name(payment, options)
  address_name = options[:billing_address][:name] if options[:billing_address] && options[:billing_address][:name]
  if payment.is_a?(String)
    address_name || 'Not Provided'
  else
    payment.name
  end
end
error_code_from(response, success) click to toggle source
# File lib/active_merchant/billing/gateways/ebanx.rb, line 273
def error_code_from(response, success)
  unless success
    return response['status_code'] if response['status'] == 'ERROR'
    response.try(:[], 'payment').try(:[], 'transaction_status').try(:[], 'code')
  end
end
message_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/ebanx.rb, line 237
def message_from(response)
  return response['status_message'] if response['status'] == 'ERROR'
  response.try(:[], 'payment').try(:[], 'transaction_status').try(:[], 'description')
end
parse(body) click to toggle source
# File lib/active_merchant/billing/gateways/ebanx.rb, line 203
def parse(body)
  JSON.parse(body)
end
payment_details(payment) click to toggle source
# File lib/active_merchant/billing/gateways/ebanx.rb, line 190
def payment_details(payment)
  if payment.is_a?(String)
    { token: payment }
  else
    {
      card_number: payment.number,
      card_name: payment.name,
      card_due_date: "#{payment.month}/#{payment.year}",
      card_cvv: payment.verification_value
    }
  end
end
post_data(action, parameters = {}) click to toggle source
# File lib/active_merchant/billing/gateways/ebanx.rb, line 250
def post_data(action, parameters = {})
  return nil if requires_http_get(action)
  return convert_to_url_form_encoded(parameters) if action == :refund
  "request_body=#{parameters.to_json}"
end
requires_http_get(action) click to toggle source
# File lib/active_merchant/billing/gateways/ebanx.rb, line 261
def requires_http_get(action)
  return true if [:capture, :void].include?(action)
  false
end
success_from(action, response) click to toggle source
# File lib/active_merchant/billing/gateways/ebanx.rb, line 223
def success_from(action, response)
  if [:purchase, :capture, :refund].include?(action)
    response.try(:[], 'payment').try(:[], 'status') == 'CO'
  elsif action == :authorize
    response.try(:[], 'payment').try(:[], 'status') == 'PE'
  elsif action == :void
    response.try(:[], 'payment').try(:[], 'status') == 'CA'
  elsif action == :store
    response.try(:[], 'status') == 'SUCCESS'
  else
    false
  end
end
url_for(hostname, action, parameters) click to toggle source
# File lib/active_merchant/billing/gateways/ebanx.rb, line 256
def url_for(hostname, action, parameters)
  return "#{hostname}#{URL_MAP[action]}?#{convert_to_url_form_encoded(parameters)}" if requires_http_get(action)
  "#{hostname}#{URL_MAP[action]}"
end