Skip to content

Commit

Permalink
Add terminal API
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeffrey committed Sep 8, 2023
1 parent 7dec190 commit b1c231f
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 1 deletion.
16 changes: 16 additions & 0 deletions examples/terminals/get.ts
Original file line number Diff line number Diff line change
@@ -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);
}
})();
16 changes: 16 additions & 0 deletions examples/terminals/list.ts
Original file line number Diff line number Diff line change
@@ -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<Terminal> = await mollieClient.terminals.page();

console.log(terminals);
} catch (error) {
console.warn(terminals);
}
})();
1 change: 1 addition & 0 deletions src/binders/payments/parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export type CreateParameters = Pick<PaymentData, 'amount' | 'description' | 'red
familyName?: string;
};
issuer?: Issuer;
terminalId?: string
/**
* The card number on the gift card. You can supply this to prefill the card number.
*
Expand Down
55 changes: 55 additions & 0 deletions src/binders/terminals/TerminalsBinder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import type TransformingNetworkClient from '../../communication/TransformingNetworkClient';
import renege from '../../plumbing/renege';
import type Callback from '../../types/Callback';
import Binder from '../Binder';
import { type PageParameters, IterateParameters } from './parameters';
import {Page} from "../../types";
import Terminal, {TerminalData} from "../../data/terminals/Terminal";

const pathSegment = 'terminals';

export default class TerminalsBinder extends Binder<TerminalData, Terminal> {
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<Terminal>;
public get(id: string, callback: Callback<Terminal>): 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<Page<Terminal>>;
public page(parameters: PageParameters, callback: Callback<Page<Terminal>>): void;
public page(parameters: PageParameters = {}) {
if (renege(this, this.page, ...arguments)) return;
return this.networkClient.page<TerminalData, Terminal>(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<TerminalData, Terminal>(pathSegment, 'terminals', query, valuesPerMinute);
}
}
8 changes: 8 additions & 0 deletions src/binders/terminals/parameters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {PaginationParameters, ThrottlingParameter} from "../../types/parameters";

export type PageParameters = PaginationParameters & {
profileId?: string;
testmode?: boolean;
};

export type IterateParameters = Omit<PageParameters, 'limit'> & ThrottlingParameter;
9 changes: 8 additions & 1 deletion src/createMollieClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
};
}

Expand All @@ -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';
61 changes: 61 additions & 0 deletions src/data/terminals/Terminal.ts
Original file line number Diff line number Diff line change
@@ -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<TerminalData, Helper<TerminalData, Terminal>>;

export default Terminal;

export function transform(networkClient: TransformingNetworkClient, input: TerminalData): Terminal {
return Object.assign(Object.create(new Helper(networkClient, input._links)), input);
}
27 changes: 27 additions & 0 deletions tests/integration/terminals.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});

0 comments on commit b1c231f

Please sign in to comment.