-
-
Notifications
You must be signed in to change notification settings - Fork 365
Expand file tree
/
Copy pathorder.controller.js
More file actions
64 lines (56 loc) · 1.48 KB
/
order.controller.js
File metadata and controls
64 lines (56 loc) · 1.48 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
import createError from "../utils/createError.js";
import Order from "../models/order.model.js";
import Gig from "../models/gig.model.js";
import Stripe from "stripe";
export const intent = async (req, res, next) => {
const stripe = new Stripe(process.env.STRIPE);
const gig = await Gig.findById(req.params.id);
const paymentIntent = await stripe.paymentIntents.create({
amount: gig.price * 100,
currency: "usd",
automatic_payment_methods: {
enabled: true,
},
});
const newOrder = new Order({
gigId: gig._id,
img: gig.cover,
title: gig.title,
buyerId: req.userId,
sellerId: gig.userId,
price: gig.price,
payment_intent: paymentIntent.id,
});
await newOrder.save();
res.status(200).send({
clientSecret: paymentIntent.client_secret,
});
};
export const getOrders = async (req, res, next) => {
try {
const orders = await Order.find({
...(req.isSeller ? { sellerId: req.userId } : { buyerId: req.userId }),
isCompleted: true,
});
res.status(200).send(orders);
} catch (err) {
next(err);
}
};
export const confirm = async (req, res, next) => {
try {
const orders = await Order.findOneAndUpdate(
{
payment_intent: req.body.payment_intent,
},
{
$set: {
isCompleted: true,
},
}
);
res.status(200).send("Order has been confirmed.");
} catch (err) {
next(err);
}
};