~singpolyma/sgx-jmp

ref: 045da39f25b74034e7bba44cb5f1db48a935ba84 sgx-jmp/lib/alt_top_up_form.rb -rw-r--r-- 2.4 KiB
045da39fStephen Paul Weber Hotfix: arguments in wrong order 1 year, 1 month ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# frozen_string_literal: true

class AltTopUpForm
	def self.for(customer)
		customer.btc_addresses.then do |addrs|
			AltTopUpForm.new(customer, *[
				(IS_CAD if customer.currency == :CAD),
				(HasBitcoinAddresses.new(addrs) unless addrs.empty?),
				AddBtcAddressField.for(addrs)
			].compact)
		end
	end

	def initialize(customer, *fields)
		@fields = fields
		@balance = customer.balance
	end

	def form
		form = Blather::Stanza::X.new(:result)
		form.type = :form
		form.title = "Buy Account Credit"
		form.instructions =
			"Besides credit cards, we support payment by Bitcoin, postal mail, " \
			"or in Canada by Interac eTransfer."

		form.fields = fields.to_a
		form
	end

	def parse(form)
		{
			add_btc_address: ["1", "true"].include?(
				form.field("add_btc_address")&.value.to_s
			)
		}
	end

	def balance
		{
			type: "fixed",
			value: "Current balance: $#{'%.4f' % @balance}"
		}
	end

	MAILING_ADDRESS = {
		var: "adr",
		type: "fixed",
		label: "Mailing Address",
		description:
			"Make payable to #{CONFIG[:payable]} and include your " \
			"Jabber ID in the mailing somewhere.",
		value: CONFIG[:adr]
	}.freeze

	def fields
		Enumerator.new do |y|
			y << balance
			y << MAILING_ADDRESS
			@fields.each do |fs|
				fs.each { |f| y << f }
			end
		end
	end

	IS_CAD = [
		var: "adr",
		type: "fixed",
		label: "Interac eTransfer Address",
		description: "Please include your Jabber ID in the note",
		value: CONFIG[:interac]
	].freeze

	class AddBtcAddressField
		def self.for(addrs)
			if addrs.empty?
				AddNewBtcAddressField.new
			else
				new
			end
		end

		def each
			yield(
				var: "add_btc_address",
				label: label,
				type: "boolean",
				value: false
			)
		end

		def label
			"Or, create a new Bitcoin address?"
		end

		class AddNewBtcAddressField < AddBtcAddressField
			def label
				"You have no Bitcoin addresses, would you like to create one?"
			end
		end
	end

	class HasBitcoinAddresses
		def initialize(addrs)
			@addrs = addrs
		end

		DESCRIPTION =
			"You can make a Bitcoin payment of any amount to any " \
			"of these addresses and it will be credited to your " \
			"account at the Canadian Bitcoins exchange rate within 5 " \
			"minutes of your transaction reaching 3 confirmations."

		def each
			yield(
				var: "btc_address",
				type: "fixed",
				label: "Bitcoin Addresses",
				description: DESCRIPTION,
				value: @addrs
			)
		end
	end
end