💸 Create abstraction for handling pricing (#1215)

This commit is contained in:
Luke Vella 2024-07-21 20:42:53 +01:00 committed by GitHub
parent 299b33df62
commit a5ee4fafe5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
22 changed files with 302 additions and 68 deletions

View file

@ -0,0 +1,16 @@
{
"name": "@rallly/billing",
"version": "0.0.0",
"private": true,
"exports": {
"./server/*": "./src/server/*.tsx",
"./next": "./src/next/index.ts",
".": "./src/index.ts"
},
"dependencies": {
"@rallly/ui": "*",
"stripe": "^13.2.0",
"@radix-ui/react-radio-group": "^1.2.0",
"next": "*"
}
}

View file

@ -0,0 +1 @@
export * from "./lib/stripe";

View file

@ -0,0 +1,20 @@
import { stripe } from "..";
export async function getPricing() {
const prices = await stripe.prices.list({
lookup_keys: ["pro-monthly", "pro-yearly"],
});
const [monthly, yearly] = prices.data;
return {
monthly: {
currency: monthly.currency,
price: monthly.unit_amount_decimal,
},
yearly: {
currency: yearly.currency,
price: yearly.unit_amount,
},
};
}

View file

@ -0,0 +1,35 @@
import Stripe from "stripe";
export type { Stripe } from "stripe";
export const stripe = new Stripe(process.env.STRIPE_SECRET_KEY as string, {
apiVersion: "2023-08-16",
typescript: true,
});
export async function getProPricing() {
const prices = await stripe.prices.list({
lookup_keys: ["pro-monthly", "pro-yearly"],
});
const [monthly, yearly] = prices.data;
if (monthly.unit_amount === null || yearly.unit_amount === null) {
throw new Error("Price not found");
}
return {
monthly: {
id: monthly.id,
amount: monthly.unit_amount,
currency: monthly.currency,
},
yearly: {
id: yearly.id,
amount: yearly.unit_amount,
currency: yearly.currency,
},
};
}
export type PricingData = Awaited<ReturnType<typeof getProPricing>>;

View file

@ -0,0 +1,23 @@
import { NextRequest, NextResponse } from "next/server";
import { getPricing } from "../lib/get-pricing";
export async function GET(
request: NextRequest,
{
params,
}: {
params: {
method: string;
};
},
) {
switch (params.method) {
case "pricing":
const data = await getPricing();
return NextResponse.json(data);
default:
return NextResponse.json({ message: "Method not found" });
}
}
export const handlers = { GET };

View file

@ -0,0 +1,5 @@
{
"extends": "@rallly/tsconfig/next.json",
"include": ["**/*.ts", "**/*.tsx", "**/*.js"],
"exclude": ["node_modules"],
}