registration.rb

  1# frozen_string_literal: true
  2
  3class Registration
  4	def self.for(iq, customer, web_register_manager)
  5		raise "TODO" if customer&.active?
  6
  7		EMPromise.resolve(customer&.registered?).then do |registered|
  8			if registered
  9				Registered.new(iq, result.phone)
 10			else
 11				web_register_manager.choose_tel(iq).then do |(riq, tel)|
 12					Activation.for(riq, customer, tel)
 13				end
 14			end
 15		end
 16	end
 17
 18	class Registered
 19		def initialize(iq, tel)
 20			@reply = iq.reply
 21			@reply.status = :completed
 22			@tel = tel
 23		end
 24
 25		def write
 26			@reply.note_type = :error
 27			@reply.note_text = <<~NOTE
 28				You are already registered with JMP number #{@tel}
 29			NOTE
 30			BLATHER << @reply
 31			nil
 32		end
 33	end
 34
 35	class Activation
 36		def self.for(iq, customer, tel)
 37			return EMPromise.resolve(new(iq, customer, tel)) if customer
 38
 39			# Create customer_id
 40			raise "TODO"
 41		end
 42
 43		def initialize(iq, customer, tel)
 44			@reply = iq.reply
 45			reply.allowed_actions = [:next]
 46
 47			@customer = customer
 48			@tel = tel
 49		end
 50
 51		attr_reader :reply, :customer, :tel
 52
 53		FORM_FIELDS = [
 54			{
 55				var: "activation_method",
 56				type: "list-single",
 57				label: "Activate using",
 58				required: true,
 59				options: [
 60					{
 61						value: "bitcoin",
 62						label: "Bitcoin"
 63					},
 64					{
 65						value: "credit_card",
 66						label: "Credit Card"
 67					},
 68					{
 69						value: "code",
 70						label: "Referral or Activation Code"
 71					}
 72				]
 73			},
 74			{
 75				var: "plan_name",
 76				type: "list-single",
 77				label: "What currency should your account balance be in?",
 78				required: true,
 79				options: [
 80					{
 81						value: "cad_beta_unlimited-v20210223",
 82						label: "Canadian Dollars"
 83					},
 84					{
 85						value: "usd_beta_unlimited-v20210223",
 86						label: "United States Dollars"
 87					}
 88				]
 89			}
 90		].freeze
 91
 92		def write
 93			form = reply.form
 94			form.type = :form
 95			form.title = "Activate JMP"
 96			form.instructions = "Going to activate #{tel} (TODO RATE CTR)"
 97			form.fields = FORM_FIELDS
 98
 99			COMMAND_MANAGER.write(reply).then { |iq|
100				Payment.for(iq, customer, tel)
101			}.then(&:write)
102		end
103	end
104
105	module Payment
106		def self.for(iq, customer, tel)
107			case iq.form.field("activation_method")&.value&.to_s
108			when "bitcoin"
109				Bitcoin.new(iq, customer, tel)
110			when "credit_card"
111				raise "TODO"
112			when "code"
113				raise "TODO"
114			else
115				raise "Invalid activation method"
116			end
117		end
118
119		class Bitcoin
120			def initialize(iq, customer, tel)
121				@reply = iq.reply
122				reply.note_type = :info
123				reply.status = :completed
124
125				plan_name = iq.form.field("plan_name").value.to_s
126				@customer = customer.with_plan(plan_name)
127				@customer_id = customer.customer_id
128				@tel = tel
129				@addr = ELECTRUM.createnewaddress
130			end
131
132			attr_reader :reply, :customer_id, :tel
133
134			def save
135				EMPromise.all([
136					REDIS.mset(
137						"pending_tel_for-#{customer_id}", tel,
138						"pending_plan_for-#{customer_id}", @customer.plan_name
139					),
140					@addr.then do |addr|
141						REDIS.sadd("jmp_customer_btc_addresses-#{customer_id}", addr)
142					end
143				])
144			end
145
146			def note_text(amount, addr)
147				<<~NOTE
148					Activate your account by sending at least #{'%.6f' % amount} BTC to
149					#{addr}
150
151					You will receive a notification when your payment is complete.
152				NOTE
153			end
154
155			def write
156				EMPromise.all([
157					@addr,
158					save,
159					BTC_SELL_PRICES.public_send(@customer.currency.to_s.downcase)
160				]).then do |(addr, _, rate)|
161					min = CONFIG[:activation_amount] / rate
162					reply.note_text = note_text(min, addr)
163					BLATHER << reply
164					nil
165				end
166			end
167		end
168	end
169end