|
| 1 | +import { getAuthToken } from "../../app/api/lib/getAuthToken"; |
| 2 | +import { API_SERVER_URL } from "../constants/env"; |
| 3 | + |
| 4 | +// keep in line with product SKUs in the backend |
| 5 | +export type ProductSKU = |
| 6 | + | "plan:starter" |
| 7 | + | "plan:growth" |
| 8 | + | "plan:custom" |
| 9 | + | "product:ecosystem_wallets" |
| 10 | + | "product:engine_standard" |
| 11 | + | "product:engine_premium" |
| 12 | + | "usage:storage" |
| 13 | + | "usage:in_app_wallet" |
| 14 | + | "usage:aa_sponsorship" |
| 15 | + | "usage:aa_sponsorship_op_grant" |
| 16 | + | null; |
| 17 | + |
| 18 | +export type InvoiceLine = { |
| 19 | + // amount for this line item |
| 20 | + amount: number; |
| 21 | + // statement descriptor |
| 22 | + description: string | null; |
| 23 | + // the thirdweb product sku or null if it is not recognized |
| 24 | + thirdwebSku: ProductSKU | null; |
| 25 | +}; |
| 26 | + |
| 27 | +export type Invoice = { |
| 28 | + // total amount excluding tax |
| 29 | + amount: number | null; |
| 30 | + // the ISO currency code (e.g. USD) |
| 31 | + currency: string; |
| 32 | + // the line items on the invoice |
| 33 | + lines: InvoiceLine[]; |
| 34 | +}; |
| 35 | + |
| 36 | +export type TeamSubscription = { |
| 37 | + id: string; |
| 38 | + type: "PLAN" | "USAGE" | "PLAN_ADD_ON" | "PRODUCT"; |
| 39 | + status: |
| 40 | + | "incomplete" |
| 41 | + | "incomplete_expired" |
| 42 | + | "trialing" |
| 43 | + | "active" |
| 44 | + | "past_due" |
| 45 | + | "canceled" |
| 46 | + | "unpaid" |
| 47 | + | "paused"; |
| 48 | + currentPeriodStart: string; |
| 49 | + currentPeriodEnd: string; |
| 50 | + trialStart: string | null; |
| 51 | + trialEnd: string | null; |
| 52 | + upcomingInvoice: Invoice; |
| 53 | +}; |
| 54 | + |
| 55 | +export async function getTeamSubscriptions(slug: string) { |
| 56 | + const token = await getAuthToken(); |
| 57 | + |
| 58 | + if (!token) { |
| 59 | + return null; |
| 60 | + } |
| 61 | + |
| 62 | + const teamRes = await fetch( |
| 63 | + `${API_SERVER_URL}/v1/teams/${slug}/subscriptions`, |
| 64 | + { |
| 65 | + headers: { |
| 66 | + Authorization: `Bearer ${token}`, |
| 67 | + }, |
| 68 | + }, |
| 69 | + ); |
| 70 | + if (teamRes.ok) { |
| 71 | + return (await teamRes.json())?.result as TeamSubscription[]; |
| 72 | + } |
| 73 | + return null; |
| 74 | +} |
| 75 | + |
| 76 | +// util fn: |
| 77 | + |
| 78 | +export function parseThirdwebSKU(sku: ProductSKU) { |
| 79 | + if (!sku) { |
| 80 | + return null; |
| 81 | + } |
| 82 | + switch (sku) { |
| 83 | + case "plan:starter": |
| 84 | + return "Starter Plan"; |
| 85 | + case "plan:growth": |
| 86 | + return "Growth Plan"; |
| 87 | + case "plan:custom": |
| 88 | + return "Custom Plan"; |
| 89 | + case "product:ecosystem_wallets": |
| 90 | + return "Ecosystem Wallets"; |
| 91 | + case "product:engine_standard": |
| 92 | + return "Engine Standard"; |
| 93 | + case "product:engine_premium": |
| 94 | + return "Engine Premium"; |
| 95 | + case "usage:storage": |
| 96 | + return "Storage"; |
| 97 | + case "usage:in_app_wallet": |
| 98 | + return "In-App Wallet"; |
| 99 | + case "usage:aa_sponsorship": |
| 100 | + return "AA Sponsorship"; |
| 101 | + case "usage:aa_sponsorship_op_grant": |
| 102 | + return "AA Sponsorship Op Grant"; |
| 103 | + default: |
| 104 | + return null; |
| 105 | + } |
| 106 | +} |
0 commit comments