* WIP * WIP * Type and migration fixes * Adds missing default import * Fixes import * Fixes tRPC imports in App Store * Migrate stripe helpers * WIP * Type fixes * Type fix? * WIP * WIP * Update index.ts * Fixes * Update workflow.tsx * Moved queries to lib * Moves QueryCell * Migrates MultiSelectCheckboxes * WIP * CryptoSection type fixes * WIP * Import fixes * Build fixes * Update app-providers.tsx * Build fixes * Upgrades hookform zod resolvers * Build fixes * Cleanup * Build fixes * Relocates QueryCell to ui * Moved List and SkeletonLoader * Revert QueryCell migration * Can't use QueryCell here * oops * CryptoSection cleanup * Update app-providers.tsx * Moved ee to features * ee to features/ee * Removes @calcom/ee * Adds possible feature locations * Build fixes * Migrates stripe to app-store lib * Colocates stripe imports * Update subscription.ts * Submodule sync Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
73 lines
2.0 KiB
TypeScript
73 lines
2.0 KiB
TypeScript
import { UserPlan } from "@prisma/client";
|
|
import Stripe from "stripe";
|
|
|
|
import stripe from "./server";
|
|
import {
|
|
getFreePlanPrice,
|
|
getPremiumPlanPrice,
|
|
getProPlanPrice,
|
|
getFreePlanProductId,
|
|
getPremiumPlanProductId,
|
|
getProPlanProductId,
|
|
} from "./utils";
|
|
|
|
interface IRetrieveSubscriptionIdResponse {
|
|
message?: string;
|
|
subscriptionId?: string;
|
|
}
|
|
|
|
export async function retrieveSubscriptionIdFromStripeCustomerId(
|
|
stripeCustomerId: string
|
|
): Promise<IRetrieveSubscriptionIdResponse> {
|
|
const customer = await stripe.customers.retrieve(stripeCustomerId, {
|
|
expand: ["subscriptions.data.plan"],
|
|
});
|
|
if (!customer || customer.deleted) {
|
|
return {
|
|
message: "Not found",
|
|
};
|
|
}
|
|
|
|
const subscription = customer.subscriptions?.data[0];
|
|
if (!subscription) {
|
|
return {
|
|
message: "Not found",
|
|
};
|
|
}
|
|
return {
|
|
subscriptionId: subscription.id,
|
|
};
|
|
}
|
|
|
|
// @NOTE: Remove when user subscription plan id is saved on db and not on stripe only
|
|
export function obtainUserPlanDetails(subscription: Stripe.Subscription) {
|
|
const proPlanProductId = getProPlanProductId();
|
|
const premiumPlanProductId = getPremiumPlanProductId();
|
|
const freePlanProductId = getFreePlanProductId();
|
|
let priceId = "";
|
|
const hasProPlan = !!subscription.items.data.find((item) => item.plan.product === proPlanProductId);
|
|
const hasPremiumPlan = !!subscription.items.data.find((item) => item.plan.product === premiumPlanProductId);
|
|
const hasFreePlan = !!subscription.items.data.find((item) => item.plan.product === freePlanProductId);
|
|
let userPlan: UserPlan;
|
|
if (hasPremiumPlan) {
|
|
priceId = getPremiumPlanPrice();
|
|
userPlan = UserPlan.PRO;
|
|
} else if (hasProPlan) {
|
|
priceId = getProPlanPrice();
|
|
userPlan = UserPlan.PRO;
|
|
} else if (hasFreePlan) {
|
|
priceId = getFreePlanPrice();
|
|
userPlan = UserPlan.FREE;
|
|
} else {
|
|
userPlan = UserPlan.TRIAL;
|
|
}
|
|
|
|
return {
|
|
userPlan,
|
|
priceId,
|
|
isProPlan: hasProPlan,
|
|
isPremiumPlan: hasPremiumPlan,
|
|
isFreePlan: hasFreePlan,
|
|
};
|
|
}
|