Revert "Nick:"

This reverts commit 0160bb6a01.
This commit is contained in:
Nicolas 2024-06-13 19:32:42 -07:00
parent 6fc1ee32fd
commit b014d99db4
3 changed files with 38 additions and 5 deletions

View File

@ -93,7 +93,7 @@
"resend": "^3.2.0",
"robots-parser": "^3.0.1",
"scrapingbee": "^1.7.4",
"stripe": "^12.2.0",
"stripe": "^15.11.0",
"turndown": "^7.1.3",
"turndown-plugin-gfm": "^1.0.2",
"typesense": "^1.5.4",

View File

@ -156,8 +156,8 @@ dependencies:
specifier: ^1.7.4
version: 1.7.4
stripe:
specifier: ^12.2.0
version: 12.18.0
specifier: ^15.11.0
version: 15.11.0
turndown:
specifier: ^7.1.3
version: 7.2.0
@ -7483,8 +7483,8 @@ packages:
engines: {node: '>=8'}
dev: true
/stripe@12.18.0:
resolution: {integrity: sha512-cYjgBM2SY/dTm8Lr6eMyyONaHTZHA/QjHxFUIW5WH8FevSRIGAVtXEmBkUXF1fsqe7QvvRgQSGSJZmjDacegGg==}
/stripe@15.11.0:
resolution: {integrity: sha512-qmZF0PN1jRVpiQrXL8eTb9Jy/6S+aUlcDquKBFT2h3PkaD7RZ444FIojVXUg67FK2zFIUNXgMv02c7csdL5qHg==}
engines: {node: '>=12.*'}
dependencies:
'@types/node': 20.14.1

View File

@ -8,6 +8,26 @@ const FREE_CREDITS = 500;
export async function billTeam(team_id: string, credits: number) {
return withAuth(supaBillTeam)(team_id, credits);
}
// Initialize Stripe with your API key
const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);
async function trackUsage(customer_id: string, quantity: number): Promise<void> {
try {
await stripe.billing.meterEvents.create({
event_name: 'credits',
payload: {
value: quantity.toString(),
stripe_customer_id: customer_id,
},
});
} catch (error) {
console.error('Error creating usage record:', error);
// throw new Error('Error creating usage record');
}
}
export async function supaBillTeam(team_id: string, credits: number) {
if (team_id === "preview") {
return { success: true, message: "Preview team, no credits used" };
@ -153,6 +173,19 @@ export async function supaBillTeam(team_id: string, credits: number) {
return await createCreditUsage({ team_id, credits });
}
if(subscription.is_usage){
// console.log('subscription', subscription);
const { data: customer } = await supabase_service
.from("customers")
.select("*")
.eq("id", subscription.user_id)
.single();
if (customer) {
await trackUsage(customer.stripe_customer_id, credits);
}
}
return await createCreditUsage({
team_id,
subscription_id: subscription.id,