UNPKG

@apicart/payments-sdk

Version:

Apicart SDK for payment gateways integration

506 lines (492 loc) 21.7 kB
/** * Utils.js v1.0.0-alpha7 * (c) 2018-2020 Apicart Company * Released under the MIT License. */ import Apicart from '@apicart/core-sdk'; /*! ***************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ function __awaiter(thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); } function __generator(thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } } var PaymentGateway = (function () { function PaymentGateway(code, enabled, id, name) { this._code = code; this._enabled = enabled; this._id = id; this._name = name; } PaymentGateway.prototype.getCode = function () { return this._code; }; PaymentGateway.prototype.setCode = function (code) { this._code = code; }; PaymentGateway.prototype.isEnabled = function () { return this._enabled; }; PaymentGateway.prototype.setEnabled = function (enabled) { this._enabled = enabled; }; PaymentGateway.prototype.getId = function () { return this._id; }; PaymentGateway.prototype.setId = function (id) { this._id = id; }; PaymentGateway.prototype.getName = function () { return this._name; }; PaymentGateway.prototype.setName = function (name) { this._name = name; }; PaymentGateway.deserialize = function (data) { return new PaymentGateway(data.code, data.enabled, data.id, data.name); }; return PaymentGateway; }()); var PaymentGatewaysPage = (function () { function PaymentGatewaysPage(filteredTotal, hasMore, total, gateways) { if (gateways === void 0) { gateways = []; } this._gateways = []; this._filteredTotal = filteredTotal; this._hasMore = hasMore; this._total = total; this._gateways = gateways; } PaymentGatewaysPage.prototype.getFilteredTotal = function () { return this._filteredTotal; }; PaymentGatewaysPage.prototype.setFilteredTotal = function (filteredTotal) { this._filteredTotal = filteredTotal; }; PaymentGatewaysPage.prototype.getGateways = function () { return this._gateways; }; PaymentGatewaysPage.prototype.hasMore = function () { return this._hasMore; }; PaymentGatewaysPage.prototype.addGateway = function (paymentGateway) { this._gateways.push(paymentGateway); }; PaymentGatewaysPage.prototype.getTotal = function () { return this._total; }; PaymentGatewaysPage.prototype.setTotal = function (total) { this._total = total; }; PaymentGatewaysPage.deserialize = function (data) { var paymentGatewaysPage = new PaymentGatewaysPage(data.filteredTotal, data.hasMore, data.total); Apicart.Utils.Loops.forEach(data.gateways, function (paymentGateway) { paymentGatewaysPage.addGateway(PaymentGateway.deserialize(paymentGateway)); }); return paymentGatewaysPage; }; return PaymentGatewaysPage; }()); var Api = (function () { function Api(token, environment, parameters) { if (parameters === void 0) { parameters = {}; } this._parameters = {}; if (!Apicart.Utils.Objects.keyExists(parameters, 'url')) { throw 'Parameter "dataSource.url" is missing.'; } this._token = token; this._environment = environment; this._parameters = parameters; } Api.prototype.gatewaysPage = function (page, size, filter) { if (size === void 0) { size = 10; } if (filter === void 0) { filter = null; } return __awaiter(this, void 0, void 0, function () { var query, response, responseData; return __generator(this, function (_a) { switch (_a.label) { case 0: query = "\n\t\t\tquery gatewaysPage($input: GatewaysPageQueryInput!) {\n\t\t\t\tgatewaysPage(input: $input) {\n\t\t\t\t\tfilteredTotal\n\t\t\t\t\ttotal\n\t\t\t\t\tgateways {\n\t\t\t\t\t\tid\n\t\t\t\t\t\tname\n\t\t\t\t\t\tcode\n\t\t\t\t\t\tenabled\n\t\t\t\t\t}\n\t\t\t\t\thasMore\n\t\t\t\t}\n\t\t\t}"; return [4, this.call(query, { input: { page: page, size: size, filter: filter } })]; case 1: response = _a.sent(); responseData = this.getResponseData(response, 'gatewaysPage'); return [2, responseData ? PaymentGatewaysPage.deserialize(responseData) : null]; } }); }); }; Api.prototype.findGateway = function (code) { return __awaiter(this, void 0, void 0, function () { var query, response, responseData; return __generator(this, function (_a) { switch (_a.label) { case 0: query = "\n\t\t\tquery findGateway($input: FindGatewayQueryInput!) {\n\t\t\t\tfindGateway(input: $input) {\n\t\t\t\t\tgateway {\n\t\t\t\t\t\tid\n\t\t\t\t\t\tname\n\t\t\t\t\t\tcode\n\t\t\t\t\t\tenabled\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}"; return [4, this.call(query, { input: { gateway: code } })]; case 1: response = _a.sent(); responseData = this.getResponseData(response, 'findGateway.gateway'); return [2, responseData ? PaymentGateway.deserialize(responseData) : null]; } }); }); }; Api.prototype.gatewayAction = function (gateway, action, parameters) { if (parameters === void 0) { parameters = {}; } return __awaiter(this, void 0, void 0, function () { var query, response; return __generator(this, function (_a) { switch (_a.label) { case 0: query = "\n\t\t\tmutation gatewayAction($input: GatewayActionMutationInput!) {\n\t\t\t\tgatewayAction(input: $input) {\n\t\t\t\t\tresult {\n\t\t\t\t\t\tstatus\n\t\t\t\t\t\tdescription\n\t\t\t\t\t\tdata\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}"; return [4, this.call(query, { input: { environment: this._environment, gateway: gateway, action: action, parameters: parameters } })]; case 1: response = _a.sent(); return [2, this.getResponseData(response, 'gatewayAction.result')]; } }); }); }; Api.prototype.pay = function (gateway, payment, orderId, parameters) { return __awaiter(this, void 0, void 0, function () { var query, response; return __generator(this, function (_a) { switch (_a.label) { case 0: query = "\n\t\t\tmutation pay($input: PayMutationInput!) {\n\t\t\t\tpay(input: $input) {\n\t\t\t\t\tresult {\n\t\t\t\t\t\tstatus\n\t\t\t\t\t\tdescription\n\t\t\t\t\t\tdata\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}"; return [4, this.call(query, { input: { environment: this._environment, gateway: gateway, payment: payment, orderId: orderId, parameters: parameters } })]; case 1: response = _a.sent(); return [2, this.getResponseData(response, 'pay.result')]; } }); }); }; Api.prototype.call = function (query, variables) { variables.input.token = this._token; return Apicart.ApiCommunicator.call(this._parameters.url, query, variables, this._parameters.connectTimeout, this._parameters.withCredentials); }; Api.prototype.getResponseData = function (response, keyPath) { return Apicart.Utils.Objects.find(response, 'data.data.' + keyPath); }; return Api; }()); var PaymentGatewayDao = (function () { function PaymentGatewayDao(dataSource, name) { this._paymentGateway = null; this._dataSource = dataSource; this._name = name; } PaymentGatewayDao.prototype.getEntity = function () { return __awaiter(this, void 0, void 0, function () { var paymentGateway; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!(this._paymentGateway === null)) return [3, 2]; return [4, this._dataSource.findGateway(this._name)]; case 1: paymentGateway = _a.sent(); if (paymentGateway === null) { throw 'Entity with name "' + this._name + '" not found.'; } this._paymentGateway = paymentGateway; _a.label = 2; case 2: return [2, this._paymentGateway]; } }); }); }; PaymentGatewayDao.prototype.getCode = function () { return __awaiter(this, void 0, void 0, function () { var entity; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4, this.getEntity()]; case 1: entity = _a.sent(); return [2, entity ? entity.getCode() : null]; } }); }); }; PaymentGatewayDao.prototype.isEnabled = function () { return __awaiter(this, void 0, void 0, function () { var entity; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4, this.getEntity()]; case 1: entity = _a.sent(); return [2, entity ? entity.isEnabled() : null]; } }); }); }; PaymentGatewayDao.prototype.getId = function () { return __awaiter(this, void 0, void 0, function () { var entity; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4, this.getEntity()]; case 1: entity = _a.sent(); return [2, entity ? entity.getId() : null]; } }); }); }; PaymentGatewayDao.prototype.getName = function () { return __awaiter(this, void 0, void 0, function () { var entity; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4, this.getEntity()]; case 1: entity = _a.sent(); return [2, entity ? entity.getName() : null]; } }); }); }; return PaymentGatewayDao; }()); var PaymentGatewaysPageDao = (function () { function PaymentGatewaysPageDao(dataSource, page, size, filter) { if (size === void 0) { size = 10; } if (filter === void 0) { filter = null; } this._size = 10; this._filter = null; this._paymentGatewaysPage = null; this._dataSource = dataSource; this._page = page; this._size = size; this._filter = filter; } PaymentGatewaysPageDao.prototype.getEntity = function () { return __awaiter(this, void 0, void 0, function () { var paymentGatewaysPage; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!(this._paymentGatewaysPage === null)) return [3, 2]; return [4, this._dataSource.gatewaysPage(this._page, this._size, this._filter)]; case 1: paymentGatewaysPage = _a.sent(); this._paymentGatewaysPage = paymentGatewaysPage; _a.label = 2; case 2: return [2, this._paymentGatewaysPage]; } }); }); }; PaymentGatewaysPageDao.prototype.getFilteredTotal = function () { return __awaiter(this, void 0, void 0, function () { var entity; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4, this.getEntity()]; case 1: entity = _a.sent(); return [2, entity ? entity.getFilteredTotal() : null]; } }); }); }; PaymentGatewaysPageDao.prototype.getGateways = function () { return __awaiter(this, void 0, void 0, function () { var entity; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4, this.getEntity()]; case 1: entity = _a.sent(); return [2, entity ? entity.getGateways() : null]; } }); }); }; PaymentGatewaysPageDao.prototype.hasMore = function () { return __awaiter(this, void 0, void 0, function () { var entity; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4, this.getEntity()]; case 1: entity = _a.sent(); return [2, entity ? entity.hasMore() : null]; } }); }); }; PaymentGatewaysPageDao.prototype.getTotal = function () { return __awaiter(this, void 0, void 0, function () { var entity; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4, this.getEntity()]; case 1: entity = _a.sent(); return [2, entity ? entity.getTotal() : null]; } }); }); }; return PaymentGatewaysPageDao; }()); var Payments = (function () { function Payments(config) { this._dataSource = null; this._config = { token: '', environment: 'sandbox', dataSource: { class: Api, parameters: { connectionTimeout: 5000, url: 'https://payment-api.apicart.' + (Apicart.Configurator.isDevEnv() ? 'dev' : 'net'), withCredentials: false } } }; this._config = this.parseConfig(config); } Payments.prototype.pay = function (gateway, payment, orderId, parameters) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2, this.getDataSource().pay(gateway, payment, orderId, parameters)]; }); }); }; Payments.prototype.findGateway = function (code) { return __awaiter(this, void 0, void 0, function () { var gatewayDao; return __generator(this, function (_a) { switch (_a.label) { case 0: gatewayDao = new PaymentGatewayDao(this.getDataSource(), code); return [4, gatewayDao.getEntity()]; case 1: _a.sent(); return [2, gatewayDao]; } }); }); }; Payments.prototype.getGatewaysPage = function (page, size, filter) { if (size === void 0) { size = 10; } if (filter === void 0) { filter = null; } return __awaiter(this, void 0, void 0, function () { var paymentGatewaysPageDao; return __generator(this, function (_a) { switch (_a.label) { case 0: paymentGatewaysPageDao = new PaymentGatewaysPageDao(this.getDataSource(), page, size, filter); return [4, paymentGatewaysPageDao.getEntity()]; case 1: _a.sent(); return [2, paymentGatewaysPageDao]; } }); }); }; Payments.prototype.gatewayAction = function (gateway, action, parameters) { if (parameters === void 0) { parameters = {}; } return this.getDataSource().gatewayAction(gateway, action, parameters); }; Payments.prototype.getToken = function () { return this._config.token; }; Payments.prototype.getDataSource = function () { if (this._dataSource === null) { var DataSourceClass = this._config.dataSource.class; this._dataSource = new DataSourceClass(this._config.token, this._config.environment, this._config.dataSource.parameters); } return this._dataSource; }; Payments.prototype.parseConfig = function (config) { var 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; }; return Payments; }()); export default Payments;