~singpolyma/sgx-jmp

1448fc7df829cd2deb3190d4e3ebe477b4f8a20f — Stephen Paul Weber 2 years ago c08d6ea
Use registration pattern for Payment kinds

Instead of a hard-coded case statement.
1 files changed, 10 insertions(+), 9 deletions(-)

M lib/registration.rb
M lib/registration.rb => lib/registration.rb +10 -9
@@ 105,20 105,19 @@ class Registration
	end

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

		def self.for(iq, customer, tel)
			case iq.form.field("activation_method")&.value&.to_s
			when "bitcoin"
				Bitcoin.new(iq, customer, tel)
			when "credit_card"
				CreditCard.for(iq, customer, tel)
			when "code"
				raise "TODO"
			else
			kinds.fetch(iq.form.field("activation_method")&.value&.to_s&.to_sym) {
				raise "Invalid activation method"
			end
			}.call(iq, customer, tel)
		end

		class Bitcoin
			Payment.kinds[:bitcoin] = method(:new)

			def initialize(iq, customer, tel)
				@reply = iq.reply
				reply.note_type = :info


@@ 169,6 168,8 @@ class Registration
		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 payment_methods.default_payment_method