-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsuccess.astro
More file actions
65 lines (53 loc) · 1.65 KB
/
success.astro
File metadata and controls
65 lines (53 loc) · 1.65 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
---
import Layout from "@/layouts/Layout.astro"
import Stripe from "stripe"
import { centsToNanoUSD, centsToString } from "@/lib/utils"
const sessionId = Astro.params.id
if (!sessionId) {
return new Response("Missing sessionId", { status: 400 })
}
const stripe = new Stripe(Astro.locals.runtime.env.STRIPE_SECRET_KEY)
const session = await stripe.checkout.sessions.retrieve(sessionId)
console.log(session)
if (session.payment_status !== "paid" || session.status !== "complete") {
return new Response("Payment not completed", { status: 400 })
}
// TODO:
// https://docs.stripe.com/api/checkout/sessions/object#checkout_session_object-currency_conversion
if (session.currency !== "usd") {
return new Response("Invalid currency", { status: 400 })
}
if (!session.metadata?.appId) {
return new Response("Invalid sessionId", { status: 400 })
}
if (!session.amount_total) {
return new Response("Missing amount_total", { status: 400 })
}
const { appId, userId } = session.metadata
if (
Astro.locals.auth().userId &&
userId &&
userId !== Astro.locals.auth().userId
) {
return new Response("Forbidden", { status: 403 })
}
await Astro.locals.db.payments.create({
appId,
userId,
stripeCheckoutSessionId: sessionId,
amount: session.amount_total,
})
const billingId: DurableObjectId =
Astro.locals.runtime.env.BILLING_DURABLE_OBJECT.idFromName(appId)
const billing = Astro.locals.runtime.env.BILLING_DURABLE_OBJECT.get(billingId)
await billing.addWorkerFunds(centsToNanoUSD(session.amount_total))
---
<Layout>
<main>
<p>
Added {centsToString(session.amount_total)} to <a href=`/apps/${appId}`
>{appId}</a
>
</p>
</main>
</Layout>