stripe
This commit is contained in:
1 parent
01c0e9ef4d
commit
1b162578c2
22 files changed
+450
-72
No files matched your search
@@ -12,6 +12,11 @@ export interface IBillingController {
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
) => Promise<void>;
|
||||
confirmPlan: (
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
) => Promise<void>;
|
||||
}
|
||||
|
||||
class BillingController implements IBillingController {
|
||||
@@ -38,6 +43,7 @@ class BillingController implements IBillingController {
|
||||
if (!orgId) throw new ApiError("Organization not found", 404);
|
||||
|
||||
const planKey: PlanKey | undefined = req.body.planKey;
|
||||
|
||||
const redirectUrl = await this.billingService.subscribePlan(
|
||||
user.email,
|
||||
orgId,
|
||||
@@ -53,6 +59,44 @@ class BillingController implements IBillingController {
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
cancelPlan = async (req: Request, res: Response, next: NextFunction) => {
|
||||
try {
|
||||
const orgId = req.user?.orgId;
|
||||
if (!orgId) throw new ApiError("Organization not found", 404);
|
||||
|
||||
const redirectUrl = await this.billingService.cancelSubscription(orgId);
|
||||
|
||||
res.status(200).json({
|
||||
message: "Subscription cancellation successful",
|
||||
data: { redirectUrl },
|
||||
});
|
||||
return;
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
confirmPlan = async (req: Request, res: Response, next: NextFunction) => {
|
||||
try {
|
||||
const orgId = req.user?.orgId;
|
||||
if (!orgId) throw new ApiError("Organization not found", 404);
|
||||
|
||||
const expectedPlanKey = req.query.plan as PlanKey;
|
||||
const success = await this.billingService.confirmPlan(
|
||||
orgId,
|
||||
expectedPlanKey
|
||||
);
|
||||
|
||||
res.status(200).json({
|
||||
message: "Plan confirmation endpoint hit",
|
||||
data: { success },
|
||||
});
|
||||
return;
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export default BillingController;
|
||||
@@ -1,46 +1,27 @@
|
||||
import type { Request, Response, NextFunction } from "express";
|
||||
import { StripeService } from "@/services/index.js";
|
||||
import { config } from "@/config/index.js";
|
||||
|
||||
export interface IStripeController {
|
||||
webhook: (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
||||
}
|
||||
|
||||
class StripeController implements IStripeController {
|
||||
private stripeService: StripeService;
|
||||
|
||||
constructor(stripeService: StripeService) {
|
||||
this.stripeService = stripeService;
|
||||
}
|
||||
webhook = async (req: Request, res: Response, next: NextFunction) => {
|
||||
try {
|
||||
const sig = req.headers["stripe-signature"] as string | undefined;
|
||||
const raw = req.body as unknown; // Buffer when express.raw is used
|
||||
|
||||
let event: any = undefined;
|
||||
if (Buffer.isBuffer(raw)) {
|
||||
const json = raw.toString("utf8");
|
||||
try {
|
||||
event = JSON.parse(json);
|
||||
} catch (_e) {
|
||||
res.status(401).json({ message: "Unauthorized" });
|
||||
}
|
||||
} else if (typeof raw === "string") {
|
||||
try {
|
||||
event = JSON.parse(raw);
|
||||
} catch (_e) {
|
||||
res.status(400).json({ message: "Invalid JSON payload" });
|
||||
}
|
||||
} else if (raw && typeof raw === "object") {
|
||||
event = raw;
|
||||
let event = req.body;
|
||||
const signature = req.headers["stripe-signature"];
|
||||
if (!signature || Array.isArray(signature)) {
|
||||
throw new Error("Stripe signature is missing or invalid");
|
||||
}
|
||||
|
||||
// Basic routing by event type (no-ops for now)
|
||||
const type = event?.type;
|
||||
switch (type) {
|
||||
case "checkout.session.completed":
|
||||
case "customer.subscription.updated":
|
||||
case "customer.subscription.deleted":
|
||||
case "invoice.payment_succeeded":
|
||||
case "invoice.payment_failed":
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
res.status(200);
|
||||
const result = await this.stripeService.webhook(event, signature);
|
||||
res.status(200).send({ received: true });
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
@@ -147,8 +147,6 @@ class TeamMemberController implements ITeamMemberController {
|
||||
throw new ApiError("No team member ID", 400);
|
||||
}
|
||||
|
||||
console.log(teamMemberId);
|
||||
|
||||
const deleted = await this.teamMemberService.delete(orgId, teamMemberId);
|
||||
invalidateCachesForUser(teamMemberId);
|
||||
if (!deleted) {
|
||||
|
||||
Reference in new issue
Block a user