Skip to content

Commit 4ea4fb6

Browse files
authored
Merge branch 'main' into support-308-redirect
2 parents 6a6b702 + 2c8a8e0 commit 4ea4fb6

File tree

14 files changed

+608
-125
lines changed

14 files changed

+608
-125
lines changed

README.md

Lines changed: 89 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ Check for breaking changes on the [releases page](https://github.com/Adyen/adyen
8989

9090
``` javascript
9191
// Step 1: Require the parts of the module you want to use
92-
const { Client, CheckoutAPI} = require('@adyen/api-library');
92+
const { Client, CheckoutAPI, EnvironmentEnum} = require('@adyen/api-library');
9393

9494
// Step 2: Initialize the client object
95-
const client = new Client({apiKey: "YOUR_API_KEY", environment: "TEST"});
95+
const client = new Client({apiKey: "YOUR_API_KEY", environment: EnvironmentEnum.TEST});
9696

9797
// Step 3: Initialize the API object
9898
const checkoutApi = new CheckoutAPI(client);
@@ -143,7 +143,7 @@ Use the Node.js `require` function to load the `Client` and API objects from the
143143
For example, to use the [Checkout API](https://docs.adyen.com/api-explorer/Checkout/70/overview):
144144

145145
``` javascript
146-
const { Client, CheckoutAPI} = require('@adyen/api-library');
146+
const { Client, CheckoutAPI, EnvironmentEnum} = require('@adyen/api-library');
147147
```
148148

149149
### Step 2: Initialize the client object
@@ -155,7 +155,7 @@ Initialize the client object, passing the following:
155155
For example:
156156

157157
``` javascript
158-
const client = new Client({apiKey: "YOUR_API_KEY", environment: "TEST"});
158+
const client = new Client({apiKey: "YOUR_API_KEY", environment: EnvironmentEnum.TEST});
159159
```
160160

161161
### Step 3: Initialize the API object
@@ -208,18 +208,18 @@ checkoutApi.PaymentsApi.payments(paymentRequest)
208208

209209
For APIS that require your [Live URL Prefix](https://docs.adyen.com/development-resources/live-endpoints#live-url-prefix) (Binlookup, BalanceControl, Checkout, Payout and Recurring) the client is set up as follows in order to start processing live payments:
210210
``` typescript
211-
const { Client } = require('@adyen/api-library');
211+
const { Client, EnvironmentEnum } = require('@adyen/api-library');
212212

213-
const client = new Client({apiKey: "YOUR_API_KEY", environment: "TEST", liveEndpointUrlPrefix: "YOUR_LIVE_URL_PREFIX"});
213+
const client = new Client({apiKey: "YOUR_API_KEY", environment: EnvironmentEnum.LIVE, liveEndpointUrlPrefix: "YOUR_LIVE_URL_PREFIX"});
214214
```
215215

216216
### Usage in TypeScript
217217

218218
Alternatively, you can use the `Types` included in this module for Typescript and `async` syntax.
219219

220220
``` typescript
221-
const { Client, CheckoutAPI, Types } = require('@adyen/api-library');
222-
const client = new Client({apiKey: "YOUR_API_KEY", environment: "TEST"});
221+
const { Client, EnvironmentEnum, CheckoutAPI, Types } = require('@adyen/api-library');
222+
const client = new Client({apiKey: "YOUR_API_KEY", environment: EnvironmentEnum.LIVE, liveEndpointUrlPrefix: "YOUR_LIVE_URL_PREFIX"});
223223

224224
const makePaymentsRequest = async () => {
225225
const paymentsRequest : Types.checkout.PaymentRequest = {
@@ -265,6 +265,8 @@ const paymentRequest: checkout.PaymentRequest = await checkout.ObjectSerializer.
265265

266266
By default, [Node.js https](https://nodejs.org/api/https.html) is used to make API requests. Alternatively, you can set a custom `HttpClient` for your `Client` object.
267267

268+
**Note**: when using your custom `HttpClient`, you must define all required properties (API key, content-type, timeouts, etc..)
269+
268270
For example, to set `axios` as your HTTP client:
269271

270272
``` javascript
@@ -330,20 +332,26 @@ const client = new Client({ config });
330332
const httpClient = new HttpURLConnectionClient();
331333
httpClient.proxy = { host: "http://google.com", port: 8888, };
332334

333-
client.setEnvironment('TEST');
335+
client.setEnvironment(EnvironmentEnum.TEST);
334336
client.httpClient = httpClient;
335337

336338
// ... more code
337339
```
338340

339-
### Using the Cloud Terminal API Integration
340-
In order to submit In-Person requests with [Terminal API over Cloud](https://docs.adyen.com/point-of-sale/design-your-integration/choose-your-architecture/cloud/) you need to initialize the client in a similar way as the steps listed above for Ecommerce transactions, but make sure to include `TerminalCloudAPI`:
341+
### Using the Cloud Terminal API
342+
For In-Person Payments integrations with the [Cloud Terminal API](https://docs.adyen.com/point-of-sale/design-your-integration/choose-your-architecture/cloud/), you must initialise the Client **setting the closest** [Region](https://docs.adyen.com/point-of-sale/design-your-integration/terminal-api/#cloud):
341343
``` javascript
342344
// Step 1: Require the parts of the module you want to use
343345
const {Client, TerminalCloudAPI} from "@adyen/api-library";
346+
const { Config, EnvironmentEnum, RegionEnum } = require("@adyen/api-library");
344347

345348
// Step 2: Initialize the client object
346-
const client = new Client({apiKey: "YOUR_API_KEY", environment: "TEST"});
349+
const config = new Config({
350+
apiKey: "YOUR_API_KEY",
351+
environment: EnvironmentEnum.LIVE,
352+
region: RegionEnum.US
353+
});
354+
const client = new Client(config);
347355

348356
// Step 3: Initialize the API object
349357
const terminalCloudAPI = new TerminalCloudAPI(client);
@@ -507,6 +515,75 @@ const paymentRequest: SaleToPOIRequest = {
507515
// Step 5: Make the request
508516
const terminalApiResponse: terminal.TerminalApiResponse = await terminalLocalAPI.request(paymentRequest);
509517
```
518+
### Using the Cloud Terminal API Integration (async)
519+
If you choose to integrate [Terminal API over Cloud](https://docs.adyen.com/point-of-sale/design-your-integration/choose-your-architecture/cloud/) **asynchronously**, you need to follow similar steps to initialize the client and prepare the request object. However the response will be asynchronous:
520+
* a successful request will return `200` status code and `ok` as response body. Make sure to setup the [event notifications](https://docs.adyen.com/point-of-sale/design-your-integration/notifications/event-notifications/)
521+
* a request that fails will return `200` status code and the `TerminalApiResponse` as response body
522+
``` typescript
523+
// Step 1: Require the parts of the module you want to use
524+
const {Client, TerminalCloudAPI} from "@adyen/api-library";
525+
526+
// Step 2: Initialize the client object
527+
const client = new Client({apiKey: "YOUR_API_KEY", environment: "TEST"});
528+
529+
// Step 3: Initialize the API object
530+
const terminalCloudAPI = new TerminalCloudAPI(client);
531+
532+
// Step 4: Create the request object
533+
const serviceID = "123456789";
534+
const saleID = "POS-SystemID12345";
535+
const POIID = "Your Device Name(eg V400m-123456789)";
536+
537+
// Use a unique transaction for every transaction you perform
538+
const transactionID = "TransactionID";
539+
const paymentRequest: SaleToPOIRequest = {
540+
MessageHeader: {
541+
MessageClass: MessageClassType.Service,
542+
MessageCategory: MessageCategoryType.Payment,
543+
MessageType: MessageType.Request,
544+
ProtocolVersion: "3.0",
545+
ServiceID: serviceID,
546+
SaleID: saleID,
547+
POIID: POIID
548+
},
549+
PaymentRequest: {
550+
SaleData: {
551+
SaleTransactionID: {
552+
TransactionID: transactionID,
553+
TimeStamp: new Date().toISOString()
554+
},
555+
556+
SaleToAcquirerData: {
557+
applicationInfo: {
558+
merchantApplication: {
559+
version: "1",
560+
name: "test",
561+
}
562+
}
563+
}
564+
},
565+
PaymentTransaction: {
566+
AmountsReq: {
567+
Currency: "EUR",
568+
RequestedAmount: 1000
569+
}
570+
}
571+
}
572+
};
573+
574+
// Step 5: Make the request
575+
const response = await terminalCloudAPI.async(paymentRequest);
576+
// handle both `string` and `TerminalApiResponse`
577+
if (typeof response === "string") {
578+
// request was successful
579+
console.log("response:", response); // should be 'ok'
580+
} else {
581+
// request failed: see details in the EventNotification object
582+
console.log("EventToNotify:", response.SaleToPOIRequest?.EventNotification?.EventToNotify);
583+
console.log("EventDetails:", response.SaleToPOIRequest?.EventNotification?.EventDetails);
584+
}
585+
```
586+
510587
## Feedback
511588
We value your input! Help us enhance our API Libraries and improve the integration experience by providing your feedback. Please take a moment to fill out [our feedback form](https://forms.gle/A4EERrR6CWgKWe5r9) to share your thoughts, suggestions or ideas.
512589

src/__mocks__/base.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
import Client from "../client";
21-
import Config from "../config";
21+
import Config, { EnvironmentEnum } from "../config";
2222
import {
2323
AmountsReq,
2424
MessageCategoryType,
@@ -37,18 +37,19 @@ import {
3737

3838
export const createClient = (apiKey = process.env.ADYEN_API_KEY): Client => {
3939
const config: Config = new Config();
40+
config.environment = EnvironmentEnum.TEST;
4041
config.terminalApiCloudEndpoint = Client.TERMINAL_API_ENDPOINT_TEST;
4142
config.terminalApiLocalEndpoint = "https://mocked_local_endpoint.com";
4243
config.marketPayEndpoint = Client.MARKETPAY_ENDPOINT_TEST;
4344
config.apiKey = apiKey == null ? "apiKey" : apiKey;
44-
return new Client({ config });
45+
return new Client(config);
4546
};
4647

4748
export const createBasicAuthClient = (): Client => {
4849
return new Client({
4950
username: process.env.ADYEN_USER!,
5051
password: process.env.ADYEN_PASSWORD!,
51-
environment: "TEST",
52+
environment: EnvironmentEnum.TEST,
5253
applicationName: "adyen-node-api-library"
5354
});
5455
};

src/__mocks__/terminalApi/async.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,25 @@
1818
*/
1919

2020
export const asyncRes = "ok";
21+
22+
export const asyncErrorRes = {
23+
SaleToPOIRequest: {
24+
EventNotification: {
25+
EventToNotify: "Reject",
26+
EventDetails: "message=Did+not+receive+a+response+from+the+POI.",
27+
RejectedMessage: "ewoi...0KfQo=",
28+
TimeStamp: "2020-03-31T10:28:39.515Z"
29+
},
30+
MessageHeader: {
31+
DeviceID: "666568147",
32+
MessageCategory: "Event",
33+
MessageClass: "Event",
34+
MessageType: "Notification",
35+
POIID: "P400Plus-123456789",
36+
ProtocolVersion: "3.0",
37+
SaleID: "saleid-4c32759faaa7",
38+
ServiceID: "31122609"
39+
}
40+
}
41+
};
42+

src/__tests__/checkout.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { checkout } from "../typings";
1616
import { IRequest } from "../typings/requestOptions";
1717
import { SessionResultResponse } from "../typings/checkout/sessionResultResponse";
1818
import { payments3DS2NativeAction } from "../__mocks__/checkout/payments3DS2NativeAction";
19+
import { EnvironmentEnum } from "../config";
1920

2021
const merchantAccount = process.env.ADYEN_MERCHANT!;
2122
const reference = "Your order number";
@@ -384,14 +385,14 @@ describe("Checkout", (): void => {
384385
// });
385386

386387
test("should have missing identifier on live", async (): Promise<void> => {
387-
client.setEnvironment("LIVE");
388+
client.config.environment = EnvironmentEnum.LIVE;
388389
try {
389390
const liveCheckout = new CheckoutAPI(client);
390391
await liveCheckout.PaymentsApi.payments(createPaymentsCheckoutRequest());
391392
fail();
392393
} catch (e) {
393394
if(e instanceof Error) {
394-
expect(e.message).toEqual("Please provide your unique live url prefix on the setEnvironment() call on the Client.");
395+
expect(e.message).toEqual("Live endpoint URL prefix must be provided for LIVE environment.");
395396

396397
} else {
397398
fail();

0 commit comments

Comments
 (0)