Module: OffsitePayments::Integrations::Realex::Common

Included in:
Helper, Notification
Defined in:
lib/offsite_payments/integrations/realex.rb

Constant Summary collapse

CURRENCY_SPECIAL_MINOR_UNITS =
{
  'BIF' => 0,
  'BYR' => 0,
  'CLF' => 0,
  'CLP' => 0,
  'CVE' => 0,
  'DJF' => 0,
  'GNF' => 0,
  'HUF' => 0,
  'ISK' => 0,
  'JPY' => 0,
  'KMF' => 0,
  'KRW' => 0,
  'PYG' => 0,
  'RWF' => 0,
  'UGX' => 0,
  'UYI' => 0,
  'VND' => 0,
  'VUV' => 0,
  'XAF' => 0,
  'XOF' => 0,
  'XPF' => 0,
  'BHD' => 3,
  'IQD' => 3,
  'JOD' => 3,
  'KWD' => 3,
  'LYD' => 3,
  'OMR' => 3,
  'TND' => 3,
  'COU' => 4
}

Instance Method Summary collapse

Instance Method Details

#create_signature(fields, secret) ⇒ Object


66
67
68
69
70
71
# File 'lib/offsite_payments/integrations/realex.rb', line 66

def create_signature(fields, secret)
  data = fields.join('.')
  digest = Digest::SHA1.hexdigest(data)
  signed = "#{digest}.#{secret}"
  Digest::SHA1.hexdigest(signed)
end

#extract_avs_code(params = {}) ⇒ Object


94
95
96
# File 'lib/offsite_payments/integrations/realex.rb', line 94

def extract_avs_code(params={})
  [extract_digits(params[:zip]), extract_digits(params[:address1])].join('|')
end

#extract_digits(value) ⇒ Object


90
91
92
# File 'lib/offsite_payments/integrations/realex.rb', line 90

def extract_digits(value)
  value.scan(/\d+/).join('')
end

#format_amount(amount, currency) ⇒ Object

Realex accepts currency amounts as an integer in the lowest value e.g.

format_amount(110.56, 'GBP')
=> 11056

77
78
79
80
81
# File 'lib/offsite_payments/integrations/realex.rb', line 77

def format_amount(amount, currency)
  units = CURRENCY_SPECIAL_MINOR_UNITS[currency] || 2
  multiple = 10**units
  return (amount.to_f * multiple.to_f).to_i
end

#format_amount_as_float(amount, currency) ⇒ Object

Realex returns currency amount as an integer


84
85
86
87
88
# File 'lib/offsite_payments/integrations/realex.rb', line 84

def format_amount_as_float(amount, currency)
  units = CURRENCY_SPECIAL_MINOR_UNITS[currency] || 2
  divisor = 10**units
  return (amount.to_f / divisor.to_f)
end