From dc23fa26a9578d26d6491f2ca169e41e13bbeafc Mon Sep 17 00:00:00 2001 From: igorp Date: Tue, 8 Oct 2019 21:03:07 -0300 Subject: [PATCH 01/11] Configured eslint with airbnb pattern --- .eslintrc.json | 20 + .gitignore | 6 +- LICENSE | 40 +- README.md | 154 +++---- lib/additional.js | 38 +- lib/address.js | 60 +-- lib/antifraud.js | 36 +- lib/authorization.js | 40 +- lib/capture.js | 40 +- lib/cart.js | 88 ++-- lib/consumer.js | 68 +-- lib/environment.js | 40 +- lib/erede.js | 140 +++--- lib/exception/RedeError.js | 18 +- lib/flight.js | 42 +- lib/iata.js | 14 +- lib/item.js | 48 +-- lib/passenger.js | 16 +- lib/phone.js | 48 +-- lib/refund.js | 40 +- lib/service/CancelTransactionService.js | 36 +- lib/service/CaptureTransactionService.js | 36 +- lib/service/CreateTransactionService.js | 28 +- lib/service/GetTransactionService.js | 60 +-- lib/service/TransactionService.js | 156 +++---- lib/store.js | 20 +- lib/submerchant.js | 16 +- lib/threedsecure.js | 54 +-- lib/transaction.js | 518 +++++++++++------------ lib/url.js | 70 +-- package.json | 76 ++-- 31 files changed, 1046 insertions(+), 1020 deletions(-) create mode 100644 .eslintrc.json diff --git a/.eslintrc.json b/.eslintrc.json new file mode 100644 index 0000000..6748bfd --- /dev/null +++ b/.eslintrc.json @@ -0,0 +1,20 @@ +{ + "env": { + "es6": true, + "node": true, + "jest": true + }, + "extends": [ + "airbnb-base" + ], + "globals": { + "Atomics": "readonly", + "SharedArrayBuffer": "readonly" + }, + "parserOptions": { + "ecmaVersion": 2018, + "sourceType": "module" + }, + "rules": { + } +} \ No newline at end of file diff --git a/.gitignore b/.gitignore index c1605d3..d63a8e4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ -.idea -node_modules/ -package-lock.json +.idea +node_modules/ +package-lock.json diff --git a/LICENSE b/LICENSE index 2c1551c..01ed47a 100644 --- a/LICENSE +++ b/LICENSE @@ -1,21 +1,21 @@ -MIT License - -Copyright (c) 2019 Rede - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +MIT License + +Copyright (c) 2019 Rede + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md index 9d9eba3..42c14b1 100644 --- a/README.md +++ b/README.md @@ -1,77 +1,77 @@ -# SDK Node.js - -SDK de integração eRede - -# Utilizando - -## Autorizando uma transação - -```js -const eRede = require('./lib/erede'); -const Transaction = require('./lib/transaction'); -const Store = require('./lib/store'); -const Environment = require('./lib/environment'); - -let store = new Store('TOKEN', 'PV', Environment.sandbox()); -let transaction = new Transaction(10, "ref123").creditCard( - '5448280000000007', - '235', - '12', - '2020', - 'Fulano de Tal' -); - -new eRede(store).create(transaction).then(transaction => { - if (transaction.returnCode === "00") { - console.log(`Transação autorizada com sucesso: ${transaction.tid}`); - } -}); -``` - -Por padrão, a transação é capturada automaticamente; caso seja necessário apenas autorizar a transação, o método `Transaction.capture()` deverá ser chamado com o parâmetro `false`: - -```js -const eRede = require('./lib/erede'); -const Transaction = require('./lib/transaction'); -const Store = require('./lib/store'); -const Environment = require('./lib/environment'); - -let store = new Store('TOKEN', 'PV', Environment.sandbox()); -let transaction = new Transaction(10, "ref123").creditCard( - '5448280000000007', - '235', - '12', - '2020', - 'Fulano de Tal' -).autoCapture(false); - -new eRede(store).create(transaction).then(transaction => { - if (transaction.returnCode === "00") { - console.log(`Transação autorizada com sucesso: ${transaction.tid}`); - } -}); -``` - -## Autorizando uma transação com parcelamento - -```js -const eRede = require('./lib/erede'); -const Transaction = require('./lib/transaction'); -const Store = require('./lib/store'); -const Environment = require('./lib/environment'); - -let store = new Store('TOKEN', 'PV', Environment.sandbox()); -let transaction = new Transaction(10, "ref123", 2).creditCard( - '5448280000000007', - '235', - '12', - '2020', - 'Fulano de Tal' -); - -new eRede(store).create(transaction).then(transaction => { - if (transaction.returnCode === "00") { - console.log(`Transação autorizada com sucesso: ${transaction.tid}`); - } -}); -``` +# SDK Node.js + +SDK de integração eRede + +# Utilizando + +## Autorizando uma transação + +```js +const eRede = require('./lib/erede'); +const Transaction = require('./lib/transaction'); +const Store = require('./lib/store'); +const Environment = require('./lib/environment'); + +let store = new Store('TOKEN', 'PV', Environment.sandbox()); +let transaction = new Transaction(10, "ref123").creditCard( + '5448280000000007', + '235', + '12', + '2020', + 'Fulano de Tal' +); + +new eRede(store).create(transaction).then(transaction => { + if (transaction.returnCode === "00") { + console.log(`Transação autorizada com sucesso: ${transaction.tid}`); + } +}); +``` + +Por padrão, a transação é capturada automaticamente; caso seja necessário apenas autorizar a transação, o método `Transaction.capture()` deverá ser chamado com o parâmetro `false`: + +```js +const eRede = require('./lib/erede'); +const Transaction = require('./lib/transaction'); +const Store = require('./lib/store'); +const Environment = require('./lib/environment'); + +let store = new Store('TOKEN', 'PV', Environment.sandbox()); +let transaction = new Transaction(10, "ref123").creditCard( + '5448280000000007', + '235', + '12', + '2020', + 'Fulano de Tal' +).autoCapture(false); + +new eRede(store).create(transaction).then(transaction => { + if (transaction.returnCode === "00") { + console.log(`Transação autorizada com sucesso: ${transaction.tid}`); + } +}); +``` + +## Autorizando uma transação com parcelamento + +```js +const eRede = require('./lib/erede'); +const Transaction = require('./lib/transaction'); +const Store = require('./lib/store'); +const Environment = require('./lib/environment'); + +let store = new Store('TOKEN', 'PV', Environment.sandbox()); +let transaction = new Transaction(10, "ref123", 2).creditCard( + '5448280000000007', + '235', + '12', + '2020', + 'Fulano de Tal' +); + +new eRede(store).create(transaction).then(transaction => { + if (transaction.returnCode === "00") { + console.log(`Transação autorizada com sucesso: ${transaction.tid}`); + } +}); +``` diff --git a/lib/additional.js b/lib/additional.js index 231326c..fa49c0f 100644 --- a/lib/additional.js +++ b/lib/additional.js @@ -1,20 +1,20 @@ -"use strict"; - -module.exports = class Additional { - constructor(gateway, module) { - this.gateway = gateway; - this.module = module; - } - - static fromJSON(json) { - let additional = new Additional(); - - for (let property in json) { - if (json.hasOwnProperty(property)) { - additional[property] = json[property]; - } - } - - return additional; - } +"use strict"; + +module.exports = class Additional { + constructor(gateway, module) { + this.gateway = gateway; + this.module = module; + } + + static fromJSON(json) { + let additional = new Additional(); + + for (let property in json) { + if (json.hasOwnProperty(property)) { + additional[property] = json[property]; + } + } + + return additional; + } }; \ No newline at end of file diff --git a/lib/address.js b/lib/address.js index 7397e56..c564f78 100644 --- a/lib/address.js +++ b/lib/address.js @@ -1,31 +1,31 @@ -"use strict"; - -module.exports = class Address { - static get BILLING() { - return 1; - } - - static get SHIPPING() { - return 2; - } - - static get BOTH() { - return 3; - } - - static get APARTMENT() { - return 1; - } - - static get HOUSE() { - return 2; - } - - static get COMMERCIAL() { - return 3; - } - - static get OTHER() { - return 4; - } +"use strict"; + +module.exports = class Address { + static get BILLING() { + return 1; + } + + static get SHIPPING() { + return 2; + } + + static get BOTH() { + return 3; + } + + static get APARTMENT() { + return 1; + } + + static get HOUSE() { + return 2; + } + + static get COMMERCIAL() { + return 3; + } + + static get OTHER() { + return 4; + } }; \ No newline at end of file diff --git a/lib/antifraud.js b/lib/antifraud.js index 638940a..b1921eb 100644 --- a/lib/antifraud.js +++ b/lib/antifraud.js @@ -1,19 +1,19 @@ -"use strict"; - -module.exports = class Antifraud { - constructor() { - this.success = false; - } - - static fromJSON(json) { - let antifraud = new self(); - - for (let property in json) { - if (json.hasOwnProperty(property)) { - antifraud[property] = json[property]; - } - } - - return antifraud; - } +"use strict"; + +module.exports = class Antifraud { + constructor() { + this.success = false; + } + + static fromJSON(json) { + let antifraud = new self(); + + for (let property in json) { + if (json.hasOwnProperty(property)) { + antifraud[property] = json[property]; + } + } + + return antifraud; + } }; \ No newline at end of file diff --git a/lib/authorization.js b/lib/authorization.js index a591ee5..7b4e3e2 100644 --- a/lib/authorization.js +++ b/lib/authorization.js @@ -1,21 +1,21 @@ -"use strict"; - -module.exports = class Authorization { - static fromJSON(json) { - let authorization = new Authorization(); - - for (let property in json) { - if (json.hasOwnProperty(property)) { - let value = json[property]; - - if (property === 'requestDateTime' || property === 'dateTime' || property === 'refundDateTime') { - value = new Date(value); - } - - authorization[property] = value; - } - } - - return authorization; - } +"use strict"; + +module.exports = class Authorization { + static fromJSON(json) { + let authorization = new Authorization(); + + for (let property in json) { + if (json.hasOwnProperty(property)) { + let value = json[property]; + + if (property === 'requestDateTime' || property === 'dateTime' || property === 'refundDateTime') { + value = new Date(value); + } + + authorization[property] = value; + } + } + + return authorization; + } }; \ No newline at end of file diff --git a/lib/capture.js b/lib/capture.js index a4fb205..646650e 100644 --- a/lib/capture.js +++ b/lib/capture.js @@ -1,21 +1,21 @@ -"use strict"; - -module.exports = class Capture { - static fromJSON(json) { - let capture = new Capture(); - - for (let property in json) { - if (json.hasOwnProperty(property)) { - let value = json[property]; - - if (property === 'requestDateTime' || property === 'dateTime' || property === 'refundDateTime') { - value = new Date(value); - } - - capture[property] = value; - } - } - - return capture; - } +"use strict"; + +module.exports = class Capture { + static fromJSON(json) { + let capture = new Capture(); + + for (let property in json) { + if (json.hasOwnProperty(property)) { + let value = json[property]; + + if (property === 'requestDateTime' || property === 'dateTime' || property === 'refundDateTime') { + value = new Date(value); + } + + capture[property] = value; + } + } + + return capture; + } }; \ No newline at end of file diff --git a/lib/cart.js b/lib/cart.js index 6aeddd9..b77f7e2 100644 --- a/lib/cart.js +++ b/lib/cart.js @@ -1,44 +1,44 @@ -"use strict"; - -const Address = require("./address"); -const Consumer = require("./consumer"); -const Iata = require("./iata"); - -module.exports = class Cart { - address(type = Address.BOTH) { - let address = Address(); - - if ((type & Address.BILLING) === Address.BILLING) { - this.billing = address; - } - - if ((type & Address.SHIPPING) === Address.SHIPPING) { - this.shipping = [address]; - } - - return address; - } - - addItem(item) { - if (this.items === undefined) { - this.items = []; - } - - this.items.push(item); - - return this; - } - - setConsumer(name, email, cpf) { - this.consumer = new Consumer(name, email, cpf); - - return this.consumer; - } - - setIata(code, departureTax, flight) { - this.iata = new Iata(code, departureTax); - this.iata.flight = flight; - - return this; - } -}; +"use strict"; + +const Address = require("./address"); +const Consumer = require("./consumer"); +const Iata = require("./iata"); + +module.exports = class Cart { + address(type = Address.BOTH) { + let address = Address(); + + if ((type & Address.BILLING) === Address.BILLING) { + this.billing = address; + } + + if ((type & Address.SHIPPING) === Address.SHIPPING) { + this.shipping = [address]; + } + + return address; + } + + addItem(item) { + if (this.items === undefined) { + this.items = []; + } + + this.items.push(item); + + return this; + } + + setConsumer(name, email, cpf) { + this.consumer = new Consumer(name, email, cpf); + + return this.consumer; + } + + setIata(code, departureTax, flight) { + this.iata = new Iata(code, departureTax); + this.iata.flight = flight; + + return this; + } +}; diff --git a/lib/consumer.js b/lib/consumer.js index 0034421..7eeaf75 100644 --- a/lib/consumer.js +++ b/lib/consumer.js @@ -1,35 +1,35 @@ -"use strict"; - -const Phone = require("./phone"); - -module.exports = class Consumer { - constructor(name, email, cpf) { - this.name = name; - this.email = email; - this.cpf = cpf; - } - - static get MALE() { - return "M"; - } - - static get FEMALE() { - return "F"; - } - - addDocument(type, number) { - if (this.documents === undefined) { - this.documents = []; - } - - this.documents.push({type: type, number: number}); - - return this; - } - - setPhone(ddd, number, type = Phone.CELLPHONE) { - this.phone = new Phone(ddd, number, type); - - return this; - } +"use strict"; + +const Phone = require("./phone"); + +module.exports = class Consumer { + constructor(name, email, cpf) { + this.name = name; + this.email = email; + this.cpf = cpf; + } + + static get MALE() { + return "M"; + } + + static get FEMALE() { + return "F"; + } + + addDocument(type, number) { + if (this.documents === undefined) { + this.documents = []; + } + + this.documents.push({type: type, number: number}); + + return this; + } + + setPhone(ddd, number, type = Phone.CELLPHONE) { + this.phone = new Phone(ddd, number, type); + + return this; + } }; \ No newline at end of file diff --git a/lib/environment.js b/lib/environment.js index 8e056d2..765382f 100644 --- a/lib/environment.js +++ b/lib/environment.js @@ -1,21 +1,21 @@ -"use strict"; - -const PRODUCTION = "https://api.userede.com.br/erede"; -const SANDBOX = "https://api.userede.com.br/desenvolvedores"; -const VERSION = "v1"; - -module.exports = class Environment { - constructor(baseUrl, version = VERSION) { - this.ip = ""; - this.sessionId = ""; - this.endpoint = `${baseUrl}/${version}`; - } - - static production() { - return new Environment(PRODUCTION, VERSION); - } - - static sandbox() { - return new Environment(SANDBOX, VERSION) - } +"use strict"; + +const PRODUCTION = "https://api.userede.com.br/erede"; +const SANDBOX = "https://api.userede.com.br/desenvolvedores"; +const VERSION = "v1"; + +module.exports = class Environment { + constructor(baseUrl, version = VERSION) { + this.ip = ""; + this.sessionId = ""; + this.endpoint = `${baseUrl}/${version}`; + } + + static production() { + return new Environment(PRODUCTION, VERSION); + } + + static sandbox() { + return new Environment(SANDBOX, VERSION) + } }; \ No newline at end of file diff --git a/lib/erede.js b/lib/erede.js index 12e1f37..bf3fb26 100644 --- a/lib/erede.js +++ b/lib/erede.js @@ -1,70 +1,70 @@ -"use strict"; - -const CancelTransactionService = require("./service/CancelTransactionService"); -const CaptureTransactionService = require("./service/CaptureTransactionService"); -const CreateTransactionService = require("./service/CreateTransactionService"); -const GetTransactionService = require("./service/GetTransactionService"); - -module.exports = class eRede { - constructor(store) { - this.store = store; - } - - async create(transaction) { - let service = new CreateTransactionService(this.store, transaction); - - return service.execute(); - } - - cancel(transaction) { - let service = new CancelTransactionService(this.store, transaction); - - return service.execute(); - } - - capture(transaction) { - let service = new CaptureTransactionService(this.store, transaction); - - return service.execute(); - } - - getByTid(tid) { - let service = new GetTransactionService(this.store); - - service.tid = tid; - - return service.execute(); - } - - getByReference(reference) { - let service = new GetTransactionService(this.store); - - service.reference = reference; - - return service.execute(); - } - - getRefunds(tid) { - let service = new GetTransactionService(this.store); - - service.tid = tid; - service.refunds = true; - - return service.execute(); - } - - zero(transaction) { - let amount = transaction.amount; - let capture = transaction.capture; - - transaction.amount = 0; - transaction.capture = true; - - transaction = this.create(transaction); - - transaction.amount = amount; - transaction.capture = capture; - - return transaction; - } -}; \ No newline at end of file + + +const CancelTransactionService = require('./service/CancelTransactionService'); +const CaptureTransactionService = require('./service/CaptureTransactionService'); +const CreateTransactionService = require('./service/CreateTransactionService'); +const GetTransactionService = require('./service/GetTransactionService'); + +module.exports = class eRede { + constructor(store) { + this.store = store; + } + + async create(transaction) { + const service = new CreateTransactionService(this.store, transaction); + + return service.execute(); + } + + cancel(transaction) { + const service = new CancelTransactionService(this.store, transaction); + + return service.execute(); + } + + capture(transaction) { + const service = new CaptureTransactionService(this.store, transaction); + + return service.execute(); + } + + getByTid(tid) { + const service = new GetTransactionService(this.store); + + service.tid = tid; + + return service.execute(); + } + + getByReference(reference) { + const service = new GetTransactionService(this.store); + + service.reference = reference; + + return service.execute(); + } + + getRefunds(tid) { + const service = new GetTransactionService(this.store); + + service.tid = tid; + service.refunds = true; + + return service.execute(); + } + + zero(transaction) { + const { amount } = transaction; + const { capture } = transaction; + + transaction.amount = 0; + transaction.capture = true; + + transaction = this.create(transaction); + + transaction.amount = amount; + transaction.capture = capture; + + return transaction; + } +}; diff --git a/lib/exception/RedeError.js b/lib/exception/RedeError.js index 86795b0..cfb7cc2 100644 --- a/lib/exception/RedeError.js +++ b/lib/exception/RedeError.js @@ -1,10 +1,10 @@ -"use strict"; - -module.exports = class RedeError extends Error { - constructor(message, code) { - super(message); - - this.returnCode = code; - this.returnMessage = message; - } +"use strict"; + +module.exports = class RedeError extends Error { + constructor(message, code) { + super(message); + + this.returnCode = code; + this.returnMessage = message; + } }; \ No newline at end of file diff --git a/lib/flight.js b/lib/flight.js index 0ad748c..6af7e50 100644 --- a/lib/flight.js +++ b/lib/flight.js @@ -1,22 +1,22 @@ -"use strinct"; - -const Passenger = require("./passenger"); - -module.exports = class Flight { - constructor(number, from, to, date) { - this.number = number; - this.from = from; - this.to = to; - this.date = date; - } - - addPassenger(name, email, ticket) { - if (this.passenger === undefined) { - this.passenger = []; - } - - this.passenger.push(new Passenger(name, email, ticket)); - - return this; - } +"use strinct"; + +const Passenger = require("./passenger"); + +module.exports = class Flight { + constructor(number, from, to, date) { + this.number = number; + this.from = from; + this.to = to; + this.date = date; + } + + addPassenger(name, email, ticket) { + if (this.passenger === undefined) { + this.passenger = []; + } + + this.passenger.push(new Passenger(name, email, ticket)); + + return this; + } }; \ No newline at end of file diff --git a/lib/iata.js b/lib/iata.js index 29e1205..81144c0 100644 --- a/lib/iata.js +++ b/lib/iata.js @@ -1,8 +1,8 @@ -"use strict"; - -module.exports = class Iata { - constructor(code, departureTax) { - this.code = code; - this.departureTax = departureTax; - } +"use strict"; + +module.exports = class Iata { + constructor(code, departureTax) { + this.code = code; + this.departureTax = departureTax; + } }; \ No newline at end of file diff --git a/lib/item.js b/lib/item.js index 93d3fd0..b6a9243 100644 --- a/lib/item.js +++ b/lib/item.js @@ -1,25 +1,25 @@ -"use strict"; - -module.exports = class Item { - constructor(id, quantity, type = Item.PHYSICAL) { - this.id = id; - this.quantity = quantity; - this.type = type; - } - - static get PHYSICAL() { - return 1; - } - - static get DIGITAL() { - return 2; - } - - static get SERVICE() { - return 3; - } - - static get AIRLINE() { - return 4; - } +"use strict"; + +module.exports = class Item { + constructor(id, quantity, type = Item.PHYSICAL) { + this.id = id; + this.quantity = quantity; + this.type = type; + } + + static get PHYSICAL() { + return 1; + } + + static get DIGITAL() { + return 2; + } + + static get SERVICE() { + return 3; + } + + static get AIRLINE() { + return 4; + } }; \ No newline at end of file diff --git a/lib/passenger.js b/lib/passenger.js index 6dfc577..c61f56d 100644 --- a/lib/passenger.js +++ b/lib/passenger.js @@ -1,9 +1,9 @@ -"use strict"; - -module.exports = class Passenger { - constructor(name, email, ticket) { - this.name = name; - this.email = email; - this.ticket = ticket; - } +"use strict"; + +module.exports = class Passenger { + constructor(name, email, ticket) { + this.name = name; + this.email = email; + this.ticket = ticket; + } }; \ No newline at end of file diff --git a/lib/phone.js b/lib/phone.js index 317fa15..bf6cba2 100644 --- a/lib/phone.js +++ b/lib/phone.js @@ -1,25 +1,25 @@ -"use strict"; - -module.exports = class Phone { - constructor(ddd, number, type = Phone.CELLPHONE) { - this.ddd = ddd; - this.number = number; - this.type = type; - } - - static get CELLPHONE() { - return 1; - } - - static get HOME() { - return 2; - } - - static get WORK() { - return 3; - } - - static get OTHER() { - return 4; - } +"use strict"; + +module.exports = class Phone { + constructor(ddd, number, type = Phone.CELLPHONE) { + this.ddd = ddd; + this.number = number; + this.type = type; + } + + static get CELLPHONE() { + return 1; + } + + static get HOME() { + return 2; + } + + static get WORK() { + return 3; + } + + static get OTHER() { + return 4; + } }; \ No newline at end of file diff --git a/lib/refund.js b/lib/refund.js index e8f449b..507f605 100644 --- a/lib/refund.js +++ b/lib/refund.js @@ -1,21 +1,21 @@ -"use strict"; - -module.exports = class Refund { - static fromJSON(json) { - let refund = new Refund(); - - for (let property in json) { - if (json.hasOwnProperty(property)) { - let value = json[property]; - - if (property === 'requestDateTime' || property === 'dateTime' || property === 'refundDateTime') { - value = new Date(value); - } - - refund[property] = value; - } - } - - return refund; - } +"use strict"; + +module.exports = class Refund { + static fromJSON(json) { + let refund = new Refund(); + + for (let property in json) { + if (json.hasOwnProperty(property)) { + let value = json[property]; + + if (property === 'requestDateTime' || property === 'dateTime' || property === 'refundDateTime') { + value = new Date(value); + } + + refund[property] = value; + } + } + + return refund; + } }; \ No newline at end of file diff --git a/lib/service/CancelTransactionService.js b/lib/service/CancelTransactionService.js index 910aab0..bbf5530 100644 --- a/lib/service/CancelTransactionService.js +++ b/lib/service/CancelTransactionService.js @@ -1,19 +1,19 @@ -"use strict"; - -const TransactionService = require('./TransactionService'); - -module.exports = class CancelTransactionService extends TransactionService { - constructor(store, transaction) { - super(store); - - this.transaction = transaction; - } - - getUrl() { - return `${super.getUrl()}/${this.transaction.tid}/refunds`; - } - - async execute() { - return this.sendRequest(TransactionService.POST, JSON.stringify(this.transaction)); - } +"use strict"; + +const TransactionService = require('./TransactionService'); + +module.exports = class CancelTransactionService extends TransactionService { + constructor(store, transaction) { + super(store); + + this.transaction = transaction; + } + + getUrl() { + return `${super.getUrl()}/${this.transaction.tid}/refunds`; + } + + async execute() { + return this.sendRequest(TransactionService.POST, JSON.stringify(this.transaction)); + } }; \ No newline at end of file diff --git a/lib/service/CaptureTransactionService.js b/lib/service/CaptureTransactionService.js index 4d597a3..719ebce 100644 --- a/lib/service/CaptureTransactionService.js +++ b/lib/service/CaptureTransactionService.js @@ -1,19 +1,19 @@ -"use strict"; - -const TransactionService = require('./TransactionService'); - -module.exports = class CaptureTransactionService extends TransactionService { - constructor(store, transaction) { - super(store); - - this.transaction = transaction; - } - - getUrl() { - return `${super.getUrl()}/${this.transaction.tid}`; - } - - execute() { - return this.sendRequest(TransactionService.PUT, JSON.stringify(this.transaction)); - } +"use strict"; + +const TransactionService = require('./TransactionService'); + +module.exports = class CaptureTransactionService extends TransactionService { + constructor(store, transaction) { + super(store); + + this.transaction = transaction; + } + + getUrl() { + return `${super.getUrl()}/${this.transaction.tid}`; + } + + execute() { + return this.sendRequest(TransactionService.PUT, JSON.stringify(this.transaction)); + } }; \ No newline at end of file diff --git a/lib/service/CreateTransactionService.js b/lib/service/CreateTransactionService.js index a080ac2..f3ae85e 100644 --- a/lib/service/CreateTransactionService.js +++ b/lib/service/CreateTransactionService.js @@ -1,15 +1,15 @@ -"use strict"; - -const TransactionService = require('./TransactionService'); - -module.exports = class CreateTransactionService extends TransactionService { - constructor(store, transaction) { - super(store); - - this.transaction = transaction; - } - - async execute() { - return await this.sendRequest(TransactionService.POST, JSON.stringify(this.transaction)); - } +"use strict"; + +const TransactionService = require('./TransactionService'); + +module.exports = class CreateTransactionService extends TransactionService { + constructor(store, transaction) { + super(store); + + this.transaction = transaction; + } + + async execute() { + return await this.sendRequest(TransactionService.POST, JSON.stringify(this.transaction)); + } }; \ No newline at end of file diff --git a/lib/service/GetTransactionService.js b/lib/service/GetTransactionService.js index 21492ad..f3b44a4 100644 --- a/lib/service/GetTransactionService.js +++ b/lib/service/GetTransactionService.js @@ -1,31 +1,31 @@ -"use strict"; - -const TransactionService = require('./TransactionService'); - -module.exports = class GetTransactionService extends TransactionService { - constructor(store) { - super(store); - - this.tid = undefined; - } - - getUrl() { - if (this.reference !== undefined) { - return `${super.getUrl()}?reference=${this.reference}`; - } - - if (this.tid === undefined) { - throw new Error('You need to specify one: the tid or the reference'); - } - - if (this.refunds !== undefined) { - return `${super.getUrl()}/${this.tid}/refunds`; - } - - return `${super.getUrl()}/${this.tid}`; - } - - execute() { - return this.sendRequest(TransactionService.GET); - } +"use strict"; + +const TransactionService = require('./TransactionService'); + +module.exports = class GetTransactionService extends TransactionService { + constructor(store) { + super(store); + + this.tid = undefined; + } + + getUrl() { + if (this.reference !== undefined) { + return `${super.getUrl()}?reference=${this.reference}`; + } + + if (this.tid === undefined) { + throw new Error('You need to specify one: the tid or the reference'); + } + + if (this.refunds !== undefined) { + return `${super.getUrl()}/${this.tid}/refunds`; + } + + return `${super.getUrl()}/${this.tid}`; + } + + execute() { + return this.sendRequest(TransactionService.GET); + } }; \ No newline at end of file diff --git a/lib/service/TransactionService.js b/lib/service/TransactionService.js index c58b221..82efbd4 100644 --- a/lib/service/TransactionService.js +++ b/lib/service/TransactionService.js @@ -1,78 +1,78 @@ -"use strict"; - -const Transaction = require('../transaction'); -const RedeError = require('../exception/RedeError'); -const http = require('https'); -const bl = require('bl'); -const URL = require('url').URL; - -module.exports = class TransactionService { - constructor(store) { - this.store = store; - } - - static get POST() { - return 'POST'; - } - - static get GET() { - return 'GET'; - } - - static get PUT() { - return 'PUT'; - } - - getUrl() { - let endpoint = this.store.environment.endpoint; - - return `${endpoint}/transactions`; - } - - async execute() { - throw new Error('Ńão implementado'); - } - - sendRequest(method, body = "") { - const url = new URL(this.getUrl()); - const options = { - hostname: url.hostname, - post: 443, - path: url.pathname, - method: method, - auth: this.store.filiation + ':' + this.store.token, - headers: { - 'Content-Type': 'application/json', - 'Content-Length': Buffer.byteLength(body) - } - }; - - return new Promise((resolve, reject) => { - let client = http.request(options, response => { - response.setEncoding('utf8'); - response.pipe(bl((error, data) => { - if (error) { - reject(error); - } - - let json = JSON.parse(data.toString()); - - if (response.statusCode >= 400) { - if (!json || json.returnMessage === undefined) { - json = {}; - json.returnMessage = 'Alguma coisa aconteceu'; - json.returnCode = '-1'; - } - - reject(new RedeError(json.returnMessage, json.returnCode)); - } - - resolve(Transaction.fromJSON(json)); - })); - }); - - client.write(body); - client.end(); - }); - } -}; +"use strict"; + +const Transaction = require('../transaction'); +const RedeError = require('../exception/RedeError'); +const http = require('https'); +const bl = require('bl'); +const URL = require('url').URL; + +module.exports = class TransactionService { + constructor(store) { + this.store = store; + } + + static get POST() { + return 'POST'; + } + + static get GET() { + return 'GET'; + } + + static get PUT() { + return 'PUT'; + } + + getUrl() { + let endpoint = this.store.environment.endpoint; + + return `${endpoint}/transactions`; + } + + async execute() { + throw new Error('Ńão implementado'); + } + + sendRequest(method, body = "") { + const url = new URL(this.getUrl()); + const options = { + hostname: url.hostname, + post: 443, + path: url.pathname, + method: method, + auth: this.store.filiation + ':' + this.store.token, + headers: { + 'Content-Type': 'application/json', + 'Content-Length': Buffer.byteLength(body) + } + }; + + return new Promise((resolve, reject) => { + let client = http.request(options, response => { + response.setEncoding('utf8'); + response.pipe(bl((error, data) => { + if (error) { + reject(error); + } + + let json = JSON.parse(data.toString()); + + if (response.statusCode >= 400) { + if (!json || json.returnMessage === undefined) { + json = {}; + json.returnMessage = 'Alguma coisa aconteceu'; + json.returnCode = '-1'; + } + + reject(new RedeError(json.returnMessage, json.returnCode)); + } + + resolve(Transaction.fromJSON(json)); + })); + }); + + client.write(body); + client.end(); + }); + } +}; diff --git a/lib/store.js b/lib/store.js index 39b510f..29e4d09 100644 --- a/lib/store.js +++ b/lib/store.js @@ -1,11 +1,11 @@ -"use strict"; - -const Environment = require("./environment.js"); - -module.exports = class Store { - constructor(token, filiation, environment = Environment.production()) { - this.token = token; - this.filiation = filiation; - this.environment = environment; - } +"use strict"; + +const Environment = require("./environment.js"); + +module.exports = class Store { + constructor(token, filiation, environment = Environment.production()) { + this.token = token; + this.filiation = filiation; + this.environment = environment; + } }; \ No newline at end of file diff --git a/lib/submerchant.js b/lib/submerchant.js index 66d9c4f..62fc592 100644 --- a/lib/submerchant.js +++ b/lib/submerchant.js @@ -1,9 +1,9 @@ -"use strict"; - -module.exports = class SubMerchant { - constructor(mcc, city, country) { - this.mcc = mcc; - this.city = city; - this.country = country; - } +"use strict"; + +module.exports = class SubMerchant { + constructor(mcc, city, country) { + this.mcc = mcc; + this.city = city; + this.country = country; + } }; \ No newline at end of file diff --git a/lib/threedsecure.js b/lib/threedsecure.js index b3385c7..9854fde 100644 --- a/lib/threedsecure.js +++ b/lib/threedsecure.js @@ -1,28 +1,28 @@ -"use strict"; - -module.exports = class ThreeDSecure { - constructor() { - this.embedded = true; - this.threeDIndicator = "1"; - } - - static get CONTINUE_ON_FAILURE() { - return "continue"; - } - - static get DECLINE_ON_FAILURE() { - return "decline"; - } - - static fromJSON(json) { - let threeds = new ThreeDSecure(); - - for (let property in json) { - if (json.hasOwnProperty(property)) { - threeds[property] = json[property]; - } - } - - return threeds; - } +"use strict"; + +module.exports = class ThreeDSecure { + constructor() { + this.embedded = true; + this.threeDIndicator = "1"; + } + + static get CONTINUE_ON_FAILURE() { + return "continue"; + } + + static get DECLINE_ON_FAILURE() { + return "decline"; + } + + static fromJSON(json) { + let threeds = new ThreeDSecure(); + + for (let property in json) { + if (json.hasOwnProperty(property)) { + threeds[property] = json[property]; + } + } + + return threeds; + } }; \ No newline at end of file diff --git a/lib/transaction.js b/lib/transaction.js index e5a62b2..803063c 100644 --- a/lib/transaction.js +++ b/lib/transaction.js @@ -1,259 +1,259 @@ -"use strict"; - -const Cart = require("./cart"); -const Url = require("./url"); -const Iata = require("./iata"); -const ThreeDSecure = require("./threedsecure"); -const Capture = require("./capture"); -const Authorization = require("./authorization"); -const Additional = require("./additional"); -const Antifraud = require("./antifraud"); -const Refund = require('./refund'); - -module.exports = class Transaction { - constructor(amount, reference, installments) { - if (amount !== undefined) { - this.amount = amount; - } - - if (reference !== undefined) { - this.reference = reference; - } - - if (installments !== undefined) { - this.installments = installments; - } - } - - static get CREDIT() { - return "credit"; - } - - static get DEBIT() { - return "debit"; - } - - static get ORIGIN_EREDE() { - return 1; - } - - static get ORIGIN_VISA_CHECKOUT() { - return 4; - } - - static get ORIGIN_MASTERPASS() { - return 6; - } - - static fromJSON(json) { - let transaction = new Transaction(json.amount, json.reference); - - for (let property in json) { - if (!json.hasOwnProperty(property)) { - continue; - } - - let value = json[property]; - let i = 0; - let t = 0; - - switch (property) { - case 'refunds': - transaction.refunds = []; - - for (i = 0, t = value.length; i < t; i++) { - transaction.refunds.push(Refund.fromJSON(value[i])); - } - - break; - case 'urls': - transaction.urls = []; - - for (i = 0, t = value.length; i < t; i++) { - transaction.urls.push(Url.fromJSON(value[i])); - } - - break; - case 'capture': - transaction.capture = Capture.fromJSON(value); - - break; - case 'authorization': - transaction.authorization = Authorization.fromJSON(value); - - break; - case 'additional': - transaction.additional = Additional.fromJSON(value); - - break; - case 'threeDSecure': - transaction.threeDSecure = ThreeDSecure.fromJSON(value); - - break; - case 'antifraud': - transaction.antifraud = Antifraud.fromJSON(value); - - break; - case 'requestDateTime': - case 'dateTime': - case 'refundDateTime': - transaction[property] = new Date(value); - break; - default: - transaction[property] = value; - } - } - - return transaction; - } - - /** - * - * @param gateway - * @param module - * @returns {module.Transaction} - */ - setAdditional(gateway, module) { - this.additional = new Additional(gateway, module); - - return this; - } - - /** - * - * @param url - * @param kind - * @returns {module.Transaction} - */ - addUrl(url, kind = Url.CALLBACK) { - if (this.urls === undefined) { - this.urls = []; - } - - this.urls.push(new Url(url, kind)); - - return this; - } - - /** - * - * @param environment - * @returns {module.Transaction} - */ - setAntifraud(environment) { - let cart = Cart(); - cart.environment = environment; - - this.antifraudRequired = true; - this.cart = cart; - - return this; - } - - /** - * - * @param cardNumber - * @param securityCode - * @param expirationMonth - * @param expirationYear - * @param cardHolderName - * @returns {module.Transaction} - */ - creditCard(cardNumber, securityCode, expirationMonth, expirationYear, cardHolderName) { - return this.card(cardNumber, securityCode, expirationMonth, expirationYear, cardHolderName, Transaction.CREDIT); - } - - /** - * - * @param cardNumber - * @param securityCode - * @param expirationMonth - * @param expirationYear - * @param cardHolderName - * @returns {module.Transaction} - */ - debitCard(cardNumber, securityCode, expirationMonth, expirationYear, cardHolderName) { - return this.card(cardNumber, securityCode, expirationMonth, expirationYear, cardHolderName, Transaction.DEBIT); - } - - /** - * - * @param cardNumber - * @param securityCode - * @param expirationMonth - * @param expirationYear - * @param cardHolderName - * @param kind - * @returns {module.Transaction} - */ - card(cardNumber, securityCode, expirationMonth, expirationYear, cardHolderName, kind) { - this.cardNumber = cardNumber; - this.securityCode = securityCode; - this.expirationMonth = expirationMonth; - this.expirationYear = expirationYear; - this.cardHolderName = cardHolderName; - this.kind = kind; - - return this; - } - - /** - * - * @param capture - * @returns {module.Transaction} - */ - autoCapture(capture = true) { - if (!capture && this.kind === Transaction.DEBIT) { - throw new Error("Debit transactions will always be captured"); - } - - this.capture = capture; - - return this; - } - - /** - * - * @param code - * @param departureTax - * @returns {module.Transaction} - */ - setIata(code, departureTax) { - this.iata = new Iata(code, departureTax); - - return this; - } - - /** - * - * @param softDescriptor - * @param paymentFacilitatorID - * @param subMerchant - * @returns {module.Transaction} - */ - setMcc(softDescriptor, paymentFacilitatorID, subMerchant) { - this.softDescriptor = softDescriptor; - this.paymentFacilitatorID = paymentFacilitatorID; - this.subMerchant = subMerchant; - - return this; - } - - /** - * - * @param onFailure - * @param embed - * @param directoryServerTransactionId - * @param threeDIndicator - * @returns {module.Transaction} - */ - setThreeDSecure(onFailure = ThreeDSecure.DECLINE_ON_FAILURE, embed = true, directoryServerTransactionId = "", threeDIndicator = "1") { - this.threeDSecure = new ThreeDSecure(); - this.threeDSecure.onFailure = onFailure; - this.threeDSecure.embedded = embed; - this.threeDSecure.threeDIndicator = threeDIndicator; - this.threeDSecure.DirectoryServerTransactionId = directoryServerTransactionId; - - return this; - } -}; +"use strict"; + +const Cart = require("./cart"); +const Url = require("./url"); +const Iata = require("./iata"); +const ThreeDSecure = require("./threedsecure"); +const Capture = require("./capture"); +const Authorization = require("./authorization"); +const Additional = require("./additional"); +const Antifraud = require("./antifraud"); +const Refund = require('./refund'); + +module.exports = class Transaction { + constructor(amount, reference, installments) { + if (amount !== undefined) { + this.amount = amount; + } + + if (reference !== undefined) { + this.reference = reference; + } + + if (installments !== undefined) { + this.installments = installments; + } + } + + static get CREDIT() { + return "credit"; + } + + static get DEBIT() { + return "debit"; + } + + static get ORIGIN_EREDE() { + return 1; + } + + static get ORIGIN_VISA_CHECKOUT() { + return 4; + } + + static get ORIGIN_MASTERPASS() { + return 6; + } + + static fromJSON(json) { + let transaction = new Transaction(json.amount, json.reference); + + for (let property in json) { + if (!json.hasOwnProperty(property)) { + continue; + } + + let value = json[property]; + let i = 0; + let t = 0; + + switch (property) { + case 'refunds': + transaction.refunds = []; + + for (i = 0, t = value.length; i < t; i++) { + transaction.refunds.push(Refund.fromJSON(value[i])); + } + + break; + case 'urls': + transaction.urls = []; + + for (i = 0, t = value.length; i < t; i++) { + transaction.urls.push(Url.fromJSON(value[i])); + } + + break; + case 'capture': + transaction.capture = Capture.fromJSON(value); + + break; + case 'authorization': + transaction.authorization = Authorization.fromJSON(value); + + break; + case 'additional': + transaction.additional = Additional.fromJSON(value); + + break; + case 'threeDSecure': + transaction.threeDSecure = ThreeDSecure.fromJSON(value); + + break; + case 'antifraud': + transaction.antifraud = Antifraud.fromJSON(value); + + break; + case 'requestDateTime': + case 'dateTime': + case 'refundDateTime': + transaction[property] = new Date(value); + break; + default: + transaction[property] = value; + } + } + + return transaction; + } + + /** + * + * @param gateway + * @param module + * @returns {module.Transaction} + */ + setAdditional(gateway, module) { + this.additional = new Additional(gateway, module); + + return this; + } + + /** + * + * @param url + * @param kind + * @returns {module.Transaction} + */ + addUrl(url, kind = Url.CALLBACK) { + if (this.urls === undefined) { + this.urls = []; + } + + this.urls.push(new Url(url, kind)); + + return this; + } + + /** + * + * @param environment + * @returns {module.Transaction} + */ + setAntifraud(environment) { + let cart = Cart(); + cart.environment = environment; + + this.antifraudRequired = true; + this.cart = cart; + + return this; + } + + /** + * + * @param cardNumber + * @param securityCode + * @param expirationMonth + * @param expirationYear + * @param cardHolderName + * @returns {module.Transaction} + */ + creditCard(cardNumber, securityCode, expirationMonth, expirationYear, cardHolderName) { + return this.card(cardNumber, securityCode, expirationMonth, expirationYear, cardHolderName, Transaction.CREDIT); + } + + /** + * + * @param cardNumber + * @param securityCode + * @param expirationMonth + * @param expirationYear + * @param cardHolderName + * @returns {module.Transaction} + */ + debitCard(cardNumber, securityCode, expirationMonth, expirationYear, cardHolderName) { + return this.card(cardNumber, securityCode, expirationMonth, expirationYear, cardHolderName, Transaction.DEBIT); + } + + /** + * + * @param cardNumber + * @param securityCode + * @param expirationMonth + * @param expirationYear + * @param cardHolderName + * @param kind + * @returns {module.Transaction} + */ + card(cardNumber, securityCode, expirationMonth, expirationYear, cardHolderName, kind) { + this.cardNumber = cardNumber; + this.securityCode = securityCode; + this.expirationMonth = expirationMonth; + this.expirationYear = expirationYear; + this.cardHolderName = cardHolderName; + this.kind = kind; + + return this; + } + + /** + * + * @param capture + * @returns {module.Transaction} + */ + autoCapture(capture = true) { + if (!capture && this.kind === Transaction.DEBIT) { + throw new Error("Debit transactions will always be captured"); + } + + this.capture = capture; + + return this; + } + + /** + * + * @param code + * @param departureTax + * @returns {module.Transaction} + */ + setIata(code, departureTax) { + this.iata = new Iata(code, departureTax); + + return this; + } + + /** + * + * @param softDescriptor + * @param paymentFacilitatorID + * @param subMerchant + * @returns {module.Transaction} + */ + setMcc(softDescriptor, paymentFacilitatorID, subMerchant) { + this.softDescriptor = softDescriptor; + this.paymentFacilitatorID = paymentFacilitatorID; + this.subMerchant = subMerchant; + + return this; + } + + /** + * + * @param onFailure + * @param embed + * @param directoryServerTransactionId + * @param threeDIndicator + * @returns {module.Transaction} + */ + setThreeDSecure(onFailure = ThreeDSecure.DECLINE_ON_FAILURE, embed = true, directoryServerTransactionId = "", threeDIndicator = "1") { + this.threeDSecure = new ThreeDSecure(); + this.threeDSecure.onFailure = onFailure; + this.threeDSecure.embedded = embed; + this.threeDSecure.threeDIndicator = threeDIndicator; + this.threeDSecure.DirectoryServerTransactionId = directoryServerTransactionId; + + return this; + } +}; diff --git a/lib/url.js b/lib/url.js index 614f5bf..13a6907 100644 --- a/lib/url.js +++ b/lib/url.js @@ -1,36 +1,36 @@ -"use strict"; - -const CALLBACK = "callback"; -const THREE_D_SECURE_FAILURE = "threeDSecureFailure"; -const THREE_D_SECURE_SUCCESS = "threeDSecureSuccess"; - -module.exports = class Url { - constructor(url, kind = Url.CALLBACK) { - this.url = url; - this.kind = kind; - } - - static get CALLBACK() { - return CALLBACK; - } - - static get THREE_D_SECURE_FAILURE() { - return THREE_D_SECURE_FAILURE; - } - - static get THREE_D_SECURE_SUCCESS() { - return THREE_D_SECURE_SUCCESS; - } - - static fromJSON(json) { - let url = new Url(); - - for (let property in json) { - if (json.hasOwnProperty(property)) { - url[property] = json[property]; - } - } - - return url; - } +"use strict"; + +const CALLBACK = "callback"; +const THREE_D_SECURE_FAILURE = "threeDSecureFailure"; +const THREE_D_SECURE_SUCCESS = "threeDSecureSuccess"; + +module.exports = class Url { + constructor(url, kind = Url.CALLBACK) { + this.url = url; + this.kind = kind; + } + + static get CALLBACK() { + return CALLBACK; + } + + static get THREE_D_SECURE_FAILURE() { + return THREE_D_SECURE_FAILURE; + } + + static get THREE_D_SECURE_SUCCESS() { + return THREE_D_SECURE_SUCCESS; + } + + static fromJSON(json) { + let url = new Url(); + + for (let property in json) { + if (json.hasOwnProperty(property)) { + url[property] = json[property]; + } + } + + return url; + } }; \ No newline at end of file diff --git a/package.json b/package.json index bb01758..2edf9cd 100644 --- a/package.json +++ b/package.json @@ -1,35 +1,41 @@ -{ - "author": "João Batista Neto", - "name": "erede-node", - "description": "SDK Node.JS Rede", - "version": "1.0.0", - "homepage": "https://github.com/DevelopersRede/erede-node", - "keywords": [ - "rede", - "erede", - "sdk" - ], - "repository": { - "type": "git", - "url": "https://github.com/DevelopersRede/erede-node.git" - }, - "bugs": { - "url": "https://github.com/DevelopersRede/erede-node/issues" - }, - "license": "MIT", - "engines": { - "node": ">= v0.6.0" - }, - "main": "./lib/erede.js", - "dependencies": { - "semver": "^5.0.3", - "bl": "^3.0.0" - }, - "config": { - "blanket": { - "pattern": "lib", - "data-cover-never": "node_modules" - } - }, - "readmeFilename": "README.md" -} +{ + "author": "João Batista Neto", + "name": "erede-node", + "description": "SDK Node.JS Rede", + "version": "1.0.0", + "homepage": "https://github.com/DevelopersRede/erede-node", + "keywords": [ + "rede", + "erede", + "sdk" + ], + "repository": { + "type": "git", + "url": "https://github.com/DevelopersRede/erede-node.git" + }, + "bugs": { + "url": "https://github.com/DevelopersRede/erede-node/issues" + }, + "license": "MIT", + "engines": { + "node": ">= v0.6.0" + }, + "main": "./lib/erede.js", + "dependencies": { + "semver": "^5.0.3", + "bl": "^3.0.0" + }, + "devDependencies": { + "eslint": "^5.16.0", + "eslint-config-airbnb-base": "^13.2.0", + "eslint-plugin-import": "^2.18.2", + "jest": "^24.9.0" + }, + "config": { + "blanket": { + "pattern": "lib", + "data-cover-never": "node_modules" + } + }, + "readmeFilename": "README.md" +} From 6b3bfef02abb4cc1b62b7ea5b6f8e6bf53a7e0c7 Mon Sep 17 00:00:00 2001 From: igorp Date: Tue, 8 Oct 2019 21:12:58 -0300 Subject: [PATCH 02/11] Refactoring warning --- README.md | 4 ++ lib/erede.js | 140 +++++++++++++++++++++++++-------------------------- package.json | 10 ++-- 3 files changed, 79 insertions(+), 75 deletions(-) diff --git a/README.md b/README.md index 42c14b1..47bc160 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,7 @@ +# Pacote com refactoring em progresso, não recomendado para ambiente de produção + +# Futuramente pretendo fazer pull request no repositório oficial do erede + # SDK Node.js SDK de integração eRede diff --git a/lib/erede.js b/lib/erede.js index bf3fb26..b086a26 100644 --- a/lib/erede.js +++ b/lib/erede.js @@ -1,70 +1,70 @@ - - -const CancelTransactionService = require('./service/CancelTransactionService'); -const CaptureTransactionService = require('./service/CaptureTransactionService'); -const CreateTransactionService = require('./service/CreateTransactionService'); -const GetTransactionService = require('./service/GetTransactionService'); - -module.exports = class eRede { - constructor(store) { - this.store = store; - } - - async create(transaction) { - const service = new CreateTransactionService(this.store, transaction); - - return service.execute(); - } - - cancel(transaction) { - const service = new CancelTransactionService(this.store, transaction); - - return service.execute(); - } - - capture(transaction) { - const service = new CaptureTransactionService(this.store, transaction); - - return service.execute(); - } - - getByTid(tid) { - const service = new GetTransactionService(this.store); - - service.tid = tid; - - return service.execute(); - } - - getByReference(reference) { - const service = new GetTransactionService(this.store); - - service.reference = reference; - - return service.execute(); - } - - getRefunds(tid) { - const service = new GetTransactionService(this.store); - - service.tid = tid; - service.refunds = true; - - return service.execute(); - } - - zero(transaction) { - const { amount } = transaction; - const { capture } = transaction; - - transaction.amount = 0; - transaction.capture = true; - - transaction = this.create(transaction); - - transaction.amount = amount; - transaction.capture = capture; - - return transaction; - } -}; + + +const CancelTransactionService = require('./service/CancelTransactionService'); +const CaptureTransactionService = require('./service/CaptureTransactionService'); +const CreateTransactionService = require('./service/CreateTransactionService'); +const GetTransactionService = require('./service/GetTransactionService'); + +module.exports = class eRede { + constructor(store) { + this.store = store; + } + + async create(transaction) { + const service = new CreateTransactionService(this.store, transaction); + + return service.execute(); + } + + cancel(transaction) { + const service = new CancelTransactionService(this.store, transaction); + + return service.execute(); + } + + capture(transaction) { + const service = new CaptureTransactionService(this.store, transaction); + + return service.execute(); + } + + getByTid(tid) { + const service = new GetTransactionService(this.store); + + service.tid = tid; + + return service.execute(); + } + + getByReference(reference) { + const service = new GetTransactionService(this.store); + + service.reference = reference; + + return service.execute(); + } + + getRefunds(tid) { + const service = new GetTransactionService(this.store); + + service.tid = tid; + service.refunds = true; + + return service.execute(); + } + + zero(transaction) { + const { amount } = transaction; + const { capture } = transaction; + + transaction.amount = 0; + transaction.capture = true; + + transaction = this.create(transaction); + + transaction.amount = amount; + transaction.capture = capture; + + return transaction; + } +}; diff --git a/package.json b/package.json index 2edf9cd..faa2f36 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,9 @@ { - "author": "João Batista Neto", + "author": "Igor Pereira da Silva", "name": "erede-node", "description": "SDK Node.JS Rede", - "version": "1.0.0", - "homepage": "https://github.com/DevelopersRede/erede-node", + "version": "0.0.1", + "homepage": "https://github.com/igorpdasilvaa/erede-node", "keywords": [ "rede", "erede", @@ -11,10 +11,10 @@ ], "repository": { "type": "git", - "url": "https://github.com/DevelopersRede/erede-node.git" + "url": "https://github.com/igorpdasilvaa/erede-node.git" }, "bugs": { - "url": "https://github.com/DevelopersRede/erede-node/issues" + "url": "https://github.com/igorpdasilvaa/erede-node/issues" }, "license": "MIT", "engines": { From f46a9c54278a7edd83467bbeb8d29f18bbaacd89 Mon Sep 17 00:00:00 2001 From: igorp Date: Tue, 8 Oct 2019 21:17:34 -0300 Subject: [PATCH 03/11] changed folders structure --- {lib => src}/additional.js | 0 {lib => src}/address.js | 0 {lib => src}/antifraud.js | 0 {lib => src}/authorization.js | 0 {lib => src}/capture.js | 0 {lib => src}/cart.js | 0 {lib => src}/consumer.js | 0 {lib => src}/environment.js | 0 {lib => src}/erede.js | 0 {lib => src}/exception/RedeError.js | 0 {lib => src}/flight.js | 0 {lib => src}/iata.js | 0 {lib => src}/item.js | 0 {lib => src}/passenger.js | 0 {lib => src}/phone.js | 0 {lib => src}/refund.js | 0 {lib => src}/service/CancelTransactionService.js | 0 {lib => src}/service/CaptureTransactionService.js | 0 {lib => src}/service/CreateTransactionService.js | 0 {lib => src}/service/GetTransactionService.js | 0 {lib => src}/service/TransactionService.js | 0 {lib => src}/store.js | 0 {lib => src}/submerchant.js | 0 {lib => src}/threedsecure.js | 0 {lib => src}/transaction.js | 0 {lib => src}/url.js | 0 26 files changed, 0 insertions(+), 0 deletions(-) rename {lib => src}/additional.js (100%) rename {lib => src}/address.js (100%) rename {lib => src}/antifraud.js (100%) rename {lib => src}/authorization.js (100%) rename {lib => src}/capture.js (100%) rename {lib => src}/cart.js (100%) rename {lib => src}/consumer.js (100%) rename {lib => src}/environment.js (100%) rename {lib => src}/erede.js (100%) rename {lib => src}/exception/RedeError.js (100%) rename {lib => src}/flight.js (100%) rename {lib => src}/iata.js (100%) rename {lib => src}/item.js (100%) rename {lib => src}/passenger.js (100%) rename {lib => src}/phone.js (100%) rename {lib => src}/refund.js (100%) rename {lib => src}/service/CancelTransactionService.js (100%) rename {lib => src}/service/CaptureTransactionService.js (100%) rename {lib => src}/service/CreateTransactionService.js (100%) rename {lib => src}/service/GetTransactionService.js (100%) rename {lib => src}/service/TransactionService.js (100%) rename {lib => src}/store.js (100%) rename {lib => src}/submerchant.js (100%) rename {lib => src}/threedsecure.js (100%) rename {lib => src}/transaction.js (100%) rename {lib => src}/url.js (100%) diff --git a/lib/additional.js b/src/additional.js similarity index 100% rename from lib/additional.js rename to src/additional.js diff --git a/lib/address.js b/src/address.js similarity index 100% rename from lib/address.js rename to src/address.js diff --git a/lib/antifraud.js b/src/antifraud.js similarity index 100% rename from lib/antifraud.js rename to src/antifraud.js diff --git a/lib/authorization.js b/src/authorization.js similarity index 100% rename from lib/authorization.js rename to src/authorization.js diff --git a/lib/capture.js b/src/capture.js similarity index 100% rename from lib/capture.js rename to src/capture.js diff --git a/lib/cart.js b/src/cart.js similarity index 100% rename from lib/cart.js rename to src/cart.js diff --git a/lib/consumer.js b/src/consumer.js similarity index 100% rename from lib/consumer.js rename to src/consumer.js diff --git a/lib/environment.js b/src/environment.js similarity index 100% rename from lib/environment.js rename to src/environment.js diff --git a/lib/erede.js b/src/erede.js similarity index 100% rename from lib/erede.js rename to src/erede.js diff --git a/lib/exception/RedeError.js b/src/exception/RedeError.js similarity index 100% rename from lib/exception/RedeError.js rename to src/exception/RedeError.js diff --git a/lib/flight.js b/src/flight.js similarity index 100% rename from lib/flight.js rename to src/flight.js diff --git a/lib/iata.js b/src/iata.js similarity index 100% rename from lib/iata.js rename to src/iata.js diff --git a/lib/item.js b/src/item.js similarity index 100% rename from lib/item.js rename to src/item.js diff --git a/lib/passenger.js b/src/passenger.js similarity index 100% rename from lib/passenger.js rename to src/passenger.js diff --git a/lib/phone.js b/src/phone.js similarity index 100% rename from lib/phone.js rename to src/phone.js diff --git a/lib/refund.js b/src/refund.js similarity index 100% rename from lib/refund.js rename to src/refund.js diff --git a/lib/service/CancelTransactionService.js b/src/service/CancelTransactionService.js similarity index 100% rename from lib/service/CancelTransactionService.js rename to src/service/CancelTransactionService.js diff --git a/lib/service/CaptureTransactionService.js b/src/service/CaptureTransactionService.js similarity index 100% rename from lib/service/CaptureTransactionService.js rename to src/service/CaptureTransactionService.js diff --git a/lib/service/CreateTransactionService.js b/src/service/CreateTransactionService.js similarity index 100% rename from lib/service/CreateTransactionService.js rename to src/service/CreateTransactionService.js diff --git a/lib/service/GetTransactionService.js b/src/service/GetTransactionService.js similarity index 100% rename from lib/service/GetTransactionService.js rename to src/service/GetTransactionService.js diff --git a/lib/service/TransactionService.js b/src/service/TransactionService.js similarity index 100% rename from lib/service/TransactionService.js rename to src/service/TransactionService.js diff --git a/lib/store.js b/src/store.js similarity index 100% rename from lib/store.js rename to src/store.js diff --git a/lib/submerchant.js b/src/submerchant.js similarity index 100% rename from lib/submerchant.js rename to src/submerchant.js diff --git a/lib/threedsecure.js b/src/threedsecure.js similarity index 100% rename from lib/threedsecure.js rename to src/threedsecure.js diff --git a/lib/transaction.js b/src/transaction.js similarity index 100% rename from lib/transaction.js rename to src/transaction.js diff --git a/lib/url.js b/src/url.js similarity index 100% rename from lib/url.js rename to src/url.js From 1ba90a2b84ca813836bcd0a70831f9c23b9f02cd Mon Sep 17 00:00:00 2001 From: igorp Date: Tue, 8 Oct 2019 21:18:30 -0300 Subject: [PATCH 04/11] Updated to match main with the new folders structure --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index faa2f36..65e6a6c 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "engines": { "node": ">= v0.6.0" }, - "main": "./lib/erede.js", + "main": "./src/erede.js", "dependencies": { "semver": "^5.0.3", "bl": "^3.0.0" From 39ced70feca135dab53a769c64c6856b753a06c4 Mon Sep 17 00:00:00 2001 From: igorp Date: Tue, 8 Oct 2019 21:43:21 -0300 Subject: [PATCH 05/11] Refactoring for eslint --- src/store.js | 20 +- src/transaction.js | 520 +++++++++++++++++++++++---------------------- 2 files changed, 270 insertions(+), 270 deletions(-) diff --git a/src/store.js b/src/store.js index 29e4d09..0bce53b 100644 --- a/src/store.js +++ b/src/store.js @@ -1,11 +1,9 @@ -"use strict"; - -const Environment = require("./environment.js"); - -module.exports = class Store { - constructor(token, filiation, environment = Environment.production()) { - this.token = token; - this.filiation = filiation; - this.environment = environment; - } -}; \ No newline at end of file +const Environment = require('./environment.js'); + +module.exports = class Store { + constructor(token, filiation, environment = Environment.production()) { + this.token = token; + this.filiation = filiation; + this.environment = environment; + } +}; diff --git a/src/transaction.js b/src/transaction.js index 803063c..e62b110 100644 --- a/src/transaction.js +++ b/src/transaction.js @@ -1,259 +1,261 @@ -"use strict"; - -const Cart = require("./cart"); -const Url = require("./url"); -const Iata = require("./iata"); -const ThreeDSecure = require("./threedsecure"); -const Capture = require("./capture"); -const Authorization = require("./authorization"); -const Additional = require("./additional"); -const Antifraud = require("./antifraud"); -const Refund = require('./refund'); - -module.exports = class Transaction { - constructor(amount, reference, installments) { - if (amount !== undefined) { - this.amount = amount; - } - - if (reference !== undefined) { - this.reference = reference; - } - - if (installments !== undefined) { - this.installments = installments; - } - } - - static get CREDIT() { - return "credit"; - } - - static get DEBIT() { - return "debit"; - } - - static get ORIGIN_EREDE() { - return 1; - } - - static get ORIGIN_VISA_CHECKOUT() { - return 4; - } - - static get ORIGIN_MASTERPASS() { - return 6; - } - - static fromJSON(json) { - let transaction = new Transaction(json.amount, json.reference); - - for (let property in json) { - if (!json.hasOwnProperty(property)) { - continue; - } - - let value = json[property]; - let i = 0; - let t = 0; - - switch (property) { - case 'refunds': - transaction.refunds = []; - - for (i = 0, t = value.length; i < t; i++) { - transaction.refunds.push(Refund.fromJSON(value[i])); - } - - break; - case 'urls': - transaction.urls = []; - - for (i = 0, t = value.length; i < t; i++) { - transaction.urls.push(Url.fromJSON(value[i])); - } - - break; - case 'capture': - transaction.capture = Capture.fromJSON(value); - - break; - case 'authorization': - transaction.authorization = Authorization.fromJSON(value); - - break; - case 'additional': - transaction.additional = Additional.fromJSON(value); - - break; - case 'threeDSecure': - transaction.threeDSecure = ThreeDSecure.fromJSON(value); - - break; - case 'antifraud': - transaction.antifraud = Antifraud.fromJSON(value); - - break; - case 'requestDateTime': - case 'dateTime': - case 'refundDateTime': - transaction[property] = new Date(value); - break; - default: - transaction[property] = value; - } - } - - return transaction; - } - - /** - * - * @param gateway - * @param module - * @returns {module.Transaction} - */ - setAdditional(gateway, module) { - this.additional = new Additional(gateway, module); - - return this; - } - - /** - * - * @param url - * @param kind - * @returns {module.Transaction} - */ - addUrl(url, kind = Url.CALLBACK) { - if (this.urls === undefined) { - this.urls = []; - } - - this.urls.push(new Url(url, kind)); - - return this; - } - - /** - * - * @param environment - * @returns {module.Transaction} - */ - setAntifraud(environment) { - let cart = Cart(); - cart.environment = environment; - - this.antifraudRequired = true; - this.cart = cart; - - return this; - } - - /** - * - * @param cardNumber - * @param securityCode - * @param expirationMonth - * @param expirationYear - * @param cardHolderName - * @returns {module.Transaction} - */ - creditCard(cardNumber, securityCode, expirationMonth, expirationYear, cardHolderName) { - return this.card(cardNumber, securityCode, expirationMonth, expirationYear, cardHolderName, Transaction.CREDIT); - } - - /** - * - * @param cardNumber - * @param securityCode - * @param expirationMonth - * @param expirationYear - * @param cardHolderName - * @returns {module.Transaction} - */ - debitCard(cardNumber, securityCode, expirationMonth, expirationYear, cardHolderName) { - return this.card(cardNumber, securityCode, expirationMonth, expirationYear, cardHolderName, Transaction.DEBIT); - } - - /** - * - * @param cardNumber - * @param securityCode - * @param expirationMonth - * @param expirationYear - * @param cardHolderName - * @param kind - * @returns {module.Transaction} - */ - card(cardNumber, securityCode, expirationMonth, expirationYear, cardHolderName, kind) { - this.cardNumber = cardNumber; - this.securityCode = securityCode; - this.expirationMonth = expirationMonth; - this.expirationYear = expirationYear; - this.cardHolderName = cardHolderName; - this.kind = kind; - - return this; - } - - /** - * - * @param capture - * @returns {module.Transaction} - */ - autoCapture(capture = true) { - if (!capture && this.kind === Transaction.DEBIT) { - throw new Error("Debit transactions will always be captured"); - } - - this.capture = capture; - - return this; - } - - /** - * - * @param code - * @param departureTax - * @returns {module.Transaction} - */ - setIata(code, departureTax) { - this.iata = new Iata(code, departureTax); - - return this; - } - - /** - * - * @param softDescriptor - * @param paymentFacilitatorID - * @param subMerchant - * @returns {module.Transaction} - */ - setMcc(softDescriptor, paymentFacilitatorID, subMerchant) { - this.softDescriptor = softDescriptor; - this.paymentFacilitatorID = paymentFacilitatorID; - this.subMerchant = subMerchant; - - return this; - } - - /** - * - * @param onFailure - * @param embed - * @param directoryServerTransactionId - * @param threeDIndicator - * @returns {module.Transaction} - */ - setThreeDSecure(onFailure = ThreeDSecure.DECLINE_ON_FAILURE, embed = true, directoryServerTransactionId = "", threeDIndicator = "1") { - this.threeDSecure = new ThreeDSecure(); - this.threeDSecure.onFailure = onFailure; - this.threeDSecure.embedded = embed; - this.threeDSecure.threeDIndicator = threeDIndicator; - this.threeDSecure.DirectoryServerTransactionId = directoryServerTransactionId; - - return this; - } -}; + + +const Cart = require('./cart'); +const Url = require('./url'); +const Iata = require('./iata'); +const ThreeDSecure = require('./threedsecure'); +const Capture = require('./capture'); +const Authorization = require('./authorization'); +const Additional = require('./additional'); +const Antifraud = require('./antifraud'); +const Refund = require('./refund'); + +module.exports = class Transaction { + constructor(amount, reference, installments) { + if (amount !== undefined) { + this.amount = amount; + } + + if (reference !== undefined) { + this.reference = reference; + } + + if (installments !== undefined) { + this.installments = installments; + } + } + + static get CREDIT() { + return 'credit'; + } + + static get DEBIT() { + return 'debit'; + } + + static get ORIGIN_EREDE() { + return 1; + } + + static get ORIGIN_VISA_CHECKOUT() { + return 4; + } + + static get ORIGIN_MASTERPASS() { + return 6; + } + + static fromJSON(json) { + const transaction = new Transaction(json.amount, json.reference); + const keys = json.keys(); + + for (let keyIndex = keys.length; keyIndex >= 0; keyIndex -= 1) { + const property = keys[keyIndex]; + const value = json[property]; + let i = 0; + let t = 0; + + switch (property) { + case 'refunds': + transaction.refunds = []; + + for (i = 0, t = value.length; i < t; i += 1) { + transaction.refunds.push(Refund.fromJSON(value[i])); + } + + break; + case 'urls': + transaction.urls = []; + + for (i = 0, t = value.length; i < t; i += 1) { + transaction.urls.push(Url.fromJSON(value[i])); + } + + break; + case 'capture': + transaction.capture = Capture.fromJSON(value); + + break; + case 'authorization': + transaction.authorization = Authorization.fromJSON(value); + + break; + case 'additional': + transaction.additional = Additional.fromJSON(value); + + break; + case 'threeDSecure': + transaction.threeDSecure = ThreeDSecure.fromJSON(value); + + break; + case 'antifraud': + transaction.antifraud = Antifraud.fromJSON(value); + + break; + case 'requestDateTime': + case 'dateTime': + case 'refundDateTime': + transaction[property] = new Date(value); + break; + default: + transaction[property] = value; + } + } + + return transaction; + } + + /** + * + * @param gateway + * @param module + * @returns {module.Transaction} + */ + setAdditional(gateway, module) { + this.additional = new Additional(gateway, module); + + return this; + } + + /** + * + * @param url + * @param kind + * @returns {module.Transaction} + */ + addUrl(url, kind = Url.CALLBACK) { + if (this.urls === undefined) { + this.urls = []; + } + + this.urls.push(new Url(url, kind)); + + return this; + } + + /** + * + * @param environment + * @returns {module.Transaction} + */ + setAntifraud(environment) { + const cart = Cart(); + cart.environment = environment; + + this.antifraudRequired = true; + this.cart = cart; + + return this; + } + + /** + * + * @param cardNumber + * @param securityCode + * @param expirationMonth + * @param expirationYear + * @param cardHolderName + * @returns {module.Transaction} + */ + creditCard(cardNumber, securityCode, expirationMonth, expirationYear, cardHolderName) { + return this.card( + cardNumber, securityCode, expirationMonth, expirationYear, cardHolderName, Transaction.CREDIT, + ); + } + + /** + * + * @param cardNumber + * @param securityCode + * @param expirationMonth + * @param expirationYear + * @param cardHolderName + * @returns {module.Transaction} + */ + debitCard(cardNumber, securityCode, expirationMonth, expirationYear, cardHolderName) { + return this.card( + cardNumber, securityCode, expirationMonth, expirationYear, cardHolderName, Transaction.DEBIT, + ); + } + + /** + * + * @param cardNumber + * @param securityCode + * @param expirationMonth + * @param expirationYear + * @param cardHolderName + * @param kind + * @returns {module.Transaction} + */ + card(cardNumber, securityCode, expirationMonth, expirationYear, cardHolderName, kind) { + this.cardNumber = cardNumber; + this.securityCode = securityCode; + this.expirationMonth = expirationMonth; + this.expirationYear = expirationYear; + this.cardHolderName = cardHolderName; + this.kind = kind; + + return this; + } + + /** + * + * @param capture + * @returns {module.Transaction} + */ + autoCapture(capture = true) { + if (!capture && this.kind === Transaction.DEBIT) { + throw new Error('Debit transactions will always be captured'); + } + + this.capture = capture; + + return this; + } + + /** + * + * @param code + * @param departureTax + * @returns {module.Transaction} + */ + setIata(code, departureTax) { + this.iata = new Iata(code, departureTax); + + return this; + } + + /** + * + * @param softDescriptor + * @param paymentFacilitatorID + * @param subMerchant + * @returns {module.Transaction} + */ + setMcc(softDescriptor, paymentFacilitatorID, subMerchant) { + this.softDescriptor = softDescriptor; + this.paymentFacilitatorID = paymentFacilitatorID; + this.subMerchant = subMerchant; + + return this; + } + + /** + * + * @param onFailure + * @param embed + * @param directoryServerTransactionId + * @param threeDIndicator + * @returns {module.Transaction} + */ + setThreeDSecure(onFailure = ThreeDSecure.DECLINE_ON_FAILURE, embed = true, directoryServerTransactionId = '', threeDIndicator = '1') { + this.threeDSecure = new ThreeDSecure(); + this.threeDSecure.onFailure = onFailure; + this.threeDSecure.embedded = embed; + this.threeDSecure.threeDIndicator = threeDIndicator; + this.threeDSecure.DirectoryServerTransactionId = directoryServerTransactionId; + + return this; + } +}; From 8475978503bb557d4333df4e52d766c59551d091 Mon Sep 17 00:00:00 2001 From: igorp Date: Sun, 13 Oct 2019 19:11:54 -0300 Subject: [PATCH 06/11] fixed wrong loop on fromJson --- src/transaction.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/transaction.js b/src/transaction.js index e62b110..7a94582 100644 --- a/src/transaction.js +++ b/src/transaction.js @@ -47,9 +47,9 @@ module.exports = class Transaction { static fromJSON(json) { const transaction = new Transaction(json.amount, json.reference); - const keys = json.keys(); + const keys = Object.keys(json); - for (let keyIndex = keys.length; keyIndex >= 0; keyIndex -= 1) { + for (let keyIndex = keys.length - 1; keyIndex >= 0; keyIndex -= 1) { const property = keys[keyIndex]; const value = json[property]; let i = 0; From b2b1874a8da389c091ea2e44b5cf8a4b8728734f Mon Sep 17 00:00:00 2001 From: igorp Date: Wed, 23 Oct 2019 21:42:16 -0300 Subject: [PATCH 07/11] eslint --- src/additional.js | 39 +++--- src/address.js | 62 ++++----- src/antifraud.js | 38 +++--- src/authorization.js | 42 +++--- src/capture.js | 43 ++++--- src/cart.js | 88 ++++++------- src/consumer.js | 70 +++++----- src/environment.js | 42 +++--- src/erede.js | 17 ++- src/exception/RedeError.js | 20 +-- src/flight.js | 42 +++--- src/iata.js | 16 +-- src/item.js | 50 ++++---- src/passenger.js | 18 +-- src/phone.js | 50 ++++---- src/refund.js | 41 +++--- src/service/CancelTransactionService.js | 38 +++--- src/service/CaptureTransactionService.js | 38 +++--- src/service/CreateTransactionService.js | 30 ++--- src/service/GetTransactionService.js | 62 ++++----- src/service/TransactionService.js | 156 +++++++++++------------ src/submerchant.js | 18 +-- src/threedsecure.js | 55 ++++---- src/url.js | 71 +++++------ 24 files changed, 570 insertions(+), 576 deletions(-) diff --git a/src/additional.js b/src/additional.js index fa49c0f..8c4f1a3 100644 --- a/src/additional.js +++ b/src/additional.js @@ -1,20 +1,19 @@ -"use strict"; - -module.exports = class Additional { - constructor(gateway, module) { - this.gateway = gateway; - this.module = module; - } - - static fromJSON(json) { - let additional = new Additional(); - - for (let property in json) { - if (json.hasOwnProperty(property)) { - additional[property] = json[property]; - } - } - - return additional; - } -}; \ No newline at end of file + + +module.exports = class Additional { + constructor(gateway, module) { + this.gateway = gateway; + this.module = module; + } + + static fromJSON(json) { + const additional = new Additional(); + + const jsonKeys = Object.keys(json); + for (let i = jsonKeys.length; i >= 0; i -= 1) { + additional[jsonKeys[i]] = json[jsonKeys[i]]; + } + + return additional; + } +}; diff --git a/src/address.js b/src/address.js index c564f78..78635da 100644 --- a/src/address.js +++ b/src/address.js @@ -1,31 +1,31 @@ -"use strict"; - -module.exports = class Address { - static get BILLING() { - return 1; - } - - static get SHIPPING() { - return 2; - } - - static get BOTH() { - return 3; - } - - static get APARTMENT() { - return 1; - } - - static get HOUSE() { - return 2; - } - - static get COMMERCIAL() { - return 3; - } - - static get OTHER() { - return 4; - } -}; \ No newline at end of file + + +module.exports = class Address { + static get BILLING() { + return 1; + } + + static get SHIPPING() { + return 2; + } + + static get BOTH() { + return 3; + } + + static get APARTMENT() { + return 1; + } + + static get HOUSE() { + return 2; + } + + static get COMMERCIAL() { + return 3; + } + + static get OTHER() { + return 4; + } +}; diff --git a/src/antifraud.js b/src/antifraud.js index b1921eb..e5999b1 100644 --- a/src/antifraud.js +++ b/src/antifraud.js @@ -1,19 +1,19 @@ -"use strict"; - -module.exports = class Antifraud { - constructor() { - this.success = false; - } - - static fromJSON(json) { - let antifraud = new self(); - - for (let property in json) { - if (json.hasOwnProperty(property)) { - antifraud[property] = json[property]; - } - } - - return antifraud; - } -}; \ No newline at end of file + + +module.exports = class Antifraud { + constructor() { + this.success = false; + } + + static fromJSON(json) { + const antifraud = new self(); + + const jsonKeys = Object.keys(json); + for (let i = jsonKeys.length; i >= 0; i -= 1) { + antifraud[jsonKeys[i]] = json[jsonKeys[i]]; + } + + + return antifraud; + } +}; diff --git a/src/authorization.js b/src/authorization.js index 7b4e3e2..759b60c 100644 --- a/src/authorization.js +++ b/src/authorization.js @@ -1,21 +1,21 @@ -"use strict"; - -module.exports = class Authorization { - static fromJSON(json) { - let authorization = new Authorization(); - - for (let property in json) { - if (json.hasOwnProperty(property)) { - let value = json[property]; - - if (property === 'requestDateTime' || property === 'dateTime' || property === 'refundDateTime') { - value = new Date(value); - } - - authorization[property] = value; - } - } - - return authorization; - } -}; \ No newline at end of file + + +module.exports = class Authorization { + static fromJSON(json) { + const authorization = new Authorization(); + + const jsonKeys = Object.keys(json); + for (let i = jsonKeys.length; i >= 0; i -= 1) { + const property = jsonKeys[i]; + let value = json[property]; + if (property === 'requestDateTime' || property === 'dateTime' || property === 'refundDateTime') { + value = new Date(value); + } + + authorization[property] = value; + } + + + return authorization; + } +}; diff --git a/src/capture.js b/src/capture.js index 646650e..27b11cf 100644 --- a/src/capture.js +++ b/src/capture.js @@ -1,21 +1,22 @@ -"use strict"; - -module.exports = class Capture { - static fromJSON(json) { - let capture = new Capture(); - - for (let property in json) { - if (json.hasOwnProperty(property)) { - let value = json[property]; - - if (property === 'requestDateTime' || property === 'dateTime' || property === 'refundDateTime') { - value = new Date(value); - } - - capture[property] = value; - } - } - - return capture; - } -}; \ No newline at end of file + + +module.exports = class Capture { + static fromJSON(json) { + const capture = new Capture(); + + const jsonKeys = Object.keys(json); + for (let i = jsonKeys.length; i >= 0; i -= 1) { + const property = jsonKeys[i]; + let value = json[property]; + + if (property === 'requestDateTime' || property === 'dateTime' || property === 'refundDateTime') { + value = new Date(value); + } + + capture[property] = value; + } + + + return capture; + } +}; diff --git a/src/cart.js b/src/cart.js index b77f7e2..9c13021 100644 --- a/src/cart.js +++ b/src/cart.js @@ -1,44 +1,44 @@ -"use strict"; - -const Address = require("./address"); -const Consumer = require("./consumer"); -const Iata = require("./iata"); - -module.exports = class Cart { - address(type = Address.BOTH) { - let address = Address(); - - if ((type & Address.BILLING) === Address.BILLING) { - this.billing = address; - } - - if ((type & Address.SHIPPING) === Address.SHIPPING) { - this.shipping = [address]; - } - - return address; - } - - addItem(item) { - if (this.items === undefined) { - this.items = []; - } - - this.items.push(item); - - return this; - } - - setConsumer(name, email, cpf) { - this.consumer = new Consumer(name, email, cpf); - - return this.consumer; - } - - setIata(code, departureTax, flight) { - this.iata = new Iata(code, departureTax); - this.iata.flight = flight; - - return this; - } -}; + + +const Address = require('./address'); +const Consumer = require('./consumer'); +const Iata = require('./iata'); + +module.exports = class Cart { + address(type = Address.BOTH) { + const address = Address(); + + if ((type & Address.BILLING) === Address.BILLING) { + this.billing = address; + } + + if ((type & Address.SHIPPING) === Address.SHIPPING) { + this.shipping = [address]; + } + + return address; + } + + addItem(item) { + if (this.items === undefined) { + this.items = []; + } + + this.items.push(item); + + return this; + } + + setConsumer(name, email, cpf) { + this.consumer = new Consumer(name, email, cpf); + + return this.consumer; + } + + setIata(code, departureTax, flight) { + this.iata = new Iata(code, departureTax); + this.iata.flight = flight; + + return this; + } +}; diff --git a/src/consumer.js b/src/consumer.js index 7eeaf75..9174e04 100644 --- a/src/consumer.js +++ b/src/consumer.js @@ -1,35 +1,35 @@ -"use strict"; - -const Phone = require("./phone"); - -module.exports = class Consumer { - constructor(name, email, cpf) { - this.name = name; - this.email = email; - this.cpf = cpf; - } - - static get MALE() { - return "M"; - } - - static get FEMALE() { - return "F"; - } - - addDocument(type, number) { - if (this.documents === undefined) { - this.documents = []; - } - - this.documents.push({type: type, number: number}); - - return this; - } - - setPhone(ddd, number, type = Phone.CELLPHONE) { - this.phone = new Phone(ddd, number, type); - - return this; - } -}; \ No newline at end of file + + +const Phone = require('./phone'); + +module.exports = class Consumer { + constructor(name, email, cpf) { + this.name = name; + this.email = email; + this.cpf = cpf; + } + + static get MALE() { + return 'M'; + } + + static get FEMALE() { + return 'F'; + } + + addDocument(type, number) { + if (this.documents === undefined) { + this.documents = []; + } + + this.documents.push({ type, number }); + + return this; + } + + setPhone(ddd, number, type = Phone.CELLPHONE) { + this.phone = new Phone(ddd, number, type); + + return this; + } +}; diff --git a/src/environment.js b/src/environment.js index 765382f..42f21f1 100644 --- a/src/environment.js +++ b/src/environment.js @@ -1,21 +1,21 @@ -"use strict"; - -const PRODUCTION = "https://api.userede.com.br/erede"; -const SANDBOX = "https://api.userede.com.br/desenvolvedores"; -const VERSION = "v1"; - -module.exports = class Environment { - constructor(baseUrl, version = VERSION) { - this.ip = ""; - this.sessionId = ""; - this.endpoint = `${baseUrl}/${version}`; - } - - static production() { - return new Environment(PRODUCTION, VERSION); - } - - static sandbox() { - return new Environment(SANDBOX, VERSION) - } -}; \ No newline at end of file + + +const PRODUCTION = 'https://api.userede.com.br/erede'; +const SANDBOX = 'https://api.userede.com.br/desenvolvedores'; +const VERSION = 'v1'; + +module.exports = class Environment { + constructor(baseUrl, version = VERSION) { + this.ip = ''; + this.sessionId = ''; + this.endpoint = `${baseUrl}/${version}`; + } + + static production() { + return new Environment(PRODUCTION, VERSION); + } + + static sandbox() { + return new Environment(SANDBOX, VERSION); + } +}; diff --git a/src/erede.js b/src/erede.js index b086a26..6389eae 100644 --- a/src/erede.js +++ b/src/erede.js @@ -53,18 +53,17 @@ module.exports = class eRede { return service.execute(); } - zero(transaction) { - const { amount } = transaction; - const { capture } = transaction; + zero(oldTransaction) { + let transactionToBezered = { ...oldTransaction }; - transaction.amount = 0; - transaction.capture = true; + transactionToBezered.amount = 0; + transactionToBezered.capture = true; - transaction = this.create(transaction); + transactionToBezered = this.create(transactionToBezered); - transaction.amount = amount; - transaction.capture = capture; + transactionToBezered.amount = oldTransaction.amount; + transactionToBezered.capture = oldTransaction.capture; - return transaction; + return transactionToBezered; } }; diff --git a/src/exception/RedeError.js b/src/exception/RedeError.js index cfb7cc2..c653c36 100644 --- a/src/exception/RedeError.js +++ b/src/exception/RedeError.js @@ -1,10 +1,10 @@ -"use strict"; - -module.exports = class RedeError extends Error { - constructor(message, code) { - super(message); - - this.returnCode = code; - this.returnMessage = message; - } -}; \ No newline at end of file + + +module.exports = class RedeError extends Error { + constructor(message, code) { + super(message); + + this.returnCode = code; + this.returnMessage = message; + } +}; diff --git a/src/flight.js b/src/flight.js index 6af7e50..f354707 100644 --- a/src/flight.js +++ b/src/flight.js @@ -1,22 +1,20 @@ -"use strinct"; - -const Passenger = require("./passenger"); - -module.exports = class Flight { - constructor(number, from, to, date) { - this.number = number; - this.from = from; - this.to = to; - this.date = date; - } - - addPassenger(name, email, ticket) { - if (this.passenger === undefined) { - this.passenger = []; - } - - this.passenger.push(new Passenger(name, email, ticket)); - - return this; - } -}; \ No newline at end of file +const Passenger = require('./passenger'); + +module.exports = class Flight { + constructor(number, from, to, date) { + this.number = number; + this.from = from; + this.to = to; + this.date = date; + } + + addPassenger(name, email, ticket) { + if (this.passenger === undefined) { + this.passenger = []; + } + + this.passenger.push(new Passenger(name, email, ticket)); + + return this; + } +}; diff --git a/src/iata.js b/src/iata.js index 81144c0..d7a6569 100644 --- a/src/iata.js +++ b/src/iata.js @@ -1,8 +1,8 @@ -"use strict"; - -module.exports = class Iata { - constructor(code, departureTax) { - this.code = code; - this.departureTax = departureTax; - } -}; \ No newline at end of file + + +module.exports = class Iata { + constructor(code, departureTax) { + this.code = code; + this.departureTax = departureTax; + } +}; diff --git a/src/item.js b/src/item.js index b6a9243..0b50a37 100644 --- a/src/item.js +++ b/src/item.js @@ -1,25 +1,25 @@ -"use strict"; - -module.exports = class Item { - constructor(id, quantity, type = Item.PHYSICAL) { - this.id = id; - this.quantity = quantity; - this.type = type; - } - - static get PHYSICAL() { - return 1; - } - - static get DIGITAL() { - return 2; - } - - static get SERVICE() { - return 3; - } - - static get AIRLINE() { - return 4; - } -}; \ No newline at end of file + + +module.exports = class Item { + constructor(id, quantity, type = Item.PHYSICAL) { + this.id = id; + this.quantity = quantity; + this.type = type; + } + + static get PHYSICAL() { + return 1; + } + + static get DIGITAL() { + return 2; + } + + static get SERVICE() { + return 3; + } + + static get AIRLINE() { + return 4; + } +}; diff --git a/src/passenger.js b/src/passenger.js index c61f56d..afa53ec 100644 --- a/src/passenger.js +++ b/src/passenger.js @@ -1,9 +1,9 @@ -"use strict"; - -module.exports = class Passenger { - constructor(name, email, ticket) { - this.name = name; - this.email = email; - this.ticket = ticket; - } -}; \ No newline at end of file + + +module.exports = class Passenger { + constructor(name, email, ticket) { + this.name = name; + this.email = email; + this.ticket = ticket; + } +}; diff --git a/src/phone.js b/src/phone.js index bf6cba2..0a30a96 100644 --- a/src/phone.js +++ b/src/phone.js @@ -1,25 +1,25 @@ -"use strict"; - -module.exports = class Phone { - constructor(ddd, number, type = Phone.CELLPHONE) { - this.ddd = ddd; - this.number = number; - this.type = type; - } - - static get CELLPHONE() { - return 1; - } - - static get HOME() { - return 2; - } - - static get WORK() { - return 3; - } - - static get OTHER() { - return 4; - } -}; \ No newline at end of file + + +module.exports = class Phone { + constructor(ddd, number, type = Phone.CELLPHONE) { + this.ddd = ddd; + this.number = number; + this.type = type; + } + + static get CELLPHONE() { + return 1; + } + + static get HOME() { + return 2; + } + + static get WORK() { + return 3; + } + + static get OTHER() { + return 4; + } +}; diff --git a/src/refund.js b/src/refund.js index 507f605..34e5df0 100644 --- a/src/refund.js +++ b/src/refund.js @@ -1,21 +1,20 @@ -"use strict"; - -module.exports = class Refund { - static fromJSON(json) { - let refund = new Refund(); - - for (let property in json) { - if (json.hasOwnProperty(property)) { - let value = json[property]; - - if (property === 'requestDateTime' || property === 'dateTime' || property === 'refundDateTime') { - value = new Date(value); - } - - refund[property] = value; - } - } - - return refund; - } -}; \ No newline at end of file + + +module.exports = class Refund { + static fromJSON(json) { + const refund = new Refund(); + + const jsonKeys = Object.keys(json); + for (let i = jsonKeys.length; i >= 0; i -= 1) { + const property = jsonKeys[i]; + let value = json[property]; + if (property === 'requestDateTime' || property === 'dateTime' || property === 'refundDateTime') { + value = new Date(value); + } + + refund[property] = value; + } + + return refund; + } +}; diff --git a/src/service/CancelTransactionService.js b/src/service/CancelTransactionService.js index bbf5530..ad611f0 100644 --- a/src/service/CancelTransactionService.js +++ b/src/service/CancelTransactionService.js @@ -1,19 +1,19 @@ -"use strict"; - -const TransactionService = require('./TransactionService'); - -module.exports = class CancelTransactionService extends TransactionService { - constructor(store, transaction) { - super(store); - - this.transaction = transaction; - } - - getUrl() { - return `${super.getUrl()}/${this.transaction.tid}/refunds`; - } - - async execute() { - return this.sendRequest(TransactionService.POST, JSON.stringify(this.transaction)); - } -}; \ No newline at end of file + + +const TransactionService = require('./TransactionService'); + +module.exports = class CancelTransactionService extends TransactionService { + constructor(store, transaction) { + super(store); + + this.transaction = transaction; + } + + getUrl() { + return `${super.getUrl()}/${this.transaction.tid}/refunds`; + } + + async execute() { + return this.sendRequest(TransactionService.POST, JSON.stringify(this.transaction)); + } +}; diff --git a/src/service/CaptureTransactionService.js b/src/service/CaptureTransactionService.js index 719ebce..71e1633 100644 --- a/src/service/CaptureTransactionService.js +++ b/src/service/CaptureTransactionService.js @@ -1,19 +1,19 @@ -"use strict"; - -const TransactionService = require('./TransactionService'); - -module.exports = class CaptureTransactionService extends TransactionService { - constructor(store, transaction) { - super(store); - - this.transaction = transaction; - } - - getUrl() { - return `${super.getUrl()}/${this.transaction.tid}`; - } - - execute() { - return this.sendRequest(TransactionService.PUT, JSON.stringify(this.transaction)); - } -}; \ No newline at end of file + + +const TransactionService = require('./TransactionService'); + +module.exports = class CaptureTransactionService extends TransactionService { + constructor(store, transaction) { + super(store); + + this.transaction = transaction; + } + + getUrl() { + return `${super.getUrl()}/${this.transaction.tid}`; + } + + execute() { + return this.sendRequest(TransactionService.PUT, JSON.stringify(this.transaction)); + } +}; diff --git a/src/service/CreateTransactionService.js b/src/service/CreateTransactionService.js index f3ae85e..49ce8e3 100644 --- a/src/service/CreateTransactionService.js +++ b/src/service/CreateTransactionService.js @@ -1,15 +1,15 @@ -"use strict"; - -const TransactionService = require('./TransactionService'); - -module.exports = class CreateTransactionService extends TransactionService { - constructor(store, transaction) { - super(store); - - this.transaction = transaction; - } - - async execute() { - return await this.sendRequest(TransactionService.POST, JSON.stringify(this.transaction)); - } -}; \ No newline at end of file + + +const TransactionService = require('./TransactionService'); + +module.exports = class CreateTransactionService extends TransactionService { + constructor(store, transaction) { + super(store); + + this.transaction = transaction; + } + + async execute() { + return this.sendRequest(TransactionService.POST, JSON.stringify(this.transaction)); + } +}; diff --git a/src/service/GetTransactionService.js b/src/service/GetTransactionService.js index f3b44a4..fae85e6 100644 --- a/src/service/GetTransactionService.js +++ b/src/service/GetTransactionService.js @@ -1,31 +1,31 @@ -"use strict"; - -const TransactionService = require('./TransactionService'); - -module.exports = class GetTransactionService extends TransactionService { - constructor(store) { - super(store); - - this.tid = undefined; - } - - getUrl() { - if (this.reference !== undefined) { - return `${super.getUrl()}?reference=${this.reference}`; - } - - if (this.tid === undefined) { - throw new Error('You need to specify one: the tid or the reference'); - } - - if (this.refunds !== undefined) { - return `${super.getUrl()}/${this.tid}/refunds`; - } - - return `${super.getUrl()}/${this.tid}`; - } - - execute() { - return this.sendRequest(TransactionService.GET); - } -}; \ No newline at end of file + + +const TransactionService = require('./TransactionService'); + +module.exports = class GetTransactionService extends TransactionService { + constructor(store) { + super(store); + + this.tid = undefined; + } + + getUrl() { + if (this.reference !== undefined) { + return `${super.getUrl()}?reference=${this.reference}`; + } + + if (this.tid === undefined) { + throw new Error('You need to specify one: the tid or the reference'); + } + + if (this.refunds !== undefined) { + return `${super.getUrl()}/${this.tid}/refunds`; + } + + return `${super.getUrl()}/${this.tid}`; + } + + execute() { + return this.sendRequest(TransactionService.GET); + } +}; diff --git a/src/service/TransactionService.js b/src/service/TransactionService.js index 82efbd4..f63ac5f 100644 --- a/src/service/TransactionService.js +++ b/src/service/TransactionService.js @@ -1,78 +1,78 @@ -"use strict"; - -const Transaction = require('../transaction'); -const RedeError = require('../exception/RedeError'); -const http = require('https'); -const bl = require('bl'); -const URL = require('url').URL; - -module.exports = class TransactionService { - constructor(store) { - this.store = store; - } - - static get POST() { - return 'POST'; - } - - static get GET() { - return 'GET'; - } - - static get PUT() { - return 'PUT'; - } - - getUrl() { - let endpoint = this.store.environment.endpoint; - - return `${endpoint}/transactions`; - } - - async execute() { - throw new Error('Ńão implementado'); - } - - sendRequest(method, body = "") { - const url = new URL(this.getUrl()); - const options = { - hostname: url.hostname, - post: 443, - path: url.pathname, - method: method, - auth: this.store.filiation + ':' + this.store.token, - headers: { - 'Content-Type': 'application/json', - 'Content-Length': Buffer.byteLength(body) - } - }; - - return new Promise((resolve, reject) => { - let client = http.request(options, response => { - response.setEncoding('utf8'); - response.pipe(bl((error, data) => { - if (error) { - reject(error); - } - - let json = JSON.parse(data.toString()); - - if (response.statusCode >= 400) { - if (!json || json.returnMessage === undefined) { - json = {}; - json.returnMessage = 'Alguma coisa aconteceu'; - json.returnCode = '-1'; - } - - reject(new RedeError(json.returnMessage, json.returnCode)); - } - - resolve(Transaction.fromJSON(json)); - })); - }); - - client.write(body); - client.end(); - }); - } -}; + + +const http = require('https'); +const bl = require('bl'); +const { URL } = require('url'); +const RedeError = require('../exception/RedeError'); +const Transaction = require('../transaction'); + +module.exports = class TransactionService { + constructor(store) { + this.store = store; + } + + static get POST() { + return 'POST'; + } + + static get GET() { + return 'GET'; + } + + static get PUT() { + return 'PUT'; + } + + getUrl() { + const { endpoint } = this.store.environment; + + return `${endpoint}/transactions`; + } + + static async execute() { + throw new Error('Ńão implementado'); + } + + sendRequest(method, body = '') { + const url = new URL(this.getUrl()); + const options = { + hostname: url.hostname, + post: 443, + path: url.pathname, + method, + auth: `${this.store.filiation}:${this.store.token}`, + headers: { + 'Content-Type': 'application/json', + 'Content-Length': Buffer.byteLength(body), + }, + }; + + return new Promise((resolve, reject) => { + const client = http.request(options, (response) => { + response.setEncoding('utf8'); + response.pipe(bl((error, data) => { + if (error) { + reject(error); + } + + let json = JSON.parse(data.toString()); + + if (response.statusCode >= 400) { + if (!json || json.returnMessage === undefined) { + json = {}; + json.returnMessage = 'Alguma coisa aconteceu'; + json.returnCode = '-1'; + } + + reject(new RedeError(json.returnMessage, json.returnCode)); + } + + resolve(Transaction.fromJSON(json)); + })); + }); + + client.write(body); + client.end(); + }); + } +}; diff --git a/src/submerchant.js b/src/submerchant.js index 62fc592..2bc2a4b 100644 --- a/src/submerchant.js +++ b/src/submerchant.js @@ -1,9 +1,9 @@ -"use strict"; - -module.exports = class SubMerchant { - constructor(mcc, city, country) { - this.mcc = mcc; - this.city = city; - this.country = country; - } -}; \ No newline at end of file + + +module.exports = class SubMerchant { + constructor(mcc, city, country) { + this.mcc = mcc; + this.city = city; + this.country = country; + } +}; diff --git a/src/threedsecure.js b/src/threedsecure.js index 9854fde..a170ea8 100644 --- a/src/threedsecure.js +++ b/src/threedsecure.js @@ -1,28 +1,27 @@ -"use strict"; - -module.exports = class ThreeDSecure { - constructor() { - this.embedded = true; - this.threeDIndicator = "1"; - } - - static get CONTINUE_ON_FAILURE() { - return "continue"; - } - - static get DECLINE_ON_FAILURE() { - return "decline"; - } - - static fromJSON(json) { - let threeds = new ThreeDSecure(); - - for (let property in json) { - if (json.hasOwnProperty(property)) { - threeds[property] = json[property]; - } - } - - return threeds; - } -}; \ No newline at end of file + + +module.exports = class ThreeDSecure { + constructor() { + this.embedded = true; + this.threeDIndicator = '1'; + } + + static get CONTINUE_ON_FAILURE() { + return 'continue'; + } + + static get DECLINE_ON_FAILURE() { + return 'decline'; + } + + static fromJSON(json) { + const threeds = new ThreeDSecure(); + + const jsonKeys = Object.keys(json); + for (let i = jsonKeys.length; i >= 0; i -= 1) { + threeds[jsonKeys[i]] = json[jsonKeys[i]]; + } + + return threeds; + } +}; diff --git a/src/url.js b/src/url.js index 13a6907..430502d 100644 --- a/src/url.js +++ b/src/url.js @@ -1,36 +1,35 @@ -"use strict"; - -const CALLBACK = "callback"; -const THREE_D_SECURE_FAILURE = "threeDSecureFailure"; -const THREE_D_SECURE_SUCCESS = "threeDSecureSuccess"; - -module.exports = class Url { - constructor(url, kind = Url.CALLBACK) { - this.url = url; - this.kind = kind; - } - - static get CALLBACK() { - return CALLBACK; - } - - static get THREE_D_SECURE_FAILURE() { - return THREE_D_SECURE_FAILURE; - } - - static get THREE_D_SECURE_SUCCESS() { - return THREE_D_SECURE_SUCCESS; - } - - static fromJSON(json) { - let url = new Url(); - - for (let property in json) { - if (json.hasOwnProperty(property)) { - url[property] = json[property]; - } - } - - return url; - } -}; \ No newline at end of file + + +const CALLBACK = 'callback'; +const THREE_D_SECURE_FAILURE = 'threeDSecureFailure'; +const THREE_D_SECURE_SUCCESS = 'threeDSecureSuccess'; + +module.exports = class Url { + constructor(url, kind = Url.CALLBACK) { + this.url = url; + this.kind = kind; + } + + static get CALLBACK() { + return CALLBACK; + } + + static get THREE_D_SECURE_FAILURE() { + return THREE_D_SECURE_FAILURE; + } + + static get THREE_D_SECURE_SUCCESS() { + return THREE_D_SECURE_SUCCESS; + } + + static fromJSON(json) { + const url = new Url(); + + const jsonKeys = Object.keys(json); + for (let i = jsonKeys.length; i >= 0; i -= 1) { + url[jsonKeys[i]] = json[jsonKeys[i]]; + } + + return url; + } +}; From 9a98a8b0c952b5c5ac564be80ac884a24837c8c7 Mon Sep 17 00:00:00 2001 From: igorp Date: Wed, 23 Oct 2019 22:24:15 -0300 Subject: [PATCH 08/11] created test with jenkins --- package.json | 3 +++ test/examples/autorizandoTransacao.test.js | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 test/examples/autorizandoTransacao.test.js diff --git a/package.json b/package.json index 65e6a6c..8794e4a 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,9 @@ "name": "erede-node", "description": "SDK Node.JS Rede", "version": "0.0.1", + "scripts": { + "test": "jest" + }, "homepage": "https://github.com/igorpdasilvaa/erede-node", "keywords": [ "rede", diff --git a/test/examples/autorizandoTransacao.test.js b/test/examples/autorizandoTransacao.test.js new file mode 100644 index 0000000..2542854 --- /dev/null +++ b/test/examples/autorizandoTransacao.test.js @@ -0,0 +1,19 @@ +const ERede = require('../../src/erede'); +const Store = require('../../src/store'); +const Environment = require('../../src/environment'); +const Transaction = require('../../src/transaction'); + +test('should authorize a transaction', () => { + const store = new Store(process.env.storeToken, process.env.storePV, Environment.sandbox()); + const transaction = new Transaction(10, Date.now()).creditCard( + '5448280000000007', + '235', + '12', + '2020', + 'Fulano', + ); + + return new ERede(store).create(transaction).then((resultOfTransaction) => { + expect(resultOfTransaction.returnCode).toBe('00'); + }); +}); From 2166f29556d811769ede6820d9277be52e422f29 Mon Sep 17 00:00:00 2001 From: igorp Date: Thu, 24 Oct 2019 20:28:18 -0300 Subject: [PATCH 09/11] finalized all tests --- test/examples/autorizandoTransacao.test.js | 24 +++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/test/examples/autorizandoTransacao.test.js b/test/examples/autorizandoTransacao.test.js index 2542854..5783e8e 100644 --- a/test/examples/autorizandoTransacao.test.js +++ b/test/examples/autorizandoTransacao.test.js @@ -3,7 +3,7 @@ const Store = require('../../src/store'); const Environment = require('../../src/environment'); const Transaction = require('../../src/transaction'); -test('should authorize a transaction', () => { +test('should authorize a transaction', async () => { const store = new Store(process.env.storeToken, process.env.storePV, Environment.sandbox()); const transaction = new Transaction(10, Date.now()).creditCard( '5448280000000007', @@ -13,7 +13,21 @@ test('should authorize a transaction', () => { 'Fulano', ); - return new ERede(store).create(transaction).then((resultOfTransaction) => { - expect(resultOfTransaction.returnCode).toBe('00'); - }); -}); + const resultOfTransaction = await new ERede(store).create(transaction); + expect(resultOfTransaction.returnCode).toBe('00'); +}, 50000); + + +test('should authorize a transaction with installment', async () => { + const store = new Store(process.env.storeToken, process.env.storePV, Environment.sandbox()); + const transaction = new Transaction(10, Date.now(), 2).creditCard( + '5448280000000007', + '235', + '12', + '2020', + 'Fulano', + ); + + const resultOfTransaction = await new ERede(store).create(transaction); + expect(resultOfTransaction.returnCode).toBe('00'); +}, 50000); From 3f79987aa2ee51c10bd800b2384b7bd77ecd95de Mon Sep 17 00:00:00 2001 From: igorp Date: Thu, 24 Oct 2019 20:29:01 -0300 Subject: [PATCH 10/11] removed warnings of readme --- README.md | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/README.md b/README.md index 47bc160..6a81cfb 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,3 @@ -# Pacote com refactoring em progresso, não recomendado para ambiente de produção - -# Futuramente pretendo fazer pull request no repositório oficial do erede - # SDK Node.js SDK de integração eRede @@ -78,4 +74,4 @@ new eRede(store).create(transaction).then(transaction => { console.log(`Transação autorizada com sucesso: ${transaction.tid}`); } }); -``` +``` \ No newline at end of file From 4b60bf8c250b168cfea871207b7700edcb5a394d Mon Sep 17 00:00:00 2001 From: igorp Date: Thu, 24 Oct 2019 20:29:37 -0300 Subject: [PATCH 11/11] changed meta-informations about the package --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 8794e4a..1df536b 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,8 @@ { - "author": "Igor Pereira da Silva", + "author": "João Batista Neto", "name": "erede-node", "description": "SDK Node.JS Rede", - "version": "0.0.1", + "version": "1.0.0", "scripts": { "test": "jest" },