mirror of
https://github.com/lukevella/rallly.git
synced 2025-06-04 19:51:51 +02:00
💸 Create abstraction for handling pricing (#1215)
This commit is contained in:
parent
299b33df62
commit
a5ee4fafe5
22 changed files with 302 additions and 68 deletions
16
packages/billing/package.json
Normal file
16
packages/billing/package.json
Normal 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": "*"
|
||||
}
|
||||
}
|
1
packages/billing/src/index.ts
Normal file
1
packages/billing/src/index.ts
Normal file
|
@ -0,0 +1 @@
|
|||
export * from "./lib/stripe";
|
20
packages/billing/src/lib/get-pricing.ts
Normal file
20
packages/billing/src/lib/get-pricing.ts
Normal 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,
|
||||
},
|
||||
};
|
||||
}
|
35
packages/billing/src/lib/stripe.ts
Normal file
35
packages/billing/src/lib/stripe.ts
Normal 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>>;
|
23
packages/billing/src/next/index.ts
Normal file
23
packages/billing/src/next/index.ts
Normal 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 };
|
5
packages/billing/tsconfig.json
Normal file
5
packages/billing/tsconfig.json
Normal file
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"extends": "@rallly/tsconfig/next.json",
|
||||
"include": ["**/*.ts", "**/*.tsx", "**/*.js"],
|
||||
"exclude": ["node_modules"],
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue