1# frozen_string_literal: true
2
3class AltTopUpForm
4 def self.for(customer)
5 customer.btc_addresses.then do |addrs|
6 AltTopUpForm.new(customer, *[
7 (IS_CAD if customer.currency == :CAD),
8 (HasBitcoinAddresses.new(addrs) unless addrs.empty?),
9 AddBtcAddressField.for(addrs)
10 ].compact)
11 end
12 end
13
14 def initialize(customer, *fields)
15 @fields = fields
16 @balance = customer.balance
17 end
18
19 def form
20 form = Blather::Stanza::X.new(:result)
21 form.type = :form
22 form.title = "Buy Account Credit"
23 form.instructions =
24 "Besides credit cards, we support payment by Bitcoin, postal mail, " \
25 "or in Canada by Interac eTransfer."
26
27 form.fields = fields.to_a
28 form
29 end
30
31 def parse(form)
32 {
33 add_btc_address: ["1", "true"].include?(
34 form.field("add_btc_address")&.value.to_s
35 )
36 }
37 end
38
39 def balance
40 {
41 type: "fixed",
42 value: "Current balance: $#{'%.4f' % @balance}"
43 }
44 end
45
46 MAILING_ADDRESS = {
47 var: "adr",
48 type: "fixed",
49 label: "Mailing Address",
50 description:
51 "Make payable to #{CONFIG[:payable]} and include your " \
52 "Jabber ID in the mailing somewhere.",
53 value: CONFIG[:adr]
54 }.freeze
55
56 def fields
57 Enumerator.new do |y|
58 y << balance
59 y << MAILING_ADDRESS
60 @fields.each do |fs|
61 fs.each { |f| y << f }
62 end
63 end
64 end
65
66 IS_CAD = [{
67 var: "adr",
68 type: "fixed",
69 label: "Interac eTransfer Address",
70 description: "Please include your Jabber ID in the note",
71 value: CONFIG[:interac]
72 }].freeze
73
74 class AddBtcAddressField
75 def self.for(addrs)
76 if addrs.empty?
77 AddNewBtcAddressField.new
78 else
79 new
80 end
81 end
82
83 def each
84 yield(
85 var: "add_btc_address",
86 label: label,
87 type: "boolean",
88 value: false
89 )
90 end
91
92 def label
93 "Or, create a new Bitcoin address?"
94 end
95
96 class AddNewBtcAddressField < AddBtcAddressField
97 def label
98 "You have no Bitcoin addresses, would you like to create one?"
99 end
100 end
101 end
102
103 class HasBitcoinAddresses
104 def initialize(addrs)
105 @addrs = addrs
106 end
107
108 DESCRIPTION =
109 "You can make a Bitcoin payment of any amount to any " \
110 "of these addresses and it will be credited to your " \
111 "account at the Canadian Bitcoins exchange rate within 5 " \
112 "minutes of your transaction reaching 3 confirmations."
113
114 def each
115 yield(
116 var: "btc_address",
117 type: "fixed",
118 label: "Bitcoin Addresses",
119 description: DESCRIPTION,
120 value: @addrs
121 )
122 end
123 end
124end