Add checkout API

This commit is contained in:
Moe Poi ~ 2021-11-20 09:09:06 +07:00
parent 281dcea2c0
commit 8ff4f43f0b

View file

@ -190,15 +190,13 @@ router.post("/login", async (req, res) => {
let session = JSON.parse(response[0].session);
session.push({
user_agent: req.body.ua || req.headers["user-agent"],
ip:
req.body.ip ||
ip: req.body.ip ||
req.headers["x-forwarded-for"] ||
req.socket.remoteAddress,
session: token,
});
conn.query(
'UPDATE users SET ? WHERE email ="' + req.body.email + '"',
{
'UPDATE users SET ? WHERE email ="' + req.body.email + '"', {
session: JSON.stringify(session),
},
function (err, result) {
@ -289,4 +287,77 @@ router.get("/verify-mail", async (req, res) => {
);
});
router.post("/checkout", async (req, res) => {
if (
!req.body.firstName ||
!req.body.lastName ||
!req.body.phoneNumber ||
!req.body.streetAddress1 ||
!req.body.streetAddress2 ||
!req.body.region ||
!req.body.province ||
!req.body.city ||
!req.body.district ||
!req.body.subDistrict ||
!req.body.postalCode ||
!req.body.logistic ||
!req.body.data
) {
res.status(400);
res.json({
message: "Bad Request",
});
} else {
const conn = db.connect();
var data = {
firstName: req.body.firstName,
lastName: req.body.lastName,
phoneNumber: req.body.phoneNumber,
streetAddress1: req.body.streetAddress1,
streetAddress2: req.body.streetAddress2,
region: req.body.region,
province: req.body.province,
city: req.body.city,
district: req.body.district,
subDistrict: req.body.subDistrict,
postalCode: req.body.postalCode,
logistic: req.body.logistic,
paymentMethod: '-',
data: req.body.data,
userId: 14,
paid: '0',
status: 'pending'
};
conn.query(
"INSERT INTO transactions SET ?",
data,
function (error, response, fields) {
if (error) {
res.status(400);
res.json({
message: "Bad Request",
});
} else {
conn.query(
'SELECT * FROM transactions WHERE id ="' + response.insertId + '"',
function (err, result) {
if (err) {
res.status(400);
res.json({
message: "Bad Request",
});
} else {
res.status(201);
res.json({
'order_id': result[0].id,
'data': result[0].data
});
}
});
}
}
);
}
});
module.exports = router;