-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(payments): Add webhook endpoint. Add payment intent
- Loading branch information
Showing
12 changed files
with
316 additions
and
74 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
prisma/migrations/20240321115301_payment_intents/migration.sql
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
Warnings: | ||
- A unique constraint covering the columns `[stripe_payment_intent_id]` on the table `User` will be added. If there are existing duplicate values, this will fail. | ||
*/ | ||
-- AlterTable | ||
ALTER TABLE "User" ADD COLUMN "stripePaymentDate" TIMESTAMP(3), | ||
ADD COLUMN "stripe_payment_intent_id" TEXT; | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "User_stripe_payment_intent_id_key" ON "User"("stripe_payment_intent_id"); |
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 |
---|---|---|
@@ -0,0 +1,70 @@ | ||
"use server"; | ||
|
||
import { PLANS } from "@/config/stripePlans"; | ||
import { getUserById } from "@/data/user"; | ||
import { currentUser } from "@/lib/auth"; | ||
import { stripe, getUserPaymentStatus } from "@/lib/stripe"; | ||
import { absoluteUrl } from "@/lib/utils"; | ||
|
||
type Tperiod = { | ||
period: "onetime" | "monthly" | "yearly"; | ||
}; | ||
|
||
export const createStripePayIntentSession = async ({ period }: Tperiod) => { | ||
try { | ||
const user = await currentUser(); | ||
if (!user) throw new Error("Unauthorized"); | ||
if (!user.id) throw new Error("Invalid user ID"); | ||
|
||
const dbUser = await getUserById(user.id); | ||
if (!dbUser) throw new Error("Unauthorized"); | ||
|
||
const billingUrl = absoluteUrl("/dash/billing"); | ||
const isPaidUser = await getUserPaymentStatus(); | ||
|
||
if (isPaidUser) { | ||
const stripeSession = await stripe.billingPortal.sessions.create({ | ||
customer: dbUser.stripeCustomerId!, | ||
return_url: billingUrl, | ||
}); | ||
return { url: stripeSession.url }; | ||
} | ||
|
||
const customer = await stripe.customers.create({ | ||
name: dbUser.name!, | ||
email: dbUser.email!, | ||
metadata: { | ||
userId: dbUser.id, | ||
}, | ||
}); | ||
|
||
const stripeSession = await stripe.checkout.sessions.create({ | ||
customer: customer.id, | ||
success_url: billingUrl, | ||
cancel_url: billingUrl, | ||
payment_method_types: ["card"], | ||
mode: "payment", | ||
billing_address_collection: "auto", | ||
line_items: [ | ||
{ | ||
// | ||
price: PLANS.find((plan) => plan.name === "Pro")?.price[ | ||
period === "onetime" ? "onetime" : period | ||
].priceIds.test, // will fix later | ||
quantity: 1, | ||
}, | ||
], | ||
metadata: { | ||
userId: dbUser.id, | ||
}, | ||
payment_intent_data: { | ||
metadata: { | ||
userId: dbUser.id, | ||
}, | ||
}, | ||
}); | ||
return { url: stripeSession.url }; | ||
} catch (error) { | ||
return { error: JSON.stringify(error) }; | ||
} | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { db } from "@/lib/db"; | ||
import { stripe } from "@/lib/stripe"; | ||
import { headers } from "next/headers"; | ||
import type Stripe from "stripe"; | ||
|
||
export async function POST(request: Request) { | ||
const body = await request.text(); | ||
const signature = headers().get("Stripe-Signature") ?? ""; | ||
|
||
let event: Stripe.Event; | ||
|
||
try { | ||
event = stripe.webhooks.constructEvent(body, signature, process.env.STRIPE_WEBHOOK_SECRET!); | ||
} catch (err) { | ||
return new Response(`Webhook Error: ${err instanceof Error ? err.message : "Unknown Error"}`, { | ||
status: 400, | ||
}); | ||
} | ||
|
||
const session = event.data.object as Stripe.Checkout.Session; | ||
|
||
if (!session?.metadata?.userId) { | ||
return new Response(null, { | ||
status: 500, | ||
}); | ||
} | ||
|
||
if (event.type === "payment_intent.succeeded") { | ||
// console.log(session) | ||
const paymentIntentId = session.id as string; | ||
// console.log("Payment Intent ID:", paymentIntentId); | ||
|
||
const paymentIntent = await stripe.paymentIntents.retrieve(paymentIntentId); | ||
// console.log("Payment Intent:", paymentIntent); | ||
|
||
const userId = session.metadata.userId; | ||
// console.log("User ID:", userId) | ||
|
||
const paymentDate = new Date(paymentIntent.created * 1000); // milliseconds since the Unix epoch | ||
// console.log("Payment Date:", paymentDate); | ||
|
||
await db.user.update({ | ||
where: { | ||
id: userId, | ||
}, | ||
data: { | ||
stripeCustomerId: session.customer as string, | ||
stripePriceId: session.line_items?.data[0].price?.id, | ||
stripePaymentIntentId: paymentIntentId, | ||
stripePaymentDate: paymentDate, | ||
}, | ||
}); | ||
} | ||
|
||
return new Response(null, { status: 200 }); | ||
} |
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,10 +1,13 @@ | ||
import BillingForm from "@/components/billing/Billing"; | ||
import { getUserSubscriptionPlan } from "@/lib/stripe"; | ||
import { getUserPaymentStatus } from "@/lib/stripe"; | ||
|
||
const Page = async () => { | ||
const subscriptionPlan = await getUserSubscriptionPlan(); | ||
// const subscriptionPlan = await getUserSubscriptionPlan(); | ||
|
||
return <BillingForm subscriptionPlan={subscriptionPlan} />; | ||
// return <BillingForm subscriptionPlan={subscriptionPlan} />; | ||
const isPaidUser = await getUserPaymentStatus(); | ||
|
||
return <BillingForm isPaidUser={isPaidUser} />; | ||
}; | ||
|
||
export default Page; |
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
Oops, something went wrong.