2024-11-27 18:09:56 +00:00
|
|
|
import { useApolloClient, useMutation } from '@apollo/client'
|
2024-12-05 14:52:32 +00:00
|
|
|
import { useCallback } from 'react'
|
2024-11-25 00:53:57 +00:00
|
|
|
import { RETRY_PAID_ACTION } from '@/fragments/paidAction'
|
2024-12-05 14:52:32 +00:00
|
|
|
import { INVOICE, CANCEL_INVOICE } from '@/fragments/wallet'
|
|
|
|
import { InvoiceExpiredError, InvoiceCanceledError } from '@/wallets/errors'
|
2024-05-28 17:18:54 +00:00
|
|
|
|
2024-12-05 14:52:32 +00:00
|
|
|
export default function useInvoice () {
|
2024-05-28 17:18:54 +00:00
|
|
|
const client = useApolloClient()
|
2024-11-25 00:53:57 +00:00
|
|
|
const [retryPaidAction] = useMutation(RETRY_PAID_ACTION)
|
2024-05-28 17:18:54 +00:00
|
|
|
|
2024-11-27 18:09:56 +00:00
|
|
|
const [cancelInvoice] = useMutation(CANCEL_INVOICE)
|
2024-05-28 17:18:54 +00:00
|
|
|
|
2024-07-01 17:02:29 +00:00
|
|
|
const isInvoice = useCallback(async ({ id }, that) => {
|
|
|
|
const { data, error } = await client.query({ query: INVOICE, fetchPolicy: 'network-only', variables: { id } })
|
2024-05-28 17:18:54 +00:00
|
|
|
if (error) {
|
|
|
|
throw error
|
|
|
|
}
|
2024-10-02 20:03:30 +00:00
|
|
|
|
2024-11-27 17:31:20 +00:00
|
|
|
const { cancelled, cancelledAt, actionError, actionState, expiresAt } = data.invoice
|
2024-11-22 14:25:20 +00:00
|
|
|
|
|
|
|
const expired = cancelledAt && new Date(expiresAt) < new Date(cancelledAt)
|
|
|
|
if (expired) {
|
2024-11-27 17:31:20 +00:00
|
|
|
throw new InvoiceExpiredError(data.invoice)
|
2024-11-22 14:25:20 +00:00
|
|
|
}
|
2024-07-01 17:02:29 +00:00
|
|
|
|
2024-07-04 17:30:42 +00:00
|
|
|
if (cancelled || actionError) {
|
2024-11-27 17:31:20 +00:00
|
|
|
throw new InvoiceCanceledError(data.invoice, actionError)
|
2024-05-28 17:18:54 +00:00
|
|
|
}
|
2024-07-01 17:02:29 +00:00
|
|
|
|
2024-10-02 20:03:30 +00:00
|
|
|
// write to cache if paid
|
|
|
|
if (actionState === 'PAID') {
|
|
|
|
client.writeQuery({ query: INVOICE, variables: { id }, data: { invoice: data.invoice } })
|
|
|
|
}
|
|
|
|
|
2024-11-27 18:09:56 +00:00
|
|
|
return { invoice: data.invoice, check: that(data.invoice) }
|
2024-05-28 17:18:54 +00:00
|
|
|
}, [client])
|
|
|
|
|
|
|
|
const cancel = useCallback(async ({ hash, hmac }) => {
|
2024-07-01 17:02:29 +00:00
|
|
|
if (!hash || !hmac) {
|
|
|
|
throw new Error('missing hash or hmac')
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log('canceling invoice:', hash)
|
2024-11-27 18:09:56 +00:00
|
|
|
const { data } = await cancelInvoice({ variables: { hash, hmac } })
|
|
|
|
return data.cancelInvoice
|
2024-05-28 17:18:54 +00:00
|
|
|
}, [cancelInvoice])
|
|
|
|
|
2024-11-28 16:48:22 +00:00
|
|
|
const retry = useCallback(async ({ id, hash, hmac }, { update }) => {
|
2024-11-25 00:53:57 +00:00
|
|
|
console.log('retrying invoice:', hash)
|
2024-11-28 16:48:22 +00:00
|
|
|
const { data, error } = await retryPaidAction({ variables: { invoiceId: Number(id) }, update })
|
2024-11-25 00:53:57 +00:00
|
|
|
if (error) throw error
|
2024-05-28 17:18:54 +00:00
|
|
|
|
2024-11-26 02:29:28 +00:00
|
|
|
const newInvoice = data.retryPaidAction.invoice
|
|
|
|
console.log('new invoice:', newInvoice?.hash)
|
|
|
|
|
|
|
|
return newInvoice
|
2024-11-28 16:48:22 +00:00
|
|
|
}, [retryPaidAction])
|
2024-05-28 17:18:54 +00:00
|
|
|
|
2024-11-25 00:53:57 +00:00
|
|
|
return { cancel, retry, isInvoice }
|
2024-05-28 17:18:54 +00:00
|
|
|
}
|