~singpolyma/sgx-jmp

ref: 1448fc7df829cd2deb3190d4e3ebe477b4f8a20f sgx-jmp/lib/registration.rb -rw-r--r-- 4.5 KiB
1448fc7dStephen Paul Weber Use registration pattern for Payment kinds 2 years 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# frozen_string_literal: true

require_relative "./oob"

class Registration
	def self.for(iq, customer, web_register_manager)
		raise "TODO" if customer&.active?

		EMPromise.resolve(customer&.registered?).then do |registered|
			if registered
				Registered.new(iq, registered.phone)
			else
				web_register_manager.choose_tel(iq).then do |(riq, tel)|
					Activation.for(riq, customer, tel)
				end
			end
		end
	end

	class Registered
		def initialize(iq, tel)
			@reply = iq.reply
			@reply.status = :completed
			@tel = tel
		end

		def write
			@reply.note_type = :error
			@reply.note_text = <<~NOTE
				You are already registered with JMP number #{@tel}
			NOTE
			BLATHER << @reply
			nil
		end
	end

	class Activation
		def self.for(iq, customer, tel)
			return EMPromise.resolve(new(iq, customer, tel)) if customer

			# Create customer_id
			raise "TODO"
		end

		def initialize(iq, customer, tel)
			@reply = iq.reply
			reply.allowed_actions = [:next]

			@customer = customer
			@tel = tel
		end

		attr_reader :reply, :customer, :tel

		FORM_FIELDS = [
			{
				var: "activation_method",
				type: "list-single",
				label: "Activate using",
				required: true,
				options: [
					{
						value: "bitcoin",
						label: "Bitcoin"
					},
					{
						value: "credit_card",
						label: "Credit Card"
					},
					{
						value: "code",
						label: "Referral or Activation Code"
					}
				]
			},
			{
				var: "plan_name",
				type: "list-single",
				label: "What currency should your account balance be in?",
				required: true,
				options: [
					{
						value: "cad_beta_unlimited-v20210223",
						label: "Canadian Dollars"
					},
					{
						value: "usd_beta_unlimited-v20210223",
						label: "United States Dollars"
					}
				]
			}
		].freeze

		def write
			form = reply.form
			form.type = :form
			form.title = "Activate JMP"
			form.instructions = "Going to activate #{tel} (TODO RATE CTR)"
			form.fields = FORM_FIELDS

			COMMAND_MANAGER.write(reply).then { |iq|
				Payment.for(iq, customer, tel)
			}.then(&:write)
		end
	end

	module Payment
		def self.kinds
			@kinds ||= {}
		end

		def self.for(iq, customer, tel)
			kinds.fetch(iq.form.field("activation_method")&.value&.to_s&.to_sym) {
				raise "Invalid activation method"
			}.call(iq, customer, tel)
		end

		class Bitcoin
			Payment.kinds[:bitcoin] = method(:new)

			def initialize(iq, customer, tel)
				@reply = iq.reply
				reply.note_type = :info
				reply.status = :completed

				plan_name = iq.form.field("plan_name").value.to_s
				@customer = customer.with_plan(plan_name)
				@customer_id = customer.customer_id
				@tel = tel
				@addr = ELECTRUM.createnewaddress
			end

			attr_reader :reply, :customer_id, :tel

			def save
				EMPromise.all([
					REDIS.mset(
						"pending_tel_for-#{customer_id}", tel,
						"pending_plan_for-#{customer_id}", @customer.plan_name
					),
					@addr.then do |addr|
						REDIS.sadd("jmp_customer_btc_addresses-#{customer_id}", addr)
					end
				])
			end

			def note_text(amount, addr)
				<<~NOTE
					Activate your account by sending at least #{'%.6f' % amount} BTC to
					#{addr}

					You will receive a notification when your payment is complete.
				NOTE
			end

			def write
				EMPromise.all([
					@addr,
					save,
					BTC_SELL_PRICES.public_send(@customer.currency.to_s.downcase)
				]).then do |(addr, _, rate)|
					min = CONFIG[:activation_amount] / rate
					reply.note_text = note_text(min, addr)
					BLATHER << reply
					nil
				end
			end
		end

		class CreditCard
			Payment.kinds[:credit_card] = ->(*args) { self.for(*args) }

			def self.for(iq, customer, tel)
				customer.payment_methods.then do |payment_methods|
					if payment_methods.default_payment_method
						Activate.new(iq, customer, tel)
					else
						new(iq, customer, tel)
					end
				end
			end

			def initialize(iq, customer, tel)
				@customer = customer
				@tel = tel

				@reply = iq.reply
				@reply.allowed_actions = [:next]
				@reply.note_type = :info
				@reply.note_text = "#{oob.desc}: #{oob.url}"
			end

			attr_reader :reply

			def oob
				oob = OOB.find_or_create(@reply.command)
				oob.url = CONFIG[:credit_card_url].call(
					@reply.to.stripped.to_s,
					@customer.customer_id
				)
				oob.desc = "Add credit card, then return here and choose next"
				oob
			end

			def write
				COMMAND_MANAGER.write(@reply).then do |riq|
					CreditCard.for(riq, @customer, @tel)
				end
			end

			class Activate
				def initialize(_iq, _customer, _tel)
					raise "TODO"
				end
			end
		end
	end
end