Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Commit

Permalink
fix: failing USD invoice generation (#368)
Browse files Browse the repository at this point in the history
  • Loading branch information
Extheoisah authored Dec 12, 2022
1 parent 1661935 commit 2ea32c5
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 14 deletions.
61 changes: 48 additions & 13 deletions components/ParsePOSPayment/Receive-Invoice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ interface Props {
const USD_MAX_INVOICE_TIME = "5.00"

function ReceiveInvoice({ recipientWalletCurrency, walletId, state, dispatch }: Props) {
const { username, amount, currency, unit, sats, memo } = useRouter().query
const { usdToSats } = useSatPrice()
const { username, amount, unit, sats, memo } = useRouter().query
const { usdToSats, satsToUsd } = useSatPrice()

const [expiredInvoiceError, setExpiredInvoiceError] = React.useState<string>("")
const [copied, setCopied] = React.useState<boolean>(false)
const [shareState, setShareState] = React.useState<"not-set">()
const [image, takeScreenShot] = useScreenshot()
Expand All @@ -56,37 +57,60 @@ function ReceiveInvoice({ recipientWalletCurrency, walletId, state, dispatch }:
const timerRef = React.useRef<Date>()

timerRef.current = new Date()
if (currency === "USD") {
if (recipientWalletCurrency === "USD") {
timerRef.current.setSeconds(
timerRef.current.getSeconds() + USD_INVOICE_EXPIRE_INTERVAL,
) // default to five mins for USD invoice
}
const expiryTimestamp = timerRef.current
const { seconds, minutes } = useTimer({
expiryTimestamp,
onExpire: () => console.warn("onExpire called on USD"),
onExpire: () => {
if (recipientWalletCurrency === "BTC") return
setExpiredInvoiceError("Invoice has expired. Generate a new invoice!")
},
})

const { createInvoice, data, errorsMessage, error, loading, invoiceStatus } =
useCreateInvoice({
const { createInvoice, data, errorsMessage, loading, invoiceStatus } = useCreateInvoice(
{
recipientWalletCurrency,
})
},
)

const paymentAmount = React.useMemo(() => {
const finalReturnValue =
recipientWalletCurrency === "USD" ? Number(amount) * 100 : usdToSats(Number(amount))
if (amount === "0.00" || isNaN(Number(amount))) {
if ((!unit || !amount || !sats) && recipientWalletCurrency === "USD") {
return (Number(state.currentAmount) * 100).toString()
}
if (sats && unit === AmountUnit.Sat) {
if (recipientWalletCurrency === "USD") {
return satsToUsd(Number(state.currentAmount)).toString()
}
return sats
} else if (Number(state.currentAmount) > 0) {
return usdToSats(Number(state.currentAmount)).toFixed(2)
}
}

if (sats && unit === AmountUnit.Sat) {
if (recipientWalletCurrency === "USD") {
return (Number(amount) * 100).toString()
}
return sats
}

return usdToSats(Number(amount)).toFixed(2)
}, [amount, unit, sats, usdToSats, state.currentAmount])
return finalReturnValue.toFixed(2)
}, [
amount,
unit,
sats,
usdToSats,
satsToUsd,
state.currentAmount,
recipientWalletCurrency,
])

React.useEffect(() => {
if (!walletId || !Number(paymentAmount)) return
Expand Down Expand Up @@ -131,6 +155,19 @@ function ReceiveInvoice({ recipientWalletCurrency, walletId, state, dispatch }:
}, 3000)
}

if ((errorString && !loading) || expiredInvoiceError) {
const invalidInvoiceError =
recipientWalletCurrency === "USD" && Number(amount?.toString()) <= 0
? "Enter an amount greater than 1 cent"
: expiredInvoiceError ?? null
return (
<div className={styles.error}>
<p>{errorString}</p>
<p>{invalidInvoiceError}</p>
</div>
)
}

if (loading || invoiceStatus === "loading" || !invoice?.paymentRequest) {
return (
<div className={styles.loading}>
Expand All @@ -151,9 +188,7 @@ function ReceiveInvoice({ recipientWalletCurrency, walletId, state, dispatch }:
</div>
)}
<div>
{error && <p className={styles.error}>{errorString}</p>}

{data && (
{data ? (
<>
<div
ref={qrImageRef}
Expand Down Expand Up @@ -206,7 +241,7 @@ function ReceiveInvoice({ recipientWalletCurrency, walletId, state, dispatch }:
</Share>
</div>
</>
)}
) : null}
</div>
<PaymentOutcome
paymentRequest={invoice?.paymentRequest}
Expand Down
11 changes: 10 additions & 1 deletion components/ParsePOSPayment/parse-payment.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@
display: flex;
justify-content: center;
align-items: center;
width: 270px;
width: 300px;
column-gap: 15px;
font-size: 14px;
font-weight: 300;
Expand Down Expand Up @@ -264,6 +264,15 @@
outline: 1px dashed rgba(0, 128, 0, 0.35);
}

.error {
flex-direction: column;
padding: 15% 1rem;
}

.error > p {
text-align: center;
}

@media (min-width: 760px) {
.loading {
padding: 1rem 0;
Expand Down

0 comments on commit 2ea32c5

Please sign in to comment.