class ActiveMerchant::Billing::FatZebraGateway

Public Class Methods

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

Public Instance Methods

authorize(money, creditcard, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/fat_zebra.rb, line 36
def authorize(money, creditcard, options = {})
  post = {}

  add_amount(post, money, options)
  add_creditcard(post, creditcard, options)
  add_extra_options(post, options)
  add_order_id(post, options)
  add_ip(post, options)
  add_metadata(post, options)
  add_three_ds(post, options)

  post[:capture] = false

  commit(:post, 'purchases', post)
end
capture(money, authorization, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/fat_zebra.rb, line 52
def capture(money, authorization, options = {})
  txn_id, = authorization.to_s.split('|')
  post = {}

  add_amount(post, money, options)
  add_extra_options(post, options)

  commit(:post, "purchases/#{CGI.escape(txn_id)}/capture", post)
end
purchase(money, creditcard, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/fat_zebra.rb, line 22
def purchase(money, creditcard, options = {})
  post = {}

  add_amount(post, money, options)
  add_creditcard(post, creditcard, options)
  add_extra_options(post, options)
  add_order_id(post, options)
  add_ip(post, options)
  add_metadata(post, options)
  add_three_ds(post, options)

  commit(:post, 'purchases', post)
end
refund(money, authorization, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/fat_zebra.rb, line 62
def refund(money, authorization, options = {})
  txn_id, = authorization.to_s.split('|')
  post = {}

  add_extra_options(post, options)
  add_amount(post, money, options)
  post[:transaction_id] = txn_id
  add_order_id(post, options)

  commit(:post, 'refunds', post)
end
scrub(transcript) click to toggle source
# File lib/active_merchant/billing/gateways/fat_zebra.rb, line 93
def scrub(transcript)
  transcript.
    gsub(%r((Authorization: Basic )\w+), '\1[FILTERED]').
    gsub(%r(("card_number\\?":\\?")[^"\\]*)i, '\1[FILTERED]').
    gsub(%r(("cvv\\?":\\?")\d+), '\1[FILTERED]')
end
store(creditcard, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/fat_zebra.rb, line 80
def store(creditcard, options = {})
  post = {}

  add_creditcard(post, creditcard)
  post[:is_billing] = true if options[:recurring]

  commit(:post, 'credit_cards', post)
end
supports_scrubbing?() click to toggle source
# File lib/active_merchant/billing/gateways/fat_zebra.rb, line 89
def supports_scrubbing?
  true
end
void(authorization, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/fat_zebra.rb, line 74
def void(authorization, options = {})
  txn_id, endpoint = authorization.to_s.split('|')

  commit(:post, "#{endpoint}/void?id=#{txn_id}", {})
end

Private Instance Methods

add_amount(post, money, options) click to toggle source
# File lib/active_merchant/billing/gateways/fat_zebra.rb, line 102
def add_amount(post, money, options)
  post[:currency] = (options[:currency] || currency(money))
  post[:currency] = post[:currency].upcase if post[:currency]
  post[:amount] = money
end
add_creditcard(post, creditcard, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/fat_zebra.rb, line 108
def add_creditcard(post, creditcard, options = {})
  if creditcard.respond_to?(:number)
    post[:card_number] = creditcard.number
    post[:card_expiry] = "#{creditcard.month}/#{creditcard.year}"
    post[:cvv] = creditcard.verification_value if creditcard.verification_value?
    post[:card_holder] = creditcard.name if creditcard.name
  elsif creditcard.is_a?(String)
    id, = creditcard.to_s.split('|')
    post[:card_token] = id
    post[:cvv] = options[:cvv]
  elsif creditcard.is_a?(Hash)
    ActiveMerchant.deprecated 'Passing the credit card as a Hash is deprecated. Use a String and put the (optional) CVV in the options hash instead.'
    post[:card_token] = creditcard[:token]
    post[:cvv] = creditcard[:cvv]
  else
    raise ArgumentError.new("Unknown credit card format #{creditcard.inspect}")
  end
end
add_extra_options(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/fat_zebra.rb, line 127
def add_extra_options(post, options)
  extra = {}
  extra[:ecm] = '32' if options[:recurring]
  extra[:name] = options[:merchant] if options[:merchant]
  extra[:location] = options[:merchant_location] if options[:merchant_location]
  extra[:card_on_file] = options.dig(:extra, :card_on_file) if options.dig(:extra, :card_on_file)
  extra[:auth_reason]  = options.dig(:extra, :auth_reason) if options.dig(:extra, :auth_reason)

  unless options[:three_d_secure].present?
    extra[:sli] = options[:sli] if options[:sli]
    extra[:xid] = options[:xid] if options[:xid]
    extra[:cavv] = options[:cavv] if options[:cavv]
  end

  post[:extra] = extra if extra.any?
end
add_ip(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/fat_zebra.rb, line 170
def add_ip(post, options)
  post[:customer_ip] = options[:ip] || '127.0.0.1'
end
add_metadata(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/fat_zebra.rb, line 174
def add_metadata(post, options)
  post[:metadata] = options.fetch(:metadata, {})
end
add_order_id(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/fat_zebra.rb, line 166
def add_order_id(post, options)
  post[:reference] = options[:order_id] || SecureRandom.hex(15)
end
add_three_ds(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/fat_zebra.rb, line 144
def add_three_ds(post, options)
  return unless three_d_secure = options[:three_d_secure]

  post[:extra] = {
    sli: three_d_secure[:eci],
    xid: three_d_secure[:xid],
    cavv: three_d_secure[:cavv],
    par: three_d_secure[:authentication_response_status],
    ver: formatted_enrollment(three_d_secure[:enrolled]),
    threeds_version: three_d_secure[:version],
    directory_server_txn_id: three_d_secure[:ds_transaction_id]
  }.compact
end
authorization_from(response, success, uri) click to toggle source
# File lib/active_merchant/billing/gateways/fat_zebra.rb, line 206
def authorization_from(response, success, uri)
  endpoint = uri.split('/')[0]
  if success
    id = response['response']['id'] || response['response']['token']
    "#{id}|#{endpoint}"
  else
    nil
  end
end
commit(method, uri, parameters = nil) click to toggle source
# File lib/active_merchant/billing/gateways/fat_zebra.rb, line 178
def commit(method, uri, parameters = nil)
  response =
    begin
      parse(ssl_request(method, get_url(uri), parameters.to_json, headers))
    rescue ResponseError => e
      return Response.new(false, 'Invalid Login') if e.response.code == '401'

      parse(e.response.body)
    end

  success = success_from(response)
  Response.new(
    success,
    message_from(response),
    response,
    test: response['test'],
    authorization: authorization_from(response, success, uri)
  )
end
formatted_enrollment(val) click to toggle source
# File lib/active_merchant/billing/gateways/fat_zebra.rb, line 158
def formatted_enrollment(val)
  case val
  when 'Y', 'N', 'U' then val
  when true, 'true' then 'Y'
  when false, 'false' then 'N'
  end
end
get_url(uri) click to toggle source
# File lib/active_merchant/billing/gateways/fat_zebra.rb, line 238
def get_url(uri)
  base = test? ? self.test_url : self.live_url
  base + '/' + uri
end
headers() click to toggle source
# File lib/active_merchant/billing/gateways/fat_zebra.rb, line 243
def headers
  {
    'Authorization' => 'Basic ' + Base64.strict_encode64(@options[:username].to_s + ':' + @options[:token].to_s).strip,
    'User-Agent' => "Fat Zebra v1.0/ActiveMerchant #{ActiveMerchant::VERSION}"
  }
end
message_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/fat_zebra.rb, line 216
def message_from(response)
  if !response['errors'].empty?
    response['errors'].join(', ')
  elsif response['response']['message']
    response['response']['message']
  else
    'Unknown Error'
  end
end
parse(response) click to toggle source
# File lib/active_merchant/billing/gateways/fat_zebra.rb, line 226
def parse(response)
  JSON.parse(response)
rescue JSON::ParserError
  msg = 'Invalid JSON response received from Fat Zebra. Please contact support@fatzebra.com.au if you continue to receive this message.'
  msg += "  (The raw response returned by the API was #{response.inspect})"
  {
    'successful' => false,
    'response' => {},
    'errors' => [msg]
  }
end
success_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/fat_zebra.rb, line 198
def success_from(response)
  (
    response['successful'] &&
    response['response'] &&
    (response['response']['successful'] || response['response']['token'] || response['response']['response_code'] == '00')
  )
end