Skip to content

Commit

Permalink
Payment Context (CS2) (#367)
Browse files Browse the repository at this point in the history
* Adds payment context endpoint (CS2)

* Fix gitignore. Add d.ts files missing
  • Loading branch information
armando-rodriguez-cko authored Nov 14, 2023
1 parent 5ee01f8 commit a0f21d5
Show file tree
Hide file tree
Showing 16 changed files with 910 additions and 3 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build-master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ jobs:
registry-url: https://registry.npmjs.org/
- id: build-and-analyse
env:
CHECKOUT_PROCESSING_CHANNEL_ID: ${{ secrets.IT_CHECKOUT_PROCESSING_CHANNEL_ID }}
CHECKOUT_PREVIOUS_SECRET_KEY: ${{ secrets.IT_CHECKOUT_PREVIOUS_SECRET_KEY }}
CHECKOUT_PREVIOUS_PUBLIC_KEY: ${{ secrets.IT_CHECKOUT_PREVIOUS_PUBLIC_KEY }}
CHECKOUT_DEFAULT_SECRET_KEY: ${{ secrets.IT_CHECKOUT_DEFAULT_SECRET_KEY }}
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/build-pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
registry-url: https://registry.npmjs.org/
- id: build-and-analyse
env:
CHECKOUT_PROCESSING_CHANNEL_ID: ${{ secrets.IT_CHECKOUT_PROCESSING_CHANNEL_ID }}
CHECKOUT_PREVIOUS_SECRET_KEY: ${{ secrets.IT_CHECKOUT_PREVIOUS_SECRET_KEY }}
CHECKOUT_PREVIOUS_PUBLIC_KEY: ${{ secrets.IT_CHECKOUT_PREVIOUS_PUBLIC_KEY }}
CHECKOUT_DEFAULT_SECRET_KEY: ${{ secrets.IT_CHECKOUT_DEFAULT_SECRET_KEY }}
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/build-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ jobs:
registry-url: https://registry.npmjs.org/
- id: build-and-analyse
env:
CHECKOUT_PROCESSING_CHANNEL_ID: ${{ secrets.IT_CHECKOUT_PROCESSING_CHANNEL_ID }}
CHECKOUT_PREVIOUS_SECRET_KEY: ${{ secrets.IT_CHECKOUT_PREVIOUS_SECRET_KEY }}
CHECKOUT_PREVIOUS_PUBLIC_KEY: ${{ secrets.IT_CHECKOUT_PREVIOUS_PUBLIC_KEY }}
CHECKOUT_DEFAULT_SECRET_KEY: ${{ secrets.IT_CHECKOUT_DEFAULT_SECRET_KEY }}
Expand Down
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,13 @@ node_modules
typings/
lib/*.js
lib/
test/*.js
*.map

.DS_Store
.vscode/*
/.vscode/

doc
out
.idea/*

dist
/dist/
1 change: 1 addition & 0 deletions src/Checkout.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,5 +170,6 @@ export default class Checkout {
this.reports = new ENDPOINTS.Reports(this.config);
this.financial = new ENDPOINTS.Financial(this.config);
this.issuing = new ENDPOINTS.Issuing(this.config);
this.paymentContexts = new ENDPOINTS.PaymentContexts(this.config);
}
}
62 changes: 62 additions & 0 deletions src/api/payment-contexts/payment-contexts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { determineError } from '../../services/errors';
import { get, post } from '../../services/http';
import { validatePayment } from '../../services/validation';

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

/**
* Sends payment contexts requests.
*
* @memberof PaymentContexts
* @param {object} body PaymentContexts Request body.
* @param {string} [idempotencyKey] Idempotency Key.
* @return {Promise<object>} A promise to payment context response.
*/
async request(body, idempotencyKey) {
try {
validatePayment(body);

const response = await post(
this.config.httpClient,
`${this.config.host}/payment-contexts`,
this.config,
this.config.sk,
body,
idempotencyKey
);
return await response.json;
} catch (error) {
throw await determineError(error);
}
}

/**
* Returns a payment-context details with the specified identifier string.
*
* @memberof PaymentContexts
* @param {string} id /^(pay|sid)_(\w{26})$/ The payment or payment session identifier.
* @return {Promise<object>} A promise to the get payment context response.
*/
async get(id) {
try {
const response = await get(
this.config.httpClient,
`${this.config.host}/payment-contexts/${id}`,
this.config,
this.config.sk
);
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 @@ -34,5 +34,6 @@ 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 Issuing } from './api/issuing/issuing';
export { default as PaymentContexts } from './api/payment-contexts/payment-contexts';
export { default as Checkout } from './Checkout';
export { default } from './Checkout';
125 changes: 125 additions & 0 deletions test/payment-contexts/payment-contexts-it.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import { expect } from "chai";
import nock from "nock";
import Checkout from '../../src/Checkout.js'
import { NotFoundError } from "../../src/services/errors.js";

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

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

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

const request = {
source: {
type: "paypal"
},
amount: 100,
currency: "USD",
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/"
}
]
}

it('should request a payment context', async () => {
const response = await cko.paymentContexts.request(request);

expect(response.id).not.to.be.null;
expect(response.partner_metadata.order_id).not.to.be.null;
});

it('should get a payment context', async () => {
const responseRquest = await cko.paymentContexts.request(request);
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.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');
});

it('should throw NotFoundError when getting an unexistant payment context', async () => {
try {
await cko.paymentContexts.get('not_found');
} catch (err) {
expect(err).to.be.instanceOf(NotFoundError);
}
});
});
Loading

0 comments on commit a0f21d5

Please sign in to comment.