Skip to content

Commit

Permalink
Update payment contexts tests and Fix https Agent (#369)
Browse files Browse the repository at this point in the history
* Fix agent option in Axios as http client

* Update payment contexts tests
  • Loading branch information
armando-rodriguez-cko authored Dec 5, 2023
1 parent d6d1636 commit 3c656c0
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 84 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ lib/

doc
out
.idea/*
.idea

/dist/
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions src/services/http.js
Original file line number Diff line number Diff line change
@@ -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');

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
62 changes: 61 additions & 1 deletion test/http/httpClient-it.js
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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'
}
);
Expand Down Expand Up @@ -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,
Expand Down
79 changes: 8 additions & 71 deletions test/payment-contexts/payment-contexts-it.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
]
}
Expand All @@ -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');
Expand Down

0 comments on commit 3c656c0

Please sign in to comment.