Module: OffsitePayments::Integrations::Klarna

Defined in:
lib/offsite_payments/integrations/klarna.rb

Defined Under Namespace

Classes: Helper, Notification

Class Method Summary collapse

Class Method Details

.cart_items_payload(fields, cart_items) ⇒ Object


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/offsite_payments/integrations/klarna.rb', line 15

def self.cart_items_payload(fields, cart_items)
  check_required_fields!(fields)

  payload = fields['purchase_country'].to_s +
            fields['purchase_currency'].to_s +
            fields['locale'].to_s

  cart_items.each_with_index do |item, i|
    payload << fields["cart_item-#{i}_type"].to_s +
               fields["cart_item-#{i}_reference"].to_s +
               fields["cart_item-#{i}_quantity"].to_s +
               fields["cart_item-#{i}_unit_price"].to_s +
               fields.fetch("cart_item-#{i}_discount_rate", '').to_s
  end

  payload << fields['merchant_id'].to_s +
             fields['merchant_terms_uri'].to_s +
             fields['merchant_checkout_uri'].to_s +
             fields['merchant_base_uri'].to_s +
             fields['merchant_confirmation_uri'].to_s

  payload
end

.digest(payload, shared_secret) ⇒ Object


45
46
47
# File 'lib/offsite_payments/integrations/klarna.rb', line 45

def self.digest(payload, shared_secret)
  Digest::SHA256.base64digest(payload + shared_secret.to_s)
end

.notification(post_body, options = {}) ⇒ Object


7
8
9
# File 'lib/offsite_payments/integrations/klarna.rb', line 7

def self.notification(post_body, options = {})
  Notification.new(post_body, options)
end

.return(query_string, options = {}) ⇒ Object


11
12
13
# File 'lib/offsite_payments/integrations/klarna.rb', line 11

def self.return(query_string, options = {})
  Return.new(query_string, options)
end

.sign(fields, cart_items, shared_secret) ⇒ Object


39
40
41
42
43
# File 'lib/offsite_payments/integrations/klarna.rb', line 39

def self.sign(fields, cart_items, shared_secret)
  payload = cart_items_payload(fields, cart_items)

  digest(payload, shared_secret)
end