alt_top_up_form.rb

  1# frozen_string_literal: true
  2
  3class AltTopUpForm
  4	def self.for(customer)
  5		customer.btc_addresses.then do |addrs|
  6			AltTopUpForm.new(*[
  7				(IS_CAD if customer.currency == :CAD),
  8				(HasBitcoinAddresses.new(addrs) unless addrs.empty?),
  9				AddBtcAddressField.for(addrs)
 10			].compact)
 11		end
 12	end
 13
 14	def initialize(*fields)
 15		@fields = fields
 16	end
 17
 18	def form
 19		form = Blather::Stanza::X.new(:result)
 20		form.type = :form
 21		form.title = "Buy Account Credit"
 22		form.instructions =
 23			"Besides credit cards, we support payment by Bitcoin, postal mail, " \
 24			"or in Canada by Interac eTransfer."
 25
 26		form.fields = fields.to_a
 27		form
 28	end
 29
 30	def parse(form)
 31		{
 32			add_btc_address: ["1", "true"].include?(
 33				form.field("add_btc_address")&.value.to_s
 34			)
 35		}
 36	end
 37
 38	MAILING_ADDRESS = {
 39		var: "adr",
 40		type: "fixed",
 41		label: "Mailing Address",
 42		description:
 43			"Make payable to #{CONFIG[:payable]} and include your " \
 44			"Jabber ID in the mailing somewhere.",
 45		value: CONFIG[:adr]
 46	}.freeze
 47
 48	def fields
 49		Enumerator.new do |y|
 50			y << MAILING_ADDRESS
 51			@fields.each do |fs|
 52				fs.each { |f| y << f }
 53			end
 54		end
 55	end
 56
 57	IS_CAD = [
 58		var: "adr",
 59		type: "fixed",
 60		label: "Interac eTransfer Address",
 61		description: "Please include your Jabber ID in the note",
 62		value: CONFIG[:interac]
 63	].freeze
 64
 65	class AddBtcAddressField
 66		def self.for(addrs)
 67			if addrs.empty?
 68				AddNewBtcAddressField.new
 69			else
 70				new
 71			end
 72		end
 73
 74		def each
 75			yield(
 76				var: "add_btc_address",
 77				label: label,
 78				type: "boolean",
 79				value: false
 80			)
 81		end
 82
 83		def label
 84			"Or, create a new Bitcoin address?"
 85		end
 86
 87		class AddNewBtcAddressField < AddBtcAddressField
 88			def label
 89				"You have no Bitcoin addresses, would you like to create one?"
 90			end
 91		end
 92	end
 93
 94	class HasBitcoinAddresses
 95		def initialize(addrs)
 96			@addrs = addrs
 97		end
 98
 99		DESCRIPTION =
100			"You can make a Bitcoin payment of any amount to any " \
101			"of these addresses and it will be credited to your " \
102			"account at the Canadian Bitcoins exchange rate within 5 " \
103			"minutes of your transaction reaching 3 confirmations."
104
105		def each
106			yield(
107				var: "btc_address",
108				type: "fixed",
109				label: "Bitcoin Addresses",
110				description: DESCRIPTION,
111				value: @addrs
112			)
113		end
114	end
115end