-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpayment.ts
77 lines (71 loc) · 2.53 KB
/
payment.ts
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
/*
* Copyright (c) 2014-2022 Bjoern Kimminich & the OWASP Juice Shop contributors.
* SPDX-License-Identifier: MIT
*/
import { Request, Response, NextFunction } from 'express'
import { CardModel } from '../models/card'
interface displayCard{
UserId: number
id: number
fullName: string
cardNum: string
expMonth: number
expYear: number
}
module.exports.getPaymentMethods = function getPaymentMethods () {
return async (req: Request, res: Response, next: NextFunction) => {
const displayableCards: displayCard[] = []
const cards = await CardModel.findAll({ where: { UserId: req.body.UserId } })
cards.forEach(card => {
const displayableCard: displayCard = {
UserId: card.UserId,
id: card.id,
fullName: card.fullName,
cardNum: '',
expMonth: card.expMonth,
expYear: card.expYear
}
const cardNumber = String(card.cardNum)
displayableCard.cardNum = '*'.repeat(12) + cardNumber.substring(cardNumber.length - 4)
displayableCards.push(displayableCard)
})
res.status(200).json({ status: 'success', data: displayableCards })
}
}
module.exports.getPaymentMethodById = function getPaymentMethodById () {
return async (req: Request, res: Response, next: NextFunction) => {
const card = await CardModel.findOne({ where: { id: req.params.id, UserId: req.body.UserId } })
const displayableCard: displayCard = {
UserId: 0,
id: 0,
fullName: '',
cardNum: '',
expMonth: 0,
expYear: 0
}
if (card) {
displayableCard.UserId = card.UserId
displayableCard.id = card.id
displayableCard.fullName = card.fullName
displayableCard.expMonth = card.expMonth
displayableCard.expYear = card.expYear
const cardNumber = String(card.cardNum)
displayableCard.cardNum = '*'.repeat(12) + cardNumber.substring(cardNumber.length - 4)
}
if (card && displayableCard) {
res.status(200).json({ status: 'success', data: displayableCard })
} else {
res.status(400).json({ status: 'error', data: 'Malicious activity detected' })
}
}
}
module.exports.delPaymentMethodById = function delPaymentMethodById () {
return async (req: Request, res: Response, next: NextFunction) => {
const card = await CardModel.destroy({ where: { id: req.params.id, UserId: req.body.UserId } })
if (card) {
res.status(200).json({ status: 'success', data: 'Card deleted successfully.' })
} else {
res.status(400).json({ status: 'error', data: 'Malicious activity detected.' })
}
}
}