-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrefactor from sky.js
187 lines (162 loc) · 6.19 KB
/
refactor from sky.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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
const axios = require('axios');
const { generateBearerTokenFromCreds } = require("skyflow-node");
const crypto = require("crypto");
exports.skyflowmain = async (event) => {
let functionResponse = {
bodyBytes: "",
headers: {
"Content-Type": "application/json",
"Error-From-Client": "false",
},
statusCode: 200,
};
try {
const buf = Buffer.from(event.BodyContent, 'base64')
const data = JSON.parse(buf)
const detokenizedCreditCard = await detokenizeCreditCardInfo(data.requestBulk.Transaction.Creditcard, event);
if (!detokenizedCreditCard.success) {
functionResponse.bodyBytes = JSON.stringify(detokenizedCreditCard)
functionResponse.statusCode = 500
return functionResponse
}
data.requestBulk.Transaction.Creditcard.Number = detokenizedCreditCard.Number;
data.requestBulk.Transaction.Creditcard.ExpMonth = detokenizedCreditCard.ExpMonth;
data.requestBulk.Transaction.Creditcard.ExpYear = detokenizedCreditCard.ExpYear;
data.requestBulk.Transaction.Creditcard.CvvCsc = detokenizedCreditCard.CvvCsc;
const responseBulk = await flowBulk(data.requestBulk, data.keyIntegration);
functionResponse.bodyBytes = JSON.stringify(responseBulk);
return functionResponse
} catch (err) {
if (err.response) {
functionResponse.bodyBytes = JSON.stringify(err.response.data);
functionResponse.statusCode = err.response.status;
functionResponse.headers["Error-From-Client"] = "true";
} else {
functionResponse.bodyBytes = `Internal error: ${JSON.stringify(err.message)}`;
functionResponse.statusCode = 500;
}
return functionResponse
}
};
const flowBulk = async (requestBulk, keyIntegration) => {
const jsonString = JSON.stringify(requestBulk);
const aesEncryption = createAESEncryption(keyIntegration.seedAES);
const requestBulkEncryptedData = aesEncryption.encrypt(jsonString);
try {
const responseBulkEncryptedData = await clientBulkFlow(requestBulkEncryptedData, keyIntegration.data0);
const responseBulkDecryptedData = JSON.parse(aesEncryption.decrypt(responseBulkEncryptedData));
const transactionStatus = responseBulkDecryptedData && 'CdResponse' in responseBulkDecryptedData && responseBulkDecryptedData.CdResponse.trim() === '0C' ? 'success' : 'declined';
return {
data: responseBulkDecryptedData,
transactionStatus: transactionStatus
};
} catch (error) {
throw error;
}
};
const clientBulkFlow = async(encryptedData, data0) => {
const url = process.env.pspUrl;
const requestData = JSON.stringify({
"data0": data0,
"data": encryptedData
});
try {
const response = await axios({
method: 'post',
url: url,
data: requestData,
headers: {
'Content-Type': 'application/json'
}
});
return response.data;
} catch (error) {
throw error;
}
}
function createAESEncryption(keyHex) {
if (keyHex.length !== 32) {
throw new Error('Invalid key length for AES-128. Key must be 32 hexadecimal characters (16 bytes).');
}
const key = Buffer.from(keyHex, 'hex');
function encrypt(plaintext) {
const iv = crypto.randomBytes(16);
const cipher = crypto.createCipheriv('aes-128-cbc', key, iv);
let encrypted = cipher.update(plaintext, 'utf8', 'base64');
encrypted += cipher.final('base64');
const encryptedText = Buffer.concat([iv, Buffer.from(encrypted, 'base64')]).toString('base64');
return encryptedText;
}
function decrypt(encryptedData) {
const encryptedBuffer = Buffer.from(encryptedData, 'base64');
const iv = encryptedBuffer.slice(0, 16);
const ciphertext = encryptedBuffer.slice(16);
const decipher = crypto.createDecipheriv('aes-128-cbc', key, iv);
let decrypted = decipher.update(ciphertext, 'base64', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
}
return { encrypt, decrypt };
}
const detokenizeCreditCardInfo = async (creditCard, event) => {
const tokenizedFields = [
{ fieldName: "Number", fieldValue: creditCard.Number },
{ fieldName: "ExpMonth", fieldValue: creditCard.ExpMonth },
{ fieldName: "ExpYear", fieldValue: creditCard.ExpYear },
{ fieldName: "CvvCsc", fieldValue: creditCard.CvvCsc }
];
const { credentials, vaultURL, vaultID } = process.env;
const requestId = event.Headers?.["X-Request-Id"]?.[0];
const detokenizedData = await getDetokenizedFields(
tokenizedFields,
credentials,
vaultURL,
vaultID,
requestId
);
return detokenizedData;
}
const getDetokenizedFields = async (
tokenizedFields,
credentials,
vaultURL,
vaultID,
requestId
) => {
const token = await generateBearerTokenFromCreds(credentials);
const endpoint = `${vaultURL}/v1/vaults/${vaultID}/detokenize`;
const detokenizationParameters = tokenizedFields.map(field => ({
token: field.fieldValue,
redaction: "PLAIN_TEXT",
}));
try {
const response = await axios.post(
endpoint,
{
detokenizationParameters: detokenizationParameters,
},
{
headers: {
"x-request-id": requestId,
"Content-Type": "application/json",
Authorization: `Bearer ${token.accessToken}`
},
}
);
const detokenizedData = {};
response.data.records.forEach(record => {
const fieldName = tokenizedFields.find(f => f.fieldValue === record.token).fieldName;
detokenizedData[fieldName] = record.value;
});
return {
...detokenizedData,
success: true,
};
} catch (error) {
console.error('Detokenization error:', error);
return {
error: error.response?.data || "Detokenization failed",
success: false,
};
}
};