Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

739 B
Raw Blame
import { PlanPeriod } from "@prisma/client";
import type Stripe from "stripe";

export function getPlanPeriodFromStripeSubscription(
  subscription: Stripe.Subscription,
): PlanPeriod | undefined {
  const recurring = subscription.items.data[0]?.price?.recurring;
  if (!recurring) {
    return undefined;
  }

  const { interval, interval_count } = recurring;
  if (interval === "month" && interval_count === 3) {
    return PlanPeriod.quarterly;
  }
  if (interval === "month" && interval_count === 1) {
    return PlanPeriod.monthly;
  }
  if (interval === "year" && interval_count === 1) {
    return PlanPeriod.yearly;
  }
  if (interval === "week" && interval_count === 2) {
    return PlanPeriod.biweekly;
  }
  return undefined;
}