From de82b86e1baa9a0be1df2783076507623c724ba5 Mon Sep 17 00:00:00 2001 From: david ruiz Date: Fri, 12 Dec 2025 13:22:03 +0100 Subject: [PATCH 1/5] Payment Setups initial development, lacks tests --- src/Checkout.js | 1 + src/api/payment-setups/payment-setups.js | 117 ++++++++++++++++++ src/index.js | 1 + types/dist/Checkout.d.ts | 2 + .../api/payment-setups/payment-setups.d.ts | 7 ++ types/dist/index.d.ts | 1 + 6 files changed, 129 insertions(+) create mode 100644 src/api/payment-setups/payment-setups.js create mode 100644 types/dist/api/payment-setups/payment-setups.d.ts diff --git a/src/Checkout.js b/src/Checkout.js index 0e0ef44..ad2559b 100644 --- a/src/Checkout.js +++ b/src/Checkout.js @@ -196,6 +196,7 @@ export default class Checkout { this.issuing = new ENDPOINTS.Issuing(this.config); this.paymentContexts = new ENDPOINTS.PaymentContexts(this.config); this.paymentSessions = new ENDPOINTS.PaymentSessions(this.config); + this.paymentSetups = new ENDPOINTS.PaymentSetups(this.config); this.forward = new ENDPOINTS.Forward(this.config); } } diff --git a/src/api/payment-setups/payment-setups.js b/src/api/payment-setups/payment-setups.js new file mode 100644 index 0000000..aca08de --- /dev/null +++ b/src/api/payment-setups/payment-setups.js @@ -0,0 +1,117 @@ +import { determineError } from '../../services/errors.js'; +import { post } from '../../services/http.js'; +import { validatePayment } from '../../services/validation.js'; + +/** + * Class dealing with the /payment-setups endpoint + * + * @export + * @class PaymentSetups + */ +export default class PaymentSetups { + constructor(config) { + this.config = config; + } + + /** + * Create a Payment Setup + * [BETA] + * Creates a Payment Setup. + * To maximize the amount of information the payment setup can use, we recommend that you create a payment setup as early + * as possible in the customer's journey. For example, the first time they land on the basket page. + * @method createAPaymentSetup + * @param {Object} body - Request body + * @returns {Promise<Object>} A promise to the Create a Payment Setup response + */ + async createAPaymentSetup(body) { + try { + validatePayment(body); + const url = `${this.config.host}/payments/setups`; + const response = await post( + this.config.httpClient, + url, + this.config, + this.config.sk, + body + ); + return await response.json; + } catch (error) { + throw await determineError(error); + } + } + + /** + * Update a Payment Setup + * [BETA] + * Updates a Payment Setup. + * You should update the payment setup whenever there are significant changes in the data relevant to the customer's + * transaction. For example, when the customer makes a change that impacts the total payment amount. + * @method updateAPaymentSetup + * @param {string} id - The unique identifier of the Payment Setup to update. + * @param {Object} body - Request body + * @returns {Promise<Object>} A promise to the Update a Payment Setup response + */ + async updateAPaymentSetup(id, body) { + try { + validatePayment(body); + const url = `${this.config.host}/payments/setups/${id}`; + const response = await put( + this.config.httpClient, + url, + this.config, + this.config.sk, + body + ); + return await response.json; + } catch (error) { + throw await determineError(error); + } + } + + /** + * Get a Payment Setup + * [BETA] + * Retrieves a Payment Setup. + * @method getAPaymentSetup + * @param {string} id - The unique identifier of the Payment Setup to retrieve. + * @returns {Promise<Object>} A promise to the Get a Payment Setup response + */ + async getAPaymentSetup(id) { + try { + const url = `${this.config.host}/payments/setups/${id}`; + const response = await get( + this.config.httpClient, + url, + this.config, + this.config.sk, + ); + return await response.json; + } catch (error) { + throw await determineError(error); + } + } + + /** + * Confirm a Payment Setup + * [BETA] + * Confirm a Payment Setup to begin processing the payment request with your chosen payment method option. + * @method confirmAPaymentSetup + * @param {string} id - The unique identifier of the Payment Setup. + * @param {string} payment_method_option_id - The unique identifier of the payment option to process the payment with. + * @returns {Promise<Object>} A promise to the Confirm a Payment Setup response + */ + async confirmAPaymentSetup(id, payment_method_option_id) { + try { + const url = `${this.config.host}/payments/setups/${id}/confirm/${payment_method_option_id}`; + const response = await post( + this.config.httpClient, + url, + this.config, + this.config.sk, + ); + return await response.json; + } catch (error) { + throw await determineError(error); + } + } +} diff --git a/src/index.js b/src/index.js index ccc847a..73ca8fe 100644 --- a/src/index.js +++ b/src/index.js @@ -35,6 +35,7 @@ export { default as Financial } from './api/financial/financial.js'; export { default as Issuing } from './api/issuing/issuing.js'; export { default as PaymentContexts } from './api/payment-contexts/payment-contexts.js'; export { default as PaymentSessions } from './api/payment-sessions/payment-sessions.js'; +export { default as PaymentSetups } from './api/payment-setups/payment-setups.js'; export { default as Forward } from './api/forward/forward.js'; export { default as Checkout } from './Checkout.js'; export { default } from './Checkout.js'; diff --git a/types/dist/Checkout.d.ts b/types/dist/Checkout.d.ts index 03bf5dc..ab16c56 100644 --- a/types/dist/Checkout.d.ts +++ b/types/dist/Checkout.d.ts @@ -27,6 +27,7 @@ import { PaymentContexts, PaymentLinks, PaymentSessions, + PaymentSetups, Payments, Platforms, Rapipago, @@ -115,6 +116,7 @@ export default class Checkout { issuing: Issuing; paymentContexts: PaymentContexts; paymentSessions: PaymentSessions; + paymentSetups: PaymentSetups; forward: Forward; config: config; diff --git a/types/dist/api/payment-setups/payment-setups.d.ts b/types/dist/api/payment-setups/payment-setups.d.ts new file mode 100644 index 0000000..8638332 --- /dev/null +++ b/types/dist/api/payment-setups/payment-setups.d.ts @@ -0,0 +1,7 @@ +import { config } from '../../Checkout'; + +export default class PaymentSetups { + constructor(config: config); + + request: (body: object) => Promise; +} diff --git a/types/dist/index.d.ts b/types/dist/index.d.ts index 41825e2..0dff14f 100644 --- a/types/dist/index.d.ts +++ b/types/dist/index.d.ts @@ -35,6 +35,7 @@ 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 PaymentSetups } from './api/payment-setups/payment-setups'; export { default as Forward } from './api/forward/forward'; export { default as Checkout } from './Checkout'; export { default } from './Checkout'; From fc16b2cf434af3ed142dbda8468d64bc14d17f1e Mon Sep 17 00:00:00 2001 From: david ruiz Date: Fri, 12 Dec 2025 13:52:51 +0100 Subject: [PATCH 2/5] Import fix for payment setups endpoint --- src/api/payment-setups/payment-setups.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/payment-setups/payment-setups.js b/src/api/payment-setups/payment-setups.js index aca08de..6605bdf 100644 --- a/src/api/payment-setups/payment-setups.js +++ b/src/api/payment-setups/payment-setups.js @@ -1,5 +1,5 @@ import { determineError } from '../../services/errors.js'; -import { post } from '../../services/http.js'; +import { get, post, put } from '../../services/http.js'; import { validatePayment } from '../../services/validation.js'; /** From 8de2c8c025f7931d033285b1f9d543727b4aefa6 Mon Sep 17 00:00:00 2001 From: david ruiz Date: Mon, 15 Dec 2025 12:28:07 +0100 Subject: [PATCH 3/5] Payment setups unit tests --- test/payment-setups/payment-setups.unit.js | 3045 ++++++++++++++++++++ 1 file changed, 3045 insertions(+) create mode 100644 test/payment-setups/payment-setups.unit.js diff --git a/test/payment-setups/payment-setups.unit.js b/test/payment-setups/payment-setups.unit.js new file mode 100644 index 0000000..bf87c88 --- /dev/null +++ b/test/payment-setups/payment-setups.unit.js @@ -0,0 +1,3045 @@ +import { AuthenticationError, ActionNotAllowed, ValidationError } from '../../src/services/errors.js'; +import Checkout from '../../src/index.js'; +import { expect } from 'chai'; +import nock from 'nock'; + +describe('Unit::Payment-Setups', () => { + describe('ConfirmAPaymentSetup response 201', () => { + it('should match response schema 1', async () => { + // Arrange + const response = { + id: 'pay_mbabizu24mvu3mela5njyhpit4', + action_id: 'act_mbabizu24mvu3mela5njyhpit4', + amount: 6540, + currency: 'USD', + approved: true, + status: 'Authorized', + auth_code: '770687', + response_code: '10000', + response_summary: 'Approved', + '3ds': { + downgraded: true, + enrolled: 'N' + }, + risk: { + flagged: true + }, + source: { + type: 'card', + id: 'src_nwd3m4in3hkuddfpjsaevunhdy', + billing_address: { + address_line1: '123 High St.', + address_line2: 'Flat 456', + city: 'London', + state: 'GB', + zip: 'SW1A 1AA', + country: 'GB' + }, + phone: { + country_code: '+1', + number: '415 555 2671' + }, + scheme: 'Visa', + last4: '6584', + fingerprint: 'B16D9C2EF0C861A8825C9BD59CCE9171D84EBC45E89CC792B5D1D2D0DDE3DAB7', + bin: '448504', + card_type: 'CREDIT', + card_category: 'COMMERCIAL', + issuer: 'GE CAPITAL FINANCIAL, INC.', + issuer_country: 'US', + product_type: 'PURCHASING', + avs_check: 'G', + cvv_check: 'Y', + payment_account_reference: 'V001898055688657091' + }, + customer: { + id: 'cus_udst2tfldj6upmye2reztkmm4i', + email: 'johnsmith@example.com', + name: 'John Smith', + phone: { + country_code: '+1', + number: '415 555 2671' + } + }, + processed_on: '2019-09-10T10:11:12Z', + reference: 'ORD-5023-4E89', + processing: { + retrieval_reference_number: '909913440644', + acquirer_transaction_id: '440644309099499894406', + recommendation_code: '02', + partner_order_id: '5GK24544NA744002L' + }, + eci: '06', + scheme_id: '489341065491658', + _links: { + self: { + href: 'https://api.sandbox.checkout.com/payments/pay_mbabizu24mvu3mela5njyhpit4' + }, + actions: { + href: 'https://api.sandbox.checkout.com/payments/pay_mbabizu24mvu3mela5njyhpit4/actions' + }, + void: { + href: 'https://api.sandbox.checkout.com/payments/pay_mbabizu24mvu3mela5njyhpit4/voids' + }, + capture: { + href: 'https://api.sandbox.checkout.com/payments/pay_mbabizu24mvu3mela5njyhpit4/captures' + } + } + }; + + nock('https://api.sandbox.checkout.com') + .post('/payments/setups/pay_setup_123/confirm/pmo_456') + .reply(201, response + ); + + // Act + const id = "pay_setup_123"; + const payment_method_option_id = "pmo_456"; + + const SK = 'sk_test_xxx'; + const cko = new Checkout(SK); + + const result = await cko.paymentSetups.confirmAPaymentSetup(id, payment_method_option_id); + + // Assert + expect(result).to.deep.equal(response); + }); + + }); + describe('ConfirmAPaymentSetup response 400', () => { + it('should match response schema 2', async () => { + // Arrange + var err = null; + const response = {}; + + nock('https://api.sandbox.checkout.com') + .post('/payments/setups/pay_setup_123/confirm/pmo_456') + .reply(400 + ); + + // Act + const id = "pay_setup_123"; + const payment_method_option_id = "pmo_456"; + + const SK = 'sk_test_xxx'; + const cko = new Checkout(SK); + + try + { + const result = await cko.paymentSetups.confirmAPaymentSetup(id, payment_method_option_id); + } catch (error) { + err = error; + } + + // Assert + expect(err).to.be.instanceOf(Error); + }); + + }); + describe('ConfirmAPaymentSetup response 401', () => { + it('should match response schema 3', async () => { + // Arrange + var err = null; + const response = {}; + + nock('https://api.sandbox.checkout.com') + .post('/payments/setups/pay_setup_123/confirm/pmo_456') + .reply(401 + ); + + // Act + const id = "pay_setup_123"; + const payment_method_option_id = "pmo_456"; + + const SK = 'sk_test_xxx'; + const cko = new Checkout(SK); + + try + { + const id = "pay_setup_123"; + const payment_method_option_id = "pmo_456"; + const result = await cko.paymentSetups.confirmAPaymentSetup(id, payment_method_option_id); + } catch (error) { + err = error; + } + + // Assert + expect(err).to.be.instanceOf(AuthenticationError); + }); + + }); + describe('ConfirmAPaymentSetup response 403', () => { + it('should match response schema 4', async () => { + // Arrange + var err = null; + const response = {}; + + nock('https://api.sandbox.checkout.com') + .post('/payments/setups/pay_setup_123/confirm/pmo_456') + .reply(403 + ); + + // Act + const id = "pay_setup_123"; + const payment_method_option_id = "pmo_456"; + + const SK = 'sk_test_xxx'; + const cko = new Checkout(SK); + + try + { + const result = await cko.paymentSetups.confirmAPaymentSetup(id, payment_method_option_id); + } catch (error) { + err = error; + } + + // Assert + expect(err).to.be.instanceOf(ActionNotAllowed); + }); + + }); + describe('ConfirmAPaymentSetup response 422', () => { + it('should match response schema 5', async () => { + // Arrange + var err = null; + const response = { + request_id: '0HL80RJLS76I7', + error_type: 'request_invalid', + error_codes: [ + 'amount_required' + ] + }; + + nock('https://api.sandbox.checkout.com') + .post('/payments/setups/pay_setup_123/confirm/pmo_456') + .reply(422 + ); + + // Act + const id = "pay_setup_123"; + const payment_method_option_id = "pmo_456"; + + const SK = 'sk_test_xxx'; + const cko = new Checkout(SK); + + try + { + const result = await cko.paymentSetups.confirmAPaymentSetup(id, payment_method_option_id); + } catch (error) { + err = error; + } + + // Assert + expect(err).to.be.instanceOf(ValidationError); + }); + + }); + + describe('CreateAPaymentSetup response 200', () => { + it('should match response schema 1', async () => { + // Arrange + const response = { + processing_channel_id: 'pc_q4dbxom5jbgudnjzjpz7j2z6uq', + amount: 10000, + currency: 'GBP', + payment_type: 'Regular', + reference: 'REF-0987-475', + description: 'Set of three t-shirts.', + payment_methods: { + klarna: { + status: 'available', + flags: [ + 'string' + ], + initialization: 'disabled', + account_holder: { + billing_address: { + country: 'GB' + } + }, + payment_method_options: { + sdk: { + id: 'opt_drzstxerxrku3apsepshbslssu', + status: 'pending', + flags: [ + 'string' + ], + action: { + type: 'sdk', + client_token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.ewogICJzZXNzaW9uX2lkIiA6ICIw', + session_id: '0b1d9815-165e-42e2-8867-35bc03789e00' + } + } + } + }, + stcpay: { + status: 'available', + flags: [ + 'string' + ], + initialization: 'disabled', + otp: '123456', + payment_method_options: { + pay_in_full: { + id: 'opt_drzstxerxrku3apsepshbslssu', + status: 'pending', + flags: [ + 'string' + ], + action: { + type: 'otp' + } + } + } + }, + tabby: { + status: 'available', + flags: [ + 'string' + ], + initialization: 'disabled', + payment_method_options: { + installments: { + id: 'opt_drzstxerxrku3apsepshbslssu', + status: 'pending', + flags: [ + 'string' + ] + } + } + }, + bizum: { + status: 'available', + flags: [ + 'string' + ], + initialization: 'disabled', + payment_method_options: { + pay_now: { + id: 'opt_drzstxerxrku3apsepshbslssu', + status: 'pending', + flags: [ + 'string' + ] + } + } + } + }, + settings: { + success_url: 'http://example.com/payments/success', + failure_url: 'http://example.com/payments/fail' + }, + customer: { + email: { + address: 'johnsmith@example.com', + verified: true + }, + name: 'John Smith', + phone: { + country_code: '44', + number: '207 946 0000' + }, + device: { + locale: 'en_GB' + }, + merchant_account: { + id: '1234', + registration_date: '2023-05-01', + last_modified: '2023-05-01', + returning_customer: true, + first_transaction_date: '2023-09-15', + last_transaction_date: '2025-03-28', + total_order_count: 6, + last_payment_amount: 55.99 + } + }, + order: { + items: [ + { + name: 'Battery Power Pack', + quantity: 1, + unit_price: 1000, + total_amount: 1000, + reference: 'BA67A', + discount_amount: 150, + url: 'http://shoppingsite.com/my-item', + image_url: 'http://shoppingsite.com/my-item/image', + type: 'digital' + } + ], + shipping: { + address: { + address_line_1: '10 Canterbury Road', + city: 'London', + zip: 'SW1 1AA' + }, + method: 'string' + }, + sub_merchants: [ + { + id: 'SUB12345', + product_category: 'Electronics', + number_of_trades: 500, + registration_date: '2023-01-15' + } + ], + discount_amount: 10 + }, + industry: { + airline_data: { + ticket: { + number: '0742464639523', + issue_date: '2025-05-01', + issuing_carrier_code: '042', + travel_package_indicator: 'A', + travel_agency_name: 'Checkout Travel Agents', + travel_agency_code: '91114362' + }, + passengers: [ + { + first_name: 'John', + last_name: 'Smith', + date_of_birth: '1990-10-31', + address: { + country: 'GB' + } + } + ], + flight_leg_details: [ + { + flight_number: 'BA1483', + carrier_code: 'BA', + class_of_travelling: 'W', + departure_airport: 'LHW', + departure_date: '2025-10-13', + departure_time: '18:30', + arrival_airport: 'JFK', + stop_over_code: 'X', + fare_basis_code: 'WUP14B' + } + ] + }, + accommodation_data: [ + { + name: 'Checkout Lodge', + booking_reference: 'REF9083748', + check_in_date: '2025-04-11', + check_out_date: '2025-04-18', + address: { + address_line_1: '123 High Street', + address_line_2: 'Flat 456', + city: 'London', + state: 'Greater London', + country: 'United Kingdom', + zip: 'SW1 1AA' + }, + number_of_rooms: 2, + guests: [ + { + first_name: 'Jia', + last_name: 'Tsang', + date_of_birth: '1970-03-19' + } + ], + room: [ + { + rate: 42.3, + number_of_nights: 5 + } + ] + } + ] + } + }; + + nock('https://api.sandbox.checkout.com') + .post('/payments/setups') + .reply(200, response + ); + + // Act + const request = { + processing_channel_id: 'pc_q4dbxom5jbgudnjzjpz7j2z6uq', + amount: 10000, + currency: 'GBP', + payment_type: 'Regular', + reference: 'REF-0987-475', + description: 'Set of three t-shirts.', + payment_methods: { + klarna: { + initialization: 'disabled', + account_holder: { + billing_address: { + country: 'GB' + } + }, + payment_method_options: { + sdk: { + id: 'opt_drzstxerxrku3apsepshbslssu', + action: { + type: 'sdk', + client_token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.ewogICJzZXNzaW9uX2lkIiA6ICIw', + session_id: '0b1d9815-165e-42e2-8867-35bc03789e00' + } + } + } + }, + stcpay: { + initialization: 'disabled', + otp: '123456', + payment_method_options: { + pay_in_full: { + id: 'opt_drzstxerxrku3apsepshbslssu', + action: { + type: 'otp' + } + } + } + }, + tabby: { + initialization: 'disabled', + payment_method_options: { + installments: { + id: 'opt_drzstxerxrku3apsepshbslssu' + } + } + }, + bizum: { + initialization: 'disabled', + payment_method_options: { + pay_now: { + id: 'opt_drzstxerxrku3apsepshbslssu' + } + } + } + }, + settings: { + success_url: 'http://example.com/payments/success', + failure_url: 'http://example.com/payments/fail' + }, + customer: { + email: { + address: 'johnsmith@example.com', + verified: true + }, + name: 'John Smith', + phone: { + country_code: '44', + number: '207 946 0000' + }, + device: { + locale: 'en_GB' + }, + merchant_account: { + id: '1234', + registration_date: '2023-05-01', + last_modified: '2023-05-01', + returning_customer: true, + first_transaction_date: '2023-09-15', + last_transaction_date: '2025-03-28', + total_order_count: 6, + last_payment_amount: 55.99 + } + }, + order: { + items: [ + { + name: 'Battery Power Pack', + quantity: 1, + unit_price: 1000, + total_amount: 1000, + reference: 'BA67A', + discount_amount: 150, + url: 'http://shoppingsite.com/my-item', + image_url: 'http://shoppingsite.com/my-item/image', + type: 'digital' + } + ], + shipping: { + address: { + address_line_1: '10 Canterbury Road', + city: 'London', + zip: 'SW1 1AA' + }, + method: 'string' + }, + sub_merchants: [ + { + id: 'SUB12345', + product_category: 'Electronics', + number_of_trades: 500, + registration_date: '2023-01-15' + } + ], + discount_amount: 10 + }, + industry: { + airline_data: { + ticket: { + number: '0742464639523', + issue_date: '2025-05-01', + issuing_carrier_code: '042', + travel_package_indicator: 'A', + travel_agency_name: 'Checkout Travel Agents', + travel_agency_code: '91114362' + }, + passengers: [ + { + first_name: 'John', + last_name: 'Smith', + date_of_birth: '1990-10-31', + address: { + country: 'GB' + } + } + ], + flight_leg_details: [ + { + flight_number: 'BA1483', + carrier_code: 'BA', + class_of_travelling: 'W', + departure_airport: 'LHW', + departure_date: '2025-10-13', + departure_time: '18:30', + arrival_airport: 'JFK', + stop_over_code: 'X', + fare_basis_code: 'WUP14B' + } + ] + }, + accommodation_data: [ + { + name: 'Checkout Lodge', + booking_reference: 'REF9083748', + check_in_date: '2025-04-11', + check_out_date: '2025-04-18', + address: { + address_line_1: '123 High Street', + address_line_2: 'Flat 456', + city: 'London', + state: 'Greater London', + country: 'United Kingdom', + zip: 'SW1 1AA' + }, + number_of_rooms: 2, + guests: [ + { + first_name: 'Jia', + last_name: 'Tsang', + date_of_birth: '1970-03-19' + } + ], + room: [ + { + rate: 42.3, + number_of_nights: 5 + } + ] + } + ] + } + }; + + const SK = 'sk_test_xxx'; + const cko = new Checkout(SK); + + const result = await cko.paymentSetups.createAPaymentSetup(request); + + // Assert + expect(result).to.deep.equal(response); + }); + + }); + describe('CreateAPaymentSetup response 400', () => { + it('should match response schema 2', async () => { + // Arrange + var err = null; + const response = {}; + + nock('https://api.sandbox.checkout.com') + .post('/payments/setups') + .reply(400 + ); + + // Act + const request = { + processing_channel_id: 'pc_q4dbxom5jbgudnjzjpz7j2z6uq', + amount: 10000, + currency: 'GBP', + payment_type: 'Regular', + reference: 'REF-0987-475', + description: 'Set of three t-shirts.', + payment_methods: { + klarna: { + initialization: 'disabled', + account_holder: { + billing_address: { + country: 'GB' + } + }, + payment_method_options: { + sdk: { + id: 'opt_drzstxerxrku3apsepshbslssu', + action: { + type: 'sdk', + client_token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.ewogICJzZXNzaW9uX2lkIiA6ICIw', + session_id: '0b1d9815-165e-42e2-8867-35bc03789e00' + } + } + } + }, + stcpay: { + initialization: 'disabled', + otp: '123456', + payment_method_options: { + pay_in_full: { + id: 'opt_drzstxerxrku3apsepshbslssu', + action: { + type: 'otp' + } + } + } + }, + tabby: { + initialization: 'disabled', + payment_method_options: { + installments: { + id: 'opt_drzstxerxrku3apsepshbslssu' + } + } + }, + bizum: { + initialization: 'disabled', + payment_method_options: { + pay_now: { + id: 'opt_drzstxerxrku3apsepshbslssu' + } + } + } + }, + settings: { + success_url: 'http://example.com/payments/success', + failure_url: 'http://example.com/payments/fail' + }, + customer: { + email: { + address: 'johnsmith@example.com', + verified: true + }, + name: 'John Smith', + phone: { + country_code: '44', + number: '207 946 0000' + }, + device: { + locale: 'en_GB' + }, + merchant_account: { + id: '1234', + registration_date: '2023-05-01', + last_modified: '2023-05-01', + returning_customer: true, + first_transaction_date: '2023-09-15', + last_transaction_date: '2025-03-28', + total_order_count: 6, + last_payment_amount: 55.99 + } + }, + order: { + items: [ + { + name: 'Battery Power Pack', + quantity: 1, + unit_price: 1000, + total_amount: 1000, + reference: 'BA67A', + discount_amount: 150, + url: 'http://shoppingsite.com/my-item', + image_url: 'http://shoppingsite.com/my-item/image', + type: 'digital' + } + ], + shipping: { + address: { + address_line_1: '10 Canterbury Road', + city: 'London', + zip: 'SW1 1AA' + }, + method: 'string' + }, + sub_merchants: [ + { + id: 'SUB12345', + product_category: 'Electronics', + number_of_trades: 500, + registration_date: '2023-01-15' + } + ], + discount_amount: 10 + }, + industry: { + airline_data: { + ticket: { + number: '0742464639523', + issue_date: '2025-05-01', + issuing_carrier_code: '042', + travel_package_indicator: 'A', + travel_agency_name: 'Checkout Travel Agents', + travel_agency_code: '91114362' + }, + passengers: [ + { + first_name: 'John', + last_name: 'Smith', + date_of_birth: '1990-10-31', + address: { + country: 'GB' + } + } + ], + flight_leg_details: [ + { + flight_number: 'BA1483', + carrier_code: 'BA', + class_of_travelling: 'W', + departure_airport: 'LHW', + departure_date: '2025-10-13', + departure_time: '18:30', + arrival_airport: 'JFK', + stop_over_code: 'X', + fare_basis_code: 'WUP14B' + } + ] + }, + accommodation_data: [ + { + name: 'Checkout Lodge', + booking_reference: 'REF9083748', + check_in_date: '2025-04-11', + check_out_date: '2025-04-18', + address: { + address_line_1: '123 High Street', + address_line_2: 'Flat 456', + city: 'London', + state: 'Greater London', + country: 'United Kingdom', + zip: 'SW1 1AA' + }, + number_of_rooms: 2, + guests: [ + { + first_name: 'Jia', + last_name: 'Tsang', + date_of_birth: '1970-03-19' + } + ], + room: [ + { + rate: 42.3, + number_of_nights: 5 + } + ] + } + ] + } + }; + + const SK = 'sk_test_xxx'; + const cko = new Checkout(SK); + + try { + const result = await cko.paymentSetups.createAPaymentSetup(request); + } catch (error) { + err = error; + } + + // Assert + expect(err).to.be.instanceOf(Error); + }); + + }); + describe('CreateAPaymentSetup response 401', () => { + it('should match response schema 3', async () => { + // Arrange + var err = null; + const response = {}; + + nock('https://api.sandbox.checkout.com') + .post('/payments/setups') + .reply(401 + ); + + // Act + const request = { + processing_channel_id: 'pc_q4dbxom5jbgudnjzjpz7j2z6uq', + amount: 10000, + currency: 'GBP', + payment_type: 'Regular', + reference: 'REF-0987-475', + description: 'Set of three t-shirts.', + payment_methods: { + klarna: { + initialization: 'disabled', + account_holder: { + billing_address: { + country: 'GB' + } + }, + payment_method_options: { + sdk: { + id: 'opt_drzstxerxrku3apsepshbslssu', + action: { + type: 'sdk', + client_token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.ewogICJzZXNzaW9uX2lkIiA6ICIw', + session_id: '0b1d9815-165e-42e2-8867-35bc03789e00' + } + } + } + }, + stcpay: { + initialization: 'disabled', + otp: '123456', + payment_method_options: { + pay_in_full: { + id: 'opt_drzstxerxrku3apsepshbslssu', + action: { + type: 'otp' + } + } + } + }, + tabby: { + initialization: 'disabled', + payment_method_options: { + installments: { + id: 'opt_drzstxerxrku3apsepshbslssu' + } + } + }, + bizum: { + initialization: 'disabled', + payment_method_options: { + pay_now: { + id: 'opt_drzstxerxrku3apsepshbslssu' + } + } + } + }, + settings: { + success_url: 'http://example.com/payments/success', + failure_url: 'http://example.com/payments/fail' + }, + customer: { + email: { + address: 'johnsmith@example.com', + verified: true + }, + name: 'John Smith', + phone: { + country_code: '44', + number: '207 946 0000' + }, + device: { + locale: 'en_GB' + }, + merchant_account: { + id: '1234', + registration_date: '2023-05-01', + last_modified: '2023-05-01', + returning_customer: true, + first_transaction_date: '2023-09-15', + last_transaction_date: '2025-03-28', + total_order_count: 6, + last_payment_amount: 55.99 + } + }, + order: { + items: [ + { + name: 'Battery Power Pack', + quantity: 1, + unit_price: 1000, + total_amount: 1000, + reference: 'BA67A', + discount_amount: 150, + url: 'http://shoppingsite.com/my-item', + image_url: 'http://shoppingsite.com/my-item/image', + type: 'digital' + } + ], + shipping: { + address: { + address_line_1: '10 Canterbury Road', + city: 'London', + zip: 'SW1 1AA' + }, + method: 'string' + }, + sub_merchants: [ + { + id: 'SUB12345', + product_category: 'Electronics', + number_of_trades: 500, + registration_date: '2023-01-15' + } + ], + discount_amount: 10 + }, + industry: { + airline_data: { + ticket: { + number: '0742464639523', + issue_date: '2025-05-01', + issuing_carrier_code: '042', + travel_package_indicator: 'A', + travel_agency_name: 'Checkout Travel Agents', + travel_agency_code: '91114362' + }, + passengers: [ + { + first_name: 'John', + last_name: 'Smith', + date_of_birth: '1990-10-31', + address: { + country: 'GB' + } + } + ], + flight_leg_details: [ + { + flight_number: 'BA1483', + carrier_code: 'BA', + class_of_travelling: 'W', + departure_airport: 'LHW', + departure_date: '2025-10-13', + departure_time: '18:30', + arrival_airport: 'JFK', + stop_over_code: 'X', + fare_basis_code: 'WUP14B' + } + ] + }, + accommodation_data: [ + { + name: 'Checkout Lodge', + booking_reference: 'REF9083748', + check_in_date: '2025-04-11', + check_out_date: '2025-04-18', + address: { + address_line_1: '123 High Street', + address_line_2: 'Flat 456', + city: 'London', + state: 'Greater London', + country: 'United Kingdom', + zip: 'SW1 1AA' + }, + number_of_rooms: 2, + guests: [ + { + first_name: 'Jia', + last_name: 'Tsang', + date_of_birth: '1970-03-19' + } + ], + room: [ + { + rate: 42.3, + number_of_nights: 5 + } + ] + } + ] + } + }; + + const SK = 'sk_test_xxx'; + const cko = new Checkout(SK); + + try { + const result = await cko.paymentSetups.createAPaymentSetup(request); + } catch (error) { + err = error; + } + + // Assert + expect(err).to.be.instanceOf(AuthenticationError); + }); + + }); + describe('CreateAPaymentSetup response 403', () => { + it('should match response schema 4', async () => { + // Arrange + var err = null; + const response = {}; + + nock('https://api.sandbox.checkout.com') + .post('/payments/setups') + .reply(403 + ); + + // Act + const request = { + processing_channel_id: 'pc_q4dbxom5jbgudnjzjpz7j2z6uq', + amount: 10000, + currency: 'GBP', + payment_type: 'Regular', + reference: 'REF-0987-475', + description: 'Set of three t-shirts.', + payment_methods: { + klarna: { + initialization: 'disabled', + account_holder: { + billing_address: { + country: 'GB' + } + }, + payment_method_options: { + sdk: { + id: 'opt_drzstxerxrku3apsepshbslssu', + action: { + type: 'sdk', + client_token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.ewogICJzZXNzaW9uX2lkIiA6ICIw', + session_id: '0b1d9815-165e-42e2-8867-35bc03789e00' + } + } + } + }, + stcpay: { + initialization: 'disabled', + otp: '123456', + payment_method_options: { + pay_in_full: { + id: 'opt_drzstxerxrku3apsepshbslssu', + action: { + type: 'otp' + } + } + } + }, + tabby: { + initialization: 'disabled', + payment_method_options: { + installments: { + id: 'opt_drzstxerxrku3apsepshbslssu' + } + } + }, + bizum: { + initialization: 'disabled', + payment_method_options: { + pay_now: { + id: 'opt_drzstxerxrku3apsepshbslssu' + } + } + } + }, + settings: { + success_url: 'http://example.com/payments/success', + failure_url: 'http://example.com/payments/fail' + }, + customer: { + email: { + address: 'johnsmith@example.com', + verified: true + }, + name: 'John Smith', + phone: { + country_code: '44', + number: '207 946 0000' + }, + device: { + locale: 'en_GB' + }, + merchant_account: { + id: '1234', + registration_date: '2023-05-01', + last_modified: '2023-05-01', + returning_customer: true, + first_transaction_date: '2023-09-15', + last_transaction_date: '2025-03-28', + total_order_count: 6, + last_payment_amount: 55.99 + } + }, + order: { + items: [ + { + name: 'Battery Power Pack', + quantity: 1, + unit_price: 1000, + total_amount: 1000, + reference: 'BA67A', + discount_amount: 150, + url: 'http://shoppingsite.com/my-item', + image_url: 'http://shoppingsite.com/my-item/image', + type: 'digital' + } + ], + shipping: { + address: { + address_line_1: '10 Canterbury Road', + city: 'London', + zip: 'SW1 1AA' + }, + method: 'string' + }, + sub_merchants: [ + { + id: 'SUB12345', + product_category: 'Electronics', + number_of_trades: 500, + registration_date: '2023-01-15' + } + ], + discount_amount: 10 + }, + industry: { + airline_data: { + ticket: { + number: '0742464639523', + issue_date: '2025-05-01', + issuing_carrier_code: '042', + travel_package_indicator: 'A', + travel_agency_name: 'Checkout Travel Agents', + travel_agency_code: '91114362' + }, + passengers: [ + { + first_name: 'John', + last_name: 'Smith', + date_of_birth: '1990-10-31', + address: { + country: 'GB' + } + } + ], + flight_leg_details: [ + { + flight_number: 'BA1483', + carrier_code: 'BA', + class_of_travelling: 'W', + departure_airport: 'LHW', + departure_date: '2025-10-13', + departure_time: '18:30', + arrival_airport: 'JFK', + stop_over_code: 'X', + fare_basis_code: 'WUP14B' + } + ] + }, + accommodation_data: [ + { + name: 'Checkout Lodge', + booking_reference: 'REF9083748', + check_in_date: '2025-04-11', + check_out_date: '2025-04-18', + address: { + address_line_1: '123 High Street', + address_line_2: 'Flat 456', + city: 'London', + state: 'Greater London', + country: 'United Kingdom', + zip: 'SW1 1AA' + }, + number_of_rooms: 2, + guests: [ + { + first_name: 'Jia', + last_name: 'Tsang', + date_of_birth: '1970-03-19' + } + ], + room: [ + { + rate: 42.3, + number_of_nights: 5 + } + ] + } + ] + } + }; + + const SK = 'sk_test_xxx'; + const cko = new Checkout(SK); + + try { + const result = await cko.paymentSetups.createAPaymentSetup(request); + } catch (error) { + err = error; + } + + // Assert + expect(err).to.be.instanceOf(ActionNotAllowed); + }); + + }); + describe('CreateAPaymentSetup response 422', () => { + it('should match response schema 5', async () => { + // Arrange + var err = null; + const response = { + request_id: '0HL80RJLS76I7', + error_type: 'request_invalid', + error_codes: [ + 'amount_required' + ] + }; + + nock('https://api.sandbox.checkout.com') + .post('/payments/setups') + .reply(422 + ); + + // Act + const request = { + processing_channel_id: 'pc_q4dbxom5jbgudnjzjpz7j2z6uq', + amount: 10000, + currency: 'GBP', + payment_type: 'Regular', + reference: 'REF-0987-475', + description: 'Set of three t-shirts.', + payment_methods: { + klarna: { + initialization: 'disabled', + account_holder: { + billing_address: { + country: 'GB' + } + }, + payment_method_options: { + sdk: { + id: 'opt_drzstxerxrku3apsepshbslssu', + action: { + type: 'sdk', + client_token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.ewogICJzZXNzaW9uX2lkIiA6ICIw', + session_id: '0b1d9815-165e-42e2-8867-35bc03789e00' + } + } + } + }, + stcpay: { + initialization: 'disabled', + otp: '123456', + payment_method_options: { + pay_in_full: { + id: 'opt_drzstxerxrku3apsepshbslssu', + action: { + type: 'otp' + } + } + } + }, + tabby: { + initialization: 'disabled', + payment_method_options: { + installments: { + id: 'opt_drzstxerxrku3apsepshbslssu' + } + } + }, + bizum: { + initialization: 'disabled', + payment_method_options: { + pay_now: { + id: 'opt_drzstxerxrku3apsepshbslssu' + } + } + } + }, + settings: { + success_url: 'http://example.com/payments/success', + failure_url: 'http://example.com/payments/fail' + }, + customer: { + email: { + address: 'johnsmith@example.com', + verified: true + }, + name: 'John Smith', + phone: { + country_code: '44', + number: '207 946 0000' + }, + device: { + locale: 'en_GB' + }, + merchant_account: { + id: '1234', + registration_date: '2023-05-01', + last_modified: '2023-05-01', + returning_customer: true, + first_transaction_date: '2023-09-15', + last_transaction_date: '2025-03-28', + total_order_count: 6, + last_payment_amount: 55.99 + } + }, + order: { + items: [ + { + name: 'Battery Power Pack', + quantity: 1, + unit_price: 1000, + total_amount: 1000, + reference: 'BA67A', + discount_amount: 150, + url: 'http://shoppingsite.com/my-item', + image_url: 'http://shoppingsite.com/my-item/image', + type: 'digital' + } + ], + shipping: { + address: { + address_line_1: '10 Canterbury Road', + city: 'London', + zip: 'SW1 1AA' + }, + method: 'string' + }, + sub_merchants: [ + { + id: 'SUB12345', + product_category: 'Electronics', + number_of_trades: 500, + registration_date: '2023-01-15' + } + ], + discount_amount: 10 + }, + industry: { + airline_data: { + ticket: { + number: '0742464639523', + issue_date: '2025-05-01', + issuing_carrier_code: '042', + travel_package_indicator: 'A', + travel_agency_name: 'Checkout Travel Agents', + travel_agency_code: '91114362' + }, + passengers: [ + { + first_name: 'John', + last_name: 'Smith', + date_of_birth: '1990-10-31', + address: { + country: 'GB' + } + } + ], + flight_leg_details: [ + { + flight_number: 'BA1483', + carrier_code: 'BA', + class_of_travelling: 'W', + departure_airport: 'LHW', + departure_date: '2025-10-13', + departure_time: '18:30', + arrival_airport: 'JFK', + stop_over_code: 'X', + fare_basis_code: 'WUP14B' + } + ] + }, + accommodation_data: [ + { + name: 'Checkout Lodge', + booking_reference: 'REF9083748', + check_in_date: '2025-04-11', + check_out_date: '2025-04-18', + address: { + address_line_1: '123 High Street', + address_line_2: 'Flat 456', + city: 'London', + state: 'Greater London', + country: 'United Kingdom', + zip: 'SW1 1AA' + }, + number_of_rooms: 2, + guests: [ + { + first_name: 'Jia', + last_name: 'Tsang', + date_of_birth: '1970-03-19' + } + ], + room: [ + { + rate: 42.3, + number_of_nights: 5 + } + ] + } + ] + } + }; + + const SK = 'sk_test_xxx'; + const cko = new Checkout(SK); + + try { + const result = await cko.paymentSetups.createAPaymentSetup(request); + } catch (error) { + err = error; + } + + // Assert + expect(err).to.be.instanceOf(ValidationError); + }); + + }); + + describe('GetAPaymentSetup response 200', () => { + it('should match response schema 1', async () => { + // Arrange + const response = { + id: 'string', + processing_channel_id: 'pc_q4dbxom5jbgudnjzjpz7j2z6uq', + amount: 10000, + currency: 'GBP', + payment_type: 'Regular', + reference: 'REF-0987-475', + description: 'Set of three t-shirts.', + payment_methods: { + klarna: { + status: 'available', + flags: [ + 'string' + ], + initialization: 'disabled', + account_holder: { + billing_address: { + country: 'GB' + } + }, + payment_method_options: { + sdk: { + id: 'opt_drzstxerxrku3apsepshbslssu', + status: 'pending', + flags: [ + 'string' + ], + action: { + type: 'sdk', + client_token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.ewogICJzZXNzaW9uX2lkIiA6ICIw', + session_id: '0b1d9815-165e-42e2-8867-35bc03789e00' + } + } + } + }, + stcpay: { + status: 'available', + flags: [ + 'string' + ], + initialization: 'disabled', + otp: '123456', + payment_method_options: { + pay_in_full: { + id: 'opt_drzstxerxrku3apsepshbslssu', + status: 'pending', + flags: [ + 'string' + ], + action: { + type: 'otp' + } + } + } + }, + tabby: { + status: 'available', + flags: [ + 'string' + ], + initialization: 'disabled', + payment_method_options: { + installments: { + id: 'opt_drzstxerxrku3apsepshbslssu', + status: 'pending', + flags: [ + 'string' + ] + } + } + }, + bizum: { + status: 'available', + flags: [ + 'string' + ], + initialization: 'disabled', + payment_method_options: { + pay_now: { + id: 'opt_drzstxerxrku3apsepshbslssu', + status: 'pending', + flags: [ + 'string' + ] + } + } + } + }, + settings: { + success_url: 'http://example.com/payments/success', + failure_url: 'http://example.com/payments/fail' + }, + customer: { + email: { + address: 'johnsmith@example.com', + verified: true + }, + name: 'John Smith', + phone: { + country_code: '44', + number: '207 946 0000' + }, + device: { + locale: 'en_GB' + }, + merchant_account: { + id: '1234', + registration_date: '2023-05-01', + last_modified: '2023-05-01', + returning_customer: true, + first_transaction_date: '2023-09-15', + last_transaction_date: '2025-03-28', + total_order_count: 6, + last_payment_amount: 55.99 + } + }, + order: { + items: [ + { + name: 'Battery Power Pack', + quantity: 1, + unit_price: 1000, + total_amount: 1000, + reference: 'BA67A', + discount_amount: 150, + url: 'http://shoppingsite.com/my-item', + image_url: 'http://shoppingsite.com/my-item/image', + type: 'digital' + } + ], + shipping: { + address: { + address_line_1: '10 Canterbury Road', + city: 'London', + zip: 'SW1 1AA' + }, + method: 'string' + }, + sub_merchants: [ + { + id: 'SUB12345', + product_category: 'Electronics', + number_of_trades: 500, + registration_date: '2023-01-15' + } + ], + discount_amount: 10 + }, + industry: { + airline_data: { + ticket: { + number: '0742464639523', + issue_date: '2025-05-01', + issuing_carrier_code: '042', + travel_package_indicator: 'A', + travel_agency_name: 'Checkout Travel Agents', + travel_agency_code: '91114362' + }, + passengers: [ + { + first_name: 'John', + last_name: 'Smith', + date_of_birth: '1990-10-31', + address: { + country: 'GB' + } + } + ], + flight_leg_details: [ + { + flight_number: 'BA1483', + carrier_code: 'BA', + class_of_travelling: 'W', + departure_airport: 'LHW', + departure_date: '2025-10-13', + departure_time: '18:30', + arrival_airport: 'JFK', + stop_over_code: 'X', + fare_basis_code: 'WUP14B' + } + ] + }, + accommodation_data: [ + { + name: 'Checkout Lodge', + booking_reference: 'REF9083748', + check_in_date: '2025-04-11', + check_out_date: '2025-04-18', + address: { + address_line_1: '123 High Street', + address_line_2: 'Flat 456', + city: 'London', + state: 'Greater London', + country: 'United Kingdom', + zip: 'SW1 1AA' + }, + number_of_rooms: 2, + guests: [ + { + first_name: 'Jia', + last_name: 'Tsang', + date_of_birth: '1970-03-19' + } + ], + room: [ + { + rate: 42.3, + number_of_nights: 5 + } + ] + } + ] + } + }; + + nock('https://api.sandbox.checkout.com') + .get('/payments/setups/pay_setup_123') + .reply(200, response + ); + + // Act + const id = "pay_setup_123"; + const request = {}; + + const SK = 'sk_test_xxx'; + const cko = new Checkout(SK); + + const result = await cko.paymentSetups.getAPaymentSetup(id); + + // Assert + expect(result).to.deep.equal(response); + }); + + }); + describe('GetAPaymentSetup response 401', () => { + it('should match response schema 2', async () => { + // Arrange + var err = null; + const response = {}; + + nock('https://api.sandbox.checkout.com') + .get('/payments/setups/pay_setup_123') + .reply(401 + ); + + // Act + const id = "pay_setup_123"; + + const SK = 'sk_test_xxx'; + const cko = new Checkout(SK); + + try { + const result = await cko.paymentSetups.getAPaymentSetup(id); + } catch (error) { + err = error; + } + + // Assert + expect(err).to.be.instanceOf(AuthenticationError); + }); + + }); + describe('GetAPaymentSetup response 403', () => { + it('should match response schema 3', async () => { + // Arrange + var err = null; + const response = {}; + + nock('https://api.sandbox.checkout.com') + .get('/payments/setups/pay_setup_123') + .reply(403 + ); + + // Act + const id = "pay_setup_123"; + + const SK = 'sk_test_xxx'; + const cko = new Checkout(SK); + + try { + const result = await cko.paymentSetups.getAPaymentSetup(id); + } catch (error) { + err = error; + } + + // Assert + expect(err).to.be.instanceOf(ActionNotAllowed); + }); + + }); + + describe('UpdateAPaymentSetup response 200', () => { + it('should match response schema 1', async () => { + // Arrange + const response = { + id: 'string', + processing_channel_id: 'pc_q4dbxom5jbgudnjzjpz7j2z6uq', + amount: 10000, + currency: 'GBP', + payment_type: 'Regular', + reference: 'REF-0987-475', + description: 'Set of three t-shirts.', + payment_methods: { + klarna: { + status: 'available', + flags: [ + 'string' + ], + initialization: 'disabled', + account_holder: { + billing_address: { + country: 'GB' + } + }, + payment_method_options: { + sdk: { + id: 'opt_drzstxerxrku3apsepshbslssu', + status: 'pending', + flags: [ + 'string' + ], + action: { + type: 'sdk', + client_token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.ewogICJzZXNzaW9uX2lkIiA6ICIw', + session_id: '0b1d9815-165e-42e2-8867-35bc03789e00' + } + } + } + }, + stcpay: { + status: 'available', + flags: [ + 'string' + ], + initialization: 'disabled', + otp: '123456', + payment_method_options: { + pay_in_full: { + id: 'opt_drzstxerxrku3apsepshbslssu', + status: 'pending', + flags: [ + 'string' + ], + action: { + type: 'otp' + } + } + } + }, + tabby: { + status: 'available', + flags: [ + 'string' + ], + initialization: 'disabled', + payment_method_options: { + installments: { + id: 'opt_drzstxerxrku3apsepshbslssu', + status: 'pending', + flags: [ + 'string' + ] + } + } + }, + bizum: { + status: 'available', + flags: [ + 'string' + ], + initialization: 'disabled', + payment_method_options: { + pay_now: { + id: 'opt_drzstxerxrku3apsepshbslssu', + status: 'pending', + flags: [ + 'string' + ] + } + } + } + }, + settings: { + success_url: 'http://example.com/payments/success', + failure_url: 'http://example.com/payments/fail' + }, + customer: { + email: { + address: 'johnsmith@example.com', + verified: true + }, + name: 'John Smith', + phone: { + country_code: '44', + number: '207 946 0000' + }, + device: { + locale: 'en_GB' + }, + merchant_account: { + id: '1234', + registration_date: '2023-05-01', + last_modified: '2023-05-01', + returning_customer: true, + first_transaction_date: '2023-09-15', + last_transaction_date: '2025-03-28', + total_order_count: 6, + last_payment_amount: 55.99 + } + }, + order: { + items: [ + { + name: 'Battery Power Pack', + quantity: 1, + unit_price: 1000, + total_amount: 1000, + reference: 'BA67A', + discount_amount: 150, + url: 'http://shoppingsite.com/my-item', + image_url: 'http://shoppingsite.com/my-item/image', + type: 'digital' + } + ], + shipping: { + address: { + address_line_1: '10 Canterbury Road', + city: 'London', + zip: 'SW1 1AA' + }, + method: 'string' + }, + sub_merchants: [ + { + id: 'SUB12345', + product_category: 'Electronics', + number_of_trades: 500, + registration_date: '2023-01-15' + } + ], + discount_amount: 10 + }, + industry: { + airline_data: { + ticket: { + number: '0742464639523', + issue_date: '2025-05-01', + issuing_carrier_code: '042', + travel_package_indicator: 'A', + travel_agency_name: 'Checkout Travel Agents', + travel_agency_code: '91114362' + }, + passengers: [ + { + first_name: 'John', + last_name: 'Smith', + date_of_birth: '1990-10-31', + address: { + country: 'GB' + } + } + ], + flight_leg_details: [ + { + flight_number: 'BA1483', + carrier_code: 'BA', + class_of_travelling: 'W', + departure_airport: 'LHW', + departure_date: '2025-10-13', + departure_time: '18:30', + arrival_airport: 'JFK', + stop_over_code: 'X', + fare_basis_code: 'WUP14B' + } + ] + }, + accommodation_data: [ + { + name: 'Checkout Lodge', + booking_reference: 'REF9083748', + check_in_date: '2025-04-11', + check_out_date: '2025-04-18', + address: { + address_line_1: '123 High Street', + address_line_2: 'Flat 456', + city: 'London', + state: 'Greater London', + country: 'United Kingdom', + zip: 'SW1 1AA' + }, + number_of_rooms: 2, + guests: [ + { + first_name: 'Jia', + last_name: 'Tsang', + date_of_birth: '1970-03-19' + } + ], + room: [ + { + rate: 42.3, + number_of_nights: 5 + } + ] + } + ] + } + }; + + nock('https://api.sandbox.checkout.com') + .put('/payments/setups/pay_setup_123') + .reply(200, response + ); + + // Act + const id = "pay_setup_123"; + const request = { + processing_channel_id: 'pc_q4dbxom5jbgudnjzjpz7j2z6uq', + amount: 10000, + currency: 'GBP', + payment_type: 'Regular', + reference: 'REF-0987-475', + description: 'Set of three t-shirts.', + payment_methods: { + klarna: { + initialization: 'disabled', + account_holder: { + billing_address: { + country: 'GB' + } + }, + payment_method_options: { + sdk: { + id: 'opt_drzstxerxrku3apsepshbslssu', + action: { + type: 'sdk', + client_token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.ewogICJzZXNzaW9uX2lkIiA6ICIw', + session_id: '0b1d9815-165e-42e2-8867-35bc03789e00' + } + } + } + }, + stcpay: { + initialization: 'disabled', + otp: '123456', + payment_method_options: { + pay_in_full: { + id: 'opt_drzstxerxrku3apsepshbslssu', + action: { + type: 'otp' + } + } + } + }, + tabby: { + initialization: 'disabled', + payment_method_options: { + installments: { + id: 'opt_drzstxerxrku3apsepshbslssu' + } + } + }, + bizum: { + initialization: 'disabled', + payment_method_options: { + pay_now: { + id: 'opt_drzstxerxrku3apsepshbslssu' + } + } + } + }, + settings: { + success_url: 'http://example.com/payments/success', + failure_url: 'http://example.com/payments/fail' + }, + customer: { + email: { + address: 'johnsmith@example.com', + verified: true + }, + name: 'John Smith', + phone: { + country_code: '44', + number: '207 946 0000' + }, + device: { + locale: 'en_GB' + }, + merchant_account: { + id: '1234', + registration_date: '2023-05-01', + last_modified: '2023-05-01', + returning_customer: true, + first_transaction_date: '2023-09-15', + last_transaction_date: '2025-03-28', + total_order_count: 6, + last_payment_amount: 55.99 + } + }, + order: { + items: [ + { + name: 'Battery Power Pack', + quantity: 1, + unit_price: 1000, + total_amount: 1000, + reference: 'BA67A', + discount_amount: 150, + url: 'http://shoppingsite.com/my-item', + image_url: 'http://shoppingsite.com/my-item/image', + type: 'digital' + } + ], + shipping: { + address: { + address_line_1: '10 Canterbury Road', + city: 'London', + zip: 'SW1 1AA' + }, + method: 'string' + }, + sub_merchants: [ + { + id: 'SUB12345', + product_category: 'Electronics', + number_of_trades: 500, + registration_date: '2023-01-15' + } + ], + discount_amount: 10 + }, + industry: { + airline_data: { + ticket: { + number: '0742464639523', + issue_date: '2025-05-01', + issuing_carrier_code: '042', + travel_package_indicator: 'A', + travel_agency_name: 'Checkout Travel Agents', + travel_agency_code: '91114362' + }, + passengers: [ + { + first_name: 'John', + last_name: 'Smith', + date_of_birth: '1990-10-31', + address: { + country: 'GB' + } + } + ], + flight_leg_details: [ + { + flight_number: 'BA1483', + carrier_code: 'BA', + class_of_travelling: 'W', + departure_airport: 'LHW', + departure_date: '2025-10-13', + departure_time: '18:30', + arrival_airport: 'JFK', + stop_over_code: 'X', + fare_basis_code: 'WUP14B' + } + ] + }, + accommodation_data: [ + { + name: 'Checkout Lodge', + booking_reference: 'REF9083748', + check_in_date: '2025-04-11', + check_out_date: '2025-04-18', + address: { + address_line_1: '123 High Street', + address_line_2: 'Flat 456', + city: 'London', + state: 'Greater London', + country: 'United Kingdom', + zip: 'SW1 1AA' + }, + number_of_rooms: 2, + guests: [ + { + first_name: 'Jia', + last_name: 'Tsang', + date_of_birth: '1970-03-19' + } + ], + room: [ + { + rate: 42.3, + number_of_nights: 5 + } + ] + } + ] + } + }; + + const SK = 'sk_test_xxx'; + const cko = new Checkout(SK); + + const result = await cko.paymentSetups.updateAPaymentSetup(id, request); + + // Assert + expect(result).to.deep.equal(response); + }); + + }); + describe('UpdateAPaymentSetup response 400', () => { + it('should match response schema 2', async () => { + // Arrange + var err = null; + const response = {}; + + nock('https://api.sandbox.checkout.com') + .put('/payments/setups/pay_setup_123') + .reply(400 + ); + + // Act + const id = "pay_setup_123"; + const request = { + processing_channel_id: 'pc_q4dbxom5jbgudnjzjpz7j2z6uq', + amount: 10000, + currency: 'GBP', + payment_type: 'Regular', + reference: 'REF-0987-475', + description: 'Set of three t-shirts.', + payment_methods: { + klarna: { + initialization: 'disabled', + account_holder: { + billing_address: { + country: 'GB' + } + }, + payment_method_options: { + sdk: { + id: 'opt_drzstxerxrku3apsepshbslssu', + action: { + type: 'sdk', + client_token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.ewogICJzZXNzaW9uX2lkIiA6ICIw', + session_id: '0b1d9815-165e-42e2-8867-35bc03789e00' + } + } + } + }, + stcpay: { + initialization: 'disabled', + otp: '123456', + payment_method_options: { + pay_in_full: { + id: 'opt_drzstxerxrku3apsepshbslssu', + action: { + type: 'otp' + } + } + } + }, + tabby: { + initialization: 'disabled', + payment_method_options: { + installments: { + id: 'opt_drzstxerxrku3apsepshbslssu' + } + } + }, + bizum: { + initialization: 'disabled', + payment_method_options: { + pay_now: { + id: 'opt_drzstxerxrku3apsepshbslssu' + } + } + } + }, + settings: { + success_url: 'http://example.com/payments/success', + failure_url: 'http://example.com/payments/fail' + }, + customer: { + email: { + address: 'johnsmith@example.com', + verified: true + }, + name: 'John Smith', + phone: { + country_code: '44', + number: '207 946 0000' + }, + device: { + locale: 'en_GB' + }, + merchant_account: { + id: '1234', + registration_date: '2023-05-01', + last_modified: '2023-05-01', + returning_customer: true, + first_transaction_date: '2023-09-15', + last_transaction_date: '2025-03-28', + total_order_count: 6, + last_payment_amount: 55.99 + } + }, + order: { + items: [ + { + name: 'Battery Power Pack', + quantity: 1, + unit_price: 1000, + total_amount: 1000, + reference: 'BA67A', + discount_amount: 150, + url: 'http://shoppingsite.com/my-item', + image_url: 'http://shoppingsite.com/my-item/image', + type: 'digital' + } + ], + shipping: { + address: { + address_line_1: '10 Canterbury Road', + city: 'London', + zip: 'SW1 1AA' + }, + method: 'string' + }, + sub_merchants: [ + { + id: 'SUB12345', + product_category: 'Electronics', + number_of_trades: 500, + registration_date: '2023-01-15' + } + ], + discount_amount: 10 + }, + industry: { + airline_data: { + ticket: { + number: '0742464639523', + issue_date: '2025-05-01', + issuing_carrier_code: '042', + travel_package_indicator: 'A', + travel_agency_name: 'Checkout Travel Agents', + travel_agency_code: '91114362' + }, + passengers: [ + { + first_name: 'John', + last_name: 'Smith', + date_of_birth: '1990-10-31', + address: { + country: 'GB' + } + } + ], + flight_leg_details: [ + { + flight_number: 'BA1483', + carrier_code: 'BA', + class_of_travelling: 'W', + departure_airport: 'LHW', + departure_date: '2025-10-13', + departure_time: '18:30', + arrival_airport: 'JFK', + stop_over_code: 'X', + fare_basis_code: 'WUP14B' + } + ] + }, + accommodation_data: [ + { + name: 'Checkout Lodge', + booking_reference: 'REF9083748', + check_in_date: '2025-04-11', + check_out_date: '2025-04-18', + address: { + address_line_1: '123 High Street', + address_line_2: 'Flat 456', + city: 'London', + state: 'Greater London', + country: 'United Kingdom', + zip: 'SW1 1AA' + }, + number_of_rooms: 2, + guests: [ + { + first_name: 'Jia', + last_name: 'Tsang', + date_of_birth: '1970-03-19' + } + ], + room: [ + { + rate: 42.3, + number_of_nights: 5 + } + ] + } + ] + } + }; + + const SK = 'sk_test_xxx'; + const cko = new Checkout(SK); + + try { + const result = await cko.paymentSetups.updateAPaymentSetup(id, request); + } catch (error) { + err = error; + } + + // Assert + expect(err).to.be.instanceOf(Error); + }); + + }); + describe('UpdateAPaymentSetup response 401', () => { + it('should match response schema 3', async () => { + // Arrange + var err = null; + const response = {}; + + nock('https://api.sandbox.checkout.com') + .put('/payments/setups/pay_setup_123') + .reply(401 + ); + + // Act + const id = "pay_setup_123"; + const request = { + processing_channel_id: 'pc_q4dbxom5jbgudnjzjpz7j2z6uq', + amount: 10000, + currency: 'GBP', + payment_type: 'Regular', + reference: 'REF-0987-475', + description: 'Set of three t-shirts.', + payment_methods: { + klarna: { + initialization: 'disabled', + account_holder: { + billing_address: { + country: 'GB' + } + }, + payment_method_options: { + sdk: { + id: 'opt_drzstxerxrku3apsepshbslssu', + action: { + type: 'sdk', + client_token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.ewogICJzZXNzaW9uX2lkIiA6ICIw', + session_id: '0b1d9815-165e-42e2-8867-35bc03789e00' + } + } + } + }, + stcpay: { + initialization: 'disabled', + otp: '123456', + payment_method_options: { + pay_in_full: { + id: 'opt_drzstxerxrku3apsepshbslssu', + action: { + type: 'otp' + } + } + } + }, + tabby: { + initialization: 'disabled', + payment_method_options: { + installments: { + id: 'opt_drzstxerxrku3apsepshbslssu' + } + } + }, + bizum: { + initialization: 'disabled', + payment_method_options: { + pay_now: { + id: 'opt_drzstxerxrku3apsepshbslssu' + } + } + } + }, + settings: { + success_url: 'http://example.com/payments/success', + failure_url: 'http://example.com/payments/fail' + }, + customer: { + email: { + address: 'johnsmith@example.com', + verified: true + }, + name: 'John Smith', + phone: { + country_code: '44', + number: '207 946 0000' + }, + device: { + locale: 'en_GB' + }, + merchant_account: { + id: '1234', + registration_date: '2023-05-01', + last_modified: '2023-05-01', + returning_customer: true, + first_transaction_date: '2023-09-15', + last_transaction_date: '2025-03-28', + total_order_count: 6, + last_payment_amount: 55.99 + } + }, + order: { + items: [ + { + name: 'Battery Power Pack', + quantity: 1, + unit_price: 1000, + total_amount: 1000, + reference: 'BA67A', + discount_amount: 150, + url: 'http://shoppingsite.com/my-item', + image_url: 'http://shoppingsite.com/my-item/image', + type: 'digital' + } + ], + shipping: { + address: { + address_line_1: '10 Canterbury Road', + city: 'London', + zip: 'SW1 1AA' + }, + method: 'string' + }, + sub_merchants: [ + { + id: 'SUB12345', + product_category: 'Electronics', + number_of_trades: 500, + registration_date: '2023-01-15' + } + ], + discount_amount: 10 + }, + industry: { + airline_data: { + ticket: { + number: '0742464639523', + issue_date: '2025-05-01', + issuing_carrier_code: '042', + travel_package_indicator: 'A', + travel_agency_name: 'Checkout Travel Agents', + travel_agency_code: '91114362' + }, + passengers: [ + { + first_name: 'John', + last_name: 'Smith', + date_of_birth: '1990-10-31', + address: { + country: 'GB' + } + } + ], + flight_leg_details: [ + { + flight_number: 'BA1483', + carrier_code: 'BA', + class_of_travelling: 'W', + departure_airport: 'LHW', + departure_date: '2025-10-13', + departure_time: '18:30', + arrival_airport: 'JFK', + stop_over_code: 'X', + fare_basis_code: 'WUP14B' + } + ] + }, + accommodation_data: [ + { + name: 'Checkout Lodge', + booking_reference: 'REF9083748', + check_in_date: '2025-04-11', + check_out_date: '2025-04-18', + address: { + address_line_1: '123 High Street', + address_line_2: 'Flat 456', + city: 'London', + state: 'Greater London', + country: 'United Kingdom', + zip: 'SW1 1AA' + }, + number_of_rooms: 2, + guests: [ + { + first_name: 'Jia', + last_name: 'Tsang', + date_of_birth: '1970-03-19' + } + ], + room: [ + { + rate: 42.3, + number_of_nights: 5 + } + ] + } + ] + } + }; + + const SK = 'sk_test_xxx'; + const cko = new Checkout(SK); + + try { + const result = await cko.paymentSetups.updateAPaymentSetup(id, request); + } catch (error) { + err = error; + } + + // Assert + expect(err).to.be.instanceOf(AuthenticationError); + }); + + }); + describe('UpdateAPaymentSetup response 403', () => { + it('should match response schema 4', async () => { + // Arrange + var err = null; + const response = {}; + + nock('https://api.sandbox.checkout.com') + .put('/payments/setups/pay_setup_123') + .reply(403 + ); + + // Act + const id = "pay_setup_123"; + const request = { + processing_channel_id: 'pc_q4dbxom5jbgudnjzjpz7j2z6uq', + amount: 10000, + currency: 'GBP', + payment_type: 'Regular', + reference: 'REF-0987-475', + description: 'Set of three t-shirts.', + payment_methods: { + klarna: { + initialization: 'disabled', + account_holder: { + billing_address: { + country: 'GB' + } + }, + payment_method_options: { + sdk: { + id: 'opt_drzstxerxrku3apsepshbslssu', + action: { + type: 'sdk', + client_token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.ewogICJzZXNzaW9uX2lkIiA6ICIw', + session_id: '0b1d9815-165e-42e2-8867-35bc03789e00' + } + } + } + }, + stcpay: { + initialization: 'disabled', + otp: '123456', + payment_method_options: { + pay_in_full: { + id: 'opt_drzstxerxrku3apsepshbslssu', + action: { + type: 'otp' + } + } + } + }, + tabby: { + initialization: 'disabled', + payment_method_options: { + installments: { + id: 'opt_drzstxerxrku3apsepshbslssu' + } + } + }, + bizum: { + initialization: 'disabled', + payment_method_options: { + pay_now: { + id: 'opt_drzstxerxrku3apsepshbslssu' + } + } + } + }, + settings: { + success_url: 'http://example.com/payments/success', + failure_url: 'http://example.com/payments/fail' + }, + customer: { + email: { + address: 'johnsmith@example.com', + verified: true + }, + name: 'John Smith', + phone: { + country_code: '44', + number: '207 946 0000' + }, + device: { + locale: 'en_GB' + }, + merchant_account: { + id: '1234', + registration_date: '2023-05-01', + last_modified: '2023-05-01', + returning_customer: true, + first_transaction_date: '2023-09-15', + last_transaction_date: '2025-03-28', + total_order_count: 6, + last_payment_amount: 55.99 + } + }, + order: { + items: [ + { + name: 'Battery Power Pack', + quantity: 1, + unit_price: 1000, + total_amount: 1000, + reference: 'BA67A', + discount_amount: 150, + url: 'http://shoppingsite.com/my-item', + image_url: 'http://shoppingsite.com/my-item/image', + type: 'digital' + } + ], + shipping: { + address: { + address_line_1: '10 Canterbury Road', + city: 'London', + zip: 'SW1 1AA' + }, + method: 'string' + }, + sub_merchants: [ + { + id: 'SUB12345', + product_category: 'Electronics', + number_of_trades: 500, + registration_date: '2023-01-15' + } + ], + discount_amount: 10 + }, + industry: { + airline_data: { + ticket: { + number: '0742464639523', + issue_date: '2025-05-01', + issuing_carrier_code: '042', + travel_package_indicator: 'A', + travel_agency_name: 'Checkout Travel Agents', + travel_agency_code: '91114362' + }, + passengers: [ + { + first_name: 'John', + last_name: 'Smith', + date_of_birth: '1990-10-31', + address: { + country: 'GB' + } + } + ], + flight_leg_details: [ + { + flight_number: 'BA1483', + carrier_code: 'BA', + class_of_travelling: 'W', + departure_airport: 'LHW', + departure_date: '2025-10-13', + departure_time: '18:30', + arrival_airport: 'JFK', + stop_over_code: 'X', + fare_basis_code: 'WUP14B' + } + ] + }, + accommodation_data: [ + { + name: 'Checkout Lodge', + booking_reference: 'REF9083748', + check_in_date: '2025-04-11', + check_out_date: '2025-04-18', + address: { + address_line_1: '123 High Street', + address_line_2: 'Flat 456', + city: 'London', + state: 'Greater London', + country: 'United Kingdom', + zip: 'SW1 1AA' + }, + number_of_rooms: 2, + guests: [ + { + first_name: 'Jia', + last_name: 'Tsang', + date_of_birth: '1970-03-19' + } + ], + room: [ + { + rate: 42.3, + number_of_nights: 5 + } + ] + } + ] + } + }; + + const SK = 'sk_test_xxx'; + const cko = new Checkout(SK); + + try { + const result = await cko.paymentSetups.updateAPaymentSetup(id, request); + } catch (error) { + err = error; + } + + // Assert + expect(err).to.be.instanceOf(ActionNotAllowed); + }); + + }); + describe('UpdateAPaymentSetup response 422', () => { + it('should match response schema 5', async () => { + // Arrange + var err = null; + const response = { + request_id: '0HL80RJLS76I7', + error_type: 'request_invalid', + error_codes: [ + 'amount_required' + ] + }; + + nock('https://api.sandbox.checkout.com') + .put('/payments/setups/pay_setup_123') + .reply(422 + ); + + // Act + const id = "pay_setup_123"; + const request = { + processing_channel_id: 'pc_q4dbxom5jbgudnjzjpz7j2z6uq', + amount: 10000, + currency: 'GBP', + payment_type: 'Regular', + reference: 'REF-0987-475', + description: 'Set of three t-shirts.', + payment_methods: { + klarna: { + initialization: 'disabled', + account_holder: { + billing_address: { + country: 'GB' + } + }, + payment_method_options: { + sdk: { + id: 'opt_drzstxerxrku3apsepshbslssu', + action: { + type: 'sdk', + client_token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.ewogICJzZXNzaW9uX2lkIiA6ICIw', + session_id: '0b1d9815-165e-42e2-8867-35bc03789e00' + } + } + } + }, + stcpay: { + initialization: 'disabled', + otp: '123456', + payment_method_options: { + pay_in_full: { + id: 'opt_drzstxerxrku3apsepshbslssu', + action: { + type: 'otp' + } + } + } + }, + tabby: { + initialization: 'disabled', + payment_method_options: { + installments: { + id: 'opt_drzstxerxrku3apsepshbslssu' + } + } + }, + bizum: { + initialization: 'disabled', + payment_method_options: { + pay_now: { + id: 'opt_drzstxerxrku3apsepshbslssu' + } + } + } + }, + settings: { + success_url: 'http://example.com/payments/success', + failure_url: 'http://example.com/payments/fail' + }, + customer: { + email: { + address: 'johnsmith@example.com', + verified: true + }, + name: 'John Smith', + phone: { + country_code: '44', + number: '207 946 0000' + }, + device: { + locale: 'en_GB' + }, + merchant_account: { + id: '1234', + registration_date: '2023-05-01', + last_modified: '2023-05-01', + returning_customer: true, + first_transaction_date: '2023-09-15', + last_transaction_date: '2025-03-28', + total_order_count: 6, + last_payment_amount: 55.99 + } + }, + order: { + items: [ + { + name: 'Battery Power Pack', + quantity: 1, + unit_price: 1000, + total_amount: 1000, + reference: 'BA67A', + discount_amount: 150, + url: 'http://shoppingsite.com/my-item', + image_url: 'http://shoppingsite.com/my-item/image', + type: 'digital' + } + ], + shipping: { + address: { + address_line_1: '10 Canterbury Road', + city: 'London', + zip: 'SW1 1AA' + }, + method: 'string' + }, + sub_merchants: [ + { + id: 'SUB12345', + product_category: 'Electronics', + number_of_trades: 500, + registration_date: '2023-01-15' + } + ], + discount_amount: 10 + }, + industry: { + airline_data: { + ticket: { + number: '0742464639523', + issue_date: '2025-05-01', + issuing_carrier_code: '042', + travel_package_indicator: 'A', + travel_agency_name: 'Checkout Travel Agents', + travel_agency_code: '91114362' + }, + passengers: [ + { + first_name: 'John', + last_name: 'Smith', + date_of_birth: '1990-10-31', + address: { + country: 'GB' + } + } + ], + flight_leg_details: [ + { + flight_number: 'BA1483', + carrier_code: 'BA', + class_of_travelling: 'W', + departure_airport: 'LHW', + departure_date: '2025-10-13', + departure_time: '18:30', + arrival_airport: 'JFK', + stop_over_code: 'X', + fare_basis_code: 'WUP14B' + } + ] + }, + accommodation_data: [ + { + name: 'Checkout Lodge', + booking_reference: 'REF9083748', + check_in_date: '2025-04-11', + check_out_date: '2025-04-18', + address: { + address_line_1: '123 High Street', + address_line_2: 'Flat 456', + city: 'London', + state: 'Greater London', + country: 'United Kingdom', + zip: 'SW1 1AA' + }, + number_of_rooms: 2, + guests: [ + { + first_name: 'Jia', + last_name: 'Tsang', + date_of_birth: '1970-03-19' + } + ], + room: [ + { + rate: 42.3, + number_of_nights: 5 + } + ] + } + ] + } + }; + + const SK = 'sk_test_xxx'; + const cko = new Checkout(SK); + + try { + const result = await cko.paymentSetups.updateAPaymentSetup(id, request); + } catch (error) { + err = error; + } + + // Assert + expect(err).to.be.instanceOf(ValidationError); + }); + + }); +}); From 36cb88f1b42d2b678cd162106076558ce088a117 Mon Sep 17 00:00:00 2001 From: david ruiz Date: Mon, 15 Dec 2025 13:26:45 +0100 Subject: [PATCH 4/5] Payment Setups integration test --- test/payment-setups/payment-setups-it.js | 224 +++++++++++++++++++++++ 1 file changed, 224 insertions(+) create mode 100644 test/payment-setups/payment-setups-it.js diff --git a/test/payment-setups/payment-setups-it.js b/test/payment-setups/payment-setups-it.js new file mode 100644 index 0000000..06642df --- /dev/null +++ b/test/payment-setups/payment-setups-it.js @@ -0,0 +1,224 @@ +import { expect } from "chai"; +import nock from "nock"; +import Checkout from '../../src/Checkout.js' +import { NotFoundError, ValidationError, AuthenticationError, ActionNotAllowed } 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-Setups', () => { + const createRequest = { + processing_channel_id: processingChannelId, + amount: 1000, + currency: "GBP", + payment_type: "Regular", + reference: `TEST-REF-${Date.now()}`, + description: "Integration test payment setup", + settings: { + success_url: "https://example.com/success", + failure_url: "https://example.com/failure" + }, + customer: { + name: "John Smith", + email: { + address: `john.smith+${Date.now()}@example.com`, + verified: true + }, + phone: { + country_code: "+44", + number: "207 946 0000" + }, + device: { + locale: "en_GB" + } + }, + payment_methods: { + klarna: { + initialization: "disabled", + account_holder: { + billing_address: { + address_line1: "123 High Street", + city: "London", + zip: "SW1A 1AA", + country: "GB" + } + } + } + } + }; + + const updateRequest = { + processing_channel_id: processingChannelId, + amount: 1500, + currency: "GBP", + payment_type: "Regular", + reference: `TEST-REF-UPDATED-${Date.now()}`, + description: "Updated integration test payment setup", + settings: { + success_url: "https://example.com/success-updated", + failure_url: "https://example.com/failure-updated" + }, + customer: { + name: "John Smith Updated", + email: { + address: `john.smith.updated+${Date.now()}@example.com`, + verified: true + }, + phone: { + country_code: "+44", + number: "207 946 0001" + }, + device: { + locale: "en_US" + } + }, + payment_methods: { + klarna: { + initialization: "disabled", + account_holder: { + billing_address: { + address_line1: "456 Updated Street", + city: "Manchester", + zip: "M1 2AB", + country: "GB" + } + } + } + } + }; + + describe('Create and manage Payment Setup lifecycle', () => { + + it('should create a payment setup', async () => { + const response = await cko.paymentSetups.createAPaymentSetup(createRequest); + + expect(response.id).not.to.be.null; + expect(response.amount).to.equal(1000); + expect(response.currency).to.equal('GBP'); + expect(response.customer.email.address).to.include('john.smith+'); + }); + + it('should get a payment setup', async () => { + // First create a payment setup + const createResponse = await cko.paymentSetups.createAPaymentSetup(createRequest); + + // Then retrieve it + const response = await cko.paymentSetups.getAPaymentSetup(createResponse.id); + + expect(response.id).to.equal(createResponse.id); + expect(response.amount).to.equal(1000); + expect(response.currency).to.equal('GBP'); + expect(response.customer.email.address).to.include('john.smith+'); + }); + + it('should update a payment setup', async () => { + // First create a payment setup + const createResponse = await cko.paymentSetups.createAPaymentSetup(createRequest); + + // Then update it + const response = await cko.paymentSetups.updateAPaymentSetup(createResponse.id, updateRequest); + + expect(response.id).to.equal(createResponse.id); + expect(response.amount).to.equal(1500); + expect(response.customer.name).to.equal('John Smith Updated'); + }); + + it('should confirm a payment setup with a payment method option', async () => { + try { + // First create a payment setup + const createResponse = await cko.paymentSetups.createAPaymentSetup(createRequest); + + // Get the payment setup to find available payment method options + const getResponse = await cko.paymentSetups.getAPaymentSetup(createResponse.id); + + // Extract first available payment method option ID (if available) + const paymentMethodOptions = getResponse.payment_method_options || []; + if (paymentMethodOptions.length > 0) { + const paymentMethodOptionId = paymentMethodOptions[0].id; + + // Confirm the payment setup + const response = await cko.paymentSetups.confirmAPaymentSetup(createResponse.id, paymentMethodOptionId); + + expect(response.id).not.to.be.null; + expect(response.status).not.to.be.null; + expect(response.approved).to.be.a('boolean'); + } else { + // If no payment method options available, skip this test + console.log('Skipping confirm test - no payment method options available'); + } + } catch (error) { + // Payment setups might not be fully configured in test environment + // so we expect certain validation errors + expect(error).to.be.instanceOf(ValidationError); + } + }); + }); + + describe('Error handling', () => { + it('should throw NotFoundError when getting non-existent payment setup', async () => { + try { + await cko.paymentSetups.getAPaymentSetup('psu_not_found'); + } catch (err) { + expect(err).to.be.instanceOf(NotFoundError); + } + }); + + it('should throw NotFoundError when updating non-existent payment setup', async () => { + const updateRequest = { + amount: 1500, + currency: "GBP" + }; + + try { + await cko.paymentSetups.updateAPaymentSetup('psu_not_found', updateRequest); + } catch (err) { + expect(err).to.be.instanceOf(NotFoundError); + } + }); + + it('should throw NotFoundError when confirming non-existent payment setup', async () => { + try { + await cko.paymentSetups.confirmAPaymentSetup('psu_not_found', 'pmo_not_found'); + } catch (err) { + expect(err).to.be.instanceOf(NotFoundError); + } + }); + + it('should throw ValidationError when creating payment setup with invalid data', async () => { + const invalidRequest = { + // Missing required fields + amount: 'invalid_amount', + currency: 'INVALID' + }; + + try { + await cko.paymentSetups.createAPaymentSetup(invalidRequest); + } catch (err) { + expect(err.name).to.equal('ValueError'); + expect(err.body).to.equal('The currency value is not valid.'); + } + }); + + it('should throw ValidationError when updating payment setup with invalid data', async () => { + // First create a valid payment setup + const createResponse = await cko.paymentSetups.createAPaymentSetup(createRequest); + + const invalidUpdateRequest = { + amount: 'invalid_amount', + currency: 'INVALID' + }; + + try { + await cko.paymentSetups.updateAPaymentSetup(createResponse.id, invalidUpdateRequest); + } catch (err) { + expect(err.name).to.equal('ValueError'); + expect(err.body).to.equal('The currency value is not valid.'); + } + }); + }); +}); \ No newline at end of file From 81f08a2aa31ada2bcdd0ff666611c2972c508b49 Mon Sep 17 00:00:00 2001 From: david-ruiz-cko Date: Wed, 17 Dec 2025 10:14:13 +0100 Subject: [PATCH 5/5] Fixing hardcoded files upload purpose bug, updated the tests with some to extend the checks (#420) --- src/api/files/files.js | 5 ++- test/files/files.js | 74 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) diff --git a/src/api/files/files.js b/src/api/files/files.js index f9a3794..12dceaa 100644 --- a/src/api/files/files.js +++ b/src/api/files/files.js @@ -20,6 +20,9 @@ export default class Files { * * @memberof Files * @param {Object} body Files request body. + * @param {string|ReadStream} body.file File URL (for remote files) or file path/ReadStream (for local files). + * @param {string|ReadStream} [body.path] Alternative property name for local file path/ReadStream. + * @param {string} body.purpose Purpose of the file upload. Valid values: 'dispute_evidence', 'additional_document', 'bank_verification', 'identity_verification'. * @return {Promise} A promise to the files response. */ async upload(body) { @@ -36,7 +39,7 @@ export default class Files { // use the local file form.append('file', body.file || body.path); } - form.append('purpose', 'dispute_evidence'); + form.append('purpose', body.purpose); const response = await post( this.config.httpClient, diff --git a/test/files/files.js b/test/files/files.js index 77d3f60..63bfb59 100644 --- a/test/files/files.js +++ b/test/files/files.js @@ -122,6 +122,80 @@ describe('Files', () => { expect(getFile.id).to.equal(file.id); }).timeout(120000); + it('should upload file with different purpose values', async () => { + const purposes = ['additional_document', 'bank_verification', 'identity_verification']; + + for (const purpose of purposes) { + // Simple mock without body inspection since FormData can't be easily inspected + nock('https://api.sandbox.checkout.com') + .post('/files') + .reply(200, { + id: 'file_test_' + purpose, + _links: { + self: { + href: `https://api.sandbox.checkout.com/files/file_test_${purpose}`, + }, + }, + }); + + const cko = new Checkout(SK); + + const file = await cko.files.upload({ + path: fs.createReadStream('./test/files/evidence.jpg'), + purpose: purpose, + }); + + expect(file.id).to.equal('file_test_' + purpose); + } + }).timeout(120000); + + it('should include purpose parameter in request body', async () => { + let capturedRequest = null; + + // Mock to capture the request details + nock('https://api.sandbox.checkout.com') + .post('/files') + .reply(function() { + // Capture the request body for verification + capturedRequest = this.req; + return [200, { + id: 'file_test_purpose_check', + _links: { + self: { + href: 'https://api.sandbox.checkout.com/files/file_test_purpose_check', + }, + }, + }]; + }); + + const cko = new Checkout(SK); + + await cko.files.upload({ + path: fs.createReadStream('./test/files/evidence.jpg'), + purpose: 'identity_verification', + }); + + // Verify the request was made + expect(capturedRequest).to.not.be.null; + + // Note: We can't easily inspect FormData content in tests, but we've verified + // the code path includes the purpose parameter in the upload implementation + }).timeout(120000); + + it('should throw ValidationError when purpose is missing', async () => { + const cko = new Checkout(SK); + + try { + const file = await cko.files.upload({ + path: fs.createReadStream('./test/files/evidence.jpg'), + // missing purpose parameter + }); + } catch (err) { + // Should throw an error when purpose is missing + expect(err).to.exist; + } + }); + it('should throw Authentication error', async () => { nock('https://api.sandbox.checkout.com') .get('/files/file_zna32sccqbwevd3ldmejtplbhu')