-
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.
Merge pull request #16 from Hombre2014/stripe
Stripe
- Loading branch information
Showing
24 changed files
with
692 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { SubscriptionButton } from '@/components/subscription-button'; | ||
import { checkSubscription } from '@/lib/subscription'; | ||
|
||
const SettingsPage = async () => { | ||
const isPro = await checkSubscription(); | ||
|
||
return ( | ||
<div className="h-full p-4 space-y-2"> | ||
<h3 className="text-lg font-medium">Settings</h3> | ||
<div className="text-muted-foreground text-sm"> | ||
{isPro | ||
? 'You are currently on a Pro plan.' | ||
: 'You are currently on a free plan.'} | ||
</div> | ||
<SubscriptionButton isPro={isPro} /> | ||
</div> | ||
); | ||
}; | ||
|
||
export default SettingsPage; |
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,73 @@ | ||
import { auth, currentUser } from '@clerk/nextjs'; | ||
import { NextResponse } from 'next/server'; | ||
|
||
import prismadb from '@/lib/prismadb'; | ||
import { stripe } from '@/lib/stripe'; | ||
import { absoluteUrl } from '@/lib/utils'; | ||
|
||
const settingsUrl = absoluteUrl('/settings'); | ||
|
||
console.log('URL: ', settingsUrl); | ||
|
||
export async function GET() { | ||
try { | ||
const { userId } = auth(); | ||
const user = await currentUser(); | ||
|
||
if (!userId || !user) { | ||
return new NextResponse('Unauthorized', { status: 401 }); | ||
} | ||
|
||
const userSubscription = await prismadb.userSubscription.findUnique({ | ||
where: { | ||
userId, | ||
}, | ||
}); | ||
|
||
if (userSubscription && userSubscription.stripeCustomerId) { | ||
const stripeSession = await stripe.billingPortal.sessions.create({ | ||
customer: userSubscription.stripeCustomerId, | ||
return_url: settingsUrl, | ||
}); | ||
|
||
return new NextResponse(JSON.stringify({ url: stripeSession.url })); | ||
} | ||
|
||
const stripeSession = await stripe.checkout.sessions.create({ | ||
success_url: settingsUrl, | ||
cancel_url: settingsUrl, | ||
payment_method_types: ['card'], | ||
mode: 'subscription', | ||
billing_address_collection: 'auto', | ||
customer_email: user.emailAddresses[0].emailAddress, | ||
line_items: [ | ||
// Only one tier option for now - Pro | ||
{ | ||
price_data: { | ||
currency: 'USD', | ||
product_data: { | ||
name: 'Chat With Pro', | ||
description: 'Create Your Own Custom AI Companions', | ||
}, | ||
unit_amount: 999, | ||
recurring: { | ||
interval: 'month', | ||
}, | ||
}, | ||
quantity: 1, | ||
}, | ||
], | ||
// Maps the user subscription to the current login user in our Database | ||
metadata: { | ||
userId, | ||
}, | ||
}); | ||
|
||
console.log('Stripe session', stripeSession); | ||
|
||
return new NextResponse(JSON.stringify({ url: stripeSession.url })); | ||
} catch (error) { | ||
console.log('[STRIPE_GET_ERROR]', error); | ||
return new NextResponse('Internal Error', { status: 500 }); | ||
} | ||
} |
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,67 @@ | ||
import Stripe from 'stripe'; | ||
import { headers } from 'next/headers'; | ||
import { NextResponse } from 'next/server'; | ||
|
||
import prismadb from '@/lib/prismadb'; | ||
import { stripe } from '@/lib/stripe'; | ||
|
||
export async function POST(req: Request) { | ||
const body = await req.text(); | ||
const signature = headers().get('Stripe-Signature') as string; | ||
|
||
let event: Stripe.Event; | ||
|
||
try { | ||
event = stripe.webhooks.constructEvent( | ||
body, | ||
signature, | ||
process.env.STRIPE_WEBHOOK_SECRET! | ||
); | ||
} catch (error: any) { | ||
return new NextResponse(`Webhook Error: ${error.message}`, { status: 400 }); | ||
} | ||
|
||
const session = event.data.object as Stripe.Checkout.Session; | ||
|
||
if (event.type === 'checkout.session.completed') { | ||
const subscription = await stripe.subscriptions.retrieve( | ||
session.subscription as string | ||
); | ||
|
||
if (!session?.metadata?.userId) { | ||
return new NextResponse('User id is required', { status: 400 }); | ||
} | ||
|
||
await prismadb.userSubscription.create({ | ||
data: { | ||
userId: session?.metadata?.userId, | ||
stripeSubscriptionId: subscription.id, | ||
stripeCustomerId: subscription.customer as string, | ||
stripePriceId: subscription.items.data[0].price.id, | ||
stripeCurrentPeriodEnd: new Date( | ||
subscription.current_period_end * 1000 | ||
), | ||
}, | ||
}); | ||
} | ||
|
||
if (event.type === 'invoice.payment_succeeded') { | ||
const subscription = await stripe.subscriptions.retrieve( | ||
session.subscription as string | ||
); | ||
|
||
await prismadb.userSubscription.update({ | ||
where: { | ||
stripeSubscriptionId: subscription.id, | ||
}, | ||
data: { | ||
stripePriceId: subscription.items.data[0].price.id, | ||
stripeCurrentPeriodEnd: new Date( | ||
subscription.current_period_end * 1000 | ||
), | ||
}, | ||
}); | ||
} | ||
|
||
return new NextResponse(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
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
Oops, something went wrong.