-
Notifications
You must be signed in to change notification settings - Fork 219
Expand file tree
/
Copy pathinitiate.ts
More file actions
116 lines (100 loc) · 3.37 KB
/
initiate.ts
File metadata and controls
116 lines (100 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { NextApiRequest, NextApiResponse } from "next";
import constants from "../../../config/constants";
import { responses } from "../../../config/strings";
import CourseModel, { Course } from "../../../models/Course";
import { getPaymentMethod } from "../../../payments";
import PurchaseModel from "../../../models/Purchase";
import finalizePurchase from "../../../lib/finalize-purchase";
import { error } from "../../../services/logger";
import UserModel from "@models/User";
import DomainModel, { Domain } from "@models/Domain";
import { auth } from "@/auth";
const { transactionSuccess, transactionFailed, transactionInitiated } =
constants;
export default async function handler(
req: NextApiRequest,
res: NextApiResponse,
) {
if (req.method !== "POST") {
return res.status(405).json({ message: "Not allowed" });
}
const domain = await DomainModel.findOne<Domain>({
name: req.headers.domain,
});
if (!domain) {
return res.status(404).json({ message: "Domain not found" });
}
const session = await auth(req, res);
let user;
if (session) {
user = await UserModel.findOne({
email: session.user!.email,
domain: domain._id,
active: true,
});
}
if (!user) {
return res.status(401).json({});
}
const { body } = req;
const { courseid, metadata } = body;
if (!courseid) {
return res.status(400).json({ error: responses.invalid_course_id });
}
try {
const course: Course | null = await CourseModel.findOne({
courseId: courseid,
domain: domain._id,
});
if (!course) {
return res.status(404).json({ error: responses.item_not_found });
}
const buyer = user!;
if (
buyer.purchases.some(
(purchase) => purchase.courseId === course.courseId,
)
) {
return res.status(200).json({
status: transactionSuccess,
});
}
if (course.cost === 0) {
try {
await finalizePurchase(user!.userId, course!.courseId);
return res.status(200).json({
status: transactionSuccess,
});
} catch (err: any) {
return res.status(500).json({ error: err.message });
}
}
const siteinfo = domain.settings;
const paymentMethod = await getPaymentMethod(domain!._id.toString());
const purchase = await PurchaseModel.create({
domain: domain._id.toString(),
courseId: course.courseId,
purchasedBy: user!.userId,
paymentMethod: paymentMethod.getName(),
amount: course.cost * 100,
currencyISOCode: siteinfo.currencyISOCode,
});
const paymentTracker = await paymentMethod.initiate({
course,
metadata: JSON.parse(metadata),
purchaseId: purchase.orderId,
});
purchase.paymentId = paymentTracker;
await purchase.save();
res.status(200).json({
status: transactionInitiated,
paymentTracker,
});
} catch (err: any) {
error(err.message, { stack: err.stack });
res.status(500).json({
status: transactionFailed,
error: err.message,
});
}
}