UNPKG

@apicart/payments-sdk

Version:

Apicart SDK for payment gateways integration

23 lines (19 loc) 12.2 kB
/** * Utils.js v1.0.0-alpha7 * (c) 2018-2020 Apicart Company * Released under the MIT License. */ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("@apicart/core-sdk")):"function"==typeof define&&define.amd?define(["@apicart/core-sdk"],e):(t=t||self).Apicart=e(t.Apicart)}(this,(function(t){"use strict"; /*! ***************************************************************************** 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 e(t,e,n,i){return new(n||(n=Promise))((function(a,r){function o(t){try{u(i.next(t))}catch(t){r(t)}}function s(t){try{u(i.throw(t))}catch(t){r(t)}}function u(t){var e;t.done?a(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(o,s)}u((i=i.apply(t,e||[])).next())}))}function n(t,e){var n,i,a,r,o={label:0,sent:function(){if(1&a[0])throw a[1];return a[1]},trys:[],ops:[]};return r={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(r[Symbol.iterator]=function(){return this}),r;function s(r){return function(s){return function(r){if(n)throw new TypeError("Generator is already executing.");for(;o;)try{if(n=1,i&&(a=2&r[0]?i.return:r[0]?i.throw||((a=i.return)&&a.call(i),0):i.next)&&!(a=a.call(i,r[1])).done)return a;switch(i=0,a&&(r=[2&r[0],a.value]),r[0]){case 0:case 1:a=r;break;case 4:return o.label++,{value:r[1],done:!1};case 5:o.label++,i=r[1],r=[0];continue;case 7:r=o.ops.pop(),o.trys.pop();continue;default:if(!(a=o.trys,(a=a.length>0&&a[a.length-1])||6!==r[0]&&2!==r[0])){o=0;continue}if(3===r[0]&&(!a||r[1]>a[0]&&r[1]<a[3])){o.label=r[1];break}if(6===r[0]&&o.label<a[1]){o.label=a[1],a=r;break}if(a&&o.label<a[2]){o.label=a[2],o.ops.push(r);break}a[2]&&o.ops.pop(),o.trys.pop();continue}r=e.call(t,o)}catch(t){r=[6,t],i=0}finally{n=a=0}if(5&r[0])throw r[1];return{value:r[0]?r[1]:void 0,done:!0}}([r,s])}}}t=t&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t;var i=function(){function t(t,e,n,i){this._code=t,this._enabled=e,this._id=n,this._name=i}return t.prototype.getCode=function(){return this._code},t.prototype.setCode=function(t){this._code=t},t.prototype.isEnabled=function(){return this._enabled},t.prototype.setEnabled=function(t){this._enabled=t},t.prototype.getId=function(){return this._id},t.prototype.setId=function(t){this._id=t},t.prototype.getName=function(){return this._name},t.prototype.setName=function(t){this._name=t},t.deserialize=function(e){return new t(e.code,e.enabled,e.id,e.name)},t}(),a=function(){function e(t,e,n,i){void 0===i&&(i=[]),this._gateways=[],this._filteredTotal=t,this._hasMore=e,this._total=n,this._gateways=i}return e.prototype.getFilteredTotal=function(){return this._filteredTotal},e.prototype.setFilteredTotal=function(t){this._filteredTotal=t},e.prototype.getGateways=function(){return this._gateways},e.prototype.hasMore=function(){return this._hasMore},e.prototype.addGateway=function(t){this._gateways.push(t)},e.prototype.getTotal=function(){return this._total},e.prototype.setTotal=function(t){this._total=t},e.deserialize=function(n){var a=new e(n.filteredTotal,n.hasMore,n.total);return t.Utils.Loops.forEach(n.gateways,(function(t){a.addGateway(i.deserialize(t))})),a},e}(),r=function(){function r(e,n,i){if(void 0===i&&(i={}),this._parameters={},!t.Utils.Objects.keyExists(i,"url"))throw'Parameter "dataSource.url" is missing.';this._token=e,this._environment=n,this._parameters=i}return r.prototype.gatewaysPage=function(t,i,r){return void 0===i&&(i=10),void 0===r&&(r=null),e(this,void 0,void 0,(function(){var e,o;return n(this,(function(n){switch(n.label){case 0:return"\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}",[4,this.call("\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}",{input:{page:t,size:i,filter:r}})];case 1:return e=n.sent(),[2,(o=this.getResponseData(e,"gatewaysPage"))?a.deserialize(o):null]}}))}))},r.prototype.findGateway=function(t){return e(this,void 0,void 0,(function(){var e,a;return n(this,(function(n){switch(n.label){case 0:return"\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}",[4,this.call("\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}",{input:{gateway:t}})];case 1:return e=n.sent(),[2,(a=this.getResponseData(e,"findGateway.gateway"))?i.deserialize(a):null]}}))}))},r.prototype.gatewayAction=function(t,i,a){return void 0===a&&(a={}),e(this,void 0,void 0,(function(){var e;return n(this,(function(n){switch(n.label){case 0:return"\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}",[4,this.call("\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}",{input:{environment:this._environment,gateway:t,action:i,parameters:a}})];case 1:return e=n.sent(),[2,this.getResponseData(e,"gatewayAction.result")]}}))}))},r.prototype.pay=function(t,i,a,r){return e(this,void 0,void 0,(function(){var e;return n(this,(function(n){switch(n.label){case 0:return"\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}",[4,this.call("\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}",{input:{environment:this._environment,gateway:t,payment:i,orderId:a,parameters:r}})];case 1:return e=n.sent(),[2,this.getResponseData(e,"pay.result")]}}))}))},r.prototype.call=function(e,n){return n.input.token=this._token,t.ApiCommunicator.call(this._parameters.url,e,n,this._parameters.connectTimeout,this._parameters.withCredentials)},r.prototype.getResponseData=function(e,n){return t.Utils.Objects.find(e,"data.data."+n)},r}(),o=function(){function t(t,e){this._paymentGateway=null,this._dataSource=t,this._name=e}return t.prototype.getEntity=function(){return e(this,void 0,void 0,(function(){var t;return n(this,(function(e){switch(e.label){case 0:return null!==this._paymentGateway?[3,2]:[4,this._dataSource.findGateway(this._name)];case 1:if(null===(t=e.sent()))throw'Entity with name "'+this._name+'" not found.';this._paymentGateway=t,e.label=2;case 2:return[2,this._paymentGateway]}}))}))},t.prototype.getCode=function(){return e(this,void 0,void 0,(function(){var t;return n(this,(function(e){switch(e.label){case 0:return[4,this.getEntity()];case 1:return[2,(t=e.sent())?t.getCode():null]}}))}))},t.prototype.isEnabled=function(){return e(this,void 0,void 0,(function(){var t;return n(this,(function(e){switch(e.label){case 0:return[4,this.getEntity()];case 1:return[2,(t=e.sent())?t.isEnabled():null]}}))}))},t.prototype.getId=function(){return e(this,void 0,void 0,(function(){var t;return n(this,(function(e){switch(e.label){case 0:return[4,this.getEntity()];case 1:return[2,(t=e.sent())?t.getId():null]}}))}))},t.prototype.getName=function(){return e(this,void 0,void 0,(function(){var t;return n(this,(function(e){switch(e.label){case 0:return[4,this.getEntity()];case 1:return[2,(t=e.sent())?t.getName():null]}}))}))},t}(),s=function(){function t(t,e,n,i){void 0===n&&(n=10),void 0===i&&(i=null),this._size=10,this._filter=null,this._paymentGatewaysPage=null,this._dataSource=t,this._page=e,this._size=n,this._filter=i}return t.prototype.getEntity=function(){return e(this,void 0,void 0,(function(){var t;return n(this,(function(e){switch(e.label){case 0:return null!==this._paymentGatewaysPage?[3,2]:[4,this._dataSource.gatewaysPage(this._page,this._size,this._filter)];case 1:t=e.sent(),this._paymentGatewaysPage=t,e.label=2;case 2:return[2,this._paymentGatewaysPage]}}))}))},t.prototype.getFilteredTotal=function(){return e(this,void 0,void 0,(function(){var t;return n(this,(function(e){switch(e.label){case 0:return[4,this.getEntity()];case 1:return[2,(t=e.sent())?t.getFilteredTotal():null]}}))}))},t.prototype.getGateways=function(){return e(this,void 0,void 0,(function(){var t;return n(this,(function(e){switch(e.label){case 0:return[4,this.getEntity()];case 1:return[2,(t=e.sent())?t.getGateways():null]}}))}))},t.prototype.hasMore=function(){return e(this,void 0,void 0,(function(){var t;return n(this,(function(e){switch(e.label){case 0:return[4,this.getEntity()];case 1:return[2,(t=e.sent())?t.hasMore():null]}}))}))},t.prototype.getTotal=function(){return e(this,void 0,void 0,(function(){var t;return n(this,(function(e){switch(e.label){case 0:return[4,this.getEntity()];case 1:return[2,(t=e.sent())?t.getTotal():null]}}))}))},t}(),u=function(){function i(e){this._dataSource=null,this._config={token:"",environment:"sandbox",dataSource:{class:r,parameters:{connectionTimeout:5e3,url:"https://payment-api.apicart."+(t.Configurator.isDevEnv()?"dev":"net"),withCredentials:!1}}},this._config=this.parseConfig(e)}return i.prototype.pay=function(t,i,a,r){return e(this,void 0,void 0,(function(){return n(this,(function(e){return[2,this.getDataSource().pay(t,i,a,r)]}))}))},i.prototype.findGateway=function(t){return e(this,void 0,void 0,(function(){var e;return n(this,(function(n){switch(n.label){case 0:return[4,(e=new o(this.getDataSource(),t)).getEntity()];case 1:return n.sent(),[2,e]}}))}))},i.prototype.getGatewaysPage=function(t,i,a){return void 0===i&&(i=10),void 0===a&&(a=null),e(this,void 0,void 0,(function(){var e;return n(this,(function(n){switch(n.label){case 0:return[4,(e=new s(this.getDataSource(),t,i,a)).getEntity()];case 1:return n.sent(),[2,e]}}))}))},i.prototype.gatewayAction=function(t,e,n){return void 0===n&&(n={}),this.getDataSource().gatewayAction(t,e,n)},i.prototype.getToken=function(){return this._config.token},i.prototype.getDataSource=function(){if(null===this._dataSource){var t=this._config.dataSource.class;this._dataSource=new t(this._config.token,this._config.environment,this._config.dataSource.parameters)}return this._dataSource},i.prototype.parseConfig=function(e){var n=t.Utils.Objects.merge(this._config,e);if(!t.Utils.Objects.keyExists(n,"token")||t.Utils.Validators.isEmpty(n.token))throw"Missing token";if(!t.Utils.Objects.keyExists(n,"dataSource")||!t.Utils.Objects.keyExists(n,"dataSource.class")||t.Utils.Validators.isEmpty(n.dataSource.class)||t.Utils.Validators.isEmpty(n.dataSource.class))throw'Parameter "dataSource" is not valid.';if(!/^[0-9a-zA-Z_!?]{64}$/i.test(n.token))throw'Parameter "token" must contains 64 characters.';return n},i}();return t.Payments=u,t}));