-
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 #144 from wizelineacademy/stripe-payments
feat: Added subscription with stripe
- Loading branch information
Showing
8 changed files
with
272 additions
and
86 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,128 @@ | ||
'use client' | ||
import { Plan } from '@/src/data/datatypes/payment' | ||
import { plans } from '@/src/data/plans' | ||
import axios from 'axios' | ||
import { NextPage } from 'next' | ||
import { useRouter } from 'next/navigation' | ||
import { useEffect, useState } from 'react' | ||
|
||
const PricingPage: NextPage = () => { | ||
interface Subscription { | ||
plan: string | ||
status: string | ||
end?: number | ||
} | ||
|
||
const [subscription, setSubscription] = useState<Subscription | null>(null) | ||
|
||
const getSubscriptionData = async () => { | ||
try { | ||
const res = await axios.get('/api/subscription') | ||
const data = res.data | ||
if (data.message === 'No subscription') { | ||
setSubscription(null) | ||
} else { | ||
setSubscription(data) | ||
} | ||
} catch (error) { | ||
console.log(error) | ||
} | ||
} | ||
|
||
const formatExpirationDate = (date: number) => { | ||
const dateObject = new Date(date * 1000) | ||
const dateString = dateObject.toLocaleString() | ||
const dateDay = dateString.split(',')[0] | ||
return dateDay | ||
} | ||
|
||
useEffect(() => { | ||
getSubscriptionData() | ||
}, []) | ||
|
||
return ( | ||
<div className='flex min-h-screen w-full flex-col items-center justify-center'> | ||
<div className='mx-auto max-w-4xl py-8'> | ||
{!subscription && ( | ||
<h2 className='mb-4 text-center text-2xl font-bold text-color-home6'> | ||
No tienes una suscripción activa | ||
</h2> | ||
)} | ||
{subscription && ( | ||
<h2 className='mb-4 text-center text-2xl font-bold text-color-home6'> | ||
Tienes una subscripción de {subscription.plan} | ||
</h2> | ||
)} | ||
{subscription && | ||
subscription?.status === 'trialing' && | ||
subscription.end && ( | ||
<> | ||
<h3 className='mx-4 mb-4 text-center text-lg text-black'> | ||
Actualmente te encuentras en modo de prueba, contrata un plan | ||
para no perder el acceso a la aplicación | ||
</h3> | ||
<h3 className='mx-4 mb-4 text-center text-lg text-black'> | ||
Tu subscripción expira el{' '} | ||
{formatExpirationDate(subscription.end)} | ||
</h3> | ||
</> | ||
)} | ||
|
||
<h1 className='mx-4 mb-4 text-center text-2xl font-bold text-color-home6'> | ||
Contrata una subscripción a Vita | ||
</h1> | ||
<div className='grid grid-cols-1 gap-4 md:grid-cols-3'> | ||
{plans.map((plan, index) => ( | ||
<PlanCard key={index} plan={plan} /> | ||
))} | ||
</div> | ||
</div> | ||
</div> | ||
) | ||
} | ||
|
||
const PlanCard: React.FC<{ plan: Plan }> = ({ plan }) => { | ||
const router = useRouter() | ||
|
||
const processPayment = async () => { | ||
try { | ||
const res = await axios.post('/api/stripe/payment', { | ||
priceId: plan.priceId, | ||
allowTrial: plan.allowTrial, | ||
}) | ||
const data = res.data | ||
router.push(data.url) | ||
} catch (error) { | ||
console.log(error) | ||
} | ||
} | ||
|
||
return ( | ||
<div className='flex h-full transform flex-col justify-between rounded-md border bg-color-home6 p-4 text-white shadow-md transition duration-300 ease-in-out hover:scale-105'> | ||
<div> | ||
<h2 className='mb-2 text-xl font-semibold text-white'>{plan.name}</h2> | ||
<p className='mb-4 text-gray-300'>${plan.price}/mes</p> | ||
<ul className='mb-4 list-disc pl-5 text-white'> | ||
{plan.features.map((feature, index) => ( | ||
<li key={index} className='mb-2'> | ||
{feature} | ||
</li> | ||
))} | ||
</ul> | ||
{plan.name === 'Bienestar Plus' && ( | ||
<h2 className='mb-5 text-lg font-bold'>Pruébalo gratis por 7 días</h2> | ||
)} | ||
</div> | ||
<div className='mt-auto'> | ||
<button | ||
className={`w-full transform rounded-md bg-blue-500 px-4 py-2 text-white transition duration-300 hover:scale-105 hover:bg-green-600 focus:outline-none focus:ring focus:ring-green-400`} | ||
onClick={processPayment} | ||
> | ||
Seleccionar | ||
</button> | ||
</div> | ||
</div> | ||
) | ||
} | ||
|
||
export default PricingPage |
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,53 @@ | ||
import { NextResponse } from 'next/server' | ||
import { db } from '@/src/db/drizzle' | ||
import { user } from '@/src/db/schema/schema' | ||
import { eq } from 'drizzle-orm' | ||
import { getServerSession } from 'next-auth' | ||
import { authOptions } from '@/src/lib/auth/authOptions' | ||
import { stripe } from '@/src/lib/stripe/stripe' | ||
import { plans } from '@/src/data/plans' | ||
|
||
export async function GET() { | ||
const session = await getServerSession(authOptions) | ||
if (!session) { | ||
return NextResponse.json('Unauthorized', { status: 404 }) | ||
} | ||
|
||
try { | ||
const existingUser = await db | ||
.select() | ||
.from(user) | ||
.where(eq(user.idUser, session.user?.id)) | ||
.limit(1) | ||
|
||
//find stripe subscription | ||
const subscriptionId = existingUser[0].membership | ||
if (!subscriptionId) { | ||
return NextResponse.json({ message: 'No subscription' }, { status: 200 }) | ||
} | ||
|
||
const subscription = await stripe.subscriptions.retrieve(subscriptionId) | ||
|
||
if ( | ||
subscription.status !== 'active' && | ||
subscription.status !== 'trialing' | ||
) { | ||
return NextResponse.json({ message: 'No subscription' }, { status: 200 }) | ||
} | ||
|
||
const subscriptionData = subscription.items.data[0].price.id | ||
const planType = plans.find((plan) => plan.priceId === subscriptionData) | ||
|
||
return NextResponse.json( | ||
{ | ||
plan: planType?.name, | ||
status: subscription.status, | ||
end: subscription.trial_end, | ||
}, | ||
{ status: 200 }, | ||
) | ||
} catch (error) { | ||
console.log(error) | ||
return NextResponse.json('Error processing registration', { status: 400 }) | ||
} | ||
} |
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.