UNPKG

@shanwker/auttar

Version:

Auttar WebSocket JS Class. An easy implementation of Auttar WebSocket Service.

8 lines (7 loc) 9.07 kB
/*! * auttarjs v0.1.2 * (c) INTI <ti@byinti.com> * Released under the MIT License. */ "use strict";function _classCallCheck(a,e){if(!(a instanceof e))throw new TypeError("Cannot call a class as a function")}function _defineProperties(a,e){for(var o=0;o<e.length;o++){var r=e[o];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(a,r.key,r)}}function _createClass(a,e,o){return e&&_defineProperties(a.prototype,e),o&&_defineProperties(a,o),a}var NAME="%c Auttar ",BACKGROUND="background:#bc0909 ; padding: 2px; border-radius: 2px; color: #fff ";function logInfo(a){console.log(NAME,BACKGROUND,a)}var privateVariables={transactions:{credit:{base:112,installment:113,installmentWithInterest:114},debit:{base:101,voucher:106},cancel:128,confirm:6,requestCancel:191},return:{success:0,timeOut:1,notAuthorizes:5,internetError:10,intertefError:12,error:20,ecommerceError:30},errorCodes:{5300:"Valor não informado",5301:"Cartão inválido",5302:"Cartão vencido",5303:"Data de vencimento inválido",5304:"Código de segurança inválido",5305:"Taxa de serviço excede limite",5306:"Operação não permitida",5307:"Dados inválidos",5308:"Valor mínimo da parcela inválido",5309:"Número de parcelas inválido",5310:"Número de parcelas excede limite",5311:"Valor da entrada maior ou igual ao valor da transação",5312:"Valor da parcela inválido",5313:"Data inválida",5314:"Prazo excede limite",5316:"NSU inválido",5317:"Operação cancelada pelo usuário",5318:"Documento inválido (CPF ou CNPJ)",5319:"Valor do documento inválido",5328:"Erro na captura de dados do Pin-Pad",5329:"Erro na captura do chip ou cartão removido antes da hora.",5364:"Data de emissão do cartão inválida",5355:"O tipo de financiamento informado não é coerente com o número de parcelas"},ws:null,timeout:null,close:!0,timeoutConn:null};function _disconnect(){privateVariables.ws.close()}function _clearTimeout(){privateVariables.close=!1,clearTimeout(privateVariables.timeoutConn)}function _timeout(){var a=arguments.length>0&&void 0!==arguments[0]?arguments[0]:1e4;privateVariables.close=!0,privateVariables.timeoutConn=setTimeout(function(){privateVariables.close?privateVariables.ws.close():_clearTimeout()},a)}function _connect(a,e){return new Promise(function(o,r){try{null===privateVariables.ws?privateVariables.ws=new WebSocket(a):2!==privateVariables.ws.readyState&&3!==privateVariables.ws.readyState||(_disconnect(),privateVariables.ws=new WebSocket(a))}catch(a){r(a)}privateVariables.ws&&(_timeout(),privateVariables.ws.onopen=function(){_clearTimeout(),privateVariables.ws.send(JSON.stringify(e)),_timeout(6e4)},privateVariables.ws.onmessage=function(a){_clearTimeout(),o(JSON.parse(a.data))},privateVariables.ws.onerror=function(a){_clearTimeout(),r(a)})})}var Auttar=function(){function a(e){_classCallCheck(this,a),this.__host=e.host||"ws://localhost:2500",this.debug=e.debug||!1,this.orderId=e.orderId||"",this.__amount=0,e.amount&&(this.amount=e.amount),this.__transactionDate=(new Date).toLocaleDateString("pt-BR",{year:"2-digit",month:"2-digit",day:"2-digit",timeZone:"America/Sao_Paulo"}).replace(/\//g,""),this.ctfTransaction={},this.__debugMessage=[]}return _createClass(a,[{key:"debugLog",value:function(a){this.debug&&logInfo(a)}},{key:"classError",value:function(a){return this.debugMessage={message:a,logLevel:"error"},new Error(a)}},{key:"credit",value:function(){var a=this,e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:1,o=arguments.length>1&&void 0!==arguments[1]&&arguments[1];return new Promise(function(r,n){var t={valorTransacao:a.amount,documento:a.orderId,operacao:privateVariables.transactions.credit.base,dataTransacao:a.__transactionDate};e>1&&(t.operacao=privateVariables.transactions.credit.installment,t.numeroParcelas=e),e>1&&o&&(t.operacao=privateVariables.transactions.credit.installmentWithInterest,t.numeroParcelas=e),a.debugMessage={message:"Pagamento com cartão de crédito. Operação: ".concat(t.operacao,". Valor ").concat(a.amount," centavos")},_connect(a.__host,t).then(function(e){if(e.retorno>0){var o=privateVariables.errorCodes[e.codigoErro]||e.display.length?e.display.map(function(a){return a.mensagem}).join(" "):" ",t=a.classError("Transação não concluída ".concat(e.codigoErro,": ").concat(o));n(t)}a.ctfTransaction=Object.assign({},e,{dataTransacao:a.__transactionDate}),a.debugMessage={message:"Resposta do servidor -> ".concat(JSON.stringify(e)),logLevel:"log"},a.debugMessage={message:a.ctfTransaction,logLevel:"json"},r(e)}).catch(function(e){return a.classError(e)})})}},{key:"debit",value:function(){var a=this,e=arguments.length>0&&void 0!==arguments[0]&&arguments[0];return new Promise(function(o,r){var n=e?privateVariables.transactions.debit.voucher:privateVariables.transactions.debit.base;a.debugMessage={message:"Pagamento com cartão de débito. Operação: ".concat(n,". Valor ").concat(a.amount," centavos")},_connect(a.__host,{valorTransacao:a.amount,documento:a.orderId,dataTransacao:a.__transactionDate,operacao:n}).then(function(e){if(e.retorno>0){var n=privateVariables.errorCodes[e.codigoErro]||e.display.length?e.display.map(function(a){return a.mensagem}).join(" "):" ",t=a.classError("Transação não concluída ".concat(e.codigoErro,": ").concat(n));r(t)}a.ctfTransaction=Object.assign({},e,{dataTransacao:a.__transactionDate}),a.debugMessage={message:"Resposta do servidor -> ".concat(JSON.stringify(e)),logLevel:"log"},a.debugMessage={message:a.ctfTransaction,logLevel:"json"},o(e)}).catch(function(e){return a.classError(e)})})}},{key:"confirm",value:function(){var a=this;return new Promise(function(e,o){var r=privateVariables.transactions.confirm;a.debugMessage={message:"Confirmação de pagamento da operação realizada.\n Operação: ".concat(a.ctfTransaction.operacao,"\n Data: ").concat(a.ctfTransaction.dataTransacao,"\n Valor: ").concat(a.amount,"\n Bandeira: ").concat(a.ctfTransaction.bandeira,"\n Cartão: ").concat(a.ctfTransaction.cartao)},_connect(a.__host,{operacao:r}).then(function(r){if(r.retorno>0){var n=privateVariables.errorCodes[r.codigoErro]||r.display.length?r.display.map(function(a){return a.mensagem}).join(" "):" ",t=a.classError("Transação não concluída ".concat(r.codigoErro,": ").concat(n));o(t)}a.ctfTransaction=Object.assign(a.ctfTransaction,r),a.debugMessage={message:"Resposta do servidor -> ".concat(JSON.stringify(r)),logLevel:"log"},a.debugMessage={message:r,logLevel:"json"},e(r)}).catch(function(e){return a.classError(e)})})}},{key:"requestCancellation",value:function(){var a=this;return new Promise(function(e,o){var r=privateVariables.transactions.requestCancel;a.debugMessage={message:"Requisição de cancelamento de compra.\n Operação: ".concat(a.ctfTransaction.operacao,"\n Data: ").concat(a.ctfTransaction.dataTransacao,"\n Valor: ").concat(a.amount,"\n NSU: ").concat(a.ctfTransaction.nsuCTF)},_connect(a.__host,{operacao:r}).then(function(r){if(r.retorno>0){var n=privateVariables.errorCodes[r.codigoErro]||r.display.length?r.display.map(function(a){return a.mensagem}).join(" "):" ",t=a.classError("Transação não concluída ".concat(r.codigoErro,": ").concat(n));o(t)}a.debugMessage={message:"Resposta do servidor -> ".concat(JSON.stringify(r)),logLevel:"log"},a.debugMessage={message:responsea,logLevel:"json"},e(r)}).catch(function(e){return a.classError(e)})})}},{key:"cancel",value:function(){var a=this,e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};return new Promise(function(o,r){var n=privateVariables.transactions.cancel,t=e.operacao||a.ctfTransaction.operacao,s=e.dataTransacao||a.ctfTransaction.dataTransacao,i=e.amount?100*parseFloat(e.amount):a.ctfTransaction.valorTransacao,c=e.nsuCTF||a.ctfTransaction.nsuCTF;a.debugMessage={message:"Cancelamento de compra.\n Operação: ".concat(t,"\n Data: ").concat(s,"\n Valor: ").concat(i,"\n NSU: ").concat(c)},_connect(a.__host,{operacao:n,valorTransacao:i,dataTransacao:s,nsuCTF:c}).then(function(e){if(e.retorno>0){var n=privateVariables.errorCodes[e.codigoErro]||e.display[0].mensagem,t=a.classError("Transação não concluída ".concat(e.codigoErro,": ").concat(n));r(t)}a.debugMessage={message:"Resposta do servidor -> ".concat(JSON.stringify(e)),logLevel:"log"},a.debugMessage={message:e,logLevel:"json"},o(e)}).catch(function(e){return a.classError(e)})})}},{key:"debugMessage",get:function(){return this.__debugMessage},set:function(a){if(this.debug){var e=Object.assign({logLevel:"info",message:""},a,{date:(new Date).toISOString()});if("log"===e.logLevel&&e.message)return this.debugLog(e.message);this.__debugMessage.push(Object.assign({},e,{date:(new Date).toISOString()})),"info"===e.logLevel&&e.message&&this.debugLog(e.message)}}},{key:"amount",get:function(){return this.__amount},set:function(a){if("number"==typeof a&&a<=0)throw new Error("Não é possível definir um valor menor ou igual a zero.");this.__amount=100*parseFloat(a)}}]),a}();module.exports=Auttar; //# sourceMappingURL=index.min.js.map