-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathpayment-confirmation.tsx
More file actions
108 lines (94 loc) · 2.84 KB
/
payment-confirmation.tsx
File metadata and controls
108 lines (94 loc) · 2.84 KB
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
import { useEffect, useRef } from 'react'
import {
useLoaderData,
data,
type LoaderFunctionArgs,
type MetaFunction,
} from 'react-router'
import { KV_PAYMENTS_PREFIX } from '@shared/types'
import { validatePaymentParams } from '~/utils/validate.server'
export const meta: MetaFunction = () => {
return [
{ title: 'Grant Interaction' },
{ name: 'description', content: 'Interaction success' },
]
}
export async function loader({ request, context }: LoaderFunctionArgs) {
const { env } = context.cloudflare
const url = new URL(request.url)
const params = Object.fromEntries([...url.searchParams])
const validation = validatePaymentParams(params)
if (!validation.success) {
return data({
success: false,
error: 'Invalid parameters',
params,
})
}
const { paymentId } = validation.data
try {
const existingData = await env.PUBLISHER_TOOLS_KV.get(
KV_PAYMENTS_PREFIX + paymentId,
)
if (existingData) {
// avoids spamming the KV store with redundant entries for the same payment
return data({ success: true, message: 'Already stored', params })
}
await env.PUBLISHER_TOOLS_KV.put(
KV_PAYMENTS_PREFIX + paymentId,
JSON.stringify(params),
{
expirationTtl: 300, // 5min
},
)
return data({ success: true, params })
} catch {
return data(
{ success: false, error: 'Failed to store data', params },
{ status: 500 },
)
}
}
export default function PaymentComplete() {
const { params } = useLoaderData<typeof loader>()
const hasPostedMessage = useRef(false)
useEffect(() => {
if (hasPostedMessage.current) return
hasPostedMessage.current = true
if (window.opener) {
window.opener.postMessage({ type: 'GRANT_INTERACTION', ...params }, '*')
}
}, [])
return (
<div className="min-h-screen flex items-center justify-center bg-gray-50">
<div className="max-w-md w-full space-y-8 p-8">
<div className="text-center">
<div className="mx-auto flex items-center justify-center h-24 w-24 rounded-full bg-green-100 mb-6">
<svg
className="h-12 w-12 text-green-600"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
xmlns="http://www.w3.org/2000/svg"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M5 13l4 4L19 7"
/>
</svg>
</div>
<h2 className="text-3xl font-bold text-gray-900 mb-4">
Payment Complete!
</h2>
<p className="text-gray-600 mb-8">
Your payment has been processed successfully.
<br />
You can now close this window.
</p>
</div>
</div>
</div>
)
}