2023-08-09 23:45:59 +00:00
|
|
|
import { useState, useCallback, useEffect } from 'react'
|
|
|
|
import { useMutation, useQuery } from '@apollo/client'
|
|
|
|
import { Button } from 'react-bootstrap'
|
|
|
|
import { gql } from 'graphql-tag'
|
2023-08-08 21:04:06 +00:00
|
|
|
import { numWithUnits } from '../lib/format'
|
2023-08-09 23:45:59 +00:00
|
|
|
import AccordianItem from './accordian-item'
|
|
|
|
import Qr, { QrSkeleton } from './qr'
|
|
|
|
import { INVOICE } from '../fragments/wallet'
|
|
|
|
import InvoiceStatus from './invoice-status'
|
|
|
|
import { useMe } from './me'
|
|
|
|
import { useShowModal } from './modal'
|
|
|
|
import { sleep } from '../lib/time'
|
2023-08-31 02:48:49 +00:00
|
|
|
import Countdown from './countdown'
|
|
|
|
|
2023-08-31 15:10:24 +00:00
|
|
|
export function Invoice ({ invoice, onPayment, info, successVerb }) {
|
2023-08-31 02:48:49 +00:00
|
|
|
const [expired, setExpired] = useState(new Date(invoice.expiredAt) <= new Date())
|
2021-05-06 21:15:22 +00:00
|
|
|
|
2021-05-13 13:28:38 +00:00
|
|
|
let variant = 'default'
|
2021-05-11 15:52:50 +00:00
|
|
|
let status = 'waiting for you'
|
2023-08-08 18:19:31 +00:00
|
|
|
let webLn = true
|
2023-08-31 02:48:49 +00:00
|
|
|
if (invoice.confirmedAt || (invoice.isHeld && invoice.satsReceived && !expired)) {
|
2021-05-13 13:28:38 +00:00
|
|
|
variant = 'confirmed'
|
2023-08-09 22:02:43 +00:00
|
|
|
status = `${numWithUnits(invoice.satsReceived, { abbreviate: false })} ${successVerb || 'deposited'}`
|
2023-08-08 18:19:31 +00:00
|
|
|
webLn = false
|
2023-08-31 02:48:49 +00:00
|
|
|
} else if (expired) {
|
2021-05-13 13:28:38 +00:00
|
|
|
variant = 'failed'
|
2023-08-31 02:48:49 +00:00
|
|
|
status = 'expired'
|
2023-08-08 18:19:31 +00:00
|
|
|
webLn = false
|
2023-08-31 02:48:49 +00:00
|
|
|
} else if (invoice.cancelled) {
|
2021-05-13 13:28:38 +00:00
|
|
|
variant = 'failed'
|
2023-08-31 02:48:49 +00:00
|
|
|
status = 'cancelled'
|
2023-08-08 18:19:31 +00:00
|
|
|
webLn = false
|
2021-05-11 15:52:50 +00:00
|
|
|
}
|
2021-05-06 21:15:22 +00:00
|
|
|
|
2023-08-10 02:13:09 +00:00
|
|
|
useEffect(() => {
|
2023-08-31 02:48:49 +00:00
|
|
|
if (invoice.confirmedAt || (invoice.isHeld && invoice.satsReceived)) {
|
|
|
|
onPayment?.(invoice)
|
2023-08-10 02:13:09 +00:00
|
|
|
}
|
2023-08-31 02:48:49 +00:00
|
|
|
}, [invoice.confirmedAt, invoice.isHeld, invoice.satsReceived])
|
2023-08-10 02:13:09 +00:00
|
|
|
|
2023-08-08 18:19:31 +00:00
|
|
|
const { nostr } = invoice
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2023-08-11 00:58:33 +00:00
|
|
|
<Qr
|
|
|
|
webLn={webLn} value={invoice.bolt11}
|
|
|
|
description={numWithUnits(invoice.satsRequested, { abbreviate: false })}
|
|
|
|
statusVariant={variant} status={status}
|
|
|
|
/>
|
2023-09-27 17:19:56 +00:00
|
|
|
{!invoice.confirmedAt &&
|
|
|
|
<div className='text-muted text-center'>
|
|
|
|
<Countdown
|
|
|
|
date={invoice.expiresAt} onComplete={() => {
|
|
|
|
setExpired(true)
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>}
|
2023-08-31 15:10:24 +00:00
|
|
|
{info && <div className='text-muted fst-italic text-center'>{info}</div>}
|
2023-08-08 18:19:31 +00:00
|
|
|
<div className='w-100'>
|
|
|
|
{nostr
|
|
|
|
? <AccordianItem
|
|
|
|
header='Nostr Zap Request'
|
|
|
|
body={
|
|
|
|
<pre>
|
|
|
|
<code>
|
|
|
|
{JSON.stringify(nostr, null, 2)}
|
|
|
|
</code>
|
|
|
|
</pre>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
: null}
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
)
|
2021-05-06 21:15:22 +00:00
|
|
|
}
|
2023-08-09 23:45:59 +00:00
|
|
|
|
2023-08-31 02:48:49 +00:00
|
|
|
const MutationInvoice = ({ id, hash, hmac, errorCount, repeat, onClose, expiresAt, ...props }) => {
|
2023-08-09 23:45:59 +00:00
|
|
|
const { data, loading, error } = useQuery(INVOICE, {
|
|
|
|
pollInterval: 1000,
|
|
|
|
variables: { id }
|
|
|
|
})
|
2023-08-31 02:48:49 +00:00
|
|
|
const [cancelInvoice] = useMutation(gql`
|
|
|
|
mutation cancelInvoice($hash: String!, $hmac: String!) {
|
|
|
|
cancelInvoice(hash: $hash, hmac: $hmac) {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`)
|
2023-08-09 23:45:59 +00:00
|
|
|
if (error) {
|
|
|
|
if (error.message?.includes('invoice not found')) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return <div>error</div>
|
|
|
|
}
|
|
|
|
if (!data || loading) {
|
2023-08-11 21:52:24 +00:00
|
|
|
return <QrSkeleton description status='loading' />
|
2023-08-09 23:45:59 +00:00
|
|
|
}
|
|
|
|
|
2023-08-10 19:41:56 +00:00
|
|
|
let errorStatus = 'Something went wrong trying to perform the action after payment.'
|
2023-08-09 23:45:59 +00:00
|
|
|
if (errorCount > 1) {
|
2023-08-31 02:48:49 +00:00
|
|
|
errorStatus = 'Something still went wrong.\nYou can retry or cancel the invoice to return your funds.'
|
2023-08-09 23:45:59 +00:00
|
|
|
}
|
2023-08-31 15:37:58 +00:00
|
|
|
|
2023-08-09 23:45:59 +00:00
|
|
|
return (
|
|
|
|
<>
|
2023-08-31 15:37:58 +00:00
|
|
|
<Invoice invoice={data.invoice} {...props} />
|
2023-08-09 23:45:59 +00:00
|
|
|
{errorCount > 0
|
|
|
|
? (
|
|
|
|
<>
|
2023-08-10 19:41:56 +00:00
|
|
|
<div className='my-3'>
|
|
|
|
<InvoiceStatus variant='failed' status={errorStatus} />
|
|
|
|
</div>
|
2023-08-31 02:48:49 +00:00
|
|
|
<div className='d-flex flex-row mt-3 justify-content-center'>
|
|
|
|
<Button className='mx-1' variant='info' onClick={repeat}>Retry</Button>
|
|
|
|
<Button
|
|
|
|
className='mx-1'
|
|
|
|
variant='danger' onClick={async () => {
|
|
|
|
await cancelInvoice({ variables: { hash, hmac } })
|
|
|
|
onClose()
|
|
|
|
}}
|
|
|
|
>Cancel
|
|
|
|
</Button>
|
|
|
|
</div>
|
2023-08-09 23:45:59 +00:00
|
|
|
</>
|
|
|
|
)
|
|
|
|
: null}
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const defaultOptions = {
|
|
|
|
forceInvoice: false,
|
2023-08-31 15:10:24 +00:00
|
|
|
requireSession: false,
|
2023-08-31 15:20:01 +00:00
|
|
|
callback: null, // (formValues) => void
|
2023-08-31 15:10:24 +00:00
|
|
|
replaceModal: false
|
2023-08-09 23:45:59 +00:00
|
|
|
}
|
2023-08-31 02:48:49 +00:00
|
|
|
export const useInvoiceable = (onSubmit, options = defaultOptions) => {
|
2023-08-09 23:45:59 +00:00
|
|
|
const me = useMe()
|
|
|
|
const [createInvoice, { data }] = useMutation(gql`
|
|
|
|
mutation createInvoice($amount: Int!) {
|
2023-08-31 02:48:49 +00:00
|
|
|
createInvoice(amount: $amount, hodlInvoice: true, expireSecs: 180) {
|
2023-08-09 23:45:59 +00:00
|
|
|
id
|
|
|
|
hash
|
2023-08-10 05:10:05 +00:00
|
|
|
hmac
|
2023-08-31 02:48:49 +00:00
|
|
|
expiresAt
|
2023-08-09 23:45:59 +00:00
|
|
|
}
|
|
|
|
}`)
|
|
|
|
const showModal = useShowModal()
|
2023-08-31 02:48:49 +00:00
|
|
|
const [formValues, setFormValues] = useState()
|
|
|
|
const [submitArgs, setSubmitArgs] = useState()
|
2023-08-09 23:45:59 +00:00
|
|
|
|
|
|
|
let errorCount = 0
|
2023-08-31 02:48:49 +00:00
|
|
|
const onPayment = useCallback(
|
2023-08-10 05:10:05 +00:00
|
|
|
(onClose, hmac) => {
|
2023-08-31 02:48:49 +00:00
|
|
|
return async ({ id, satsReceived, expiresAt, hash }) => {
|
2023-08-11 22:53:21 +00:00
|
|
|
await sleep(500)
|
2023-08-31 17:26:42 +00:00
|
|
|
const repeat = () => {
|
|
|
|
onClose()
|
2023-08-31 02:48:49 +00:00
|
|
|
// call onSubmit handler and pass invoice data
|
|
|
|
onSubmit({ satsReceived, hash, hmac, ...formValues }, ...submitArgs)
|
2023-08-31 15:15:52 +00:00
|
|
|
.then(() => {
|
|
|
|
options?.callback?.(formValues)
|
|
|
|
})
|
2023-08-09 23:45:59 +00:00
|
|
|
.catch((error) => {
|
2023-08-31 17:26:42 +00:00
|
|
|
// if error happened after payment, show repeat and cancel options
|
|
|
|
// by passing `errorCount` and `repeat`
|
2023-08-09 23:45:59 +00:00
|
|
|
console.error(error)
|
|
|
|
errorCount++
|
|
|
|
showModal(onClose => (
|
2023-08-31 02:48:49 +00:00
|
|
|
<MutationInvoice
|
2023-08-09 23:45:59 +00:00
|
|
|
id={id}
|
|
|
|
hash={hash}
|
2023-08-10 05:10:05 +00:00
|
|
|
hmac={hmac}
|
2023-08-31 02:48:49 +00:00
|
|
|
expiresAt={expiresAt}
|
|
|
|
onClose={onClose}
|
|
|
|
onPayment={onPayment(onClose, hmac)}
|
2023-08-09 23:45:59 +00:00
|
|
|
successVerb='received'
|
|
|
|
errorCount={errorCount}
|
|
|
|
repeat={repeat}
|
|
|
|
/>
|
|
|
|
), { keepOpen: true })
|
|
|
|
})
|
2023-08-31 17:26:42 +00:00
|
|
|
}
|
2023-08-31 02:48:49 +00:00
|
|
|
// prevents infinite loop of calling `onPayment`
|
2023-08-09 23:45:59 +00:00
|
|
|
if (errorCount === 0) await repeat()
|
|
|
|
}
|
2023-08-31 02:48:49 +00:00
|
|
|
}, [onSubmit, submitArgs]
|
2023-08-09 23:45:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const invoice = data?.createInvoice
|
|
|
|
useEffect(() => {
|
|
|
|
if (invoice) {
|
|
|
|
showModal(onClose => (
|
2023-08-31 02:48:49 +00:00
|
|
|
<MutationInvoice
|
2023-08-09 23:45:59 +00:00
|
|
|
id={invoice.id}
|
|
|
|
hash={invoice.hash}
|
2023-08-10 05:10:05 +00:00
|
|
|
hmac={invoice.hmac}
|
2023-08-31 02:48:49 +00:00
|
|
|
expiresAt={invoice.expiresAt}
|
|
|
|
onClose={onClose}
|
|
|
|
onPayment={onPayment(onClose, invoice.hmac)}
|
2023-08-09 23:45:59 +00:00
|
|
|
successVerb='received'
|
|
|
|
/>
|
2023-08-31 15:10:24 +00:00
|
|
|
), { replaceModal: options.replaceModal, keepOpen: true }
|
2023-08-09 23:45:59 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}, [invoice?.id])
|
|
|
|
|
2023-08-31 02:48:49 +00:00
|
|
|
// this function will be called before the Form's onSubmit handler is called
|
|
|
|
// and the form must include `cost` or `amount` as a value
|
|
|
|
const onSubmitWrapper = useCallback(async (formValues, ...submitArgs) => {
|
|
|
|
let { cost, amount } = formValues
|
|
|
|
cost ??= amount
|
|
|
|
|
|
|
|
// action only allowed if logged in
|
2023-08-09 23:45:59 +00:00
|
|
|
if (!me && options.requireSession) {
|
|
|
|
throw new Error('you must be logged in')
|
|
|
|
}
|
2023-08-31 02:48:49 +00:00
|
|
|
|
|
|
|
// if no cost is passed, just try the action first
|
|
|
|
if (!cost || (me && !options.forceInvoice)) {
|
2023-08-09 23:45:59 +00:00
|
|
|
try {
|
2023-08-31 02:48:49 +00:00
|
|
|
return await onSubmit(formValues, ...submitArgs)
|
2023-08-09 23:45:59 +00:00
|
|
|
} catch (error) {
|
2023-08-31 15:10:24 +00:00
|
|
|
if (!payOrLoginError(error)) {
|
|
|
|
throw error
|
2023-08-09 23:45:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-08-31 02:48:49 +00:00
|
|
|
setFormValues(formValues)
|
|
|
|
setSubmitArgs(submitArgs)
|
|
|
|
await createInvoice({ variables: { amount: cost } })
|
2023-08-11 21:35:37 +00:00
|
|
|
// tell onSubmit handler that we want to keep local storage
|
|
|
|
// even though the submit handler was "successful"
|
|
|
|
return { keepLocalStorage: true }
|
2023-08-31 02:48:49 +00:00
|
|
|
}, [onSubmit, setFormValues, setSubmitArgs, createInvoice])
|
2023-08-09 23:45:59 +00:00
|
|
|
|
2023-08-31 02:48:49 +00:00
|
|
|
return onSubmitWrapper
|
2023-08-09 23:45:59 +00:00
|
|
|
}
|
2023-08-31 15:10:24 +00:00
|
|
|
|
|
|
|
export const InvoiceModal = ({ onPayment, amount }) => {
|
|
|
|
const createInvoice = useInvoiceable(onPayment, { replaceModal: true })
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
createInvoice({ amount })
|
|
|
|
}, [])
|
|
|
|
}
|
|
|
|
|
|
|
|
export const payOrLoginError = (error) => {
|
|
|
|
const matches = ['insufficient funds', 'you must be logged in or pay']
|
|
|
|
if (Array.isArray(error)) {
|
|
|
|
return error.some(({ message }) => matches.some(m => message.includes(m)))
|
|
|
|
}
|
|
|
|
return matches.some(m => error.toString().includes(m))
|
|
|
|
}
|