add bitcoin support to storefront invoice view

Refactor invoice view building to share logic across XMR and BTC, including BIP21 QR codes and payment detail validation.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-09-06 00:41:57 +02:00
co-authored by Cursor
parent ced92ead37
commit cf92212226
4 changed files with 107 additions and 27 deletions
@@ -2,13 +2,16 @@ import Decimal from 'decimal.js';
import { InternalServerErrorException } from '@nestjs/common';
import type { StorefrontInvoicePaymentView } from '../../modules/storefrontCore/types/StorefrontInvoicePaymentView';
import type { StorefrontInvoiceView } from '../../modules/storefrontCore/types/StorefrontInvoiceView';
import type { CryptoAtomicConverter } from '../../types/CryptoAtomicConverter';
import type { Invoice } from '../../modules/payment/entities/Invoice';
import type { InvoicePayment } from '../../modules/payment/entities/InvoicePayment';
import { PaymentMethod } from '../../modules/payment/types/PaymentMethod';
import dayjs from '../../plugins/dayjs';
import { generateQrCodeDataUrl } from '../generateQrCodeDataUrl';
import { convertBtcAtomicToBtc } from '../bitcoin/convertBtcAtomicToBtc';
import { convertXmrAtomicToXmr } from '../monero/convertXmrAtomicToXmr';
import { subtractAtomic } from '../atomic/subtractAtomic';
import { paymentMethodLabel } from '../../consts/paymentMethodLabel';
import { deriveInvoiceState } from './deriveInvoiceState';
import { formatInvoicePaymentConfirmationStatus } from './formatInvoicePaymentConfirmationStatus';
import { resolveInvoiceStatusMessage } from './resolveInvoiceStatusMessage';
@@ -19,17 +22,44 @@ import { sumInvoicePaymentAmountsAtomic } from './sumInvoicePaymentAmountsAtomic
export const toStorefrontInvoiceView = async (invoice: Invoice): Promise<StorefrontInvoiceView> => {
switch (invoice.paymentMethod) {
case PaymentMethod.Xmr:
return toXmrInvoiceView(invoice);
if (!invoice.moneroDetails) {
throw new InternalServerErrorException('Invoice is missing Monero payment details');
}
return buildStorefrontInvoiceView({
invoice,
paymentLabel: paymentMethodLabel[PaymentMethod.Xmr],
convertAtomicToCrypto: convertXmrAtomicToXmr,
buildQrCodePayload: (paymentAddress, amountCrypto) =>
`monero:${paymentAddress}?tx_amount=${amountCrypto}`
});
case PaymentMethod.Btc:
if (!invoice.btcDetails) {
throw new InternalServerErrorException('Invoice is missing Bitcoin payment details');
}
return buildStorefrontInvoiceView({
invoice,
paymentLabel: paymentMethodLabel[PaymentMethod.Btc],
convertAtomicToCrypto: convertBtcAtomicToBtc,
buildQrCodePayload: (paymentAddress, amountCrypto) => `bitcoin:${paymentAddress}?amount=${amountCrypto}`
});
default:
throw new InternalServerErrorException(`Unsupported payment method: ${String(invoice.paymentMethod)}`);
}
};
const toXmrInvoiceView = async (invoice: Invoice): Promise<StorefrontInvoiceView> => {
if (!invoice.moneroDetails) {
throw new InternalServerErrorException('Invoice is missing Monero payment details');
}
const buildStorefrontInvoiceView = async ({
invoice,
paymentLabel,
convertAtomicToCrypto,
buildQrCodePayload
}: {
invoice: Invoice;
paymentLabel: string;
convertAtomicToCrypto: CryptoAtomicConverter;
buildQrCodePayload: (paymentAddress: string, amountCrypto: string) => string;
}): Promise<StorefrontInvoiceView> => {
const invoiceState = deriveInvoiceState(invoice);
const {
@@ -42,22 +72,20 @@ const toXmrInvoiceView = async (invoice: Invoice): Promise<StorefrontInvoiceView
hasPendingConfirmations
} = invoiceState;
const cryptoCurrency = 'XMR';
const expectedTotalCrypto = convertXmrAtomicToXmr(invoice.expectedTotalAtomic);
const expectedTotalCrypto = convertAtomicToCrypto(invoice.expectedTotalAtomic);
const receivedAtomic = sumInvoicePaymentAmountsAtomic(invoice.payments);
const receivedTotalCrypto = receivedAtomic === '0' ? null : convertXmrAtomicToXmr(receivedAtomic);
const receivedTotalCrypto = receivedAtomic === '0' ? null : convertAtomicToCrypto(receivedAtomic);
const requiredConfirmations = resolveInvoiceRequiredConfirmations(invoice);
const payments = [...(invoice.payments ?? [])]
.sort((left, right) => left.createdAt.getTime() - right.createdAt.getTime())
.map(payment => toPaymentView(payment, requiredConfirmations));
.map(payment => toPaymentView(payment, requiredConfirmations, convertAtomicToCrypto));
const showPaymentCapture = (isAwaitingPayment || isUnderpaid) && !isExpired;
const expiresInDuration = showPaymentCapture ? formatInvoiceExpiresInDuration(invoice.expiresAt) : null;
let remainingTotalCrypto: string | null = null;
let instructionPrefix: string | null = null;
let instructionAmountCrypto: string | null = null;
let instructionSuffix: string | null = null;
@@ -65,22 +93,20 @@ const toXmrInvoiceView = async (invoice: Invoice): Promise<StorefrontInvoiceView
if (showPaymentCapture) {
if (isAwaitingPayment) {
remainingTotalCrypto = expectedTotalCrypto;
instructionAmountCrypto = expectedTotalCrypto;
instructionPrefix = 'Send exactly';
instructionSuffix = 'to the address below.';
} else {
const remainingTotalCryptoAtomic = subtractAtomic(invoice.expectedTotalAtomic, receivedAtomic);
remainingTotalCrypto = convertXmrAtomicToXmr(remainingTotalCryptoAtomic, Decimal.ROUND_CEIL);
instructionAmountCrypto = remainingTotalCrypto;
instructionAmountCrypto = convertAtomicToCrypto(remainingTotalCryptoAtomic, Decimal.ROUND_CEIL);
instructionPrefix = 'Send';
instructionSuffix = 'more to the same address below.';
}
qrCodeUrl = await generateQrCodeDataUrl(
`monero:${invoice.paymentAddress}?tx_amount=${instructionAmountCrypto}`
);
const qrCodePayload = buildQrCodePayload(invoice.paymentAddress, instructionAmountCrypto);
qrCodeUrl = await generateQrCodeDataUrl(qrCodePayload);
}
const statusMessage = resolveInvoiceStatusMessage(invoiceState);
@@ -88,7 +114,7 @@ const toXmrInvoiceView = async (invoice: Invoice): Promise<StorefrontInvoiceView
const statusVariant = resolveInvoiceStatusVariant(invoiceState);
return {
cryptoCurrency,
cryptoCurrency: paymentLabel,
expectedTotalCrypto,
receivedTotalCrypto,
paymentAddress: invoice.paymentAddress,
@@ -122,12 +148,16 @@ const toXmrInvoiceView = async (invoice: Invoice): Promise<StorefrontInvoiceView
};
};
const toPaymentView = (payment: InvoicePayment, requiredConfirmations: number): StorefrontInvoicePaymentView => {
const toPaymentView = (
payment: InvoicePayment,
requiredConfirmations: number,
convertAtomicToCrypto: CryptoAtomicConverter
): StorefrontInvoicePaymentView => {
const isConfirmed = payment.confirmations >= requiredConfirmations;
return {
txHash: payment.txHash,
amountCrypto: convertXmrAtomicToXmr(payment.amountAtomic),
amountCrypto: convertAtomicToCrypto(payment.amountAtomic),
confirmationStatus: formatInvoicePaymentConfirmationStatus({
confirmations: payment.confirmations,
requiredConfirmations,