forked from lmsqueezy/nextjs-billing
-
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
12 changed files
with
548 additions
and
128 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { getSession } from "@/lib/auth"; | ||
import type { Metadata } from 'next'; | ||
import prisma from "@/lib/prisma"; | ||
import Link from 'next/link'; | ||
import { PlansComponent } from '@/components/manage'; | ||
import { getPlans, getSubscription } from '@/lib/data'; | ||
import { redirect } from 'next/navigation'; | ||
|
||
|
||
export const metadata: Metadata = { | ||
title: 'Billing' | ||
} | ||
|
||
|
||
export default async function Billing() { | ||
const session = await getSession(); | ||
|
||
const plans = await getPlans(); | ||
|
||
const sub = await getSubscription(session?.user.id); | ||
|
||
if (!sub) { | ||
redirect('/billing') | ||
} | ||
|
||
return ( | ||
<div className="container mx-auto max-w-lg"> | ||
|
||
<Link href="/billing/" className="text-gray-500 text-sm mb-6">← Back to billing</Link> | ||
|
||
<h1 className="text-xl font-bold mb-3 text-center">Change plan</h1> | ||
|
||
{sub.status == 'on_trial' && ( | ||
<div className="my-8 p-4 text-sm text-amber-800 rounded-md border border-amber-200 bg-amber-50"> | ||
You are currently on a free trial. You will not be charged when changing plans during a trial. | ||
</div> | ||
)} | ||
|
||
<PlansComponent plans={plans} sub={sub} /> | ||
|
||
<script src="https://app.lemonsqueezy.com/js/lemon.js" defer></script> | ||
|
||
</div> | ||
) | ||
} | ||
|
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,84 @@ | ||
import { getSession } from "@/lib/auth"; | ||
import { getSubscription } from '@/lib/data'; | ||
import LemonSqueezy from '@lemonsqueezy/lemonsqueezy.js' | ||
|
||
const ls = new LemonSqueezy(process.env.LEMONSQUEEZY_API_KEY); | ||
|
||
|
||
export async function GET(request, { params }) { | ||
// Get subscription update billing link | ||
try { | ||
const subscription = await ls.getSubscription({ id: params.id }) | ||
return Response.json({ error: false, subscription: { | ||
update_billing_url: subscription['data']['attributes']['urls']['update_payment_method'] | ||
} }, { status: 200 }) | ||
} catch(e) { | ||
return Response.json({ error: true, message: e.message }, { status: 400 }) | ||
} | ||
} | ||
|
||
|
||
export async function POST(request, { params }) { | ||
const session = await getSession(); | ||
|
||
const res = await request.json() | ||
|
||
var subscription; | ||
|
||
|
||
if (res.variantId && res.productId) { | ||
|
||
// Update plan | ||
|
||
try { | ||
subscription = await ls.updateSubscription({ | ||
id: params.id, | ||
productId: res.productId, | ||
variantId: res.variantId, | ||
}) | ||
} catch(e) { | ||
return Response.json({ error: true, message: e.message }, { status: 400 }) | ||
} | ||
|
||
} else if (res.action == 'resume') { | ||
|
||
// Resume | ||
|
||
try { | ||
subscription = await ls.resumeSubscription({ id: params.id }) | ||
} catch(e) { | ||
return Response.json({ error: true, message: e.message }, { status: 400 }) | ||
} | ||
|
||
} else if (res.action == 'cancel') { | ||
|
||
// Cancel | ||
|
||
try { | ||
await ls.cancelSubscription({ id: params.id }) | ||
// TODO get proper result from cancelSubscription() | ||
subscription = { | ||
data: { | ||
attributes: { | ||
status: 'cancelled', | ||
|
||
} | ||
} | ||
} | ||
} catch(e) { | ||
return Response.json({ error: true, message: e.message }, { status: 400 }) | ||
} | ||
|
||
} else { | ||
|
||
// Missing data in request | ||
|
||
return Response.json({ error: true, message: 'Correct data not found.' }, { status: 400 }) | ||
|
||
} | ||
|
||
// Return values needed to refresh state in UI | ||
// DB will be updated in the background with webhooks | ||
return Response.json({ error: false, subscription: subscription['data']['attributes'] }, { 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
Oops, something went wrong.