1# frozen_string_literal: true
2
3require_relative "sim_pricing"
4
5class SIMKind
6 def initialize(variant)
7 @klass =
8 case variant
9 when "sim"
10 SIMOrder
11 when "esim"
12 SIMOrder::ESIM
13 else
14 raise "Unknown SIM kind"
15 end
16 @variant = variant
17 end
18
19 def self.from_form(form)
20 new(form.field("sim_kind")&.value)
21 end
22
23 # @param [Customer] customer
24 # @raise [ArgumentError] If matching SIM price is missing from `CONFIG[:sims]`
25 def get_processor(customer)
26 price = price_cents(customer.currency)
27 raise ArgumentError, "Invalid config" unless price
28
29 @klass.for(
30 customer,
31 price: price,
32 plan: SIMPricing.plan_label(customer.currency, CONFIG)
33 )
34 end
35
36 # @return [String] The result of either
37 # SIMOrder.label or SIMOrder::ESIM.label
38 def label
39 @klass.label
40 end
41
42 # @param [Customer] customer
43 # @return [Float, NilClass] Returns nil if `customer.currency`
44 # is nil, or if @variant is malformed.
45 def price(customer)
46 price = price_cents(customer.currency || :USD)
47 price / 100.to_d if price
48 end
49
50protected
51
52 # @param [Symbol] currency One of the currencies from
53 # CONFIG[:plans] entries
54 def price_cents(currency)
55 CONFIG.dig(:sims, @variant.to_sym, currency)
56 end
57end