add stripe (#121)

* add some stripe stuff

* more stripe stuff

* more stripe things

* more stripr stuff

* more stripe stuff

* more stripe stuff

* add more stuff

* add more stripe stuff

* more stuff

* fix types
This commit is contained in:
KM Koushik
2025-03-23 07:06:56 +11:00
committed by GitHub
parent 6cfe41cd86
commit 403ad8b93e
34 changed files with 1352 additions and 238 deletions

View File

@@ -0,0 +1,13 @@
export const PLAN_PERKS = {
FREE: [
"Send up to 3000 emails per month",
"Send up to 100 emails per day",
"Can have 1 contact book",
],
BASIC: [
"Includes $10 of usage monthly",
"Send transactional emails at $0.0004 per email",
"Send marketing emails at $0.001 per email",
"Can have unlimited contact books",
],
};

66
apps/web/src/lib/usage.ts Normal file
View File

@@ -0,0 +1,66 @@
import { EmailUsageType, Plan } from "@prisma/client";
/**
* Unit price for unsend
* 1 marketing email = 1 unit
* 4 transaction emails = 1 unit
*/
export const UNIT_PRICE = 0.001;
/**
* Number of credits per plan
* Credits are the units of measurement for email sending capacity.
* Each plan comes with a specific number of credits that can be used for sending emails.
* Marketing emails consume 1 credit per email, while transactional emails consume 0.25 credits per email.
*/
export const PLAN_CREDIT_UNITS = {
[Plan.BASIC]: 10_000,
};
export const USAGE_UNIT_PRICE: Record<EmailUsageType, number> = {
[EmailUsageType.MARKETING]: 0.001,
[EmailUsageType.TRANSACTIONAL]: 0.0004,
};
/**
* Gets timestamp for usage reporting, set to yesterday's date
* Converts to Unix timestamp (seconds since epoch)
* @returns Unix timestamp in seconds for yesterday's date
*/
export function getUsageTimestamp() {
const yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
return Math.floor(yesterday.getTime() / 1000);
}
/**
* Gets yesterday's date in YYYY-MM-DD format
* @returns Yesterday's date string in YYYY-MM-DD format
*/
export function getUsageDate(): string {
const yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
const isoString = yesterday.toISOString();
return isoString.split("T")[0] as string;
}
/**
* Calculates total usage units based on marketing and transaction emails
* @param marketingUsage Number of marketing emails sent
* @param transactionUsage Number of transaction emails sent
* @returns Total usage units rounded down to nearest integer
*/
export function getUsageUinits(
marketingUsage: number,
transactionUsage: number
) {
return marketingUsage + Math.floor(transactionUsage / 4);
}
export function getCost(usage: number, type: EmailUsageType) {
const calculatedUsage =
type === EmailUsageType.MARKETING ? usage : Math.floor(usage / 4);
return calculatedUsage * UNIT_PRICE;
}