-
Notifications
You must be signed in to change notification settings - Fork 275
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
5 changed files
with
111 additions
and
155 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,68 @@ | ||
"use server"; | ||
|
||
import { redirect } from "next/navigation"; | ||
import { authOptions } from "@/lib/auth"; | ||
import { stripe } from "@/lib/stripe"; | ||
import { getUserSubscriptionPlan } from "@/lib/subscription"; | ||
import { absoluteUrl } from "@/lib/utils"; | ||
import { getServerSession } from "next-auth"; | ||
|
||
export interface responseAction { | ||
status: "success" | "error"; | ||
stripeUrl?: string; | ||
} | ||
|
||
// const billingUrl = absoluteUrl("/dashboard/billing") | ||
const billingUrl = absoluteUrl("/pricing"); | ||
|
||
export async function generateUserStripe( | ||
priceId: string | ||
): Promise<responseAction> { | ||
let redirectUrl = ""; | ||
|
||
try { | ||
const session = await getServerSession(authOptions); | ||
|
||
if (!session?.user || !session.user.email) { | ||
throw new Error("Unauthorized"); | ||
} | ||
|
||
const subscriptionPlan = await getUserSubscriptionPlan(session.user.id); | ||
|
||
if (subscriptionPlan.isPaid && subscriptionPlan.stripeCustomerId) { | ||
// User on Paid Plan - Create a portal session to manage subscription. | ||
const stripeSession = await stripe.billingPortal.sessions.create({ | ||
customer: subscriptionPlan.stripeCustomerId, | ||
return_url: billingUrl, | ||
}); | ||
|
||
redirectUrl = stripeSession.url; | ||
} else { | ||
// User on Free Plan - Create a checkout session to upgrade. | ||
const stripeSession = await stripe.checkout.sessions.create({ | ||
success_url: billingUrl, | ||
cancel_url: billingUrl, | ||
payment_method_types: ["card"], | ||
mode: "subscription", | ||
billing_address_collection: "auto", | ||
customer_email: session.user.email, | ||
line_items: [ | ||
{ | ||
price: priceId, | ||
quantity: 1, | ||
}, | ||
], | ||
metadata: { | ||
userId: session.user.id, | ||
}, | ||
}); | ||
|
||
redirectUrl = stripeSession.url!; | ||
} | ||
} catch (error) { | ||
throw new Error("Failed to generate user stripe session"); | ||
} | ||
|
||
// no revalidatePath because redirect | ||
redirect(redirectUrl); | ||
} |
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,40 @@ | ||
"use server" | ||
|
||
import { revalidatePath } from "next/cache" | ||
import { getServerSession } from "next-auth" | ||
|
||
import { authOptions } from "@/lib/auth" | ||
import { prisma } from "@/lib/db" | ||
import { userNameSchema } from "@/lib/validations/user" | ||
|
||
export interface FormData { | ||
name: string | ||
} | ||
|
||
export async function updateUserName(userId: string, data: FormData) { | ||
try { | ||
const session = await getServerSession(authOptions) | ||
|
||
if (!session?.user || session.user.id !== userId) { | ||
throw new Error("Unauthorized") | ||
} | ||
|
||
const { name } = userNameSchema.parse(data) | ||
|
||
// Update the user name. | ||
await prisma.user.update({ | ||
where: { | ||
id: userId, | ||
}, | ||
data: { | ||
name: name, | ||
}, | ||
}) | ||
|
||
revalidatePath("/dashboard/settings") | ||
return { status: "success" } | ||
} catch (error) { | ||
console.log(error) | ||
return { status: "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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.