Class: ActiveMerchant::Billing::Integrations::Valitor::Helper

Inherits:
Helper
  • Object
show all
Includes:
RequiresParameters
Defined in:
lib/active_merchant/billing/integrations/valitor/helper.rb

Constant Summary collapse

DEFAULT_SUCCESS_TEXT =
"The transaction has been completed."

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, mapping, #raw_html_fields, #shipping_address, #test?

Constructor Details

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

Returns a new instance of Helper.



12
13
14
15
16
17
18
19
20
21
# File 'lib/active_merchant/billing/integrations/valitor/helper.rb', line 12

def initialize(order, , options={})
  options[:currency] ||= 'ISK'
  super
  add_field 'Adeinsheimild', '0'
  add_field 'KaupandaUpplysingar', '0'
  add_field 'SlokkvaHaus', '0'
  @security_number = options[:credential2]
  @amount          = options[:amount]
  @order           = order
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ActiveMerchant::Billing::Integrations::Helper

Instance Method Details

#authorize_onlyObject



36
37
38
# File 'lib/active_merchant/billing/integrations/valitor/helper.rb', line 36

def authorize_only
  add_field 'Adeinsheimild', '1'
end

#collect_customer_infoObject



40
41
42
# File 'lib/active_merchant/billing/integrations/valitor/helper.rb', line 40

def collect_customer_info
  add_field 'KaupandaUpplysingar', '1'
end

#form_fieldsObject



73
74
75
76
77
# File 'lib/active_merchant/billing/integrations/valitor/helper.rb', line 73

def form_fields
  product(1, :amount => @amount, :description => @order) if Array(@products).empty?
  @fields[mappings[:success_text]] ||= DEFAULT_SUCCESS_TEXT
  @fields.merge('RafraenUndirskrift' => signature)
end

#format_amount(amount, currency) ⇒ Object



79
80
81
# File 'lib/active_merchant/billing/integrations/valitor/helper.rb', line 79

def format_amount(amount, currency)
  Gateway::CURRENCIES_WITHOUT_FRACTIONS.include?(currency) ? amount.to_f.round : sprintf("%.2f", amount)
end

#hide_headerObject



44
45
46
# File 'lib/active_merchant/billing/integrations/valitor/helper.rb', line 44

def hide_header
  add_field 'SlokkvaHaus', '1'
end

#product(id, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/active_merchant/billing/integrations/valitor/helper.rb', line 48

def product(id, options={})
  raise ArgumentError, "Product id #{id} is not an integer between 1 and 500" unless id.to_i > 0 && id.to_i <= 500
  requires!(options, :amount, :description)
  options.assert_valid_keys([:description, :quantity, :amount, :discount])

  add_field("Vara_#{id}_Verd", format_amount(options[:amount], @fields[mappings[:currency]]))
  add_field("Vara_#{id}_Fjoldi", options[:quantity] || "1")
  
  add_field("Vara_#{id}_Lysing", options[:description]) if options[:description]
  add_field("Vara_#{id}_Afslattur", options[:discount] || '0')
  
  @products ||= []
  @products << id.to_i
end

#signatureObject

Raises:

  • (ArgumentError)


63
64
65
66
67
68
69
70
71
# File 'lib/active_merchant/billing/integrations/valitor/helper.rb', line 63

def signature
  raise ArgumentError, "Security number not set" unless @security_number
  parts = [@security_number, @fields['Adeinsheimild']]
  @products.sort.uniq.each do |id|
    parts.concat(["Vara_#{id}_Fjoldi", "Vara_#{id}_Verd", "Vara_#{id}_Afslattur"].collect{|e| @fields[e]})
  end if @products
  parts.concat(%w(VefverslunID Tilvisunarnumer SlodTokstAdGjaldfaera SlodTokstAdGjaldfaeraServerSide Gjaldmidill).collect{|e| @fields[e]})
  Digest::MD5.hexdigest(parts.compact.join(''))
end