# frozen_string_literal: true
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)
end
end
def initialize(customer, *fields)
@fields = fields
@balance = customer.balance
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 eTransfer."
form.fields = fields.to_a
form
end
def parse(form)
{
add_btc_address: ["1", "true"].include?(
form.field("add_btc_address")&.value.to_s
)
}
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 eTransfer 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)
@addrs = addrs
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: DESCRIPTION,
value: @addrs
)
end
end
end