From 54924c0ac2121b727aa1ad5babfe219aecd20ccd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20Rodr=C3=ADguez?= <127134616+armando-rodriguez-cko@users.noreply.github.com> Date: Mon, 4 Dec 2023 10:02:14 +0100 Subject: [PATCH 1/2] Fix agent option in Axios as http client --- src/services/http.js | 18 +++++------ test/http/httpClient-it.js | 62 +++++++++++++++++++++++++++++++++++++- 2 files changed, 70 insertions(+), 10 deletions(-) diff --git a/src/services/http.js b/src/services/http.js index 75563fe..17d0514 100644 --- a/src/services/http.js +++ b/src/services/http.js @@ -1,12 +1,12 @@ /* eslint-disable no-throw-literal */ import fetch from 'node-fetch'; +import axios from 'axios'; import { API_VERSION_HEADER, LIVE_ACCESS_URL, REQUEST_ID_HEADER, SANDBOX_ACCESS_URL, } from '../config'; -import axios from 'axios'; const pjson = require('../../package.json'); @@ -140,7 +140,7 @@ export const createAccessToken = async (config, httpClient, body) => { }, data: new URLSearchParams(requestBody), timeout: config.timeout, - httpAgent: config.agent, + httpsAgent: config.agent, }) .catch((error) => { if (error.response) { @@ -196,7 +196,7 @@ export const createAccessToken = async (config, httpClient, body) => { }; // eslint-disable-next-line consistent-return -const http = async (httpClient, method, path, config, auth, request, idempotencyKey) => { +const httpRequest = async (httpClient, method, path, config, auth, request, idempotencyKey) => { let authHeader = null; if (auth) { @@ -234,7 +234,7 @@ const http = async (httpClient, method, path, config, auth, request, idempotency headers, data: config.formData ? request : JSON.stringify(request), timeout: config.timeout, - httpAgent: config.agent, + httpsAgent: config.agent, }) .catch((error) => { if (error.response) { @@ -276,18 +276,18 @@ const http = async (httpClient, method, path, config, auth, request, idempotency }; export const get = async (httpClient, path, config, auth) => - http(httpClient, 'get', path, config, auth); + httpRequest(httpClient, 'get', path, config, auth); export const post = async (httpClient, path, config, auth, request, idempotencyKey) => - http(httpClient, 'post', path, config, auth, request, idempotencyKey); + httpRequest(httpClient, 'post', path, config, auth, request, idempotencyKey); export const patch = async (httpClient, path, config, auth, request) => - http(httpClient, 'patch', path, config, auth, request); + httpRequest(httpClient, 'patch', path, config, auth, request); export const put = async (httpClient, path, config, auth, request) => - http(httpClient, 'put', path, config, auth, request); + httpRequest(httpClient, 'put', path, config, auth, request); export const _delete = async (httpClient, path, config, auth) => - http(httpClient, 'delete', path, config, auth); + httpRequest(httpClient, 'delete', path, config, auth); export default createAccessToken; diff --git a/test/http/httpClient-it.js b/test/http/httpClient-it.js index b9677cf..74f286f 100644 --- a/test/http/httpClient-it.js +++ b/test/http/httpClient-it.js @@ -1,5 +1,6 @@ import { expect } from "chai"; import nock from "nock"; +import https from 'https'; import Checkout from '../../src/Checkout.js' import { ApiTimeout } from "../../src/services/errors.js"; @@ -40,12 +41,41 @@ describe('Integration::HttpClient', () => { expect(token.card_category).to.equal('CONSUMER'); }); + it('should get a token response setting agent', async () => { + const checkout = new Checkout( + process.env.CHECKOUT_DEFAULT_SECRET_KEY, + { + pk: process.env.CHECKOUT_DEFAULT_PUBLIC_KEY, + timeout: 3000, + httpClient: 'axios', + agent: new https.Agent({ keepAlive: true }), + } + ); + const token = await checkout.tokens.request( + { + type: 'card', + number: '4543474002249996', + expiry_month: 6, + expiry_year: 2025, + cvv: '956', + }); + + expect(token.type).to.equal('card'); + expect(token.expiry_month).to.equal(6); + expect(token.expiry_year).to.equal(2025); + expect(token.scheme).to.equal('VISA'); + expect(token.last4).to.equal('9996'); + expect(token.bin).to.equal('454347'); + expect(token.card_type).to.equal('CREDIT'); + expect(token.card_category).to.equal('CONSUMER'); + }); + it('should get a timeout error response', async () => { const checkout = new Checkout( process.env.CHECKOUT_DEFAULT_SECRET_KEY, { pk: process.env.CHECKOUT_DEFAULT_PUBLIC_KEY, - timeout: 200, + timeout: 100, httpClient: 'axios' } ); @@ -96,6 +126,36 @@ describe('Integration::HttpClient', () => { expect(token.card_category).to.equal('CONSUMER'); }); + it('should get a token response setting agent', async () => { + const checkout = new Checkout( + process.env.CHECKOUT_DEFAULT_SECRET_KEY, + { + pk: process.env.CHECKOUT_DEFAULT_PUBLIC_KEY, + timeout: 3000, + agent: new https.Agent({ keepAlive: true }), + } + ); + + const token = await checkout.tokens.request( + { + type: 'card', + number: '4543474002249996', + expiry_month: 6, + expiry_year: 2025, + cvv: '956', + } + ); + + expect(token.type).to.equal('card'); + expect(token.expiry_month).to.equal(6); + expect(token.expiry_year).to.equal(2025); + expect(token.scheme).to.equal('VISA'); + expect(token.last4).to.equal('9996'); + expect(token.bin).to.equal('454347'); + expect(token.card_type).to.equal('CREDIT'); + expect(token.card_category).to.equal('CONSUMER'); + }); + it('should get a timeout error response', async () => { const checkout = new Checkout( process.env.CHECKOUT_DEFAULT_SECRET_KEY, From 7474cc0a1529d0ab33fe41d7b3e959b977640ba0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20Rodr=C3=ADguez?= <127134616+armando-rodriguez-cko@users.noreply.github.com> Date: Mon, 4 Dec 2023 23:04:18 +0100 Subject: [PATCH 2/2] Update payment contexts tests --- .gitignore | 2 +- package-lock.json | 4 +- test/payment-contexts/payment-contexts-it.js | 79 ++------------------ 3 files changed, 11 insertions(+), 74 deletions(-) diff --git a/.gitignore b/.gitignore index 35d9ce7..5f159dc 100755 --- a/.gitignore +++ b/.gitignore @@ -44,6 +44,6 @@ lib/ doc out -.idea/* +.idea /dist/ diff --git a/package-lock.json b/package-lock.json index bd2783e..e146b64 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "checkout-sdk-node", - "version": "2.2.5", + "version": "2.3.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "checkout-sdk-node", - "version": "2.2.5", + "version": "2.3.1", "license": "MIT", "dependencies": { "axios": "^0.27.2", diff --git a/test/payment-contexts/payment-contexts-it.js b/test/payment-contexts/payment-contexts-it.js index 52c7c2b..9c1e39e 100644 --- a/test/payment-contexts/payment-contexts-it.js +++ b/test/payment-contexts/payment-contexts-it.js @@ -17,79 +17,18 @@ describe('Integration::Payment-Contexts', () => { source: { type: "paypal" }, - amount: 100, - currency: "USD", + amount: 2000, + currency: "EUR", + paymentType: "Regular", capture: true, - shipping: { - first_name: "John", - last_name: "Smith", - address: { - address_line1: "Checkout.com", - address_line2: "90 Tottenham Court Road", - city: "London", - state: "str", - zip: "W1T 4TJ", - country: "GB" - }, - phone: { - country_code: "+1", - number: "415 555 2671" - }, - from_address_zip: "10014", - type: "pickup_in_person" - }, - processing: { - order_id: "123456789", - tax_amount: 3000, - discount_amount: 0, - duty_amount: 0, - shipping_amount: 300, - shipping_tax_amount: 100, - aft: true, - preferred_scheme: "mastercard", - merchant_initiated_reason: "Delayed_charge", - campaign_id: 0, - product_type: "QR Code", - open_id: "oUpF8uMuAJO_M2pxb1Q9zNjWeS6o", - original_order_amount: 10, - receipt_id: 10, - terminal_type: "WAP", - os_type: "ANDROID", - invoice_id: "string", - brand_name: "Super Brand", - locale: "en-US", - shipping_preference: "SET_PROVIDED_ADDRESS", - user_action: "PAY_NOW", - set_transaction_context: { - key: "string", - value: "string" - }, - purchase_country: "GB", - custom_payment_method_ids: [ - "string" - ], - merchant_callback_url: "string", - line_of_business: "Flights" - }, processing_channel_id: processingChannelId, - reference: "ORD-5023-4E89", - description: "Set of 3 masks", success_url: "https://example.com/payments/success", failure_url: "https://example.com/payments/fail", items: [ { - name: "Kevlar batterang", - quantity: 2, - unit_price: 50, - reference: "858818ac", - commodity_code: "DEF123", - unit_of_measure: "metres", - total_amount: 29000, - tax_amount: 1000, - discount_amount: 1000, - wxpay_goods_id: 1001, - url: "https://example.com/", - image_url: "https://example.com/" + name: "mask", + quantity: 1, + unit_price: 2000, } ] } @@ -106,10 +45,8 @@ describe('Integration::Payment-Contexts', () => { const response = await cko.paymentContexts.get(responseRquest.id); expect(response.payment_request.source.type).to.equal('paypal'); - expect(response.payment_request.amount).to.equal(100); - expect(response.payment_request.currency).to.equal("USD"); - expect(response.payment_request.currency.shipping).to.not.be.null - expect(response.payment_request.currency.processing).to.not.be.null + expect(response.payment_request.amount).to.equal(2000); + expect(response.payment_request.currency).to.equal("EUR"); expect(response.payment_request.currency.items).to.not.be.null expect(response.payment_request.success_url).to.equal('https://example.com/payments/success'); expect(response.payment_request.failure_url).to.equal('https://example.com/payments/fail');