Skip to content

Commit ccfa1d1

Browse files
authored
Merge pull request #1527 from Adyen/predefinedcontent-referenceid-helper
TerminalAPI: PredefinedContentHelper for managing Display Events
2 parents 60ceb8c + cd0bdd3 commit ccfa1d1

File tree

4 files changed

+56
-37
lines changed

4 files changed

+56
-37
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { PredefinedContentHelper, DisplayNotificationEvent } from "../typings/terminal/predefinedContentHelper";
2+
3+
describe("PredefinedContentHelper", () => {
4+
it("should extract a valid event", () => {
5+
6+
const ReferenceID = "TransactionID=oLkO001517998574000&TimeStamp=2018-02-07T10%3a16%3a14.000Z&event=PIN_ENTERED";
7+
8+
const helper = new PredefinedContentHelper(ReferenceID);
9+
expect(helper.getEvent()).toBe(DisplayNotificationEvent.PIN_ENTERED);
10+
});
11+
12+
it("should return null for an invalid event", () => {
13+
const helper = new PredefinedContentHelper("event=INVALID_EVENT");
14+
expect(helper.getEvent()).toBeNull();
15+
});
16+
17+
it("should extract TransactionID", () => {
18+
const helper = new PredefinedContentHelper("TransactionID=12345&TimeStamp=2018-02-07T10%3a16%3a14.000Z&event=PIN_ENTERED");
19+
expect(helper.getTransactionId()).toBe("12345");
20+
});
21+
22+
it("should extract query parameter", () => {
23+
const helper = new PredefinedContentHelper("TimeStamp=2024-07-11T12:00:00Z");
24+
expect(helper.getTimeStamp()).toBe("2024-07-11T12:00:00Z");
25+
});
26+
27+
it("should extract arbitrary key", () => {
28+
const helper = new PredefinedContentHelper("foo=bar&baz=qux");
29+
expect(helper.get("foo")).toBe("bar");
30+
expect(helper.get("baz")).toBe("qux");
31+
expect(helper.get("missing")).toBeNull();
32+
});
33+
34+
it("should convert params to object", () => {
35+
const helper = new PredefinedContentHelper("a=1&b=2&event=WAIT_FOR_PIN");
36+
expect(helper.toObject()).toEqual({ a: "1", b: "2", event: "WAIT_FOR_PIN" });
37+
});
38+
39+
it("should handle empty referenceId", () => {
40+
const helper = new PredefinedContentHelper("");
41+
expect(helper.getEvent()).toBeNull();
42+
expect(helper.getTransactionId()).toBeNull();
43+
expect(helper.getTimeStamp()).toBeNull();
44+
expect(helper.toObject()).toEqual({});
45+
});
46+
});

src/__tests__/terminalCloudAPI.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { asyncRes } from "../__mocks__/terminalApi/async";
44
import { syncRefund, syncRes, syncResEventNotification, syncResEventNotificationWithAdditionalAttributes, syncResEventNotificationWithUnknownEnum } from "../__mocks__/terminalApi/sync";
55
import Client from "../client";
66
import TerminalCloudAPI from "../services/terminalCloudAPI";
7-
import { terminal} from "../typings";
7+
import { terminal } from "../typings";
88

99
let client: Client;
1010
let terminalCloudAPI: TerminalCloudAPI;

src/typings/terminal/models.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,8 @@ export * from "./transmitResponse";
265265
export * from "./uTMCoordinates";
266266
export * from "./unitOfMeasureType";
267267
export * from "./versionType";
268+
// helpers
269+
export * from "./predefinedContentHelper";
268270

269271
import { AbortRequest } from "./abortRequest";
270272
import { AccountType } from "./accountType";

src/typings/terminal/predefinedContentHelper.ts

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,43 +15,17 @@ export class PredefinedContentHelper {
1515
*
1616
* @example
1717
* const helper = new PredefinedContentHelper("...&event=PIN_ENTERED");
18-
* const event = helper.getEvent(); // "PIN_ENTERED" or null
18+
* const event = helper.getEvent(); // DisplayNotificationEvent.PIN_ENTERED or null
1919
*/
2020
getEvent(): DisplayNotificationEvent | null {
2121
const event = this.params.get("event");
2222

23-
switch (event) {
24-
case "TENDER_CREATED":
25-
case "CARD_INSERTED":
26-
case "CARD_PRESENTED":
27-
case "CARD_SWIPED":
28-
case "WAIT_FOR_APP_SELECTION":
29-
case "APPLICATION_SELECTED":
30-
case "ASK_SIGNATURE":
31-
case "CHECK_SIGNATURE":
32-
case "SIGNATURE_CHECKED":
33-
case "WAIT_FOR_PIN":
34-
case "PIN_ENTERED":
35-
case "PRINT_RECEIPT":
36-
case "RECEIPT_PRINTED":
37-
case "CARD_REMOVED":
38-
case "TENDER_FINAL":
39-
case "ASK_DCC":
40-
case "DCC_ACCEPTED":
41-
case "DCC_REJECTED":
42-
case "ASK_GRATUITY":
43-
case "GRATUITY_ENTERED":
44-
case "BALANCE_QUERY_STARTED":
45-
case "BALANCE_QUERY_COMPLETED":
46-
case "LOAD_STARTED":
47-
case "LOAD_COMPLETED":
48-
case "PROVIDE_CARD_DETAILS":
49-
case "CARD_DETAILS_PROVIDED":
50-
return event as DisplayNotificationEvent;
51-
default:
52-
return null;
23+
if (event && Object.values(DisplayNotificationEvent).includes(event as DisplayNotificationEvent)) {
24+
return event as DisplayNotificationEvent;
5325
}
26+
return null;
5427
}
28+
5529
getTransactionId(): string | null {
5630
return this.params.get("TransactionID");
5731
}
@@ -65,14 +39,11 @@ export class PredefinedContentHelper {
6539
}
6640

6741
toObject(): Record<string, string> {
68-
const result: Record<string, string> = {};
69-
for (const [key, value] of this.params.entries()) {
70-
result[key] = value;
71-
}
72-
return result;
42+
return Object.fromEntries(this.params);
7343
}
7444
}
7545

46+
// Supported events for display notifications
7647
export enum DisplayNotificationEvent {
7748
TENDER_CREATED = "TENDER_CREATED",
7849
CARD_INSERTED = "CARD_INSERTED",

0 commit comments

Comments
 (0)