Bill after charge

Stephen Paul Weber created

instead of this blind retry

Change summary

lib/registration.rb       |  29 ++-----
test/test_registration.rb | 156 ++++++++++++++++------------------------
2 files changed, 72 insertions(+), 113 deletions(-)

Detailed changes

lib/registration.rb 🔗

@@ -398,9 +398,7 @@ class Registration
 				@customer = customer
 			end
 
-			def call
-				yield @customer
-			end
+			def call; end
 
 			def self.price(tel)
 				tel.price
@@ -408,19 +406,14 @@ class Registration
 		end
 
 		class CreditCard
-			Payment.kinds[:credit_card] = ->(*args, **kw) { self.for(*args, **kw) }
-
-			def self.for(in_customer, tel, finish: Finish, maybe_bill: MaybeBill, **)
-				maybe_bill.new(in_customer, tel, finish: finish).call do |customer|
-					new(customer, tel, finish: finish, maybe_bill: maybe_bill)
-				end
-			end
+			Payment.kinds[:credit_card] = method(:new)
 
 			def initialize(customer, tel, finish: Finish, maybe_bill: MaybeBill)
 				@customer = customer
 				@tel = tel
 				@finish = finish
-				@maybe_bill = maybe_bill
+				@maybe_bill = maybe_bill.new(customer, tel, finish: finish)
+				@price = maybe_bill.price(tel)
 			end
 
 			def oob(reply)
@@ -428,7 +421,7 @@ class Registration
 				oob.url = CONFIG[:credit_card_url].call(
 					reply.to.stripped.to_s.gsub("\\", "%5C"),
 					@customer.customer_id
-				) + "&amount=#{@maybe_bill.price(@tel).ceil}"
+				) + "&amount=#{@price.ceil}"
 				oob.desc = "Pay by credit card, save, then next here to continue"
 				oob
 			end
@@ -442,15 +435,9 @@ class Registration
 				}.then do |iq|
 					next Activation.for(@customer, nil, @tel).then(&:write) if iq.prev?
 
-					try_again
+					@maybe_bill.call { self }&.then(&:write)
 				end
 			end
-
-			def try_again
-				CreditCard.for(
-					@customer, @tel, finish: @finish, maybe_bill: @maybe_bill
-				).then(&:write)
-			end
 		end
 
 		class InviteCode
@@ -627,8 +614,8 @@ class Registration
 				Payment.for(
 					iq, @customer, @tel,
 					maybe_bill: ::Registration::Payment::JustCharge
-				).write
-			}
+				)
+			}.then(&:write)
 		end
 
 	protected

test/test_registration.rb 🔗

@@ -674,6 +674,7 @@ class RegistrationTest < Minitest::Test
 		end
 
 		def test_for_credit_card
+			tel = TelSelections::ChooseTel::Tn.for_pending_value("+15555550000")
 			iq = Blather::Stanza::Iq::Command.new
 			iq.from = "test@example.com"
 			iq.form.fields = [
@@ -686,8 +687,8 @@ class RegistrationTest < Minitest::Test
 				Registration::Payment.for(
 					iq,
 					cust,
-					""
-				).sync
+					tel
+				)
 			end
 			assert_kind_of Registration::Payment::CreditCard, result
 		end
@@ -821,7 +822,8 @@ class RegistrationTest < Minitest::Test
 				)
 			end
 
-			def test_for
+			def test_new
+				tel = TelSelections::ChooseTel::Tn.for_pending_value("+15555550000")
 				cust = Minitest::Mock.new(customer)
 				cust.expect(
 					:payment_methods,
@@ -831,35 +833,11 @@ class RegistrationTest < Minitest::Test
 					Command.execution.customer_repo.expect(:find, cust, ["test"])
 					assert_kind_of(
 						Registration::Payment::CreditCard,
-						Registration::Payment::CreditCard.for(
-							cust,
-							"+15555550000"
-						).sync
+						Registration::Payment::CreditCard.new(cust, tel)
 					)
 				end
 			end
-			em :test_for
-
-			def test_for_has_balance
-				cust = Minitest::Mock.new(customer)
-				cust.expect(:balance, 100)
-				cust.expect(:payment_methods, EMPromise.resolve(nil))
-				Registration::Payment::MaybeBill::BillPlan.expect(
-					:new,
-					Registration::BillPlan.new(nil, nil)
-				) { true }
-				execute_command do
-					Command.execution.customer_repo.expect(:find, cust, ["test"])
-					assert_kind_of(
-						Registration::BillPlan,
-						Registration::Payment::CreditCard.for(
-							cust,
-							"+15555550000"
-						).sync
-					)
-				end
-			end
-			em :test_for_has_balance
+			em :test_new
 
 			def test_write
 				result = execute_command do
@@ -1746,77 +1724,71 @@ class RegistrationTest < Minitest::Test
 				["catapult_fwd_timeout-customer_test@component", 25]
 			)
 
-			local_tel = TelSelections::ChooseTel::Tn::LocalInventory.new(
-				TelSelections::ChooseTel::Tn.new("+15555550000"),
-				"bandwidth_account_id",
-				price: 10.0
+			local_tel = Minitest::Mock.new(
+				TelSelections::ChooseTel::Tn::LocalInventory.new(
+					TelSelections::ChooseTel::Tn.new("+15555550000"),
+					"bandwidth_account_id",
+					price: 10.0
+				)
 			)
 
-			Registration::Payment::CreditCard.stub(
-				:new,
-				OpenStruct.new(
-					write: lambda {
-						# simulates successful try_again
-						Registration::Payment::CreditCard.for(
-							high_cust,
-							local_tel,
-							# we know maybe_bill will be passed as JustCharge
-							# since that's hardcoded
-							maybe_bill: Registration::Payment::JustCharge
-						)
-					}
+			result = execute_command do
+				local_tel.expect(:charge, EMPromise.reject(:test_result), [Customer])
+				@sgx.expect(
+					:register!,
+					EMPromise.resolve(@sgx.with(
+						registered?: Blather::Stanza::Iq::IBR.new.tap do |ibr|
+							ibr.phone = "+15555550000"
+						end
+					)),
+					["+15555550000"]
 				)
-			) do
-				result = execute_command do
-					@sgx.expect(
-						:register!,
-						EMPromise.resolve(@sgx.with(
-							registered?: Blather::Stanza::Iq::IBR.new.tap do |ibr|
-								ibr.phone = "+15555550000"
-							end
-						)),
-						["+15555550000"]
-					)
 
-					Command::COMMAND_MANAGER.expect(
-						:write,
-						EMPromise.resolve(Blather::Stanza::Iq::Command.new.tap { |iq|
-							iq.form.fields = [
-								{ var: "activation_method", value: "credit_card" }
-							]
-						}),
-						[Matching.new do |iq|
-							assert_equal :form, iq.form.type
-							assert iq.form.field("activation_method")
-						end]
-					)
+				Command::COMMAND_MANAGER.expect(
+					:write,
+					EMPromise.resolve(Blather::Stanza::Iq::Command.new.tap { |iq|
+						iq.from = "customer@example.org"
+						iq.form.fields = [
+							{ var: "activation_method", value: "credit_card" }
+						]
+					}),
+					[Matching.new do |iq|
+						assert_equal :form, iq.form.type
+						assert_equal "Purchase Number", iq.form.title
+					end]
+				)
 
-					Command.execution.customer_repo.expect(
-						:find,
-						EMPromise.resolve(high_cust),
-						["test"]
-					)
+				Command.execution.customer_repo.expect(
+					:find,
+					EMPromise.resolve(high_cust),
+					["test"]
+				)
 
-					Command::COMMAND_MANAGER.expect(
-						:write,
-						EMPromise.reject(:test_result),
-						[Matching.new do |iq|
-							assert_equal :form, iq.form.type
-							assert iq.form.field("subdomain")
-						end]
-					)
+				Command::COMMAND_MANAGER.expect(
+					:write,
+					EMPromise.resolve(Blather::Stanza::Iq::Command.new.tap { |iq|
+						iq.from = "customer@example.org"
+					}),
+					[Matching.new do |iq|
+						assert_equal(
+							"Pay by credit card, save, then next here to continue: " \
+							"http://creditcard.example.com?&amount=10",
+							iq.note.text
+						)
+					end]
+				)
 
-					Registration::Finish.new(
-						low_cust,
-						local_tel
-					).write.catch { |e| e }
-				end
-				assert_equal :test_result, result
-				assert_mock @sgx
-				assert_mock Registration::Finish::REDIS
-				assert_mock Bwmsgsv2Repo::REDIS
-				assert_mock Command::COMMAND_MANAGER
+				Registration::Finish.new(
+					low_cust,
+					local_tel
+				).write.catch { |e| e }
 			end
+
+			assert_equal :test_result, result
+			assert_mock @sgx
+			assert_mock Registration::Finish::REDIS
+			assert_mock Bwmsgsv2Repo::REDIS
+			assert_mock Command::COMMAND_MANAGER
 		end
 		em :test_write_local_inventory_must_pay