-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathterminalLocalAPI.ts
82 lines (73 loc) · 3.11 KB
/
terminalLocalAPI.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
78
79
80
81
82
/*
* ######
* ######
* ############ ####( ###### #####. ###### ############ ############
* ############# #####( ###### #####. ###### ############# #############
* ###### #####( ###### #####. ###### ##### ###### ##### ######
* ###### ###### #####( ###### #####. ###### ##### ##### ##### ######
* ###### ###### #####( ###### #####. ###### ##### ##### ######
* ############# ############# ############# ############# ##### ######
* ############ ############ ############# ############ ##### ######
* ######
* #############
* ############
* Adyen NodeJS API Library
* Copyright (c) 2021 Adyen B.V.
* This file is open source and available under the MIT license.
* See the LICENSE file for more info.
*/
import Service from "../service";
import Client from "../client";
import getJsonResponse from "../helpers/getJsonResponse";
import NexoCrypto from "../security/nexoCrypto";
import LocalRequest from "./resource/terminal/local/localRequest";
import {
ObjectSerializer,
SecurityKey,
TerminalApiRequest,
TerminalApiResponse,
SaleToPOISecuredMessage,
TerminalApiSecuredRequest,
TerminalApiSecuredResponse
} from "../typings/terminal/models";
class TerminalLocalAPI extends Service {
private readonly localRequest: LocalRequest;
private nexoCrypto: NexoCrypto;
public constructor(client: Client) {
super(client);
this.apiKeyRequired = true;
this.localRequest = new LocalRequest(this);
this.nexoCrypto = new NexoCrypto();
}
public async request(
terminalApiRequest: TerminalApiRequest,
securityKey: SecurityKey,
): Promise<TerminalApiResponse> {
const formattedRequest = ObjectSerializer.serialize(terminalApiRequest, "TerminalApiRequest");
const saleToPoiSecuredMessage: SaleToPOISecuredMessage = NexoCrypto.encrypt(
terminalApiRequest.SaleToPOIRequest.MessageHeader,
JSON.stringify(formattedRequest),
securityKey,
);
const securedPaymentRequest: TerminalApiSecuredRequest = ObjectSerializer.serialize({
SaleToPOIRequest: saleToPoiSecuredMessage,
}, "TerminalApiSecuredRequest");
const jsonResponse = await getJsonResponse<TerminalApiSecuredRequest, TerminalApiResponse>(
this.localRequest,
securedPaymentRequest
);
// Catch an empty jsonResponse (i.e. Abort Request)
if(!jsonResponse) {
return new TerminalApiResponse();
} else {
const terminalApiSecuredResponse: TerminalApiSecuredResponse =
ObjectSerializer.deserialize(jsonResponse, "TerminalApiSecuredResponse");
const response = this.nexoCrypto.decrypt(
terminalApiSecuredResponse.SaleToPOIResponse,
securityKey,
);
return ObjectSerializer.deserialize(JSON.parse(response), "TerminalApiResponse");
}
}
}
export default TerminalLocalAPI;