buy_account_credit_form.rb

 1# frozen_string_literal: true
 2
 3require_relative "./xep0122_field"
 4
 5class BuyAccountCreditForm
 6	def initialize(customer)
 7		@customer = customer
 8	end
 9
10	AMOUNT_FIELD =
11		XEP0122Field.new(
12			"xs:decimal",
13			range: (0..1000),
14			var: "amount",
15			label: "Amount of credit to buy",
16			required: true
17		).field
18
19	def balance
20		{
21			type: "fixed",
22			value: "Current balance: $#{'%.2f' % @customer.balance}"
23		}
24	end
25
26	def add_to_form(form)
27		@customer.payment_methods.then do |payment_methods|
28			form.type = :form
29			form.title = "Buy Account Credit"
30			form.fields = [
31				balance,
32				payment_methods.to_list_single,
33				AMOUNT_FIELD
34			]
35		end
36	end
37end