Return last attempted invoice in canceled state

This commit is contained in:
ekzyis 2024-11-27 16:52:21 +01:00
parent 7e25e29507
commit b1cdd953a0
3 changed files with 21 additions and 14 deletions

View File

@ -42,9 +42,8 @@ export function usePaidMutation (mutation,
walletError = null walletError = null
if (err instanceof WalletError) { if (err instanceof WalletError) {
walletError = err walletError = err
// wallet payment error handling always creates a new invoice to retry // get the last invoice that was attempted but failed and was canceled
// unless no wallet was even able to attempt a payment if (err.invoice) walletInvoice = err.invoice
if (err.newInvoice) walletInvoice = err.newInvoice
} }
const invoiceError = err instanceof InvoiceCanceledError || err instanceof InvoiceExpiredError const invoiceError = err instanceof InvoiceCanceledError || err instanceof InvoiceExpiredError
@ -62,6 +61,7 @@ export function usePaidMutation (mutation,
} }
} }
walletInvoice = await invoiceHelper.retry(walletInvoice)
return await waitForQrPayment(walletInvoice, walletError, { persistOnNavigate, waitFor }) return await waitForQrPayment(walletInvoice, walletError, { persistOnNavigate, waitFor })
}, [waitForWalletPayment, waitForQrPayment, invoiceHelper]) }, [waitForWalletPayment, waitForQrPayment, invoiceHelper])

View File

@ -54,10 +54,10 @@ export class WalletsNotAvailableError extends WalletConfigurationError {
} }
export class WalletAggregateError extends WalletError { export class WalletAggregateError extends WalletError {
constructor (errors, newInvoice) { constructor (errors, invoice) {
super('WalletAggregateError') super('WalletAggregateError')
this.name = 'WalletAggregateError' this.name = 'WalletAggregateError'
this.errors = errors this.errors = errors
this.newInvoice = newInvoice this.invoice = invoice
} }
} }

View File

@ -33,20 +33,22 @@ export function useWalletPayment () {
}, {}) }, {})
const walletsWithPayments = useMemo(() => { const walletsWithPayments = useMemo(() => {
return wallets.map(wallet => { return wallets
const logger = loggers[wallet.def.name] .filter(wallet => canSend(wallet))
return { .map(wallet => {
...wallet, const logger = loggers[wallet.def.name]
sendPayment: sendPayment(wallet, logger) return {
} ...wallet,
}) sendPayment: sendPayment(wallet, logger)
}
})
}, [wallets, loggers]) }, [wallets, loggers])
const waitForPayment = useCallback(async (invoice, { waitFor }) => { const waitForPayment = useCallback(async (invoice, { waitFor }) => {
let walletError = new WalletAggregateError([]) let walletError = new WalletAggregateError([])
let walletInvoice = invoice let walletInvoice = invoice
for (const wallet of walletsWithPayments) { for (const [i, wallet] of walletsWithPayments.entries()) {
const controller = invoiceController(walletInvoice, invoiceHelper.isInvoice) const controller = invoiceController(walletInvoice, invoiceHelper.isInvoice)
try { try {
return await new Promise((resolve, reject) => { return await new Promise((resolve, reject) => {
@ -63,7 +65,12 @@ export function useWalletPayment () {
const paymentAttempt = err instanceof WalletPaymentError const paymentAttempt = err instanceof WalletPaymentError
if (paymentAttempt) { if (paymentAttempt) {
await invoiceHelper.cancel(walletInvoice) await invoiceHelper.cancel(walletInvoice)
walletInvoice = await invoiceHelper.retry(walletInvoice)
// is there another wallet to try?
const lastAttempt = i === walletsWithPayments.length - 1
if (!lastAttempt) {
walletInvoice = await invoiceHelper.retry(walletInvoice)
}
} }
// TODO: receiver fallbacks // TODO: receiver fallbacks