forked from learn-anything/learn-anything
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstripe.ts
67 lines (64 loc) · 1.74 KB
/
stripe.ts
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
import { Resolver } from "@grafbase/generated"
import { GraphQLError } from "graphql"
import Stripe from "stripe"
import { hankoIdFromToken } from "../lib/hanko-validate"
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!, {
apiVersion: "2023-10-16",
typescript: true
})
// TODO:
// @ts-ignore
const StripeResolver: Resolver["Query.stripe"] = async (
parent,
args,
context,
info
) => {
try {
const hankoId = await hankoIdFromToken(context)
if (hankoId) {
switch (args.plan) {
case "month":
const monthSubscription = await stripe.checkout.sessions.create({
success_url: process.env.STRIPE_SUCCESS_URL!,
mode: "subscription",
metadata: {
userEmail: args.userEmail
},
line_items: [
{
quantity: 1,
price: process.env.STRIPE_MONTH_SUBSCRIPTION!
}
]
})
return monthSubscription.url
case "year":
const yearSubscription = await stripe.checkout.sessions.create({
success_url: process.env.STRIPE_SUCCESS_URL!,
mode: "subscription",
metadata: {
userEmail: args.userEmail
},
line_items: [
{
quantity: 1,
price: process.env.STRIPE_YEAR_SUBSCRIPTION!
}
]
})
return yearSubscription.url
default:
return {
stripeCheckoutUrl: null
}
}
} else {
throw new GraphQLError("Missing or invalid Authorization header")
}
} catch (err) {
console.error(err)
throw new GraphQLError(JSON.stringify(err))
}
}
export default StripeResolver