invoices: self-contained payout addresses

Crypto options were limited to a hard-coded asset list, with one
address per coin in a side table. A payout address now carries
everything an invoice needs: ticker, name, QR URI scheme, amount
parameter, CoinGecko id, address and an active flag. Any coin can be
added, several addresses per ticker are fine, and known tickers fill the
QR and rate fields from the built-in templates.

The new-invoice form picks a payout address. The invoice snapshots the
ticker, address, name, scheme and amount parameter, and the PDF and QR
render from that copy, so editing, retiring or deleting a payout entry
never changes an issued invoice. "Fetch live" looks up the rate by the
payout address's CoinGecko id.

Migration 012 keeps the existing rows and backfills both the payout
metadata and the snapshots on existing crypto invoices.

Assisted-by: Claude:opus-5
This commit is contained in:
Sergei Poljanski 2026-10-10 09:17:09 +04:00
commit 33ba7650c3
Signed by: asxpi
GPG key ID: 4F8851660FA4121B
19 changed files with 420 additions and 176 deletions

View file

@ -72,7 +72,7 @@ class AppTest < Minitest::Test
client_name: 'ACME', client_email: 'billing@example.com', client_address: '',
currency: 'EUR', gel_rate: '3.05',
issued_on: Date.today.to_s, due_on: (Date.today + 14).to_s, notes: '',
crypto_coin: 'LTC', crypto_address: '', crypto_rate: '', crypto_amount: '',
payout_id: '', crypto_rate: '', crypto_amount: '',
items: { '0' => { 'description' => 'Engineering — test', 'qty' => '2', 'unit_price' => '100.50' } } }
end
@ -94,16 +94,22 @@ class AppTest < Minitest::Test
skip 'TEST_DATABASE_URL not set' unless TestDb.available?
token = csrf_token_from('/admin/invoices/new', admin_env)
params = invoice_form_params(token).merge(
crypto_coin: 'USDT-TRC20', crypto_address: 'TXYZexampleexampleexampleexample12',
crypto_rate: '1.00', crypto_amount: ''
)
payout = PayoutAddress.create(coin: 'USDT-TRC20', name: 'Tether (Tron)', address: 'TXYZexampleexampleexampleexample12',
updated_by: 't', updated_at: Time.now.utc)
params = invoice_form_params(token).merge(payout_id: payout.id.to_s, crypto_rate: '1.00', crypto_amount: '')
post '/admin/invoices', params, admin_env
assert_equal 302, last_response.status, last_response.body
invoice = Invoice.first
assert_equal 'USDT-TRC20', invoice.crypto_coin
assert_equal 'TXYZexampleexampleexampleexample12', invoice.crypto_address
assert_nil invoice.crypto_scheme # token: bare address QR
assert_equal BigDecimal('201'), invoice.crypto_amount_due
payout.update(address: 'TNEWexampleexampleexampleexample99', name: 'Renamed')
assert_equal 'TXYZexampleexampleexampleexample12', invoice.refresh.crypto_address # snapshot kept
ensure
payout&.destroy
end
def test_create_invoice_validation_failure_rerenders_form

View file

@ -43,29 +43,30 @@ class CryptoAssetTest < Minitest::Test
end
class CryptoQrTest < Minitest::Test
def uri(scheme, amount_param, address, amount)
CryptoQr.uri(scheme: scheme, amount_param: amount_param, address: address, amount: amount)
end
def test_bip21_uri_with_amount
assert_equal 'bitcoin:bc1qaddr?amount=0.005',
CryptoQr.uri('BTC', 'bc1qaddr', BigDecimal('0.00500000'))
assert_equal 'litecoin:ltc1qaddr?amount=1.25',
CryptoQr.uri('LTC', 'ltc1qaddr', BigDecimal('1.25'))
assert_equal 'bitcoin:bc1qaddr?amount=0.005', uri('bitcoin', 'amount', 'bc1qaddr', BigDecimal('0.00500000'))
assert_equal 'litecoin:ltc1qaddr?amount=1.25', uri('litecoin', 'amount', 'ltc1qaddr', BigDecimal('1.25'))
end
def test_monero_uses_tx_amount
assert_equal 'monero:4Aaddr?tx_amount=2.5',
CryptoQr.uri('XMR', '4Aaddr', BigDecimal('2.5'))
assert_equal 'monero:4Aaddr?tx_amount=2.5', uri('monero', 'tx_amount', '4Aaddr', BigDecimal('2.5'))
end
def test_eth_scheme_without_amount
assert_equal 'ethereum:0xAddr', CryptoQr.uri('ETH', '0xAddr', BigDecimal('1.5'))
def test_scheme_without_amount_param
assert_equal 'ethereum:0xAddr', uri('ethereum', nil, '0xAddr', BigDecimal('1.5'))
end
def test_tokens_encode_bare_address
assert_equal 'TAddr', CryptoQr.uri('USDT-TRC20', 'TAddr', BigDecimal('100'))
assert_equal 'AAddr', CryptoQr.uri('USDC-ALGO', 'AAddr', nil)
def test_no_scheme_encodes_bare_address
assert_equal 'TAddr', uri(nil, nil, 'TAddr', BigDecimal('100'))
assert_equal 'AAddr', uri(nil, nil, 'AAddr', nil)
end
def test_png_renders
png = CryptoQr.png('BTC', 'bc1qexampleexampleexample', BigDecimal('0.01'))
png = CryptoQr.png(scheme: 'bitcoin', amount_param: 'amount', address: 'bc1qexampleexampleexample', amount: BigDecimal('0.01'))
assert png.start_with?("\x89PNG".b)
end
end

View file

@ -71,14 +71,32 @@ class InvoiceTest < Minitest::Test
assert_equal BigDecimal('1.25'), inv.crypto_amount_due
end
def test_build_rejects_unknown_coin
def test_build_fills_known_ticker_from_template
inv = Invoice.build(
client_name: 'ACME', client_email: 'billing@example.com', currency: 'EUR', gel_rate: '3.05',
items: [{ 'description' => 'work', 'qty' => '1', 'unit_price' => '100' }],
crypto_coin: 'ltc', crypto_address: 'ltc1qexampleexampleexampleexample'
)
assert_equal ['Litecoin', 'litecoin', 'amount'], [inv.crypto_name, inv.crypto_scheme, inv.crypto_amount_param]
end
# Any ticker works once a payout address defines it; the invoice keeps
# its own copy of the QR metadata.
def test_build_takes_snapshot_for_custom_coin
inv = Invoice.build(
client_name: 'ACME', client_email: 'billing@example.com', currency: 'EUR', gel_rate: '3.05',
items: [{ 'description' => 'work', 'qty' => '1', 'unit_price' => '100' }],
crypto_coin: 'DOGE', crypto_address: 'D6examplexampleexampleexample',
crypto_name: 'Dogecoin', crypto_scheme: 'dogecoin', crypto_amount_param: 'amount'
)
assert_equal ['DOGE', 'Dogecoin', 'dogecoin'], [inv.crypto_coin, inv.crypto_name, inv.crypto_scheme]
end
def test_build_rejects_malformed_ticker
assert_raises(ArgumentError) do
Invoice.build(
client_name: 'ACME', client_email: 'billing@example.com',
currency: 'EUR', gel_rate: '3.05',
items: [{ 'description' => 'work', 'qty' => '1', 'unit_price' => '100' }],
crypto_coin: 'DOGE', crypto_address: 'D6examplexampleexampleexample'
)
Invoice.build(client_name: 'A', client_email: 'a@example.com', currency: 'EUR', gel_rate: '3',
items: [{ 'description' => 'w', 'qty' => '1', 'unit_price' => '1' }],
crypto_coin: 'bad coin', crypto_address: 'D6examplexampleexampleexample')
end
end
end
@ -86,7 +104,7 @@ end
class InvoiceValidationTest < Minitest::Test
BASE = {
client_name: 'ACME', client_email: 'billing@example.com', currency: 'EUR',
gel_rate: '3.05', crypto_coin: 'LTC', crypto_address: '', crypto_rate: '', crypto_amount: '',
gel_rate: '3.05', payout_id: '', crypto_rate: '', crypto_amount: '',
items: [{ 'description' => 'work', 'qty' => '1', 'unit_price' => '100' }]
}.freeze
@ -122,17 +140,21 @@ class InvoiceValidationTest < Minitest::Test
assert_includes validate(items: [{ 'description' => '', 'qty' => '1', 'unit_price' => '1' }]), :items
end
def test_crypto_address_format
assert_includes validate(crypto_address: 'not!an@address'), :crypto_address
def test_unknown_or_inactive_payout_rejected
assert_includes validate(payout_id: '999999'), :payout_id
p = PayoutAddress.create(coin: 'LTC', name: 'Litecoin', address: 'ltc1qvalidvalidvalidvalid', active: false,
updated_by: 't', updated_at: Time.now.utc)
assert_includes validate(payout_id: p.id.to_s), :payout_id
p.update(active: true)
assert_empty validate(payout_id: p.id.to_s)
ensure
p&.destroy
end
def test_crypto_unknown_coin_rejected
v = validate(crypto_coin: 'DOGE', crypto_address: 'D6exampleexampleexampleexample')
assert_includes v, :crypto_coin
end
def test_crypto_valid_asset_passes
v = validate(crypto_coin: 'USDT-TRC20', crypto_address: 'TXYZexampleexampleexampleexample12')
assert_empty v
def test_crypto_rate_must_be_positive
p = PayoutAddress.create(coin: 'LTC', name: 'Litecoin', address: 'ltc1qvalidvalidvalidvalid', updated_by: 't', updated_at: Time.now.utc)
assert_includes validate(payout_id: p.id.to_s, crypto_rate: '-1'), :crypto_rate
ensure
p&.destroy
end
end

View file

@ -14,37 +14,76 @@ class PayoutAddressTest < Minitest::Test
DB.connection[:payout_addresses].delete
end
def token
get '/admin/invoices/payout-addresses'
def token(path = '/admin/invoices/payout-addresses')
get path
last_response.body[/name="authenticity_token" value="([^"]+)"/, 1]
end
def add(fields)
post '/admin/invoices/payout-addresses', { authenticity_token: token, payout: { 'new' => fields } }
end
def test_seed_from_env_once
ENV['CRYPTO_ADDRESSES'] = '{"BTC":"bc1qseedseedseedseed","XYZ":"nope00000000"}'
PayoutAddress.seed_from_env!
assert_equal({ 'BTC' => 'bc1qseedseedseedseed' }, PayoutAddress.map)
rows = PayoutAddress.all
assert_equal [['BTC', 'bc1qseedseedseedseed', 'Bitcoin', 'bitcoin']], rows.map { |r| [r.coin, r.address, r.name, r.scheme] }
ENV['CRYPTO_ADDRESSES'] = '{"BTC":"bc1qotherotherother"}'
PayoutAddress.seed_from_env!
assert_equal 'bc1qseedseedseedseed', PayoutAddress.map['BTC'] # not re-seeded
assert_equal 1, PayoutAddress.count
ensure
ENV.delete('CRYPTO_ADDRESSES')
end
def test_admin_edits_validates_and_removes
def test_add_known_ticker_fills_from_template_and_custom_coin
admin_login!
post '/admin/invoices/payout-addresses', { authenticity_token: token, address: { 'LTC' => 'ltc1qaaaaaaaaaaaaaaaa', 'BTC' => 'bad addr!' } }
assert_equal 302, last_response.status
assert_equal 'ltc1qaaaaaaaaaaaaaaaa', PayoutAddress.map['LTC']
assert_nil PayoutAddress.map['BTC']
add('coin' => 'ltc', 'address' => 'ltc1qaaaaaaaaaaaaaaaa', 'name' => 'Litecoin (Ledger)')
add('coin' => 'LTC', 'address' => 'ltc1qbbbbbbbbbbbbbbbb', 'name' => 'Litecoin (Exchange)')
add('coin' => 'DOGE', 'address' => 'DAddr1234567890', 'name' => 'Dogecoin', 'scheme' => 'dogecoin:', 'amount_param' => 'amount', 'coingecko' => 'dogecoin')
add('coin' => 'BTC', 'address' => 'bad addr!')
led = PayoutAddress[address: 'ltc1qaaaaaaaaaaaaaaaa']
assert_equal ['LTC', 'litecoin', 'amount', 'litecoin'], [led.coin, led.scheme, led.amount_param, led.coingecko]
assert_equal 2, PayoutAddress.where(coin: 'LTC').count
doge = PayoutAddress[coin: 'DOGE']
assert_equal 'dogecoin', doge.scheme # colon stripped
get '/admin/invoices/payout-addresses'
assert_includes last_response.body, 'BTC: address looks invalid'
assert_includes DB.connection[:audit_log].select_map(:action), 'invoice.payout_addresses'
assert_includes last_response.body, 'QR → dogecoin:DAddr1234567890?amount=0.5'
get '/admin/invoices/new'
assert_includes last_response.body, 'ltc1qaaaaaaaaaaaaaaaa' # prefilled from DB
assert_includes last_response.body, 'Litecoin (Exchange) · LTC'
assert_includes last_response.body, 'Dogecoin · DOGE'
end
post '/admin/invoices/payout-addresses', { authenticity_token: token, address: { 'LTC' => '' } }
assert_nil PayoutAddress.map['LTC']
def test_edit_validate_retire_and_remove
admin_login!
add('coin' => 'LTC', 'address' => 'ltc1qaaaaaaaaaaaaaaaa')
row = PayoutAddress.first
post '/admin/invoices/payout-addresses', { authenticity_token: token,
payout: { row.id.to_s => { 'coin' => 'LTC', 'name' => 'Main', 'address' => row.address, 'scheme' => 'Lite Coin', 'active' => '1' } } }
assert_equal 'Litecoin', row.refresh.name # bad scheme: nothing saved
get '/admin/invoices/payout-addresses'
assert_includes last_response.body, "scheme must look like"
post '/admin/invoices/payout-addresses', { authenticity_token: token,
payout: { row.id.to_s => { 'coin' => 'LTC', 'name' => 'Main', 'address' => row.address, 'scheme' => 'litecoin', 'amount_param' => 'amount' } } } # active unticked
row.refresh
assert_equal 'Main', row.name
refute row.active
get '/admin/invoices/new'
refute_includes last_response.body, 'Main · LTC' # retired: not offered
post "/admin/invoices/payout-addresses/#{row.id}/delete", { authenticity_token: token }
assert_nil PayoutAddress[row.id]
assert_includes DB.connection[:audit_log].select_map(:action), 'invoice.payout_address_delete'
end
def test_rate_endpoint_uses_payout_coingecko_id
admin_login!
add('coin' => 'XYZ', 'address' => 'XYZaddress1234567') # custom coin, no CoinGecko id
get "/admin/crypto-rate?payout=#{PayoutAddress.first.id}&currency=EUR"
assert_equal 502, last_response.status
assert_includes last_response.body, 'enter the rate by hand'
end
def test_read_only_user_sees_disabled_form
@ -53,9 +92,9 @@ class PayoutAddressTest < Minitest::Test
admin_login!(username: 'reader', password: 'reader-password1')
get '/admin/invoices/payout-addresses'
assert last_response.ok?
assert_includes last_response.body, 'disabled'
refute_includes last_response.body, 'Save addresses'
post '/admin/invoices/payout-addresses', { authenticity_token: token, address: { 'LTC' => 'ltc1qbbbbbbbbbbbbbbbb' } }
refute_includes last_response.body, '>Save</button>'
add('coin' => 'LTC', 'address' => 'ltc1qbbbbbbbbbbbbbbbb')
assert_equal 403, last_response.status
end
end