~singpolyma/sgx-jmp

561f199ef8e6bc33ac8646bdbe45ad6fc3fa57be — Stephen Paul Weber 1 year, 11 days ago 12cc57d
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