Skip to content

50+ open-source generative AI apps you can clone, deploy, and monetize — image generators, video tools, virtual try-ons, AI SaaS templates, and platform integrations. One-click Vercel deploy on every template.

Restore: src/lib/services/billing.js

Anil Chandra Naidu Matcha6600e8e80254
diff --git a/src/lib/services/billing.js b/src/lib/services/billing.js
new file mode 100644
index 0000000..4ca83a9
--- /dev/null
+++ b/src/lib/services/billing.js
@@ -0,0 +1,48 @@
+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 };
+ }
+};