Verified google play purchases get first month free one time

Stephen Paul Weber created

Change summary

lib/registration.rb       | 69 ++++++++++++++++++++++++++++++++++++----
sgx_jmp.rb                |  7 +++
test/test_registration.rb | 21 ++++++++++++
3 files changed, 89 insertions(+), 8 deletions(-)

Detailed changes

lib/registration.rb 🔗

@@ -16,13 +16,13 @@ require_relative "./tel_selections"
 require_relative "./welcome_message"
 
 class Registration
-	def self.for(customer, tel_selections)
+	def self.for(customer, google_play_userid, tel_selections)
 		if (reg = customer.registered?)
 			Registered.for(customer, reg.phone)
 		else
 			tel_selections[customer.jid].then(&:choose_tel).then do |tel|
 				BandwidthTnReservationRepo.new.ensure(customer, tel)
-				FinishOrStartActivation.for(customer, tel)
+				FinishOrStartActivation.for(customer, google_play_userid, tel)
 			end
 		end
 	end
@@ -47,19 +47,20 @@ class Registration
 	end
 
 	class FinishOrStartActivation
-		def self.for(customer, tel)
+		def self.for(customer, google_play_userid, tel)
 			if customer.active?
 				Finish.new(customer, tel)
 			elsif customer.balance >= CONFIG[:activation_amount_accept]
 				BillPlan.new(customer, tel)
 			else
-				new(customer, tel)
+				new(customer, google_play_userid, tel)
 			end
 		end
 
-		def initialize(customer, tel)
+		def initialize(customer, google_play_userid, tel)
 			@customer = customer
 			@tel = tel
+			@google_play_userid = google_play_userid
 		end
 
 		def write
@@ -67,15 +68,17 @@ class Registration
 				reply.allowed_actions = [:next]
 				reply.note_type = :info
 				reply.note_text = File.read("#{__dir__}/../fup.txt")
-			}.then { Activation.for(@customer, @tel).write }
+			}.then { Activation.for(@customer, @google_play_userid, @tel).write }
 		end
 	end
 
 	class Activation
-		def self.for(customer, tel)
+		def self.for(customer, google_play_userid, tel)
 			jid = ProxiedJID.new(customer.jid).unproxied
 			if CONFIG[:approved_domains].key?(jid.domain.to_sym)
 				Allow.for(customer, tel, jid)
+			elsif google_play_userid
+				GooglePlay.new(customer, google_play_userid, tel)
 			else
 				new(customer, tel)
 			end
@@ -120,6 +123,58 @@ class Registration
 			}.catch { nil }
 		end
 
+		class GooglePlay
+			def initialize(customer, google_play_userid, tel)
+				@customer = customer
+				@google_play_userid = google_play_userid
+				@tel = tel
+			end
+
+			def used
+				REDIS.sismember("google_play_userids", @google_play_userid)
+			end
+
+			def form
+				FormTemplate.render(
+					"registration/google_play",
+					tel: @tel
+				)
+			end
+
+			def write
+				used.then do |u|
+					next Activation.for(@customer, nil, @tel).write if u.to_s == "1"
+
+					Command.reply { |reply|
+						reply.allowed_actions = [:next]
+						reply.command << form
+					}.then(&method(:activate)).then do
+						Finish.new(@customer, @tel).write
+					end
+				end
+			end
+
+			def activate(iq)
+				REDIS.sadd("google_play_userids", @google_play_userid).then {
+					plan_name = iq.form.field("plan_name").value.to_s
+					@customer = @customer.with_plan(plan_name)
+					@customer.activate_plan_starting_now
+				}.then do
+					if iq.form.field("code")
+						use_referral_code(iq.form.field("code").value.to_s)
+					end
+				end
+			end
+
+		protected
+
+			def use_referral_code(code)
+				InvitesRepo.new.claim_code(@customer.customer_id, code) {
+					@customer.extend_plan
+				}.catch_only(InvitesRepo::Invalid) { nil }
+			end
+		end
+
 		class Allow < Activation
 			def self.for(customer, tel, jid)
 				credit_to = CONFIG[:approved_domains][jid.domain.to_sym]

sgx_jmp.rb 🔗

@@ -494,12 +494,17 @@ Command.new(
 	list_for: ->(*) { true },
 	customer_repo: CustomerRepo.new(sgx_repo: Bwmsgsv2Repo.new)
 ) {
+	google_play_userid = if Command.execution.iq.from.domain == "cheogram.com"
+		Command.execution.iq.command.find(
+			"./ns:userId", ns: "https://ns.cheogram.com/google-play"
+		)&.first&.content
+	end
 	Command.customer.catch_only(CustomerRepo::NotFound) {
 		Sentry.add_breadcrumb(Sentry::Breadcrumb.new(message: "Customer.create"))
 		Command.execution.customer_repo.create(Command.execution.iq.from.stripped)
 	}.then { |customer|
 		Sentry.add_breadcrumb(Sentry::Breadcrumb.new(message: "Registration.for"))
-		Registration.for(customer, TEL_SELECTIONS).then(&:write)
+		Registration.for(customer, google_play_userid, TEL_SELECTIONS).then(&:write)
 	}.then {
 		StatsD.increment("registration.completed")
 	}.catch_only(Command::Execution::FinalStanza) do |e|

test/test_registration.rb 🔗

@@ -16,6 +16,7 @@ class RegistrationTest < Minitest::Test
 		result = execute_command(iq) do
 			Registration.for(
 				customer(sgx: sgx),
+				nil,
 				Minitest::Mock.new
 			)
 		end
@@ -38,6 +39,7 @@ class RegistrationTest < Minitest::Test
 					expires_at: Time.now + 999,
 					sgx: sgx
 				),
+				nil,
 				web_manager
 			)
 		end
@@ -58,6 +60,7 @@ class RegistrationTest < Minitest::Test
 					sgx: sgx,
 					jid: Blather::JID.new("test\\40approved.example.com@component")
 				),
+				nil,
 				web_manager
 			)
 		end
@@ -65,6 +68,23 @@ class RegistrationTest < Minitest::Test
 	end
 	em :test_for_not_activated_approved
 
+	def test_for_not_activated_googleplay
+		sgx = OpenStruct.new(registered?: false)
+		web_manager = TelSelections.new(redis: FakeRedis.new)
+		web_manager.set("test@example.net", "+15555550000")
+		iq = Blather::Stanza::Iq::Command.new
+		iq.from = "test@approved.example.com"
+		result = execute_command(iq) do
+			Registration::Activation.for(
+				customer(sgx: sgx),
+				"GARBLEDYGOOK==",
+				web_manager
+			)
+		end
+		assert_kind_of Registration::Activation::GooglePlay, result
+	end
+	em :test_for_not_activated_googleplay
+
 	def test_for_not_activated_with_customer_id
 		sgx = OpenStruct.new(registered?: false)
 		web_manager = TelSelections.new(redis: FakeRedis.new)
@@ -74,6 +94,7 @@ class RegistrationTest < Minitest::Test
 		result = execute_command(iq) do
 			Registration::Activation.for(
 				customer(sgx: sgx),
+				nil,
 				web_manager
 			)
 		end