From b1c231f5ffa52e54966fcc3c5609ff43c01a003a Mon Sep 17 00:00:00 2001 From: Jeffrey Date: Fri, 8 Sep 2023 09:37:26 +0200 Subject: [PATCH] Add terminal API --- examples/terminals/get.ts | 16 +++++++ examples/terminals/list.ts | 16 +++++++ src/binders/payments/parameters.ts | 1 + src/binders/terminals/TerminalsBinder.ts | 55 +++++++++++++++++++++ src/binders/terminals/parameters.ts | 8 ++++ src/createMollieClient.ts | 9 +++- src/data/terminals/Terminal.ts | 61 ++++++++++++++++++++++++ tests/integration/terminals.test.ts | 27 +++++++++++ 8 files changed, 192 insertions(+), 1 deletion(-) create mode 100644 examples/terminals/get.ts create mode 100644 examples/terminals/list.ts create mode 100644 src/binders/terminals/TerminalsBinder.ts create mode 100644 src/binders/terminals/parameters.ts create mode 100644 src/data/terminals/Terminal.ts create mode 100644 tests/integration/terminals.test.ts diff --git a/examples/terminals/get.ts b/examples/terminals/get.ts new file mode 100644 index 00000000..9236e8bc --- /dev/null +++ b/examples/terminals/get.ts @@ -0,0 +1,16 @@ +/** + * @docs https://docs.mollie.com/reference/v2/terminals-api/get-terminal + */ +import createMollieClient, { Terminal } from '@mollie/api-client'; + +const mollieClient = createMollieClient({ apiKey: 'test_dHar4XY7LxsDOtmnkVtjNVWXLSlXsM' }); + +(async () => { + try { + const terminal: Terminal = await mollieClient.terminals.get('term_7MgL4wea46qkRcoTZjWEH'); + + console.log(terminal); + } catch (error) { + console.warn(error); + } +})(); diff --git a/examples/terminals/list.ts b/examples/terminals/list.ts new file mode 100644 index 00000000..0b2c049c --- /dev/null +++ b/examples/terminals/list.ts @@ -0,0 +1,16 @@ +/** + * @docs https://docs.mollie.com/reference/v2/terminals-api/list-terminals + */ +import createMollieClient, { List, Terminal } from '@mollie/api-client'; + +const mollieClient = createMollieClient({ apiKey: 'test_dHar4XY7LxsDOtmnkVtjNVWXLSlXsM' }); + +(async () => { + try { + const terminals: List = await mollieClient.terminals.page(); + + console.log(terminals); + } catch (error) { + console.warn(terminals); + } +})(); diff --git a/src/binders/payments/parameters.ts b/src/binders/payments/parameters.ts index 09599c13..06455624 100644 --- a/src/binders/payments/parameters.ts +++ b/src/binders/payments/parameters.ts @@ -74,6 +74,7 @@ export type CreateParameters = Pick { + constructor(protected readonly networkClient: TransformingNetworkClient) { + super(); + } + + /** + * Retrieve a single terminal object by its terminal ID. This terminal object symbolizes the physical device that you have received from us. + * + * For more information on accepting point-of-sale payments, please refer to the [point-of-sale guide](https://docs.mollie.com/point-of-sale/overview). + * + * @see https://docs.mollie.com/reference/v2/terminals-api/get-terminal + */ + public get(id: string): Promise; + public get(id: string, callback: Callback): void; + public get(id: string) { + if (renege(this, this.get, ...arguments)) return; + return this.networkClient.get(`${pathSegment}/${id}`); + } + + /** + * Retrieve a list of all of your terminals. + * + * The results are paginated. See pagination for more information. + * + * @see https://docs.mollie.com/reference/v2/terminals-api/list-terminals + */ + public page(parameters?: PageParameters): Promise>; + public page(parameters: PageParameters, callback: Callback>): void; + public page(parameters: PageParameters = {}) { + if (renege(this, this.page, ...arguments)) return; + return this.networkClient.page(pathSegment, 'terminals', parameters).then(result => this.injectPaginationHelpers(result, this.page, parameters)); + } + + /** + * Retrieve a list of all of your terminals. + * + * The results are paginated. See pagination for more information. + * + * @see https://docs.mollie.com/reference/v2/terminals-api/list-terminals + */ + public iterate(parameters?: IterateParameters) { + const { valuesPerMinute, ...query } = parameters ?? {}; + return this.networkClient.iterate(pathSegment, 'terminals', query, valuesPerMinute); + } +} diff --git a/src/binders/terminals/parameters.ts b/src/binders/terminals/parameters.ts new file mode 100644 index 00000000..a37a2764 --- /dev/null +++ b/src/binders/terminals/parameters.ts @@ -0,0 +1,8 @@ +import {PaginationParameters, ThrottlingParameter} from "../../types/parameters"; + +export type PageParameters = PaginationParameters & { + profileId?: string; + testmode?: boolean; +}; + +export type IterateParameters = Omit & ThrottlingParameter; diff --git a/src/createMollieClient.ts b/src/createMollieClient.ts index 9e83a2ce..2a9c81bc 100644 --- a/src/createMollieClient.ts +++ b/src/createMollieClient.ts @@ -23,6 +23,7 @@ import { transform as transformOnboarding } from './data/onboarding/Onboarding'; import { transform as transformPaymentLink } from './data/paymentLinks/PaymentLink'; import { transform as transformIssuer } from './data/issuer/IssuerModel'; import { transform as transformSettlement } from './data/settlements/SettlementModel'; +import { transform as transformTerminal } from './data/terminals/Terminal'; // Binders import ApplePayBinder from './binders/applePay/ApplePayBinder'; @@ -57,6 +58,7 @@ import SettlementChargebacksBinder from './binders/settlements/chargebacks/Settl import SettlementsBinder from './binders/settlements/SettlementsBinder'; import SubscriptionsBinder from './binders/subscriptions/SubscriptionsBinder'; import SubscriptionPaymentsBinder from './binders/subscriptions/payments/SubscriptionPaymentsBinder'; +import TerminalsBinder from "./binders/terminals/TerminalsBinder"; /** * Create Mollie client. @@ -95,7 +97,8 @@ export default function createMollieClient(options: Options) { .add('onboarding', transformOnboarding) .add('payment-link', transformPaymentLink) .add('issuer', transformIssuer) - .add('settlement', transformSettlement), + .add('settlement', transformSettlement) + .add('terminal', transformTerminal), ); return { @@ -164,6 +167,9 @@ export default function createMollieClient(options: Options) { settlementCaptures: new SettlementCapturesBinder(transformingNetworkClient), settlementRefunds: new SettlementRefundsBinder(transformingNetworkClient), settlementChargebacks: new SettlementChargebacksBinder(transformingNetworkClient), + + // Terminals + terminals: new TerminalsBinder(transformingNetworkClient) }; } @@ -180,4 +186,5 @@ export { RefundEmbed, RefundStatus } from './data/refunds/data'; export { SubscriptionStatus } from './data/subscriptions/data'; export { ProfileStatus } from './data/profiles/data'; export { OnboardingStatus } from './data/onboarding/data'; +export { TerminalStatus } from './data/terminals/Terminal'; export { default as MollieApiError } from './errors/ApiError'; diff --git a/src/data/terminals/Terminal.ts b/src/data/terminals/Terminal.ts new file mode 100644 index 00000000..cdc35f4f --- /dev/null +++ b/src/data/terminals/Terminal.ts @@ -0,0 +1,61 @@ +import type TransformingNetworkClient from '../../communication/TransformingNetworkClient'; +import type Seal from '../../types/Seal'; +import Helper from "../Helper"; +import type Model from '../Model'; +import {Links} from "../global"; + +export interface TerminalData extends Model<'terminal'> { + /** + * The identifier used for referring to the profile the terminal was created on. For example, pfl_QkEhN94Ba. + */ + profileId: string + /** + * The terminal's status. Refer to the documentation regarding statuses for more info about which statuses occur at what point. + * + * @see https://docs.mollie.com/reference/v2/terminals-api/get-terminal#response + */ + status: TerminalStatus + /** + * The brand of the terminal. For example, ‘PAX’. + */ + brand: string + /** + * The model of the terminal. For example for a PAX A920, this field’s value will be ‘A920’. + */ + model: string + /** + * The serial number of the terminal. The serial number is provided at terminal creation time. + */ + serialNumber: string + /** + * The currency which is set for the terminal, in [ISO 4217](https://en.wikipedia.org/wiki/ISO_4217) format. Please take into consideration that currently our terminals are bound to a specific currency, chosen during setup. + */ + currency?: string + /** + * A short description of the terminal. The description can be used as an identifier for the terminal. Currently, the description is set when the terminal is initially configured. It will be visible in the dashboard as well as on the device itself. + */ + description: string + /** + * The date and time the terminal was created, in [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) format. + */ + createdAt: string + /** + * The date and time the terminal was last updated, in [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) format. + */ + updatedAt: string + _links: Links +} + +export enum TerminalStatus { + pending = 'pending', + active = 'active', + inactive = 'inactive' +} + +type Terminal = Seal>; + +export default Terminal; + +export function transform(networkClient: TransformingNetworkClient, input: TerminalData): Terminal { + return Object.assign(Object.create(new Helper(networkClient, input._links)), input); +} diff --git a/tests/integration/terminals.test.ts b/tests/integration/terminals.test.ts new file mode 100644 index 00000000..9f3e5a8d --- /dev/null +++ b/tests/integration/terminals.test.ts @@ -0,0 +1,27 @@ +import axios from 'axios'; +import httpAdapter from 'axios/lib/adapters/http'; +import dotenv from 'dotenv'; +import createMollieClient from '../..'; + +/** + * Overwrite the default XMLHttpRequestAdapter + */ +axios.defaults.adapter = httpAdapter; + +/** + * Load the API_KEY environment variable + */ +dotenv.config(); + +const mollieClient = createMollieClient({ apiKey: process.env.API_KEY }); + +describe('terminals', () => { + it('should integrate', async () => { + const terminals = await mollieClient.terminals.page(); + + expect(terminals).toBeDefined(); + + const terminal = await mollieClient.terminals.get(terminals[0].id); + expect(terminal).toBeDefined(); + }); +});