~singpolyma/sgx-jmp

ref: 847be4f6375b8cb1a0597d25ee390ad7d0542e0f sgx-jmp/lib/registration.rb -rw-r--r-- 10.1 KiB
847be4f6Stephen Paul Weber Add btc_addresses to Customer 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
# frozen_string_literal: true

require "erb"
require "ruby-bandwidth-iris"
require "securerandom"

require_relative "./bandwidth_tn_order"
require_relative "./em"
require_relative "./oob"
require_relative "./web_register_manager"

class Registration
	def self.for(iq, customer, web_register_manager)
		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 = :info
			@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)
			if customer.active?
				Finish.new(iq, customer, tel)
			else
				EMPromise.resolve(new(iq, customer, tel))
			end
		end

		def initialize(iq, customer, tel)
			@reply = iq.reply
			@reply.status = :executing
			@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: "Invite 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

		ACTIVATE_INSTRUCTION =
			"To activate your account, you can either deposit " \
			"$#{CONFIG[:activation_amount]} to your balance or enter " \
			"your invite code if you have one."

		CRYPTOCURRENCY_INSTRUCTION =
			"(If you'd like to pay in a cryptocurrency other than " \
			"Bitcoin, currently we recommend using a service like " \
			"simpleswap.io, morphtoken.com, changenow.io, or godex.io. " \
			"Manual payment via Bitcoin Cash is also available if you " \
			"contact support.)"

		def add_instructions(form, center)
			[
				"You've selected #{tel} (#{center}) as your JMP number",
				ACTIVATE_INSTRUCTION,
				CRYPTOCURRENCY_INSTRUCTION
			].each do |txt|
				form << Blather::XMPPNode.new(:instructions, form.document).tap do |i|
					i << txt
				end
			end
		end

		def write
			rate_center.then do |center|
				form = reply.form
				form.type = :form
				form.title = "Activate JMP"
				add_instructions(form, center)
				form.fields = FORM_FIELDS

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

	protected

		def rate_center
			EM.promise_fiber do
				center = BandwidthIris::Tn.get(tel).get_rate_center
				"#{center[:rate_center]}, #{center[:state]}"
			end
		end
	end

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

		def self.for(iq, customer, tel)
			plan_name = iq.form.field("plan_name").value.to_s
			customer = customer.with_plan(plan_name)
			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

				@customer = customer
				@customer_id = customer.customer_id
				@tel = tel
			end

			attr_reader :reply, :customer_id, :tel

			def legacy_session_save
				sid = SecureRandom.hex
				REDIS.mset(
					"reg-sid_for-#{customer_id}", sid,
					"reg-session_tel-#{sid}", tel
				)
			end

			def save
				EMPromise.all([
					legacy_session_save,
					REDIS.mset(
						"pending_tel_for-#{customer_id}", tel,
						"pending_plan_for-#{customer_id}", @customer.plan_name
					)
				])
			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

		protected

			def addr
				@addr ||= @customer.btc_addresses.then do |addrs|
					addrs.first || @customer.add_btc_address
				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 (method = payment_methods.default_payment_method)
						Activate.new(iq, customer, method, tel)
					else
						new(iq, customer, tel)
					end
				end
			end

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

				@reply = iq.reply
				@reply.status = :executing
				@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.gsub("\\", "%5C"),
					@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).write
				end
			end

			class Activate
				def initialize(iq, customer, payment_method, tel)
					@iq = iq
					@customer = customer
					@payment_method = payment_method
					@tel = tel
				end

				def write
					Transaction.sale(
						@customer,
						amount: CONFIG[:activation_amount],
						payment_method: @payment_method
					).then(
						method(:sold),
						->(_) { declined }
					)
				end

			protected

				def sold(tx)
					tx.insert.then {
						@customer.bill_plan
					}.then do
						Finish.new(@iq, @customer, @tel).write
					end
				end

				DECLINE_MESSAGE =
					"Your bank declined the transaction. " \
					"Often this happens when a person's credit card " \
					"is a US card that does not support international " \
					"transactions, as JMP is not based in the USA, though " \
					"we do support transactions in USD.\n\n" \
					"If you were trying a prepaid card, you may wish to use "\
					"Privacy.com instead, as they do support international " \
					"transactions.\n\n " \
					"You may add another card and then choose next"

				def decline_oob(reply)
					oob = OOB.find_or_create(reply.command)
					oob.url = CONFIG[:credit_card_url].call(
						reply.to.stripped.to_s.gsub("\\", "%5C"),
						@customer.customer_id
					)
					oob.desc = DECLINE_MESSAGE
					oob
				end

				def declined
					reply = @iq.reply
					reply_oob = decline_oob(reply)
					reply.status = :executing
					reply.allowed_actions = [:next]
					reply.note_type = :error
					reply.note_text = "#{reply_oob.desc}: #{reply_oob.url}"
					COMMAND_MANAGER.write(reply).then do |riq|
						CreditCard.for(riq, @customer, @tel).write
					end
				end
			end
		end

		class InviteCode
			Payment.kinds[:code] = method(:new)

			class Invalid < StandardError; end

			FIELDS = [{
				var: "code",
				type: "text-single",
				label: "Your invite code",
				required: true
			}].freeze

			def initialize(iq, customer, tel, error: nil)
				@customer = customer
				@tel = tel
				@reply = iq.reply
				@reply.status = :executing
				@reply.allowed_actions = [:next]
				@form = @reply.form
				@form.type = :form
				@form.title = "Enter Invite Code"
				@form.instructions = error
				@form.fields = FIELDS
			end

			def write
				COMMAND_MANAGER.write(@reply).then do |iq|
					guard_too_many_tries.then {
						verify(iq.form.field("code")&.value&.to_s)
					}.then {
						Finish.new(iq, @customer, @tel)
					}.catch_only(Invalid) { |e|
						invalid_code(iq, e)
					}.then(&:write)
				end
			end

		protected

			def guard_too_many_tries
				REDIS.get("jmp_invite_tries-#{@customer.customer_id}").then do |t|
					raise Invalid, "Too many wrong attempts" if t > 10
				end
			end

			def invalid_code(iq, e)
				EMPromise.all([
					REDIS.incr("jmp_invite_tries-#{@customer.customer_id}").then do
						REDIS.expire("jmp_invite_tries-#{@customer.customer_id}", 60 * 60)
					end,
					InviteCode.new(iq, @customer, @tel, error: e.message)
				]).then(&:last)
			end

			def customer_id
				@customer.customer_id
			end

			def verify(code)
				EM.promise_fiber do
					DB.transaction do
						valid = DB.exec(<<~SQL, [customer_id, code]).cmd_tuples.positive?
							UPDATE invites SET used_by_id=$1, used_at=LOCALTIMESTAMP
							WHERE code=$2 AND used_by_id IS NULL
						SQL
						raise Invalid, "Not a valid invite code: #{code}" unless valid
						@customer.activate_plan_starting_now
					end
				end
			end
		end
	end

	class Finish
		def initialize(iq, customer, tel)
			@reply = iq.reply
			@reply.status = :completed
			@reply.note_type = :info
			@reply.note_text = "Your JMP account has been activated as #{tel}"
			@customer = customer
			@tel = tel
		end

		def write
			BandwidthTNOrder.create(@tel).then(&:poll).then(
				->(_) { customer_active_tel_purchased },
				lambda do |_|
					@reply.note_type = :error
					@reply.note_text =
						"The JMP number #{@tel} is no longer available, " \
						"please visit https://jmp.chat and choose another."
					BLATHER << @reply
				end
			)
		end

	protected

		def cheogram_sip_addr
			"sip:#{ERB::Util.url_encode(@reply.to.stripped.to_s)}@sip.cheogram.com"
		end

		def customer_active_tel_purchased
			@customer.register!(@tel).then {
				EMPromise.all([
					REDIS.set("catapult_fwd-#{@tel}", cheogram_sip_addr),
					REDIS.set(
						"catapult_fwd_timeout-#{@reply.to.stripped}",
						25 # ~5 seconds / ring, 5 rings
					)
				])
			}.then { BLATHER << @reply }
		end
	end
end