Module: Klarna::API::Methods::CostCalculations

Included in:
Klarna::API::Methods
Defined in:
lib/klarna/api/methods/cost_calculations.rb

Instance Method Summary collapse

Instance Method Details

#calculate_monthly_cost(sum, currency, pclass_id, flags = nil) ⇒ Object

Calculate monthly cost “in the most proper way”.

Raises:

  • (NotImplementedError)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/klarna/api/methods/cost_calculations.rb', line 28

def calculate_monthly_cost(sum, currency, pclass_id, flags = nil)
  # # TODO: OpenStruct this
  # pclasses = ::Klarna.store_pclasses[currency.to_s.underscore]
  # month_count = pclasses[pclass_id][:months]
  # monthly_fee = pclasses[pclass_id][:invoice_fee]
  # start_fee = pclasses[pclass_id][:start_fee]
  # rate = pclasses[pclass_id][:interest_rate]
  # type = pclasses[pclass_id][:type] # QUESTION: Where do I get this from - not in API-call.
  #
  # # TODO: Call Klarna and ask where I get "type" from - not with pclasses.
  # case type
  # when ::Klarna::API::PClassFlags::ANNUITY
  #   self.periodic_cost(sum, month_count, monthly_fee, start_fee, rate, currency, flags)
  # when ::Klarna::API::PClassFlags::DIVISOR
  #   self.monthly_cost(sum, month_count, monthly_fee, start_fee, rate, currency, flags)
  # else
  #   raise InvalidArgumentError, "Invalid Klarna campaign/pclass type: #{type.inspect}"
  # end
  raise NotImplementedError
end

#fetch_pclasses(currency_code) ⇒ Object

Purpose: Obtain pclass values from Klarna.

Note:

This function is only to be used to obtain pclass values for stores ONE TIME ONLY.
It is not allowed to use this function for continuous calculation of monthly costs or
with every purchase in the checkout. The pclass values do NOT change.

Raises:

  • (NotImplementedError)


16
17
18
19
20
21
22
23
24
# File 'lib/klarna/api/methods/cost_calculations.rb', line 16

def fetch_pclasses(currency_code)
  # params = [
  #   self.store_id,
  #   currency_code,
  #   self.digest(currency_code)
  # ]
  # self.call(:fetch_pclasses, *params)
  raise NotImplementedError
end

#monthly_cost(sum, month_count, monthly_fee, start_fee, rate, currency, flags = nil) ⇒ Object

Calculate the monthly cost for account purchases.

Raises:

  • (NotImplementedError)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/klarna/api/methods/cost_calculations.rb', line 61

def monthly_cost(sum, month_count, monthly_fee, start_fee, rate, currency, flags = nil)
  # interest_cost = self.calculate_interest_cost(sum, rate)
  # period_cost = (sum + interest_cost) / month_count
  # flags ||= ::Klarna::API::MonthlyCostFlags::LIMIT
  #
  # # TODO: Remove this line - after debugging.
  # ::Klarna.log [::Klarna::API::AVERAGE_INTEREST_PERIOD, calc_rate, interest_cost, period_cost].join(', ')
  #
  # monthly_cost = case flags
  #   when ::Klarna::API::MonthlyCostFlags::LIMIT
  #     period_cost
  #   when ::Klarna::API::MonthlyCostFlags::ACTUAL
  #     begin
  #       lowest_monthly_payment = ::Klarna::API::LOWEST_PAYMENT_BY_CURRENCY[currency]
  #     rescue
  #       raise ::Klarna::API::KlarnaStandardError,
  #       "No such currency: #{currency.inspect}. Valid currencies: SEK:1, NOK:2, EUR:3, DKK:4"
  #     end
  #     period_cost += monthly_fee
  #     (period_cost < lowest_monthly_payment) ? lowest_monthly_payment : period_cost
  #   else
  #     raise ::Klarna::API::KlarnaStandardError,
  #       "Invalid flag: #{flags.inspect}. Valid flags: LIMIT:0, ACTUAL:1"
  #       end
  # ::Klarna.log_result("Calculation: monthly_cost = %s") do
  #   self.round_up(monthly_cost, currency)
  # end
  raise NotImplementedError
end

#periodic_cost(sum, month_count, monthly_fee, start_fee, rate, currency, flags = nil) ⇒ Object

Calculate the monthly cost for goods which can be paid for by part payment.

Raises:

  • (NotImplementedError)


51
52
53
54
55
56
57
# File 'lib/klarna/api/methods/cost_calculations.rb', line 51

def periodic_cost(sum, month_count, monthly_fee, start_fee, rate, currency, flags = nil)
  # daily_rate = self.calculate_daily_rate(rate)
  # monthly_payment = self.calculate_monthly_payment(sum + start_fee, daily_rate, month_count)
  # monthly_cost = monthly_payment + monthly_fee
  # monthly_cost.round
  raise NotImplementedError
end