Skip to content

Commit a68b09b

Browse files
authored
add deserialization test and update readme with deserialization example (#1306)
1 parent 99f0d12 commit a68b09b

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

README.md

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ checkoutApi.PaymentsApi.payments(paymentRequest)
199199
.then(paymentResponse => console.log(paymentResponse.pspReference))
200200
.catch(error => console.log(error));
201201
```
202-
## Client setup when going live
202+
### Client setup when going live
203203

204204
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:
205205
``` typescript
@@ -208,7 +208,7 @@ const { Client } = require('@adyen/api-library');
208208
const client = new Client({apiKey: "YOUR_API_KEY", environment: "TEST", liveEndpointUrlPrefix: "YOUR_LIVE_URL_PREFIX"});
209209
```
210210

211-
## Usage in TypeScript
211+
### Usage in TypeScript
212212

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

@@ -244,8 +244,19 @@ Alternatively, you can use the `Types` included in this module for Typescript an
244244

245245
makePaymentsRequest();
246246
```
247+
248+
#### Deserializing JSON Strings
249+
In some setups you might need to deserialize JSON strings to request objects. For example, when using the libraries in combination with [Dropin/Components](https://github.com/Adyen/adyen-web). Please use the built-in deserialization functions:
250+
``` typescript
251+
// Import the required model class
252+
import { checkout } from "../typings";
253+
254+
// Deserialize using built-in ObjectSerializer class
255+
const requestJson: JSON = JSON.parse(`YOUR_JSON_STRING`);
256+
const paymentRequest: checkout.PaymentRequest = await checkout.ObjectSerializer.deserialize(requestJson,"PaymentRequest");
257+
```
247258

248-
## Custom HTTP client configuration
259+
### Custom HTTP client configuration
249260

250261
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.
251262

@@ -277,7 +288,7 @@ const client = new Client({
277288
// ... more code
278289
```
279290

280-
## Parsing and Authenticating Banking Webhooks
291+
### Parsing and Authenticating Banking Webhooks
281292
Parse an AccountHolderNotificationRequest webhook;
282293
``` typescript
283294
let bankingWebhookHandler = new BankingWebhookHandler(YOUR_BANKING_WEBHOOK);
@@ -293,14 +304,14 @@ Verify the authenticity (where you retrieve the hmac key from the CA and the sig
293304
``` typescript
294305
const isValid = hmacValidator.validateBankingHMAC("YOUR_HMAC_KEY", "YOUR_HMAC_SIGNATURE", jsonString)
295306
```
296-
## Management Webhooks
307+
### Management Webhooks
297308
Management webhooks are verified the exact same way as the banking webhooks. To parse them however, instead you use:
298309
``` typescript
299310
let managementWebhookHandler = new ManagementWebhookHandler(YOUR_MANAGEMENT_WEBHOOK);
300311
const genericWebhook = managementWebhookHandler.getGenericWebhook();
301312
```
302313

303-
## Proxy configuration
314+
### Proxy configuration
304315

305316
To configure a proxy connection, set the `proxy` property of your `HttpURLConnectionClient` object.
306317

@@ -320,7 +331,7 @@ client.httpClient = httpClient;
320331
// ... more code
321332
```
322333

323-
## Using the Cloud Terminal API Integration
334+
### Using the Cloud Terminal API Integration
324335
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`:
325336
``` javascript
326337
// Step 1: Require the parts of the module you want to use
@@ -378,7 +389,7 @@ const paymentRequest: SaleToPOIRequest = {
378389
const terminalAPIResponse: terminal.TerminalApiResponse = await terminalCloudAPI.sync(paymentRequest);
379390
```
380391

381-
### Optional: perform an abort request
392+
#### Optional: perform an abort request
382393

383394
To perform an [abort request](https://docs.adyen.com/point-of-sale/basic-tapi-integration/cancel-a-transaction/) you can use the following example:
384395
``` javascript
@@ -408,7 +419,7 @@ const abortRequest: SaleToPOIRequest = {
408419
const terminalAPIResponse: terminal.TerminalApiResponse = await terminalCloudAPI.sync(abortRequest);
409420
```
410421

411-
### Optional: perform a status request
422+
#### Optional: perform a status request
412423

413424
To perform a [status request](https://docs.adyen.com/point-of-sale/basic-tapi-integration/verify-transaction-status/) you can use the following example:
414425
``` javascript
@@ -436,7 +447,7 @@ const statusRequest: SaleToPOIRequest = {
436447
const terminalAPIResponse: terminal.TerminalApiResponse = await terminalCloudAPI.sync(statusRequest);
437448
```
438449

439-
## Using the Local Terminal API Integration
450+
### Using the Local Terminal API Integration
440451
The procedure to send In-Person requests using [Terminal API over Local Connection](https://docs.adyen.com/point-of-sale/design-your-integration/choose-your-architecture/local/) is similar to the Cloud Terminal API one, however, additional encryption details are required to perform the requests. Make sure to [install the certificate as described here](https://docs.adyen.com/point-of-sale/design-your-integration/choose-your-architecture/local/#protect-communications)
441452
```javascript
442453
// Step 1: Require the parts of the module you want to use

src/__tests__/checkout.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,26 @@ afterEach(() => {
140140
});
141141

142142
describe("Checkout", (): void => {
143+
test("should deserialize JSON into valid paymentRequest object", async (): Promise<void> => {
144+
const requestJson: JSON = JSON.parse(`{
145+
"amount": {
146+
"currency": "USD",
147+
"value": 1000
148+
},
149+
"reference": "Your order number",
150+
"paymentMethod": {
151+
"type": "applepay",
152+
"applePayToken": "VNRWtuNlNEWkRCSm1xWndjMDFFbktkQU..."
153+
},
154+
"returnUrl": "https://your-company.com/...",
155+
"merchantAccount": "YOUR_MERCHANT_ACCOUNT"
156+
}`);
157+
const paymentRequest: checkout.PaymentRequest = await checkout.ObjectSerializer.deserialize(requestJson,"PaymentRequest");
158+
expect(paymentRequest.returnUrl).toEqual("https://your-company.com/...");
159+
expect(paymentRequest.amount.value).toBe(1000);
160+
const paymentMethodDetails: checkout.ApplePayDetails = paymentRequest.paymentMethod as checkout.ApplePayDetails;
161+
expect(paymentMethodDetails.applePayToken).toEqual("VNRWtuNlNEWkRCSm1xWndjMDFFbktkQU...");
162+
});
143163
test("should add idempotency key to request headers", async (): Promise<void> => {
144164
const paymentsRequest: checkout.PaymentRequest = createPaymentsCheckoutRequest();
145165
scope.post("/payments")

0 commit comments

Comments
 (0)