/
[merchantChargeId].tsx
297 lines (281 loc) · 16.1 KB
/
[merchantChargeId].tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import * as React from 'react'
import {DownloadIcon} from '@heroicons/react/outline'
import {useLocalStorage} from 'react-use'
import {GetServerSideProps} from 'next'
import fromUnixTime from 'date-fns/fromUnixTime'
import Layout from 'components/app/layout'
import format from 'date-fns/format'
import {trpc} from '../../trpc/trpc.client'
import {Transfer} from '../../purchase-transfer/purchase-transfer'
import {MailIcon} from '@heroicons/react/solid'
import {z} from 'zod'
import {useRouter} from 'next/router'
export const getServerSideProps: GetServerSideProps = async ({query}) => {
const {merchantChargeId} = z
.object({merchantChargeId: z.string()})
.parse(query)
return {
props: {
merchantChargeId,
},
}
}
const Invoice: React.FC<
React.PropsWithChildren<{
merchantChargeId: string
}>
> = ({merchantChargeId}) => {
const [invoiceMetadata, setInvoiceMetadata] = useLocalStorage(
'invoice-metadata',
'',
)
const [isMounted, setIsMounted] = React.useState(false)
React.useEffect(() => {
setIsMounted(true)
}, [])
const router = useRouter()
const {data: chargeDetails, status} = trpc.invoices.getChargeDetails.useQuery(
{
merchantChargeId,
},
)
React.useEffect(() => {
if (chargeDetails?.state !== 'SUCCESS' && status !== 'loading') {
router.push('/invoices')
}
}, [status, chargeDetails?.state])
const {data: purchaseUserTransfers, refetch} =
trpc.purchaseUserTransfer.forPurchaseId.useQuery({
id: chargeDetails?.result?.purchaseId,
})
if (chargeDetails?.state !== 'SUCCESS') {
return null
}
const {charge, product, bulkCoupon, quantity} = chargeDetails.result
const customer = z
.object({name: z.string().nullish(), email: z.string().nullish()})
.parse(charge.customer)
const formatUsd = (amount: number) => {
return Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
}).format(amount)
}
const created = fromUnixTime(charge.created)
const date = format(created, 'MMMM d, y')
const amount = charge.amount / 100
const instructorName = `${process.env.NEXT_PUBLIC_PARTNER_FIRST_NAME} ${process.env.NEXT_PUBLIC_PARTNER_LAST_NAME}`
const productName = `${process.env.NEXT_PUBLIC_SITE_TITLE} by ${instructorName}`
const emailData =
isMounted &&
`mailto:?subject=Invoice for ${process.env.NEXT_PUBLIC_SITE_TITLE}&body=Invoice for ${process.env.NEXT_PUBLIC_HOST} purchase: ${window.location}`
return (
<Layout
meta={{title: `Invoice ${merchantChargeId}`}}
className="print:bg-white print:text-black"
>
<main className="mx-auto max-w-screen-md">
<div className="flex flex-col justify-between pb-5 pt-12 print:hidden">
<h1 className="font-text text-lg font-bold leading-tight sm:text-xl">
Your Invoice for {process.env.NEXT_PUBLIC_SITE_TITLE}
</h1>
<div className="flex flex-col items-center gap-2 pt-3 sm:flex-row">
<button
onClick={() => {
window.print()
}}
className="flex items-center rounded-md bg-primary px-3 py-2 text-sm font-semibold leading-6 text-white transition-colors duration-200 ease-in-out"
>
<span className="pr-2">Download PDF or Print</span>
<DownloadIcon aria-hidden="true" className="w-5" />
</button>
{emailData && (
<a
href={emailData}
className="flex items-center rounded-md bg-gray-200 px-3 py-2 text-sm font-semibold leading-6 transition-colors duration-200 ease-in-out dark:bg-gray-800"
>
<span className="pr-2">Send via email</span>
<MailIcon aria-hidden="true" className="w-5" />
</a>
)}
</div>
</div>
<div className="rounded-t-md bg-white pr-12 text-gray-900 shadow-xl print:shadow-none">
<div className="px-10 py-16">
<div className="grid w-full grid-cols-3 items-start justify-between ">
<div className="col-span-2 flex items-center">
<span className="font-text pl-2 text-2xl font-bold">
{/* {process.env.NEXT_PUBLIC_SITE_TITLE} */}
<Logo className="w-[200px]" />
</span>
</div>
<div>
<h2 className="mb-2 text-xs uppercase text-gray-500">From</h2>
<span className="font-semibold">{productName}</span>
<br />
co Skill Recordings Inc.
<br />
12333 Sowden Rd
<br />
Ste. B, PMB #97429
<br />
Houston, TX 77080-2059
<br />
972-992-5951
</div>
</div>
<div className="grid grid-cols-3 pb-64">
<div className="col-span-2">
<p className="mb-2 text-2xl font-bold">Invoice</p>
Invoice ID: <strong>{merchantChargeId}</strong>
<br />
Created: <strong>{date}</strong>
<br />
Status:{' '}
<strong>
{charge.status === 'succeeded'
? charge.refunded
? 'Refunded'
: 'Paid'
: 'Pending'}
</strong>
</div>
<div className="pt-12">
<h2 className="mb-2 text-xs uppercase text-gray-500">
Invoice For
</h2>
<div>
{customer.name}
<br />
{customer.email}
{/* <br />
{charge.billing_details.address?.city}
<br />
{charge.billing_details.address?.postal_code}
<br />
{charge.billing_details.address?.country} */}
</div>
{isMounted && (
<>
<textarea
aria-label="Invoice notes"
className="form-textarea mt-4 h-full w-full rounded-md border-2 border-primary bg-gray-50 p-3 placeholder-gray-700 print:hidden print:border-none print:bg-transparent print:p-0"
value={invoiceMetadata}
onChange={(e) => setInvoiceMetadata(e.target.value)}
placeholder="Enter additional info here (optional)"
/>
<div className="hidden print:block">{invoiceMetadata}</div>
</>
)}
</div>
</div>
<h2 className="sr-only">Purchase details</h2>
<table className="w-full table-auto text-left">
<thead className="table-header-group">
<tr className="table-row">
<th scope="col">Description</th>
<th scope="col">Unit Price</th>
<th scope="col">Quantity</th>
<th scope="col" className="text-right">
Amount
</th>
</tr>
</thead>
<tbody>
{quantity ? (
<tr className="table-row">
<td>{product.name}</td>
<td>
{charge.currency.toUpperCase()}{' '}
{formatUsd(charge.amount / 100 / quantity)}
</td>
<td>{quantity}</td>
<td className="text-right">
{amount === null
? `${charge.currency.toUpperCase()} 0.00`
: `${charge.currency.toUpperCase()} ${formatUsd(
amount,
)}`}
</td>
</tr>
) : (
<tr className="table-row">
<td>{product.name}</td>
<td>
{charge.currency.toUpperCase()} {formatUsd(amount)}
</td>
<td>1</td>
<td className="text-right">
{amount === null
? `${charge.currency.toUpperCase()} 0.00`
: `${charge.currency.toUpperCase()} ${formatUsd(
amount,
)}`}
</td>
</tr>
)}
</tbody>
</table>
<div className="flex flex-col items-end py-16">
<div>
<span className="mr-3">Total</span>
<strong className="text-lg">
{charge.currency.toUpperCase()} {formatUsd(amount)}
</strong>
</div>
</div>
</div>
</div>
{!bulkCoupon && purchaseUserTransfers ? (
<div className="py-16 print:hidden">
<Transfer
purchaseUserTransfers={purchaseUserTransfers}
refetch={refetch}
/>
</div>
) : null}
</main>
</Layout>
)
}
export default Invoice
const Logo: React.FC<{className?: string}> = ({className}) => {
return (
<svg
xmlns="http://www.w3.org/2000/svg"
className="w-[200px]"
fill="none"
viewBox="0 0 264 70"
>
<g fill="currentColor">
<path d="M92.593 36.536v6.189h9.061v2.453H90V25.662h11.515v2.454h-8.922v5.994h8.225v2.426h-8.225Z"></path>
<path
fillRule="evenodd"
d="M109.942 25.662h7.081c1.813 0 3.29.614 4.489 1.813 1.199 1.198 1.812 2.676 1.812 4.46 0 1.757-.613 3.262-1.812 4.461-1.199 1.199-2.676 1.812-4.489 1.812h-4.488v6.97h-2.593V25.662Zm2.593 10.12h4.488c1.088 0 1.98-.362 2.677-1.086.697-.753 1.059-1.673 1.059-2.76 0-2.231-1.589-3.848-3.736-3.848h-4.488v7.695Z"
clipRule="evenodd"
/>
<path d="M131.126 43.379V27.342h-3.089v-1.828h8.809v1.828h-3.127V43.38h3.127v1.828h-8.809V43.38h3.089Zm21.11 2.161c-2.955 0-5.409-.975-7.332-2.927-1.924-1.951-2.872-4.349-2.872-7.193 0-2.843.948-5.241 2.872-7.193 1.923-1.951 4.377-2.927 7.332-2.927 3.569 0 6.747 1.84 8.42 4.74l-2.259 1.31c-1.115-2.147-3.485-3.54-6.161-3.54-2.258 0-4.098.724-5.52 2.174-1.422 1.45-2.119 3.262-2.119 5.436 0 2.147.697 3.96 2.119 5.41 1.422 1.449 3.262 2.174 5.52 2.174 2.676 0 5.046-1.394 6.161-3.513l2.259 1.282c-.809 1.45-1.98 2.621-3.485 3.485-1.506.865-3.151 1.283-4.935 1.283Z" />
<path
fillRule="evenodd"
d="M101.654 42.725v2.453H90V25.662h11.515v2.454h-8.922v5.994h8.225v2.426h-8.225v6.189h9.061Zm-8.761-.3h9.061v3.053H89.7V25.362h12.115v3.054h-8.922v5.394h8.225v3.026h-8.225v5.589Zm16.749-17.063h7.381c1.889 0 3.444.643 4.701 1.9 1.257 1.257 1.9 2.812 1.9 4.673 0 1.834-.643 3.416-1.9 4.673-1.257 1.257-2.812 1.9-4.701 1.9h-4.188v6.97h-3.193V25.362Zm2.893 19.816h-2.593V25.662h7.081c1.813 0 3.29.614 4.489 1.813 1.199 1.198 1.812 2.676 1.812 4.46 0 1.757-.613 3.262-1.812 4.461-1.199 1.199-2.676 1.812-4.489 1.812h-4.488v6.97Zm15.502-17.836v-1.828h8.809v1.828h-3.127V43.38h3.127v1.828h-8.809V43.38h3.089V27.342h-3.089Zm2.789.3h-3.089v-2.428h9.409v2.428h-3.127V43.08h3.127v2.428h-9.409V43.08h3.089V27.642Zm30.239 2.507-2.789 1.618-.145-.279c-1.059-2.038-3.32-3.379-5.895-3.379-2.188 0-3.947.7-5.306 2.085-1.362 1.388-2.033 3.123-2.033 5.226 0 2.076.671 3.81 2.033 5.199 1.359 1.386 3.118 2.085 5.306 2.085 2.575 0 4.837-1.341 5.896-3.353l.144-.274 2.787 1.583-.145.26c-.837 1.499-2.047 2.708-3.598 3.599-1.554.891-3.25 1.322-5.084 1.322-3.028 0-5.561-1.003-7.546-3.017-1.982-2.01-2.958-4.485-2.958-7.404 0-2.918.976-5.392 2.958-7.403 1.985-2.014 4.518-3.017 7.546-3.017 3.672 0 6.951 1.894 8.68 4.89l.149.259Zm-48.23 5.334h4.188c1.012 0 1.823-.334 2.459-.993.643-.696.977-1.542.977-2.555 0-2.072-1.462-3.547-3.436-3.547h-4.188v7.095Zm6.865-.787c-.697.724-1.589 1.087-2.677 1.087h-4.488v-7.695h4.488c2.147 0 3.736 1.617 3.736 3.847 0 1.088-.362 2.008-1.059 2.76Zm40.805 6.337c-.8 1.33-1.92 2.413-3.334 3.225-1.506.865-3.151 1.283-4.935 1.283-2.955 0-5.409-.976-7.332-2.928-1.924-1.951-2.872-4.349-2.872-7.193 0-2.843.948-5.241 2.872-7.193 1.923-1.951 4.377-2.927 7.332-2.927 3.462 0 6.556 1.732 8.266 4.482l.131.218.023.04-2.259 1.31a6.226 6.226 0 0 0-.144-.263c-1.168-1.997-3.451-3.278-6.017-3.278-2.258 0-4.098.725-5.52 2.175-1.422 1.45-2.119 3.262-2.119 5.436 0 2.147.697 3.96 2.119 5.41 1.422 1.449 3.262 2.174 5.52 2.174 2.565 0 4.848-1.28 6.015-3.251.051-.086.1-.173.146-.262l2.259 1.282-.07.123c-.027.046-.053.092-.081.137Z"
clipRule="evenodd"
/>
<path d="M174.058 45.178h-3.011l-5.604-19.516h2.732l4.461 16.06 4.739-16.06h2.621l4.74 16.06 4.46-16.06h2.733l-5.604 19.516h-3.011l-4.628-15.585-4.628 15.585Zm27.294-8.643v6.19h9.061v2.453h-11.654V25.662h11.515v2.454h-8.922v5.994h8.225v2.425h-8.225Zm28.222-1.477c1.784.864 2.928 2.509 2.928 4.6 0 1.561-.558 2.872-1.645 3.931-1.088 1.06-2.426 1.59-4.015 1.59h-8.42V25.661h7.807c1.533 0 2.815.502 3.875 1.534 1.059 1.031 1.589 2.286 1.589 3.791 0 1.729-.697 3.095-2.119 4.07Zm-3.345-6.97h-5.214v5.966h5.214c1.644 0 2.899-1.31 2.899-2.983 0-.809-.279-1.506-.864-2.091-.558-.585-1.227-.892-2.035-.892Zm-5.214 14.665h5.827c1.756 0 3.095-1.394 3.095-3.179 0-.864-.307-1.617-.92-2.23-.586-.613-1.311-.92-2.175-.92h-5.827v6.329Z" />
<path
fillRule="evenodd"
d="M174.281 45.478h-3.46l-5.777-20.116h3.359l4.241 15.27 4.507-15.27h3.069l4.507 15.27 4.241-15.27h3.359l-5.776 20.116h-3.461l-4.404-14.83-4.405 14.83Zm-.223-.3h-3.011l-5.604-19.516h2.732l4.461 16.06 4.739-16.06h2.621l4.74 16.06 4.46-16.06h2.733l-5.604 19.516h-3.011l-4.628-15.585-4.628 15.585Zm36.655-2.753v3.053h-12.254V25.362h12.115v3.054h-8.922v5.394h8.225v3.026h-8.225v5.589h9.061Zm-9.361.3v-6.19h8.225V34.11h-8.225v-5.994h8.922v-2.454h-11.515v19.516h11.654v-2.453h-9.061Zm16.77 2.753V25.362h8.107c1.607 0 2.966.53 4.084 1.619 1.118 1.088 1.68 2.42 1.68 4.006 0 1.66-.615 3.019-1.861 4.029 1.635.957 2.67 2.6 2.67 4.642 0 1.642-.59 3.03-1.736 4.146-1.146 1.117-2.561 1.674-4.224 1.674h-8.72Zm11.752-10.264a6.066 6.066 0 0 0-.3-.156c.094-.065.185-.131.273-.2 1.238-.958 1.846-2.256 1.846-3.87 0-1.506-.53-2.76-1.589-3.792-1.06-1.032-2.342-1.534-3.875-1.534h-7.807v19.516h8.42c1.589 0 2.927-.53 4.015-1.589 1.087-1.06 1.645-2.37 1.645-3.931 0-1.971-1.017-3.546-2.628-4.444Zm-1.827-6.027c-.505-.53-1.097-.8-1.818-.8h-4.914v5.367h4.914c1.469 0 2.599-1.166 2.599-2.683 0-.73-.248-1.35-.776-1.879l-.005-.005Zm-1.818 4.867c1.644 0 2.899-1.31 2.899-2.983 0-.809-.279-1.506-.864-2.091-.558-.585-1.227-.892-2.035-.892h-5.214v5.966h5.214Zm2.575 3.502-.005-.005c-.527-.553-1.173-.827-1.957-.827h-5.527v5.729h5.527c1.582 0 2.795-1.251 2.795-2.879a2.75 2.75 0 0 0-.833-2.018Zm-7.789 5.197v-6.329h5.827c.864 0 1.589.307 2.175.92.613.613.92 1.366.92 2.23 0 1.785-1.339 3.179-3.095 3.179h-5.827Z"
clipRule="evenodd"
/>
<path d="M239.041 32.853a.89.89 0 0 1-1.254 0 .89.89 0 0 1 0-1.254.89.89 0 0 1 1.254 0 .889.889 0 0 1 0 1.254Zm2.753-7.187h2.904c1.003 0 1.839.355 2.508 1.055.679.7 1.013 1.567 1.013 2.6 0 1.035-.334 1.902-1.013 2.602-.669.7-1.505 1.055-2.508 1.055h-2.904v-7.313Zm1.442 5.933h1.462c.638 0 1.15-.209 1.547-.627.397-.428.595-.971.595-1.65 0-.68-.198-1.223-.595-1.64-.397-.429-.909-.637-1.547-.637h-1.462v4.554Zm8.753-1.64v1.64h3.082v1.379h-4.524v-7.313h4.471v1.38h-3.029V28.6h2.768v1.358h-2.768Zm8.997 3.019H259.2l-2.455-7.313h1.567l1.786 5.62 1.776-5.62h1.578l-2.466 7.313Z" />
</g>
<path
fill="#30AFFF"
d="M36.277 33.738a64.504 64.504 0 0 1-4.257 2.15c-6.333 2.912-15.383 5.86-26.228 5.981l-1.249.014-.226-1.228a31.016 31.016 0 0 1-.531-5.638C3.786 17.804 17.787 3.802 35 3.802a31.05 31.05 0 0 1 13.295 2.975l4.146-2.113A34.774 34.774 0 0 0 35 0C15.712 0 0 15.712 0 35c0 7.7 2.504 14.83 6.74 20.617 7.252-1.235 11.802-4.14 11.802-4.14s-2.905 4.544-4.14 11.798A34.803 34.803 0 0 0 35 70c19.288 0 35-15.712 35-35a34.778 34.778 0 0 0-4.652-17.42l-2.11 4.138a31.037 31.037 0 0 1 2.976 13.299C66.214 52.23 52.213 66.23 35 66.23c-1.942 0-3.804-.196-5.635-.53l-1.231-.225.014-1.251c.12-10.854 3.069-19.903 5.98-26.234a64.386 64.386 0 0 1 2.149-4.253Z"
/>
<path
fill="currentColor"
d="m53.235 27.155-8.03-2.344-2.345-8.047L69.5.5 53.235 27.155Z"
/>
</svg>
)
}