~singpolyma/sgx-jmp

94298f5def79ee9a115022ae30d35aa9666f338c — Stephen Paul Weber 2 months ago 8715e74 master
Refactor alt top up to use FormTemplate
M forms/admin_payment_methods.rb => forms/admin_payment_methods.rb +1 -3
@@ 5,6 5,4 @@ unless @payment_methods.empty?
	field @payment_methods.to_list_single(label: "Credit Cards")
end

AltTopUpForm::HasBitcoinAddresses.new(@btc_addresses, desc: nil).each do |spec|
	field spec
end
render "alt_top_up/btc_addresses", btc_addresses: @btc_addresses

A forms/alt_top_up.rb => forms/alt_top_up.rb +29 -0
@@ 0,0 1,29 @@
form!
title "Buy Account Credit"

instructions(
	"Besides credit cards, we support payment by Bitcoin, postal mail, " \
	"or in Canada by Interac e-Transfer."
)

field(
	type: "fixed",
	value: "Current balance: $#{'%.4f' % @balance}"
)

render "alt_top_up/mailing_address"
render "alt_top_up/interac" if @currency == :CAD
render "alt_top_up/btc_addresses"

add_btc_label = if !@btc_addresses || @btc_addresses.empty?
	"You have no Bitcoin addresses, would you like to create one?"
else
	"Or, create a new Bitcoin address?"
end

field(
	var: "add_btc_address",
	label: add_btc_label,
	type: "boolean",
	value: false
)

A forms/alt_top_up/btc_addresses.rb => forms/alt_top_up/btc_addresses.rb +15 -0
@@ 0,0 1,15 @@
DESCRIPTION =
	"You can make a Bitcoin payment of any amount to any " \
	"of these addresses and it will be credited to your " \
	"account at the Canadian Bitcoins exchange rate within 5 " \
	"minutes of your transaction reaching 3 confirmations.".freeze

if @btc_addresses && !@btc_addresses.empty?
	field(
		var: "btc_addresses",
		type: "fixed",
		label: "Bitcoin Addresses",
		description: DESCRIPTION,
		value: @btc_addresses
	)
end

A forms/alt_top_up/interac.rb => forms/alt_top_up/interac.rb +7 -0
@@ 0,0 1,7 @@
field(
	var: "interac_email",
	type: "fixed",
	label: "Interac e-Transfer Address",
	description: "Please include your Jabber ID in the note",
	value: CONFIG[:interac]
)

A forms/alt_top_up/mailing_address.rb => forms/alt_top_up/mailing_address.rb +9 -0
@@ 0,0 1,9 @@
field(
	var: "adr",
	type: "fixed",
	label: "Mailing Address",
	description:
		"Make payable to #{CONFIG[:payable]} and include your " \
		"Jabber ID in the mailing somewhere.",
	value: CONFIG[:adr]
)

A forms/registration/mail.rb => forms/registration/mail.rb +13 -0
@@ 0,0 1,13 @@
result!
title "Activate by Mail or Interac e-Tranfer"

instructions(
	"Activate your account by sending at least " \
	"$#{CONFIG[:activation_amount]}\nWe support payment by " \
	"postal mail or, in Canada, by Interac e-Transfer.\n\n" \
	"You will receive a notification when your payment is complete." \
	"#{@final_message}"
)

render "alt_top_up/mailing_address"
render "alt_top_up/interac" if @currency == :CAD

M lib/alt_top_up_form.rb => lib/alt_top_up_form.rb +10 -103
@@ 3,29 3,23 @@
class AltTopUpForm
	def self.for(customer)
		customer.btc_addresses.then do |addrs|
			AltTopUpForm.new(customer, *[
				(IS_CAD if customer.currency == :CAD),
				(HasBitcoinAddresses.new(addrs) unless addrs.empty?),
				AddBtcAddressField.for(addrs)
			].compact)
			AltTopUpForm.new(customer, addrs)
		end
	end

	def initialize(customer, *fields)
		@fields = fields
	def initialize(customer, btc_addresses)
		@balance = customer.balance
		@currency = customer.currency
		@btc_addresses = btc_addresses
	end

	def form
		form = Blather::Stanza::X.new(:result)
		form.type = :form
		form.title = "Buy Account Credit"
		form.instructions =
			"Besides credit cards, we support payment by Bitcoin, postal mail, " \
			"or in Canada by Interac e-Transfer."

		form.fields = fields.to_a
		form
		FormTemplate.render(
			"alt_top_up",
			balance: @balance,
			currency: @currency,
			btc_addresses: @btc_addresses
		)
	end

	def parse(form)


@@ 35,91 29,4 @@ class AltTopUpForm
			)
		}
	end

	def balance
		{
			type: "fixed",
			value: "Current balance: $#{'%.4f' % @balance}"
		}
	end

	MAILING_ADDRESS = {
		var: "adr",
		type: "fixed",
		label: "Mailing Address",
		description:
			"Make payable to #{CONFIG[:payable]} and include your " \
			"Jabber ID in the mailing somewhere.",
		value: CONFIG[:adr]
	}.freeze

	def fields
		Enumerator.new do |y|
			y << balance
			y << MAILING_ADDRESS
			@fields.each do |fs|
				fs.each { |f| y << f }
			end
		end
	end

	IS_CAD = [{
		var: "adr",
		type: "fixed",
		label: "Interac e-Transfer Address",
		description: "Please include your Jabber ID in the note",
		value: CONFIG[:interac]
	}].freeze

	class AddBtcAddressField
		def self.for(addrs)
			if addrs.empty?
				AddNewBtcAddressField.new
			else
				new
			end
		end

		def each
			yield(
				var: "add_btc_address",
				label: label,
				type: "boolean",
				value: false
			)
		end

		def label
			"Or, create a new Bitcoin address?"
		end

		class AddNewBtcAddressField < AddBtcAddressField
			def label
				"You have no Bitcoin addresses, would you like to create one?"
			end
		end
	end

	class HasBitcoinAddresses
		def initialize(addrs, desc: DESCRIPTION)
			@addrs = addrs
			@desc = desc
		end

		DESCRIPTION =
			"You can make a Bitcoin payment of any amount to any " \
			"of these addresses and it will be credited to your " \
			"account at the Canadian Bitcoins exchange rate within 5 " \
			"minutes of your transaction reaching 3 confirmations."

		def each
			yield(
				var: "btc_address",
				type: "fixed",
				label: "Bitcoin Addresses",
				description: @desc,
				value: @addrs
			)
		end
	end
end

M lib/registration.rb => lib/registration.rb +5 -18
@@ 406,24 406,11 @@ class Registration
			end

			def form
				form = Blather::Stanza::X.new(:result)
				form.title = "Activate by Mail or Interac e-Transfer"
				form.instructions =
					"Activate your account by sending at least " \
					"$#{CONFIG[:activation_amount]}\nWe support payment by " \
					"postal mail or, in Canada, by Interac e-Transfer.\n\n" \
					"You will receive a notification when your payment is complete." \
					"#{@final_message}"

				form.fields = fields.to_a
				form
			end

			def fields
				[
					AltTopUpForm::MAILING_ADDRESS,
					AltTopUpForm::IS_CAD
				].flatten
				FormTemplate.render(
					"registration/mail",
					currency: @customer.currency,
					final_message: @final_message
				)
			end

			def write

M test/test_add_bitcoin_address.rb => test/test_add_bitcoin_address.rb +2 -2
@@ 8,14 8,14 @@ class AddBitcoinAddressTest < Minitest::Test
	def test_for
		iq = Blather::Stanza::Iq::Command.new
		cust = customer
		AddBitcoinAddress.for(iq, AltTopUpForm.new(cust), cust)
		AddBitcoinAddress.for(iq, AltTopUpForm.new(cust, []), cust)
	end

	def test_for_add_bitcoin
		iq = Blather::Stanza::Iq::Command.new
		iq.form.fields = [{ var: "add_btc_address", value: "true" }]
		cust = customer
		AddBitcoinAddress.for(iq, AltTopUpForm.new(cust), cust)
		AddBitcoinAddress.for(iq, AltTopUpForm.new(cust, []), cust)
	end

	def test_write

M test/test_alt_top_up_form.rb => test/test_alt_top_up_form.rb +6 -8
@@ 48,8 48,7 @@ class AltTopUpFormTest < Minitest::Test
		assert_kind_of(
			Blather::Stanza::X,
			AltTopUpForm.new(
				customer,
				AltTopUpForm::AddBtcAddressField.new
				customer, ["some_addr"]
			).form
		)
	end


@@ 58,8 57,7 @@ class AltTopUpFormTest < Minitest::Test
		assert_kind_of(
			Blather::Stanza::X,
			AltTopUpForm.new(
				customer,
				AltTopUpForm::AddBtcAddressField::AddNewBtcAddressField.new
				customer, []
			).form
		)
	end


@@ 69,7 67,7 @@ class AltTopUpFormTest < Minitest::Test
		iq_form.fields = [
			{ var: "add_btc_address", value: "true" }
		]
		assert AltTopUpForm.new(customer).parse(iq_form)[:add_btc_address]
		assert AltTopUpForm.new(customer, []).parse(iq_form)[:add_btc_address]
	end

	def test_parse_1


@@ 77,7 75,7 @@ class AltTopUpFormTest < Minitest::Test
		iq_form.fields = [
			{ var: "add_btc_address", value: "1" }
		]
		assert AltTopUpForm.new(customer).parse(iq_form)[:add_btc_address]
		assert AltTopUpForm.new(customer, []).parse(iq_form)[:add_btc_address]
	end

	def test_parse_false


@@ 85,11 83,11 @@ class AltTopUpFormTest < Minitest::Test
		iq_form.fields = [
			{ var: "add_btc_address", value: "false" }
		]
		refute AltTopUpForm.new(customer).parse(iq_form)[:add_btc_address]
		refute AltTopUpForm.new(customer, []).parse(iq_form)[:add_btc_address]
	end

	def test_parse_not_presend
		iq_form = Blather::Stanza::X.new
		refute AltTopUpForm.new(customer).parse(iq_form)[:add_btc_address]
		refute AltTopUpForm.new(customer, []).parse(iq_form)[:add_btc_address]
	end
end

M test/test_helper.rb => test/test_helper.rb +3 -1
@@ 112,7 112,9 @@ CONFIG = {
	},
	bandwidth_site: "test_site",
	bandwidth_peer: "test_peer",
	keepgo: { api_key: "keepgokey", access_token: "keepgotoken" }
	keepgo: { api_key: "keepgokey", access_token: "keepgotoken" },
	adr: "A Mailing Address",
	interac: "interac@example.com"
}.freeze

def panic(e)

M test/test_registration.rb => test/test_registration.rb +35 -0
@@ 408,6 408,41 @@ class RegistrationTest < Minitest::Test
			em :test_write
		end

		class MailTest < Minitest::Test
			def setup
				@mail = Registration::Payment::Mail.new(
					customer(plan_name: "test_cad"),
					"+15555550000"
				)
			end

			def test_write
				result = execute_command do
					Command::COMMAND_MANAGER.expect(
						:write,
						EMPromise.reject(:test_result),
						[Matching.new do |reply|
							assert_equal [:execute, :prev], reply.allowed_actions
							refute reply.form.instructions.empty?
							assert_equal(
								"A Mailing Address",
								reply.form.field("adr").value
							)
							assert_equal(
								"interac@example.com",
								reply.form.field("interac_email").value
							)
						end]
					)

					@mail.write.catch { |e| e }
				end

				assert_equal :test_result, result
			end
			em :test_write
		end

		class ActivateTest < Minitest::Test
			Registration::Payment::CreditCard::Activate::Finish =
				Minitest::Mock.new