-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathstorage.js
91 lines (71 loc) · 2.75 KB
/
storage.js
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
// storage.js
// Local storage for storing payments used in the Admin Panel
// ========
// payments saved in memory
// contains a list of PaymentModel (pre-authorisations)
// each PaymentModel contains a list of PaymentDetailsModel (each operation performed
// after pre-authorisation - adjust|extend|capture|reversal)
var payments = [];
class PaymentModel {
constructor(merchantReference, pspReference, amount, currency, bookingDate, expiryDate, paymentMethodBrand, paymentDetailsModelList) {
this.merchantReference = merchantReference;
this.pspReference = pspReference;
this.amount = amount;
this.currency = currency;
this.bookingDate = bookingDate;
this.expiryDate = expiryDate;
this.paymentMethodBrand = paymentMethodBrand;
this.paymentDetailsModelList = paymentDetailsModelList
}
}
class PaymentDetailsModel {
constructor(merchantReference, pspReference, originalReference, amount, currency, dateTime, eventCode, refusalReason, paymentMethodBrand, success) {
this.merchantReference = merchantReference;
this.pspReference = pspReference;
this.originalReference = originalReference;
this.amount = amount;
this.currency = currency;
this.dateTime = dateTime;
this.eventCode = eventCode;
this.refusalReason = refusalReason;
this.paymentMethodBrand = paymentMethodBrand;
this.success = success;
}
}
// get all payments
const getAll = () => {
return payments;
}
// get payment by merchantReference
const getByMerchantReference = (merchantReference) => {
return payments.find(obj => obj.merchantReference === merchantReference);
}
// add new PaymentDetailsModel to history
const addToHistory = (paymentDetailsModel) => {
if(paymentDetailsModel.merchantReference == null) {
throw Error("Merchant Reference is undefined");
}
// get payment by merchant reference
const paymentModel = getByMerchantReference(paymentDetailsModel.merchantReference);
if(paymentModel == null) {
console.log("Payment not found in storage - Reference: " + paymentDetailsModel.merchantReference);
} else {
// add to history
paymentModel.paymentDetailsModelList.push(paymentDetailsModel);
paymentModel.lastUpdated = new Date();
}
}
// update payment given its merchantReference
const updatePayment = (merchantReference, amount, expiryDate) => {
var index = payments.findIndex(x => x.merchantReference === merchantReference);
if(index >= 0) {
payments[index].amount = amount;
payments[index].expiryDate = expiryDate;
} else {
console.log("Payment not found in storage - Reference: " + merchantReference);
}
}
const put = (paymentModel) => {
payments.push(paymentModel)
}
module.exports = { PaymentModel, PaymentDetailsModel, getAll, getByMerchantReference, addToHistory, updatePayment, put }