Skip to content

Commit

Permalink
Adds payment sessions support (#372)
Browse files Browse the repository at this point in the history
  • Loading branch information
armando-rodriguez-cko committed Jan 2, 2024
1 parent 7a0e735 commit 4eaa148
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Checkout.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,5 +171,6 @@ export default class Checkout {
this.financial = new ENDPOINTS.Financial(this.config);
this.issuing = new ENDPOINTS.Issuing(this.config);
this.paymentContexts = new ENDPOINTS.PaymentContexts(this.config);
this.paymentSessions = new ENDPOINTS.PaymentSessions(this.config);
}
}
39 changes: 39 additions & 0 deletions src/api/payment-sessions/payment-sessions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { determineError } from '../../services/errors';
import { post } from '../../services/http';
import { validatePayment } from '../../services/validation';

/*
* Class dealing with the /payment-sessions endpoint
*
* @export
* @class PaymentSessions
*/
export default class PaymentSessions {
constructor(config) {
this.config = config;
}

/**
* Sends payment sessions requests.
*
* @memberof PaymentSessions
* @param {object} body PaymentSessions Request body.
* @return {Promise<object>} A promise to payment context response.
*/
async request(body) {
try {
validatePayment(body);

const response = await post(
this.config.httpClient,
`${this.config.host}/payment-sessions`,
this.config,
this.config.sk,
body
);
return await response.json;
} catch (error) {
throw await determineError(error);
}
}
}
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ export { default as Reports } from './api/reports/reports';
export { default as Financial } from './api/financial/financial';
export { default as Issuing } from './api/issuing/issuing';
export { default as PaymentContexts } from './api/payment-contexts/payment-contexts';
export { default as PaymentSessions } from './api/payment-sessions/payment-sessions';
export { default as Checkout } from './Checkout';
export { default } from './Checkout';
1 change: 1 addition & 0 deletions src/services/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const validatePayment = (request) => {
if (request.amount && ('' + request.amount).indexOf('.') !== -1) {
throw new ValueError('The amount can not contain decimals.');
}

if (!(request.currency in CURRENCIES)) {
throw new ValueError('The currency value is not valid.');
}
Expand Down
46 changes: 46 additions & 0 deletions test/payment-sessions/payment-sessions-common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
export const commonRequest = {
amount: 1000,
currency: 'GBP',
reference: 'ORD-123A',
billing: {
address: {
country: 'GB'
}
},
customer: {
name: 'John Smith',
email: '[email protected]'
},
success_url: 'https://example.com/payments/success',
failure_url: 'https://example.com/payments/failure'
};

export const commonResponse = {
id: 'ps_2aOig7knIeGNYPlyuxUEPQyOmxN',
amount: 1000,
locale: 'en-GB',
currency: 'GBP',
payment_methods: [
{
type: 'card',
scheme_choice_enabled: false,
store_payment_details: 'disabled'
},
{
type: 'applepay',
display_name: 'Parser Digital',
country_code: 'GB',
currency_code: 'GBP',
},
{
type: 'googlepay',
}
],
feature_flags: [],
risk: { enabled: true },
_links: {
self: {
href: 'https://api.sandbox.checkout.com/payment-sessions/ps_2aOig7knIeGNYPlyuxUEPQyOmxN'
}
}
};
27 changes: 27 additions & 0 deletions test/payment-sessions/payment-sessions-it.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { expect } from "chai";
import nock from "nock";
import Checkout from '../../src/Checkout.js'
import { commonRequest, commonResponse } from "./payment-sessions-common.js";

afterEach(() => {
nock.cleanAll();
nock.enableNetConnect();
});

const cko = new Checkout(process.env.CHECKOUT_DEFAULT_SECRET_KEY);

describe('Integration::Payment-Sessions', () => {

it('should request a payment session', async () => {
const response = await cko.paymentSessions.request(commonRequest);

expect(response.id).not.to.be.null;
expect(response.amount).to.equal(commonResponse.amount);
expect(response.locale).to.equal(commonResponse.locale);
expect(response.currency).to.equal(commonResponse.currency);
expect(response.customer).not.to.be.null;
expect(response.payment_methods).not.to.be.null;
expect(response._links).not.to.be.null;
});

});
28 changes: 28 additions & 0 deletions test/payment-sessions/payment-sessions-unit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import nock from "nock";
import { expect } from "chai";
import Checkout from "../../src/Checkout.js";
import { commonRequest, commonResponse } from "./payment-sessions-common.js";


describe('Unit::Payment-Sessions', () => {
const SK = 'sk_test_0b9b5db6-f223-49d0-b68f-f6643dd4f808';

it('should request a payment session', async () => {
nock('https://api.sandbox.checkout.com')
.post('/payment-sessions')
.reply(201, commonResponse);

const cko = new Checkout(SK);

const paymentContextResponse = await cko.paymentSessions.request(commonRequest);

expect(paymentContextResponse.id).to.equal('ps_2aOig7knIeGNYPlyuxUEPQyOmxN');
expect(paymentContextResponse.amount).to.equal(1000);
expect(paymentContextResponse.locale).to.equal('en-GB');
expect(paymentContextResponse.currency).to.equal('GBP');
expect(paymentContextResponse.payment_methods[0].type).to.equal('card');
expect(paymentContextResponse.risk.enabled).to.equal(true);
expect(paymentContextResponse._links.self.href).to.equal('https://api.sandbox.checkout.com/payment-sessions/ps_2aOig7knIeGNYPlyuxUEPQyOmxN');
});

});
2 changes: 2 additions & 0 deletions types/dist/Checkout.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
Oxxo,
PagoFacil,
PaymentLinks,
PaymentSessions,
Payments,
Platforms,
Rapipago,
Expand Down Expand Up @@ -108,6 +109,7 @@ export default class Checkout {
cardMetadata: CardMetadata;
reports: Reports;
financial: Financial;
paymentSessions: PaymentSessions;
config: config;

constructor(key?: string, options?: options);
Expand Down
7 changes: 7 additions & 0 deletions types/dist/api/payment-sessions/payment-sessions.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { config } from '../../Checkout';

export default class PaymentSessions {
constructor(config: config);

request: (body: object) => Promise<object>;
}
1 change: 1 addition & 0 deletions types/dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ export { default as Balances } from './api/balances/balances';
export { default as CardMetadata } from './api/card-metadata/card-metadata';
export { default as Reports } from './api/reports/reports';
export { default as Financial } from './api/financial/financial';
export { default as PaymentSessions } from './api/payment-sessions/payment-sessions';
export { default as Checkout } from './Checkout';
export { default } from './Checkout';

0 comments on commit 4eaa148

Please sign in to comment.