-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: create UI for subscription functionality * feat: create subscription functionality by using stripe
- Loading branch information
Showing
13 changed files
with
269 additions
and
28 deletions.
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,37 @@ | ||
import { NextResponse } from 'next/server'; | ||
import { getServerSession } from 'next-auth'; | ||
|
||
import { authOptions } from '@/app/api/auth/[...nextauth]/auth-options'; | ||
import { env } from '@/env.mjs'; | ||
import { stripeServer } from '@/lib/stripe'; | ||
|
||
export const GET = async () => { | ||
const session = await getServerSession(authOptions); | ||
|
||
if (!session?.user) { | ||
return NextResponse.json( | ||
{ | ||
error: { | ||
code: 'no-access', | ||
message: 'You are not signed in.', | ||
}, | ||
}, | ||
{ status: 401 } | ||
); | ||
} | ||
|
||
const checkoutSession = await stripeServer.checkout.sessions.create({ | ||
mode: 'subscription', | ||
customer: session.user.stripeCustomerId, | ||
line_items: [ | ||
{ | ||
price: env.STRIPE_SUBSCRIPTION_PRICE_ID, | ||
quantity: 1, | ||
}, | ||
], | ||
success_url: `${env.NEXT_PUBLIC_SITE_URL}?session_id={CHECKOUT_SESSION_ID}`, | ||
cancel_url: env.NEXT_PUBLIC_SITE_URL, | ||
}); | ||
|
||
return NextResponse.json({ session: checkoutSession }, { 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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
import Stripe from 'stripe'; | ||
|
||
import { env } from '@/env.mjs'; | ||
import prisma from '@/lib/prisma'; | ||
import { stripeServer } from '@/lib/stripe'; | ||
|
||
const webhookHandler = async (req: NextRequest) => { | ||
try { | ||
const buf = await req.text(); | ||
const sig = req.headers.get('stripe-signature')!; | ||
|
||
let event: Stripe.Event; | ||
|
||
try { | ||
event = stripeServer.webhooks.constructEvent( | ||
buf, | ||
sig, | ||
env.STRIPE_WEBHOOK_SECRET_KEY | ||
); | ||
} catch (err) { | ||
return NextResponse.json( | ||
{ | ||
error: { | ||
message: 'Webhook Error', | ||
}, | ||
}, | ||
{ status: 400 } | ||
); | ||
} | ||
|
||
const subscription = event.data.object as Stripe.Subscription; | ||
|
||
switch (event.type) { | ||
case 'customer.subscription.created': | ||
await prisma.user.update({ | ||
where: { | ||
stripeCustomerId: subscription.customer as string, | ||
}, | ||
data: { | ||
isActive: true, | ||
}, | ||
}); | ||
break; | ||
default: | ||
break; | ||
} | ||
return NextResponse.json({ received: true }); | ||
} catch { | ||
return NextResponse.json( | ||
{ | ||
error: { | ||
message: 'Method Not Allowed', | ||
}, | ||
}, | ||
{ status: 405 } | ||
).headers.set('Allow', 'POST'); | ||
} | ||
}; | ||
|
||
export { webhookHandler as POST }; |
Oops, something went wrong.