|
| 1 | +import { conform, useForm } from '@conform-to/react' |
| 2 | +import { getFieldsetConstraint, parse } from '@conform-to/zod' |
| 3 | +import { type WalletAddress } from '@interledger/open-payments' |
| 4 | +import { |
| 5 | + type ActionFunctionArgs, |
| 6 | + json, |
| 7 | + redirect, |
| 8 | + type LoaderFunctionArgs |
| 9 | +} from '@remix-run/node' |
| 10 | +import { Form, useActionData, useLoaderData } from '@remix-run/react' |
| 11 | +import { z } from 'zod' |
| 12 | +import { Header } from '~/components/header' |
| 13 | +import Quote from '~/components/quoteDialog' |
| 14 | +import { Button } from '~/components/ui/button' |
| 15 | +import { Field } from '~/components/ui/form/form' |
| 16 | +import { PayWithInterledgerMark } from '~/components/ui/logo' |
| 17 | +import { useDialogContext } from '~/lib/context/dialog' |
| 18 | +import { fetchQuote, initializePayment } from '~/lib/open-payments.server' |
| 19 | +import { getValidWalletAddress } from '~/lib/validators.server' |
| 20 | +import { commitSession, destroySession, getSession } from '~/session' |
| 21 | +import { formatAmount } from '~/utils/helpers' |
| 22 | + |
| 23 | +export async function loader({ request }: LoaderFunctionArgs) { |
| 24 | + const searchParams = new URL(request.url).searchParams |
| 25 | + const isQuote = searchParams.get('quote') || false |
| 26 | + const receiver = searchParams.get('receiver') || '' |
| 27 | + const session = await getSession(request.headers.get('Cookie')) |
| 28 | + |
| 29 | + let receiverName = '' |
| 30 | + let receiveAmount = null |
| 31 | + let debitAmount = null |
| 32 | + |
| 33 | + if (isQuote) { |
| 34 | + const quote = session.get('quote') |
| 35 | + const receiver = session.get('receiver-wallet-address') |
| 36 | + |
| 37 | + if (quote === undefined) { |
| 38 | + throw new Error('Payment session expired.') |
| 39 | + } |
| 40 | + |
| 41 | + receiverName = |
| 42 | + receiver.publicName === undefined ? 'Recepient' : receiver.publicName |
| 43 | + |
| 44 | + receiveAmount = formatAmount({ |
| 45 | + value: quote.receiveAmount.value, |
| 46 | + assetCode: quote.receiveAmount.assetCode, |
| 47 | + assetScale: quote.receiveAmount.assetScale |
| 48 | + }) |
| 49 | + |
| 50 | + debitAmount = formatAmount({ |
| 51 | + value: quote.debitAmount.value, |
| 52 | + assetCode: quote.debitAmount.assetCode, |
| 53 | + assetScale: quote.debitAmount.assetScale |
| 54 | + }) |
| 55 | + } else if (receiver !== '') { |
| 56 | + try { |
| 57 | + await getValidWalletAddress(receiver) |
| 58 | + } catch (error) { |
| 59 | + throw new Error( |
| 60 | + 'Receiver Wallet Address is not valid. Please check and try again.' |
| 61 | + ) |
| 62 | + } |
| 63 | + } else { |
| 64 | + return redirect('/', { |
| 65 | + headers: { 'Set-Cookie': await destroySession(session) } |
| 66 | + }) |
| 67 | + } |
| 68 | + |
| 69 | + return json({ |
| 70 | + receiver: receiver, |
| 71 | + receiveAmount: receiveAmount ? receiveAmount.amountWithCurrency : null, |
| 72 | + debitAmount: debitAmount ? debitAmount.amountWithCurrency : null, |
| 73 | + receiverName: receiverName, |
| 74 | + isQuote: isQuote |
| 75 | + } as const) |
| 76 | +} |
| 77 | + |
| 78 | +const schema = z.object({ |
| 79 | + receiver: z.string(), |
| 80 | + walletAddress: z |
| 81 | + .string() |
| 82 | + .transform((val) => val.replace('$', 'https://')) |
| 83 | + .pipe(z.string().url({ message: 'The input is not a wallet address.' })), |
| 84 | + amount: z.coerce.number(), |
| 85 | + note: z.string().optional() |
| 86 | +}) |
| 87 | + |
| 88 | +export default function PayWithInterledger() { |
| 89 | + const data = useLoaderData<typeof loader>() |
| 90 | + const actionData = useActionData<typeof action>() |
| 91 | + const { setOpen } = useDialogContext() |
| 92 | + const [form, fields] = useForm({ |
| 93 | + id: 'pay-with-interledger-form', |
| 94 | + constraint: getFieldsetConstraint(schema), |
| 95 | + lastSubmission: actionData, |
| 96 | + shouldRevalidate: 'onSubmit' |
| 97 | + }) |
| 98 | + |
| 99 | + data.isQuote ? setOpen(true) : setOpen(false) |
| 100 | + |
| 101 | + return ( |
| 102 | + <> |
| 103 | + <Header /> |
| 104 | + <div className="flex h-full flex-col justify-center gap-10"> |
| 105 | + <div className="mx-auto w-full max-w-sm"> |
| 106 | + <Form method="POST" {...form.props}> |
| 107 | + <div className="flex flex-col gap-4"> |
| 108 | + <Field |
| 109 | + label="Pay into Wallet Address" |
| 110 | + variant="highlight" |
| 111 | + {...conform.input(fields.receiver)} |
| 112 | + defaultValue={data.receiver} |
| 113 | + readOnly |
| 114 | + /> |
| 115 | + <Field |
| 116 | + label="Pay from" |
| 117 | + placeholder="Enter your wallet address" |
| 118 | + {...conform.input(fields.walletAddress)} |
| 119 | + errors={fields.walletAddress.errors} |
| 120 | + /> |
| 121 | + <Field |
| 122 | + label="Amount" |
| 123 | + placeholder="Amount" |
| 124 | + {...conform.input(fields.amount)} |
| 125 | + errors={fields.amount.errors} |
| 126 | + /> |
| 127 | + <Field |
| 128 | + label="Payment note" |
| 129 | + placeholder="Note" |
| 130 | + {...conform.input(fields.note)} |
| 131 | + errors={fields.note.errors} |
| 132 | + /> |
| 133 | + <div className="flex justify-center"> |
| 134 | + <Button |
| 135 | + aria-label="pay" |
| 136 | + type="submit" |
| 137 | + name="intent" |
| 138 | + value="pay" |
| 139 | + size="xl" |
| 140 | + > |
| 141 | + <span className="text-md">Pay with</span> |
| 142 | + <PayWithInterledgerMark className="h-8 w-40 mx-2" /> |
| 143 | + </Button> |
| 144 | + </div> |
| 145 | + </div> |
| 146 | + </Form> |
| 147 | + </div> |
| 148 | + <Quote |
| 149 | + receiverName={data.receiverName} |
| 150 | + receiveAmount={data.receiveAmount || ''} |
| 151 | + debitAmount={data.debitAmount || ''} |
| 152 | + /> |
| 153 | + </div> |
| 154 | + </> |
| 155 | + ) |
| 156 | +} |
| 157 | + |
| 158 | +export async function action({ request }: ActionFunctionArgs) { |
| 159 | + const session = await getSession(request.headers.get('Cookie')) |
| 160 | + |
| 161 | + let walletAddress = {} as WalletAddress |
| 162 | + let receiverWalletAddress = {} as WalletAddress |
| 163 | + |
| 164 | + const formData = await request.formData() |
| 165 | + const intent = formData.get('intent') |
| 166 | + |
| 167 | + if (intent === 'pay') { |
| 168 | + const submission = await parse(formData, { |
| 169 | + schema: schema.superRefine(async (data, context) => { |
| 170 | + try { |
| 171 | + walletAddress = await getValidWalletAddress(data.walletAddress) |
| 172 | + receiverWalletAddress = await getValidWalletAddress(data.receiver) |
| 173 | + } catch (error) { |
| 174 | + context.addIssue({ |
| 175 | + path: ['walletAddress'], |
| 176 | + code: z.ZodIssueCode.custom, |
| 177 | + message: 'Your wallet address is not valid.' |
| 178 | + }) |
| 179 | + } |
| 180 | + }), |
| 181 | + async: true |
| 182 | + }) |
| 183 | + |
| 184 | + if (!submission.value || submission.intent !== 'submit') { |
| 185 | + return json(submission) |
| 186 | + } |
| 187 | + |
| 188 | + const quote = await fetchQuote(submission.value, receiverWalletAddress) |
| 189 | + session.set('quote', quote) |
| 190 | + session.set('wallet-address', { |
| 191 | + walletAddress: walletAddress |
| 192 | + }) |
| 193 | + session.set('receiver-wallet-address', receiverWalletAddress) |
| 194 | + |
| 195 | + return redirect(`/pay-with-interledger?quote=true`, { |
| 196 | + headers: { 'Set-Cookie': await commitSession(session) } |
| 197 | + }) |
| 198 | + } else if (intent === 'confirm') { |
| 199 | + const quote = session.get('quote') |
| 200 | + const walletAddressInfo = session.get('wallet-address') |
| 201 | + |
| 202 | + if (quote === undefined || walletAddressInfo === undefined) { |
| 203 | + throw new Error('Payment session expired.') |
| 204 | + } |
| 205 | + |
| 206 | + const grant = await initializePayment({ |
| 207 | + walletAddress: walletAddressInfo.walletAddress.id, |
| 208 | + quote: quote |
| 209 | + }) |
| 210 | + |
| 211 | + session.set('payment-grant', grant) |
| 212 | + return redirect(grant.interact.redirect, { |
| 213 | + headers: { 'Set-Cookie': await commitSession(session) } |
| 214 | + }) |
| 215 | + } else { |
| 216 | + return redirect('/', { |
| 217 | + headers: { 'Set-Cookie': await destroySession(session) } |
| 218 | + }) |
| 219 | + } |
| 220 | +} |
0 commit comments