stacker.news/components/use-qr-payment.js
soxa f72af08882
fix: WebLN QR fallback for anon users (#1858)
* fix: WebLN QR fallback for anon users

* wip: clear zap color on payment fail

* reverse clearItemMeAnonSats

* webln-specific retry bypass

* cleanup

* send WebLN payment when user is Anon AND on QR

* skip wallet checking on anon

* Use WalletError for all errors in webln.sendPayment

---------

Co-authored-by: ekzyis <ek@stacker.news>
Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com>
2025-03-01 16:56:18 -06:00

53 lines
1.8 KiB
JavaScript

import { useCallback } from 'react'
import Invoice from '@/components/invoice'
import { InvoiceCanceledError, InvoiceExpiredError, AnonWalletError } from '@/wallets/errors'
import { useShowModal } from '@/components/modal'
import useInvoice from '@/components/use-invoice'
import { sendPayment } from '@/wallets/webln/client'
export default function useQrPayment () {
const invoice = useInvoice()
const showModal = useShowModal()
const waitForQrPayment = useCallback(async (inv, walletError,
{
keepOpen = true,
cancelOnClose = true,
persistOnNavigate = false,
waitFor = inv => inv?.satsReceived > 0
} = {}
) => {
// if anon user and webln is available, try to pay with webln
if (typeof window.webln !== 'undefined' && (walletError instanceof AnonWalletError)) {
sendPayment(inv.bolt11).catch(e => { console.error('WebLN payment failed:', e) })
}
return await new Promise((resolve, reject) => {
let paid
const cancelAndReject = async (onClose) => {
if (!paid && cancelOnClose) {
const updatedInv = await invoice.cancel(inv, { userCancel: true })
reject(new InvoiceCanceledError(updatedInv))
}
resolve(inv)
}
showModal(onClose =>
<Invoice
id={inv.id}
modal
description
status='loading'
successVerb='received'
walletError={walletError}
waitFor={waitFor}
onExpired={inv => reject(new InvoiceExpiredError(inv))}
onCanceled={inv => { onClose(); reject(new InvoiceCanceledError(inv, inv?.actionError)) }}
onPayment={(inv) => { paid = true; onClose(); resolve(inv) }}
poll
/>,
{ keepOpen, persistOnNavigate, onClose: cancelAndReject })
})
}, [invoice])
return waitForQrPayment
}