-
Notifications
You must be signed in to change notification settings - Fork 30
/
purchase.tsx
282 lines (258 loc) · 8.12 KB
/
purchase.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
import * as React from 'react'
import {GetServerSideProps} from 'next'
import Layout from '@/components/layout'
import {
convertToSerializeForNextResponse,
determinePurchaseType,
type PurchaseType,
} from '@skillrecordings/commerce-server'
import type {SanityProduct} from '@skillrecordings/commerce-server/dist/@types'
import {
EXISTING_BULK_COUPON,
INDIVIDUAL_TO_BULK_UPGRADE,
NEW_BULK_COUPON,
NEW_INDIVIDUAL_PURCHASE,
} from '@skillrecordings/types'
import {getSdk, Purchase} from '@skillrecordings/database'
import CopyInviteLink from '@skillrecordings/skill-lesson/team/copy-invite-link'
import Image from 'next/legacy/image'
import Balancer from 'react-wrap-balancer'
import {first} from 'lodash'
import {getAllProducts} from '@/server/products.server'
import {type SanityDocument} from '@sanity/client'
import {InvoiceCard} from '@/pages/invoices'
import {MailIcon} from '@heroicons/react/solid'
import {paymentOptions} from '../api/skill/[...skillRecordings]'
export const getServerSideProps: GetServerSideProps = async (context) => {
const {query} = context
const session_id =
query.session_id instanceof Array ? query.session_id[0] : query.session_id
const paymentProvider = paymentOptions.providers.stripe
if (!session_id || !paymentProvider) {
return {
notFound: true,
}
}
const purchaseInfo = await paymentProvider.getPurchaseInfo(session_id)
const {
email,
chargeIdentifier,
quantity: seatsPurchased,
product,
} = purchaseInfo
const stripeProductName = product.name
const purchase = await getSdk().getPurchaseForStripeCharge(chargeIdentifier)
if (!purchase || !email) {
return {
notFound: true,
}
}
const purchaseType = await determinePurchaseType({
checkoutSessionId: session_id,
})
const products = await getAllProducts()
const purchasedProduct = first(
products.filter(
(product: SanityProduct) => product.productId === purchase.productId,
),
)
return {
props: {
purchase: convertToSerializeForNextResponse(purchase),
email,
seatsPurchased,
purchaseType,
bulkCouponId: purchase.bulkCoupon?.id || null,
product: purchasedProduct || null,
stripeProductName,
},
}
}
const InlineTeamInvite = ({
bulkCouponId,
seatsPurchased,
}: {
bulkCouponId?: string
seatsPurchased: number
}) => {
if (!bulkCouponId) return null
return (
<div className="mx-auto w-full">
<h2 className="pb-2 font-heading text-sm font-black uppercase">
Invite your team
</h2>
<div className="flex flex-col rounded-lg border border-gray-100 bg-white p-5 shadow-xl shadow-gray-400/5">
<p className="pb-2 font-semibold">
You have purchased {seatsPurchased} seats.
</p>
<p className="pb-2">
Invite your team to claim seats right away with this invite link.
Don't worry about saving this anywhere, it will always be available on
your{' '}
<a
className="font-semibold underline"
href={`${process.env.NEXT_PUBLIC_URL}/team`}
>
Team page
</a>{' '}
once you sign in.
</p>
<CopyInviteLink bulkCouponId={bulkCouponId} />
</div>
</div>
)
}
type ThankYouProps = {
email: string
product: SanityDocument
title: string
byline: JSX.Element | null
}
const ThankYou: React.FC<ThankYouProps> = ({title, byline, product, email}) => {
return (
<header className="mx-auto w-full">
<div className="flex flex-col items-center gap-10 sm:flex-row">
{product?.image?.url && (
<div className="flex flex-shrink-0 items-center justify-center">
<Image
src={product.image.url}
alt={product.title}
width={250}
height={250}
/>
</div>
)}
<div className="flex flex-col items-start">
<h1 className="font-heading text-3xl font-black sm:text-3xl lg:text-4xl">
<span className="block pb-4 font-heading text-sm font-black uppercase text-brand-red">
Success!
</span>
{title}
</h1>
<p className="pt-5 font-medium">{byline}</p>
</div>
</div>
</header>
)
}
const LoginLink: React.FC<{email: string}> = ({email}) => {
return (
<div className="relative mx-auto flex w-full items-center justify-between gap-5 overflow-hidden rounded-xl bg-brand-red p-7 shadow-2xl shadow-gray-400/20 selection:bg-white selection:text-brand-red sm:p-12">
<div className="relative z-10">
<p className="inline-flex rounded-full bg-white px-3 py-1 font-heading text-xs font-black uppercase text-brand-red sm:text-sm">
Final step
</p>
<h2 className="mx-auto py-5 font-heading text-2xl font-black sm:text-3xl lg:text-4xl">
<Balancer>
Please check your inbox for a <i>login link</i> that just got sent.
</Balancer>
</h2>
<div className="mb-3 inline-flex items-center gap-1 rounded-lg bg-white/20 px-4 py-3">
<MailIcon className="h-5 w-5 flex-shrink-0" />{' '}
<strong className="inline-block break-all font-semibold">
Email sent to: {email}
</strong>
</div>
<p className="mx-auto text-sm font-medium leading-relaxed sm:text-base">
As a final step to access the course you need to check your inbox (
<strong>{email}</strong>) where you will find an email from{' '}
<strong>{process.env.NEXT_PUBLIC_SUPPORT_EMAIL}</strong> with a link
to access your purchase and start learning.
</p>
</div>
</div>
)
}
const ThanksVerify: React.FC<
React.PropsWithChildren<{
email: string
seatsPurchased: number
purchaseType: PurchaseType
bulkCouponId: string
product: SanityDocument
stripeProductName: string
purchase: Purchase
}>
> = ({
email,
seatsPurchased,
purchaseType,
bulkCouponId,
product,
stripeProductName,
purchase,
}) => {
let byline = null
let title = `Thank you for purchasing ${stripeProductName}`
let loginLink = null
let inviteTeam = (
<InlineTeamInvite
bulkCouponId={bulkCouponId}
seatsPurchased={seatsPurchased}
/>
)
switch (purchaseType) {
case NEW_INDIVIDUAL_PURCHASE:
loginLink = LoginLink
break
case NEW_BULK_COUPON:
byline = (
<>
Your purchase is for <strong>{seatsPurchased}</strong> seat
{seatsPurchased > 1 && 's'}. You can always add more seats later when
your team grows.
</>
)
loginLink = LoginLink
break
case EXISTING_BULK_COUPON:
title = `Thank you for purchasing more seats for ${
product?.name || process.env.NEXT_PUBLIC_SITE_TITLE
}!`
byline = (
<>
Your purchase is for <strong>{seatsPurchased}</strong> additional seat
{seatsPurchased > 1 && 's'}. You can always add more seats later when
your team grows.
</>
)
break
case INDIVIDUAL_TO_BULK_UPGRADE:
title = `Thank you for purchasing more seats for ${
product?.name || process.env.NEXT_PUBLIC_SITE_TITLE
}!`
byline = (
<>
Your purchase is for <strong>{seatsPurchased}</strong> additional seat
{seatsPurchased > 1 && 's'}. You can always add more seats later when
your team grows.
</>
)
break
}
return (
<>
<Layout footer={null} meta={{title: 'Purchase Successful'}}>
<main className="mx-auto flex w-full max-w-screen-md flex-col gap-8 px-5 py-10">
<ThankYou
title={title}
byline={byline}
product={product}
email={email}
/>
{inviteTeam && inviteTeam}
{loginLink && loginLink({email})}
<div>
<h2 className="pb-2 font-heading text-sm font-black uppercase">
Get your invoice
</h2>
<InvoiceCard
purchase={{product: {name: stripeProductName}, ...purchase}}
/>
</div>
</main>
</Layout>
</>
)
}
export default ThanksVerify