From aa7ab05b3e5004581bbc3d9dd0b7b29f982d4d58 Mon Sep 17 00:00:00 2001 From: beppe Date: Mon, 26 Jan 2026 13:59:42 +0100 Subject: [PATCH] Handle prefix correctly --- src/__tests__/service.spec.ts | 279 +++++++++++++++++++++++++++++++++- src/service.ts | 16 +- 2 files changed, 283 insertions(+), 12 deletions(-) diff --git a/src/__tests__/service.spec.ts b/src/__tests__/service.spec.ts index 31a828008..b80d494c5 100644 --- a/src/__tests__/service.spec.ts +++ b/src/__tests__/service.spec.ts @@ -38,7 +38,7 @@ describe("Service", () => { client.config.environment = EnvironmentEnum.LIVE; const service = new TestService(client); - expect(() => service.testCreateBaseUrl("https://pal-live.adyen.com/pal/servlet/")) + expect(() => service.testCreateBaseUrl("https://pal-test.adyen.com/pal/servlet/")) .toThrow("Live endpoint URL prefix must be provided for LIVE environment."); }); @@ -54,7 +54,7 @@ describe("Service", () => { client.config.environment = EnvironmentEnum.LIVE; const service = new TestService(client); - expect(() => service.testCreateBaseUrl("https://pal-live.adyen.com/pal/servlet/")) + expect(() => service.testCreateBaseUrl("https://pal-test.adyen.com/pal/servlet/")) .toThrow("Live endpoint URL prefix must be provided for LIVE environment."); }); @@ -119,6 +119,22 @@ describe("Service", () => { ); }); + it("should throw error if liveEndpointUrlPrefix is empty for possdk", () => { + // create Client for TEST environment without liveEndpointUrlPrefix + const config = new Config({ + apiKey: "test_key", + environment: EnvironmentEnum.TEST, + liveEndpointUrlPrefix: "" + }); + client = new Client(config); + // change to LIVE + client.config.environment = EnvironmentEnum.LIVE; + + const service = new TestService(client); + expect(() => service.testCreateBaseUrl("https://checkout-test.adyen.com/possdk/v68")) + .toThrow("Live endpoint URL prefix must be provided for LIVE environment."); + }); + it("should replace -test with -live for other URLs", () => { const config = new Config({ apiKey: "test_key", @@ -132,11 +148,11 @@ describe("Service", () => { expect(service.testCreateBaseUrl(url)).toBe("https://some-live.adyen.com/api/"); }); + // SessionAuthentication it("should build TEST url for SessionAuthentication", () => { const config = new Config({ apiKey: "test_key", - environment: EnvironmentEnum.TEST, - liveEndpointUrlPrefix: "mycompany" + environment: EnvironmentEnum.TEST }); client = new Client(config); @@ -148,8 +164,7 @@ describe("Service", () => { it("should build LIVE url for SessionAuthentication", () => { const config = new Config({ apiKey: "test_key", - environment: EnvironmentEnum.LIVE, - liveEndpointUrlPrefix: "mycompany" + environment: EnvironmentEnum.LIVE }); client = new Client(config); @@ -158,4 +173,256 @@ describe("Service", () => { expect(service.testCreateBaseUrl(url)).toBe("https://authe-live.adyen.com/authe/api/v1"); }); + // LEM + it("should build TEST url for LEM", () => { + const config = new Config({ + apiKey: "test_key", + environment: EnvironmentEnum.TEST + }); + client = new Client(config); + + const service = new TestService(client); + const url = "https://kyc-test.adyen.com/lem/v4"; + expect(service.testCreateBaseUrl(url)).toBe("https://kyc-test.adyen.com/lem/v4"); + }); + + it("should build LIVE url for LEM", () => { + const config = new Config({ + apiKey: "test_key", + environment: EnvironmentEnum.LIVE + }); + client = new Client(config); + + const service = new TestService(client); + const url = "https://kyc-test.adyen.com/lem/v4"; + expect(service.testCreateBaseUrl(url)).toBe("https://kyc-live.adyen.com/lem/v4"); + }); + + // BalancePlatform + it("should build TEST url for BalancePlatform", () => { + const config = new Config({ + apiKey: "test_key", + environment: EnvironmentEnum.TEST + }); + client = new Client(config); + + const service = new TestService(client); + const url = "https://balanceplatform-api-test.adyen.com/bcl/v2"; + expect(service.testCreateBaseUrl(url)).toBe("https://balanceplatform-api-test.adyen.com/bcl/v2"); + }); + + it("should build LIVE url for BalancePlatform", () => { + const config = new Config({ + apiKey: "test_key", + environment: EnvironmentEnum.LIVE + }); + client = new Client(config); + + const service = new TestService(client); + const url = "https://balanceplatform-api-test.adyen.com/bcl/v2"; + expect(service.testCreateBaseUrl(url)).toBe("https://balanceplatform-api-live.adyen.com/bcl/v2"); + }); + + // Management + it("should build TEST url for Management", () => { + const config = new Config({ + apiKey: "test_key", + environment: EnvironmentEnum.TEST + }); + client = new Client(config); + + const service = new TestService(client); + const url = "https://management-test.adyen.com/v3"; + expect(service.testCreateBaseUrl(url)).toBe("https://management-test.adyen.com/v3"); + }); + + it("should build LIVE url for Management", () => { + const config = new Config({ + apiKey: "test_key", + environment: EnvironmentEnum.LIVE + }); + client = new Client(config); + + const service = new TestService(client); + const url = "https://management-test.adyen.com/v3"; + expect(service.testCreateBaseUrl(url)).toBe("https://management-live.adyen.com/v3"); + }); + + // Transfers + it("should build TEST url for Transfers", () => { + const config = new Config({ + apiKey: "test_key", + environment: EnvironmentEnum.TEST + }); + client = new Client(config); + + const service = new TestService(client); + const url = "https://balanceplatform-api-test.adyen.com/btl/v4"; + expect(service.testCreateBaseUrl(url)).toBe("https://balanceplatform-api-test.adyen.com/btl/v4"); + }); + + it("should build LIVE url for Transfers", () => { + const config = new Config({ + apiKey: "test_key", + environment: EnvironmentEnum.LIVE + }); + client = new Client(config); + + const service = new TestService(client); + const url = "https://balanceplatform-api-test.adyen.com/btl/v4"; + expect(service.testCreateBaseUrl(url)).toBe("https://balanceplatform-api-live.adyen.com/btl/v4"); + }); + + // BinLookup + it("should build TEST url for BinLookup", () => { + const config = new Config({ + apiKey: "test_key", + environment: EnvironmentEnum.TEST + }); + client = new Client(config); + + const service = new TestService(client); + const url = "https://pal-test.adyen.com/pal/servlet/BinLookup/v54"; + expect(service.testCreateBaseUrl(url)).toBe("https://pal-test.adyen.com/pal/servlet/BinLookup/v54"); + }); + + it("should build LIVE url for BinLookup", () => { + const config = new Config({ + apiKey: "test_key", + environment: EnvironmentEnum.LIVE, + liveEndpointUrlPrefix: "mycompany" + }); + client = new Client(config); + + const service = new TestService(client); + const url = "https://pal-test.adyen.com/pal/servlet/BinLookup/v54"; + expect(service.testCreateBaseUrl(url)).toBe("https://mycompany-pal-live.adyenpayments.com/pal/servlet/BinLookup/v54"); + }); + + it("should throw error if liveEndpointUrlPrefix is empty for BinLookup", () => { + // create Client for TEST environment without liveEndpointUrlPrefix + const config = new Config({ + apiKey: "test_key", + environment: EnvironmentEnum.TEST, + liveEndpointUrlPrefix: "" + }); + client = new Client(config); + // change to LIVE + client.config.environment = EnvironmentEnum.LIVE; + + const service = new TestService(client); + expect(() => service.testCreateBaseUrl("https://pal-test.adyen.com/pal/servlet/BinLookup/v54")) + .toThrow("Live endpoint URL prefix must be provided for LIVE environment."); + }); + + // Payout + it("should build TEST url for Payout", () => { + const config = new Config({ + apiKey: "test_key", + environment: EnvironmentEnum.TEST + }); + client = new Client(config); + + const service = new TestService(client); + const url = "https://pal-test.adyen.com/pal/servlet/Payout/v68"; + expect(service.testCreateBaseUrl(url)).toBe("https://pal-test.adyen.com/pal/servlet/Payout/v68"); + }); + + it("should build LIVE url for Payout", () => { + const config = new Config({ + apiKey: "test_key", + environment: EnvironmentEnum.LIVE, + liveEndpointUrlPrefix: "mycompany" + }); + client = new Client(config); + + const service = new TestService(client); + const url = "https://pal-test.adyen.com/pal/servlet/Payout/v68"; + expect(service.testCreateBaseUrl(url)).toBe("https://mycompany-pal-live.adyenpayments.com/pal/servlet/Payout/v68"); + }); + + it("should throw error if liveEndpointUrlPrefix is empty for Payout", () => { + // create Client for TEST environment without liveEndpointUrlPrefix + const config = new Config({ + apiKey: "test_key", + environment: EnvironmentEnum.TEST, + liveEndpointUrlPrefix: "" + }); + client = new Client(config); + // change to LIVE + client.config.environment = EnvironmentEnum.LIVE; + + const service = new TestService(client); + expect(() => service.testCreateBaseUrl("https://pal-test.adyen.com/pal/servlet/Payout/v68")) + .toThrow("Live endpoint URL prefix must be provided for LIVE environment."); + }); + + // Recurring + it("should build TEST url for Recurring", () => { + const config = new Config({ + apiKey: "test_key", + environment: EnvironmentEnum.TEST + }); + client = new Client(config); + + const service = new TestService(client); + const url = "https://pal-test.adyen.com/pal/servlet/Recurring/v68"; + expect(service.testCreateBaseUrl(url)).toBe("https://pal-test.adyen.com/pal/servlet/Recurring/v68"); + }); + + it("should build LIVE url for Recurring", () => { + const config = new Config({ + apiKey: "test_key", + environment: EnvironmentEnum.LIVE, + liveEndpointUrlPrefix: "mycompany" + }); + client = new Client(config); + + const service = new TestService(client); + const url = "https://pal-test.adyen.com/pal/servlet/Recurring/v68"; + expect(service.testCreateBaseUrl(url)).toBe("https://mycompany-pal-live.adyenpayments.com/pal/servlet/Recurring/v68"); + }); + + it("should throw error if liveEndpointUrlPrefix is empty for Recurring", () => { + // create Client for TEST environment without liveEndpointUrlPrefix + const config = new Config({ + apiKey: "test_key", + environment: EnvironmentEnum.TEST, + liveEndpointUrlPrefix: "" + }); + client = new Client(config); + // change to LIVE + client.config.environment = EnvironmentEnum.LIVE; + + const service = new TestService(client); + expect(() => service.testCreateBaseUrl("https://pal-test.adyen.com/pal/servlet/Recurring/v68")) + .toThrow("Live endpoint URL prefix must be provided for LIVE environment."); + }); + + // Open Banking + it("should build TEST url for Open Banking", () => { + const config = new Config({ + apiKey: "test_key", + environment: EnvironmentEnum.TEST + }); + client = new Client(config); + + const service = new TestService(client); + const url = "https://obgateway-test.adyen.com/obgateway/v1"; + expect(service.testCreateBaseUrl(url)).toBe("https://obgateway-test.adyen.com/obgateway/v1"); + }); + + it("should build LIVE url for Open Banking", () => { + const config = new Config({ + apiKey: "test_key", + environment: EnvironmentEnum.LIVE + }); + client = new Client(config); + + const service = new TestService(client); + const url = "https://obgateway-test.adyen.com/obgateway/v1"; + expect(service.testCreateBaseUrl(url)).toBe("https://obgateway-live.adyen.com/obgateway/v1"); + }); + + }); \ No newline at end of file diff --git a/src/service.ts b/src/service.ts index d78d85623..f752f6b67 100644 --- a/src/service.ts +++ b/src/service.ts @@ -48,26 +48,30 @@ class Service { throw new Error("Endpoint URL must be provided."); } + // handle TEST urls if (config.environment !== EnvironmentEnum.LIVE) { return url.replace("-live", "-test"); } - if(config.environment === EnvironmentEnum.LIVE) { - if(!config?.liveEndpointUrlPrefix) { - throw new Error("Live endpoint URL prefix must be provided for LIVE environment."); - } - } - + // handle LIVE urls if (url.includes("/authe/")) { return url.replace("https://test.adyen.com/", "https://authe-live.adyen.com/"); } if (url.includes("pal-")) { + // LIVE pal URL must provide prefix + if(!config.liveEndpointUrlPrefix) { + throw new Error("Live endpoint URL prefix must be provided for LIVE environment."); + } return url.replace("https://pal-test.adyen.com/pal/servlet/", `https://${this.client.config.liveEndpointUrlPrefix}-pal-live.adyenpayments.com/pal/servlet/`); } if (url.includes("checkout-")) { + // LIVE checkout URL must provide prefix + if(!config.liveEndpointUrlPrefix) { + throw new Error("Live endpoint URL prefix must be provided for LIVE environment."); + } if (url.includes("/possdk/v68")) { return url.replace("https://checkout-test.adyen.com/", `https://${this.client.config.liveEndpointUrlPrefix}-checkout-live.adyenpayments.com/`);