@apicart/payments-sdk
Version:
Apicart SDK for payment gateways integration
161 lines (137 loc) • 3.48 kB
text/typescript
import Apicart from '@apicart/core-sdk';
import PaymentGateway from '../Entity/PaymentGateway/PaymentGateway';
import PaymentGatewaysPage from '../Entity/PaymentGatewaysPage/PaymentGatewaysPage';
import { DataSourceInterface } from '../Contract/DataSource/DataSourceInterface';
export default class Api implements DataSourceInterface
{
private _token: string;
private _environment: string;
private _parameters: any = {};
constructor(token: string, environment: string, parameters: Record<string, any> = {})
{
if (!Apicart.Utils.Objects.keyExists(parameters, 'url')) {
throw 'Parameter "dataSource.url" is missing.';
}
this._token = token;
this._environment = environment;
this._parameters = parameters;
}
/** @inheritdoc */
public async gatewaysPage(
page: number,
size = 10,
filter: string | null = null
): Promise<PaymentGatewaysPage | null> {
const query = `
query gatewaysPage($input: GatewaysPageQueryInput!) {
gatewaysPage(input: $input) {
filteredTotal
total
gateways {
id
name
code
enabled
}
hasMore
}
}`
;
const response = await this.call(query, {
input: {
page: page,
size: size,
filter: filter
}
});
const responseData = this.getResponseData(response, 'gatewaysPage');
return responseData ? PaymentGatewaysPage.deserialize(responseData) : null;
}
/** @inheritdoc */
public async findGateway(code: string): Promise<PaymentGateway | null>
{
const query = `
query findGateway($input: FindGatewayQueryInput!) {
findGateway(input: $input) {
gateway {
id
name
code
enabled
}
}
}`
;
const response = await this.call(query, {
input: {
gateway: code
}
});
const responseData = this.getResponseData(response, 'findGateway.gateway');
return responseData ? PaymentGateway.deserialize(responseData) : null;
}
/** @inheritdoc */
public async gatewayAction(gateway: string, action: string, parameters: Record<string, any> = {}): Promise<any>
{
const query = `
mutation gatewayAction($input: GatewayActionMutationInput!) {
gatewayAction(input: $input) {
result {
status
description
data
}
}
}`
;
const response = await this.call(query, {
input: {
environment: this._environment,
gateway: gateway,
action: action,
parameters: parameters
}
});
return this.getResponseData(response, 'gatewayAction.result');
}
/** @inheritdoc */
public async pay(gateway: string, payment: string, orderId: string, parameters: Record<string, any>): Promise<any>
{
const query = `
mutation pay($input: PayMutationInput!) {
pay(input: $input) {
result {
status
description
data
}
}
}`
;
const response = await this.call(query, {
input: {
environment: this._environment,
gateway: gateway,
payment: payment,
orderId: orderId,
parameters: parameters
}
});
return this.getResponseData(response, 'pay.result');
}
private call(query: string, variables: any): Promise<any>
{
variables.input.token = this._token;
return Apicart.ApiCommunicator.call(
this._parameters.url,
query,
variables,
this._parameters.connectTimeout,
this._parameters.withCredentials
);
}
private getResponseData(response: any|null, keyPath: string): any|null
{
return Apicart.Utils.Objects.find(response, 'data.data.' + keyPath);
}
}