1# frozen_string_literal: true
2
3require_relative "trust_level_repo"
4require_relative "credit_card_sale"
5
6class BuyAccountCreditForm
7 def self.trust_level_repo(**kwargs)
8 kwargs[:trust_level_repo] || TrustLevelRepo.new(**kwargs)
9 end
10
11 def self.for(customer)
12 trust_level_repo.find(customer).then do |trust_level|
13 customer.payment_methods.then do |payment_methods|
14 new(customer.balance, payment_methods, trust_level.max_top_up_amount)
15 end
16 end
17 end
18
19 def initialize(balance, payment_methods, max_top_up_amount)
20 @balance = balance
21 @payment_methods = payment_methods
22 @max_top_up_amount = max_top_up_amount
23 end
24
25 def form
26 FormTemplate.render(
27 :top_up,
28 balance: @balance,
29 payment_methods: @payment_methods,
30 range: [15, @max_top_up_amount]
31 )
32 end
33
34 def parse(form)
35 amount = form.field("amount")&.value&.to_s
36
37 {
38 payment_method: @payment_methods.fetch(
39 form.field("payment_method")&.value.to_i
40 ),
41 amount: amount
42 }
43 end
44end