@apicart/payments-sdk
Version:
Apicart SDK for payment gateways integration
109 lines (90 loc) • 3.08 kB
text/typescript
import Apicart from '@apicart/core-sdk';
import Api from './DataSource/Api';
import PaymentGatewayDao from './Dao/PaymentGateway/PaymentGatewayDao';
import { PaymentGatewayDaoInterface } from './Contract/Dao/PaymentGateway/PaymentGatewayDaoInterface';
import PaymentGatewaysPageDao from './Dao/PaymentGatewaysPage/PaymentGatewaysPageDao';
import { PaymentGatewaysPageDaoInterface } from './Contract/Dao/PaymentGatewaysPage/PaymentGatewaysPageDaoInterface';
import { DataSourceInterface } from './Contract/DataSource/DataSourceInterface';
export default class Payments
{
private _dataSource: DataSourceInterface = null;
private _config: any = {
token: '',
environment: 'sandbox',
dataSource: {
class: Api,
parameters: {
connectionTimeout: 5000,
url: 'https://payment-api.apicart.' + (Apicart.Configurator.isDevEnv() ? 'dev' : 'net'),
withCredentials: false
}
}
}
constructor(config: Record<string, any>)
{
this._config = this.parseConfig(config);
}
public async pay(
gateway: string,
payment: string,
orderId: string,
parameters: Record<string, any>
): Promise<any> {
return this.getDataSource().pay(gateway, payment, orderId, parameters);
}
public async findGateway(code: string): Promise<PaymentGatewayDaoInterface | null>
{
const gatewayDao = new PaymentGatewayDao(this.getDataSource(), code);
await gatewayDao.getEntity();
return gatewayDao;
}
public async getGatewaysPage(
page: number,
size = 10,
filter: string = null
): Promise<PaymentGatewaysPageDaoInterface | null> {
const paymentGatewaysPageDao = new PaymentGatewaysPageDao(this.getDataSource(), page, size, filter);
await paymentGatewaysPageDao.getEntity();
return paymentGatewaysPageDao;
}
public gatewayAction(gateway: string, action: string, parameters: Record<string, any> = {}): Promise<any>
{
return this.getDataSource().gatewayAction(gateway, action, parameters);
}
public getToken(): string
{
return this._config.token;
}
private getDataSource(): DataSourceInterface
{
if (this._dataSource === null) {
const DataSourceClass = this._config.dataSource.class;
this._dataSource = new DataSourceClass(
this._config.token,
this._config.environment,
this._config.dataSource.parameters
);
}
return this._dataSource;
}
private parseConfig(config: Record<string, any>): Record<string, any>
{
const newConfig = Apicart.Utils.Objects.merge(this._config, config);
if (!Apicart.Utils.Objects.keyExists(newConfig, 'token')
|| Apicart.Utils.Validators.isEmpty(newConfig.token)
) {
throw 'Missing token';
}
if (!Apicart.Utils.Objects.keyExists(newConfig, 'dataSource')
|| !Apicart.Utils.Objects.keyExists(newConfig, 'dataSource.class')
|| Apicart.Utils.Validators.isEmpty(newConfig.dataSource.class)
|| Apicart.Utils.Validators.isEmpty(newConfig.dataSource.class)
) {
throw 'Parameter "dataSource" is not valid.';
}
if (!(/^[0-9a-zA-Z_!?]{64}$/i).test(newConfig.token)) {
throw 'Parameter "token" must contains 64 characters.';
}
return newConfig;
}
}