Class: OffsitePayments::Integrations::QuickpayV10::Helper

Inherits:
Helper
  • Object
show all
Defined in:
lib/offsite_payments/integrations/quickpay_v10.rb

Overview

credential2: Payment window API key

Instance Attribute Summary

Attributes inherited from Helper

#fields

Instance Method Summary collapse

Methods inherited from Helper

#add_field, #add_fields, #add_raw_html_field, #billing_address, #form_method, inherited, mapping, #raw_html_fields, #shipping_address, #test?

Methods included from MoneyCompatibility

#to_cents

Constructor Details

#initialize(order, account, options = {}) ⇒ Helper

Returns a new instance of Helper.



19
20
21
22
23
24
25
26
27
# File 'lib/offsite_payments/integrations/quickpay_v10.rb', line 19

def initialize(order, , options = {})
  payment_window_api_key options.delete(:credential2)
  super
  add_field('version', 'v10')
  add_field('type', 'payment')
  add_field('language', 'da')
  add_field('autocapture', 0)
  add_field('order_id', format_order_number(order))
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class OffsitePayments::Helper

Instance Method Details

#flatten_params(obj, result = {}, path = []) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/offsite_payments/integrations/quickpay_v10.rb', line 37

def flatten_params(obj, result = {}, path = [])
  case obj
  when Hash
    obj.each do |k, v|
      flatten_params(v, result, [*path, k])
    end
  when Array
    obj.each_with_index do |v, i|
      flatten_params(v, result, [*path, i])
    end
  else
    result[path.map{|p| "[#{p}]"}.join.to_sym] = obj
  end
  result
end

#form_fieldsObject



33
34
35
# File 'lib/offsite_payments/integrations/quickpay_v10.rb', line 33

def form_fields
  @fields.merge('checksum' => generate_checksum)
end

#format_order_number(number) ⇒ Object

Limited to 20 digits max



61
62
63
# File 'lib/offsite_payments/integrations/quickpay_v10.rb', line 61

def format_order_number(number)
  number.to_s.gsub(/[^\w]/, '').rjust(4, "0")[0...20]
end

#generate_checksumObject



53
54
55
56
57
58
# File 'lib/offsite_payments/integrations/quickpay_v10.rb', line 53

def generate_checksum
  flattened_params = flatten_params(@fields)
  values = flattened_params.sort.map { |_, value| value }
  base = values.join(' ')
  OpenSSL::HMAC.hexdigest('sha256', @payment_window_api_key, base)
end

#payment_window_api_key(value) ⇒ Object



29
30
31
# File 'lib/offsite_payments/integrations/quickpay_v10.rb', line 29

def payment_window_api_key(value)
  @payment_window_api_key = value
end