-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
414 additions
and
127 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,12 @@ | ||
import { createClient } from "@supabase/supabase-js"; | ||
|
||
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL ?? ""; | ||
const supabaseKey = process.env.NEXT_PUBLIC_SUPABASE_SERVICE_ROLE_KEY ?? ""; | ||
const supabaseUrl = | ||
process.env.NODE_ENV === "development" | ||
? "http://127.0.0.1:54321" | ||
: process.env.NEXT_PUBLIC_SUPABASE_URL ?? ""; | ||
const supabaseKey = | ||
(process.env.NODE_ENV === "development" | ||
? process.env.LOCAL_SUPABASE_SERVICE_ROLE_KEY | ||
: process.env.NEXT_PUBLIC_SUPABASE_SERVICE_ROLE_KEY) ?? ""; | ||
|
||
export const supabase = createClient(supabaseUrl, supabaseKey); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,105 @@ | ||
import { NextApiRequest, NextApiResponse } from "next"; | ||
import Stripe from "stripe"; | ||
import { supabase } from "../../lib/supabase"; | ||
import { buffer } from "micro"; | ||
import { RandomWordOptions, generateSlug } from "random-word-slugs"; | ||
|
||
const randomWordOptions: RandomWordOptions<4> = { | ||
format: "title", | ||
partsOfSpeech: ["adjective", "adjective", "noun", "noun"], | ||
}; // 9752352516 unique combinations | ||
|
||
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!, { | ||
apiVersion: "2023-10-16", | ||
}); | ||
|
||
const webhookSecret = | ||
process.env.LOCAL_STRIPE_WEBHOOK_SECRET ?? | ||
process.env.STRIPE_WEBHOOK_SECRET ?? | ||
""; | ||
|
||
export const config = { | ||
api: { | ||
bodyParser: false, | ||
}, | ||
}; | ||
|
||
export default async function webhookHandler( | ||
req: NextApiRequest, | ||
res: NextApiResponse | ||
) { | ||
console.log(req.body); | ||
res.status(200).json({ message: "Webhook received" }); | ||
if (req.method === "POST") { | ||
let event: Stripe.Event; | ||
const sig = req.headers["stripe-signature"]!; | ||
const buf = await buffer(req); | ||
|
||
try { | ||
const rawBody = await buffer(req); | ||
event = stripe.webhooks.constructEvent(rawBody, sig, webhookSecret); | ||
} catch (err) { | ||
const errorMessage = err instanceof Error ? err.message : "Unknown error"; | ||
console.log(`❌ Error: ${errorMessage}`); | ||
res.status(400).send(`Webhook Error: ${errorMessage}`); | ||
return; | ||
} | ||
|
||
console.log("✅ Success:", event.id); | ||
|
||
switch (event.type) { | ||
case "charge.succeeded": | ||
const charge = event.data.object as Stripe.Charge; | ||
console.log(`💵 Charge id: ${charge.id}`); | ||
|
||
const userEmail = charge.billing_details.email; | ||
|
||
console.log(charge.billing_details); | ||
// credit_amount follows SQL naming convention in this case to comply with Supabase Stored Procedures | ||
let credits = 0; | ||
|
||
// @ts-ignore | ||
switch (charge.amount) { | ||
case 500: | ||
credits = 25; | ||
break; | ||
case 2000: | ||
credits = 100; | ||
break; | ||
case 8000: | ||
credits = 500; | ||
break; | ||
} | ||
|
||
const { data: insertCodeData } = await supabase | ||
.from("credit_codes") | ||
.insert([ | ||
{ | ||
code: generateSlug(4, randomWordOptions), | ||
credits, | ||
}, | ||
]) | ||
.select(); | ||
|
||
await supabase.from("transactions").insert([ | ||
{ | ||
user_email: userEmail, | ||
credit_code_id: insertCodeData?.[0].id, | ||
stripe_charge_id: charge.id, | ||
amount: charge.amount, | ||
}, | ||
]); | ||
break; | ||
case "payment_intent.payment_failed": | ||
const paymentIntent = event.data.object as Stripe.PaymentIntent; | ||
console.log( | ||
`❌ Payment failed: ${paymentIntent.last_payment_error?.message}` | ||
); | ||
default: | ||
console.log(`Unhandled event type ${event.type}`); | ||
} | ||
|
||
res.json({ received: true }); | ||
} else { | ||
res.setHeader("Allow", "POST"); | ||
res.status(405).end("Method Not Allowed"); | ||
} | ||
} |
Oops, something went wrong.