class OffsitePayments::Integrations::Klarna::Helper

Public Class Methods

new(order, account, options = {}) click to toggle source
Calls superclass method
# File lib/offsite_payments/integrations/klarna.rb, line 54
def initialize(order, account, options = {})
  super
  @shared_secret = options[:credential2]
  @order = order

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

Public Instance Methods

billing_address(billing_fields) click to toggle source
# 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_fields() click to toggle source
Calls superclass method
# File lib/offsite_payments/integrations/klarna.rb, line 110
def form_fields
  sign_fields

  super
end
line_item(item) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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_fields() click to toggle source
# 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

Private Instance Methods

append_order_query_param(url) click to toggle source
# File lib/offsite_payments/integrations/klarna.rb, line 136
def append_order_query_param(url)
  u = URI.parse(url)
  params = Rack::Utils.parse_nested_query(u.query)
  params["order"] = @order
  u.query = params.to_query

  u.to_s
end
guess_locale_based_on_country(country_code) click to toggle source
# File lib/offsite_payments/integrations/klarna.rb, line 145
def guess_locale_based_on_country(country_code)
  case country_code
  when /no/i
    "nb-no"
  when /fi/i
    "fi-fi"
  when /se/i
    "sv-se"
  else
    "sv-se"
  end
end
percentage_to_two_decimal_precision_whole_number(percentage) click to toggle source
# File lib/offsite_payments/integrations/klarna.rb, line 176
def percentage_to_two_decimal_precision_whole_number(percentage)
  (percentage * 10000).to_i
end
tax_included_unit_price(item) click to toggle source
# File lib/offsite_payments/integrations/klarna.rb, line 158
def tax_included_unit_price(item)
  item.fetch(:unit_price, '').to_i + item.fetch(:tax_amount, '').to_i
end
tax_rate_for(item) click to toggle source
# File lib/offsite_payments/integrations/klarna.rb, line 162
def tax_rate_for(item)
  subtotal_price = item.fetch(:unit_price, 0).to_f * item.fetch(:quantity, 0).to_i
  tax_amount = item.fetch(:tax_amount, 0).to_f

  if subtotal_price > 0
    tax_rate = tax_amount / subtotal_price
    tax_rate = tax_rate.round(4)

    percentage_to_two_decimal_precision_whole_number(tax_rate)
  else
    0
  end
end
type_for(item) click to toggle source
# File lib/offsite_payments/integrations/klarna.rb, line 123
def type_for(item)
  case item.fetch(:type, '')
  when 'shipping'
    'shipping_fee'
  when 'line item'
    'physical'
  when 'discount'
    'discount'
  else
    raise StandardError, "Unable to determine type for item #{item.to_yaml}"
  end
end