Class: OffsitePayments::Integrations::Klarna::Helper

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

Instance Attribute Summary

Attributes inherited from Helper

#fields

Instance Method Summary collapse

Methods inherited from Helper

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

Constructor Details

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

Returns a new instance of Helper.



54
55
56
57
58
59
60
61
# File 'lib/offsite_payments/integrations/klarna.rb', line 54

def initialize(order, , options = {})
  super
  @shared_secret = options[:credential2]
  @order = order

  add_field('platform_type', application_id)
  add_field('test_mode', test?.to_s)
end

Dynamic Method Handling

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

Instance Method Details

#billing_address(billing_fields) ⇒ Object



90
91
92
93
94
95
# File 'lib/offsite_payments/integrations/klarna.rb', line 90

def billing_address(billing_fields)
  country = billing_fields[:country]

  add_field('purchase_country', country)
  add_field('locale', guess_locale_based_on_country(country))
end

#form_fieldsObject



110
111
112
113
114
# File 'lib/offsite_payments/integrations/klarna.rb', line 110

def form_fields
  sign_fields

  super
end

#line_item(item) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/offsite_payments/integrations/klarna.rb', line 73

def line_item(item)
  @line_items ||= []
  @line_items << item

  i = @line_items.size - 1

  add_field("cart_item-#{i}_type", type_for(item))
  add_field("cart_item-#{i}_reference", item.fetch(:reference, ''))
  add_field("cart_item-#{i}_name", item.fetch(:name, ''))
  add_field("cart_item-#{i}_quantity", item.fetch(:quantity, ''))
  add_field("cart_item-#{i}_unit_price", tax_included_unit_price(item)).to_s
  add_field("cart_item-#{i}_discount_rate", item.fetch(:discount_rate, ''))
  add_field("cart_item-#{i}_tax_rate", tax_rate_for(item)).to_s

  @fields
end

#notify_url(url) ⇒ Object



63
64
65
66
# File 'lib/offsite_payments/integrations/klarna.rb', line 63

def notify_url(url)
  url = append_order_query_param(url)
  add_field('merchant_push_uri', url)
end

#return_url(url) ⇒ Object



68
69
70
71
# File 'lib/offsite_payments/integrations/klarna.rb', line 68

def return_url(url)
  url = append_order_query_param(url)
  add_field('merchant_confirmation_uri', url)
end

#shipping_address(shipping_fields) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/offsite_payments/integrations/klarna.rb', line 97

def shipping_address(shipping_fields)
  add_field('shipping_address_given_name', shipping_fields[:first_name])
  add_field('shipping_address_family_name', shipping_fields[:last_name])

  street_address = [shipping_fields[:address1], shipping_fields[:address2]].compact.join(', ')
  add_field('shipping_address_street_address', street_address)

  add_field('shipping_address_postal_code', shipping_fields[:zip])
  add_field('shipping_address_city', shipping_fields[:city])
  add_field('shipping_address_country', shipping_fields[:country])
  add_field('shipping_address_phone', shipping_fields[:phone])
end

#sign_fieldsObject



116
117
118
119
# File 'lib/offsite_payments/integrations/klarna.rb', line 116

def sign_fields
  merchant_digest = Klarna.sign(@fields, @line_items, @shared_secret)
  add_field('merchant_digest', merchant_digest)
end