Download src/lib/services/billing.js from NeoPy/hugging-c-ai: direct link, hf CLI and curl.
- Browser
- Download file 1.55 kB
-
https://huggingface.co/NeoPy/hugging-c-ai/resolve/main/src/lib/services/billing.js
- Command line
-
hf download hf://NeoPy/hugging-c-ai/src/lib/services/billing.js
-
curl -L -o billing.js https://huggingface.co/NeoPy/hugging-c-ai/resolve/main/src/lib/services/billing.js
1.55 kB
| import { stripe } from "../stripe"; | |
| import config from "../config"; | |
| import { UserService } from "./user"; | |
| export const BillingService = { | |
| async createCheckoutSession(userId, planId) { | |
| const plan = config.stripe.plans[planId]; | |
| if (!plan) throw new Error("Invalid plan selected"); | |
| const session = await stripe.checkout.sessions.create({ | |
| payment_method_types: ["card"], | |
| line_items: [ | |
| { | |
| price_data: { | |
| currency: "usd", | |
| product_data: { | |
| name: `${config.stripe.plans[planId].name}`, | |
| description: `Purchase ${plan.credits} credits to perform AI generations.`, | |
| }, | |
| unit_amount: plan.price, | |
| }, | |
| quantity: 1, | |
| }, | |
| ], | |
| mode: "payment", | |
| success_url: `${config.auth.url}/pricing?success=true`, | |
| cancel_url: `${config.auth.url}/pricing?canceled=true`, | |
| metadata: { userId, credits: plan.credits.toString() }, | |
| }); | |
| return session.url; | |
| }, | |
| async handleWebhook(body, signature) { | |
| const event = stripe.webhooks.constructEvent(body, signature, config.stripe.webhookSecret); | |
| if (event.type === "checkout.session.completed") { | |
| const session = event.data.object; | |
| const userId = session.metadata.userId; | |
| const credits = parseInt(session.metadata.credits || "0", 10); | |
| if (userId && credits > 0) { | |
| await UserService.addCredits(userId, credits); | |
| return { success: true, userId, credits }; | |
| } | |
| } | |
| return { success: false }; | |
| } | |
| }; | |