mirror of
https://github.com/ahmadk953/tasko.git
synced 2025-05-04 12:43:24 +00:00
Initial Commit
This commit is contained in:
commit
f3e2f01bd7
150 changed files with 13612 additions and 0 deletions
75
actions/stripe-redirect/index.ts
Normal file
75
actions/stripe-redirect/index.ts
Normal file
|
@ -0,0 +1,75 @@
|
|||
"use server";
|
||||
|
||||
import { auth, currentUser } from "@clerk/nextjs";
|
||||
import { revalidatePath } from "next/cache";
|
||||
|
||||
import { db } from "@/lib/db";
|
||||
import { createSafeAction } from "@/lib/create-safe-action";
|
||||
import { absoluteUrl } from "@/lib/utils";
|
||||
import { stripe } from "@/lib/stripe";
|
||||
|
||||
import { InputType, ReturnType } from "./types";
|
||||
import { StripeRedirect } from "./schema";
|
||||
|
||||
const handler = async (data: InputType): Promise<ReturnType> => {
|
||||
const { userId, orgId } = auth();
|
||||
const user = await currentUser();
|
||||
|
||||
if (!userId || !orgId || !user) return { error: "Unauthorized" };
|
||||
|
||||
const settingsUrl = absoluteUrl(`/organization/${orgId}`);
|
||||
|
||||
let url = "";
|
||||
|
||||
try {
|
||||
const orgSubscription = await db.orgSubscription.findUnique({
|
||||
where: { orgId },
|
||||
});
|
||||
|
||||
if (orgSubscription?.stripeCustomerId) {
|
||||
const stripeSession = await stripe.billingPortal.sessions.create({
|
||||
customer: orgSubscription.stripeCustomerId,
|
||||
return_url: settingsUrl,
|
||||
});
|
||||
|
||||
url = stripeSession.url;
|
||||
} else {
|
||||
const stripeSession = await stripe.checkout.sessions.create({
|
||||
success_url: settingsUrl,
|
||||
cancel_url: settingsUrl,
|
||||
payment_method_types: ["card", "paypal"],
|
||||
mode: "subscription",
|
||||
billing_address_collection: "auto",
|
||||
customer_email: user.emailAddresses[0].emailAddress,
|
||||
line_items: [
|
||||
{
|
||||
price_data: {
|
||||
currency: "usd",
|
||||
product_data: {
|
||||
name: "Tasko Pro",
|
||||
description: "Unlimited boards for your organization",
|
||||
},
|
||||
unit_amount: 2000,
|
||||
recurring: { interval: "month" },
|
||||
},
|
||||
quantity: 1,
|
||||
},
|
||||
],
|
||||
metadata: {
|
||||
orgId,
|
||||
},
|
||||
});
|
||||
|
||||
url = stripeSession.url ?? "";
|
||||
}
|
||||
} catch (error) {
|
||||
return {
|
||||
error: "Something went wrong",
|
||||
};
|
||||
}
|
||||
|
||||
revalidatePath(`/organization/${orgId}`);
|
||||
return { data: url };
|
||||
};
|
||||
|
||||
export const stripeRedirect = createSafeAction(StripeRedirect, handler);
|
Loading…
Add table
Add a link
Reference in a new issue