From f8b20d12e6ca03019f6de480af1ce8f9724c42ad Mon Sep 17 00:00:00 2001 From: jaffar cardoso Date: Wed, 19 Aug 2020 14:40:49 -0300 Subject: [PATCH] ativando o antifraude e ajust jslint --- .eslintrc.js | 36 +++++++ README.md | 24 +++++ lib/additional.js | 4 +- lib/address.js | 2 +- lib/antifraud.js | 8 +- lib/authorization.js | 4 +- lib/capture.js | 4 +- lib/cart.js | 8 +- lib/consumer.js | 8 +- lib/environment.js | 14 +-- lib/erede.js | 10 +- lib/exception/RedeError.js | 2 +- lib/flight.js | 4 +- lib/iata.js | 2 +- lib/item.js | 2 +- lib/passenger.js | 2 +- lib/phone.js | 2 +- lib/refund.js | 4 +- lib/service/CancelTransactionService.js | 2 +- lib/service/CaptureTransactionService.js | 2 +- lib/service/CreateTransactionService.js | 2 +- lib/service/GetTransactionService.js | 2 +- lib/service/TransactionService.js | 4 +- lib/store.js | 4 +- lib/submerchant.js | 2 +- lib/threedsecure.js | 10 +- lib/transaction.js | 116 +++++++++++------------ lib/url.js | 10 +- package.json | 13 ++- 29 files changed, 187 insertions(+), 120 deletions(-) create mode 100644 .eslintrc.js diff --git a/.eslintrc.js b/.eslintrc.js new file mode 100644 index 0000000..ecb6479 --- /dev/null +++ b/.eslintrc.js @@ -0,0 +1,36 @@ +module.exports = { + 'env': { + 'browser': true, + 'node': true, + 'es6': true + }, + 'parserOptions': { + 'ecmaVersion': 9, + 'sourceType': 'module' + }, + 'extends': 'eslint:recommended', + 'rules': { + 'indent': [ + 'error', + 4 + ], + 'linebreak-style': [ + 'error', + 'unix' + ], + 'quotes': [ + 'error', + 'single' + ], + 'semi': [ + 'error', + 'always' + ], + 'require-atomic-updates': 'off', + 'no-console': 'off', + 'no-trailing-spaces': 'error' + }, + 'globals': { + + } +}; diff --git a/README.md b/README.md index 9d9eba3..ad63a33 100644 --- a/README.md +++ b/README.md @@ -75,3 +75,27 @@ new eRede(store).create(transaction).then(transaction => { } }); ``` +## Autorizando uma transação com antifraud + +```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' +).setAntifraud(Environment.sandbox()).autoCapture(false); + +new eRede(store).create(transaction).then(transaction => { + if (transaction.returnCode === "00") { + console.log(`Transação autorizada com sucesso: ${transaction.tid}`); + console.log(`Transação autorizada com sucesso: ${transaction.antifraud}`); + } +}); +``` \ No newline at end of file diff --git a/lib/additional.js b/lib/additional.js index 231326c..09f08ba 100644 --- a/lib/additional.js +++ b/lib/additional.js @@ -1,4 +1,4 @@ -"use strict"; +'use strict'; module.exports = class Additional { constructor(gateway, module) { @@ -10,7 +10,7 @@ module.exports = class Additional { let additional = new Additional(); for (let property in json) { - if (json.hasOwnProperty(property)) { + if (Object.prototype.hasOwnProperty.call(json, property)) { additional[property] = json[property]; } } diff --git a/lib/address.js b/lib/address.js index 7397e56..8ba6375 100644 --- a/lib/address.js +++ b/lib/address.js @@ -1,4 +1,4 @@ -"use strict"; +'use strict'; module.exports = class Address { static get BILLING() { diff --git a/lib/antifraud.js b/lib/antifraud.js index 638940a..075f412 100644 --- a/lib/antifraud.js +++ b/lib/antifraud.js @@ -1,4 +1,4 @@ -"use strict"; +'use strict'; module.exports = class Antifraud { constructor() { @@ -6,10 +6,10 @@ module.exports = class Antifraud { } static fromJSON(json) { - let antifraud = new self(); - + let antifraud = new this(); for (let property in json) { - if (json.hasOwnProperty(property)) { + + if (Object.prototype.hasOwnProperty.call(json, property)) { antifraud[property] = json[property]; } } diff --git a/lib/authorization.js b/lib/authorization.js index a591ee5..5c3f609 100644 --- a/lib/authorization.js +++ b/lib/authorization.js @@ -1,11 +1,11 @@ -"use strict"; +'use strict'; module.exports = class Authorization { static fromJSON(json) { let authorization = new Authorization(); for (let property in json) { - if (json.hasOwnProperty(property)) { + if (Object.prototype.hasOwnProperty.call(json, property)) { let value = json[property]; if (property === 'requestDateTime' || property === 'dateTime' || property === 'refundDateTime') { diff --git a/lib/capture.js b/lib/capture.js index a4fb205..c174ade 100644 --- a/lib/capture.js +++ b/lib/capture.js @@ -1,11 +1,11 @@ -"use strict"; +'use strict'; module.exports = class Capture { static fromJSON(json) { let capture = new Capture(); for (let property in json) { - if (json.hasOwnProperty(property)) { + if (Object.prototype.hasOwnProperty.call(json, property)) { let value = json[property]; if (property === 'requestDateTime' || property === 'dateTime' || property === 'refundDateTime') { diff --git a/lib/cart.js b/lib/cart.js index 6aeddd9..5d212bf 100644 --- a/lib/cart.js +++ b/lib/cart.js @@ -1,8 +1,8 @@ -"use strict"; +'use strict'; -const Address = require("./address"); -const Consumer = require("./consumer"); -const Iata = require("./iata"); +const Address = require('./address'); +const Consumer = require('./consumer'); +const Iata = require('./iata'); module.exports = class Cart { address(type = Address.BOTH) { diff --git a/lib/consumer.js b/lib/consumer.js index 0034421..38d5333 100644 --- a/lib/consumer.js +++ b/lib/consumer.js @@ -1,6 +1,6 @@ -"use strict"; +'use strict'; -const Phone = require("./phone"); +const Phone = require('./phone'); module.exports = class Consumer { constructor(name, email, cpf) { @@ -10,11 +10,11 @@ module.exports = class Consumer { } static get MALE() { - return "M"; + return 'M'; } static get FEMALE() { - return "F"; + return 'F'; } addDocument(type, number) { diff --git a/lib/environment.js b/lib/environment.js index 8e056d2..137791f 100644 --- a/lib/environment.js +++ b/lib/environment.js @@ -1,13 +1,13 @@ -"use strict"; +'use strict'; -const PRODUCTION = "https://api.userede.com.br/erede"; -const SANDBOX = "https://api.userede.com.br/desenvolvedores"; -const VERSION = "v1"; +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.ip = ''; + this.sessionId = ''; this.endpoint = `${baseUrl}/${version}`; } @@ -16,6 +16,6 @@ module.exports = class Environment { } static sandbox() { - return new Environment(SANDBOX, VERSION) + return new Environment(SANDBOX, VERSION); } }; \ No newline at end of file diff --git a/lib/erede.js b/lib/erede.js index 12e1f37..5323050 100644 --- a/lib/erede.js +++ b/lib/erede.js @@ -1,9 +1,9 @@ -"use strict"; +'use strict'; -const CancelTransactionService = require("./service/CancelTransactionService"); -const CaptureTransactionService = require("./service/CaptureTransactionService"); -const CreateTransactionService = require("./service/CreateTransactionService"); -const GetTransactionService = require("./service/GetTransactionService"); +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) { diff --git a/lib/exception/RedeError.js b/lib/exception/RedeError.js index 86795b0..2f5fdf8 100644 --- a/lib/exception/RedeError.js +++ b/lib/exception/RedeError.js @@ -1,4 +1,4 @@ -"use strict"; +'use strict'; module.exports = class RedeError extends Error { constructor(message, code) { diff --git a/lib/flight.js b/lib/flight.js index 0ad748c..97f0b35 100644 --- a/lib/flight.js +++ b/lib/flight.js @@ -1,6 +1,6 @@ -"use strinct"; +'use strinct'; -const Passenger = require("./passenger"); +const Passenger = require('./passenger'); module.exports = class Flight { constructor(number, from, to, date) { diff --git a/lib/iata.js b/lib/iata.js index 29e1205..abc3848 100644 --- a/lib/iata.js +++ b/lib/iata.js @@ -1,4 +1,4 @@ -"use strict"; +'use strict'; module.exports = class Iata { constructor(code, departureTax) { diff --git a/lib/item.js b/lib/item.js index 93d3fd0..098c9e3 100644 --- a/lib/item.js +++ b/lib/item.js @@ -1,4 +1,4 @@ -"use strict"; +'use strict'; module.exports = class Item { constructor(id, quantity, type = Item.PHYSICAL) { diff --git a/lib/passenger.js b/lib/passenger.js index 6dfc577..c1e7eb3 100644 --- a/lib/passenger.js +++ b/lib/passenger.js @@ -1,4 +1,4 @@ -"use strict"; +'use strict'; module.exports = class Passenger { constructor(name, email, ticket) { diff --git a/lib/phone.js b/lib/phone.js index 317fa15..d8e2de0 100644 --- a/lib/phone.js +++ b/lib/phone.js @@ -1,4 +1,4 @@ -"use strict"; +'use strict'; module.exports = class Phone { constructor(ddd, number, type = Phone.CELLPHONE) { diff --git a/lib/refund.js b/lib/refund.js index e8f449b..a828539 100644 --- a/lib/refund.js +++ b/lib/refund.js @@ -1,11 +1,11 @@ -"use strict"; +'use strict'; module.exports = class Refund { static fromJSON(json) { let refund = new Refund(); for (let property in json) { - if (json.hasOwnProperty(property)) { + if (Object.prototype.hasOwnProperty.call(json, property)) { let value = json[property]; if (property === 'requestDateTime' || property === 'dateTime' || property === 'refundDateTime') { diff --git a/lib/service/CancelTransactionService.js b/lib/service/CancelTransactionService.js index 910aab0..31f02b4 100644 --- a/lib/service/CancelTransactionService.js +++ b/lib/service/CancelTransactionService.js @@ -1,4 +1,4 @@ -"use strict"; +'use strict'; const TransactionService = require('./TransactionService'); diff --git a/lib/service/CaptureTransactionService.js b/lib/service/CaptureTransactionService.js index 4d597a3..dcd93bb 100644 --- a/lib/service/CaptureTransactionService.js +++ b/lib/service/CaptureTransactionService.js @@ -1,4 +1,4 @@ -"use strict"; +'use strict'; const TransactionService = require('./TransactionService'); diff --git a/lib/service/CreateTransactionService.js b/lib/service/CreateTransactionService.js index a080ac2..fd91f00 100644 --- a/lib/service/CreateTransactionService.js +++ b/lib/service/CreateTransactionService.js @@ -1,4 +1,4 @@ -"use strict"; +'use strict'; const TransactionService = require('./TransactionService'); diff --git a/lib/service/GetTransactionService.js b/lib/service/GetTransactionService.js index 21492ad..e784938 100644 --- a/lib/service/GetTransactionService.js +++ b/lib/service/GetTransactionService.js @@ -1,4 +1,4 @@ -"use strict"; +'use strict'; const TransactionService = require('./TransactionService'); diff --git a/lib/service/TransactionService.js b/lib/service/TransactionService.js index c58b221..afb119e 100644 --- a/lib/service/TransactionService.js +++ b/lib/service/TransactionService.js @@ -1,4 +1,4 @@ -"use strict"; +'use strict'; const Transaction = require('../transaction'); const RedeError = require('../exception/RedeError'); @@ -33,7 +33,7 @@ module.exports = class TransactionService { throw new Error('Ńão implementado'); } - sendRequest(method, body = "") { + sendRequest(method, body = '') { const url = new URL(this.getUrl()); const options = { hostname: url.hostname, diff --git a/lib/store.js b/lib/store.js index 39b510f..e434cdc 100644 --- a/lib/store.js +++ b/lib/store.js @@ -1,6 +1,6 @@ -"use strict"; +'use strict'; -const Environment = require("./environment.js"); +const Environment = require('./environment.js'); module.exports = class Store { constructor(token, filiation, environment = Environment.production()) { diff --git a/lib/submerchant.js b/lib/submerchant.js index 66d9c4f..6597b0f 100644 --- a/lib/submerchant.js +++ b/lib/submerchant.js @@ -1,4 +1,4 @@ -"use strict"; +'use strict'; module.exports = class SubMerchant { constructor(mcc, city, country) { diff --git a/lib/threedsecure.js b/lib/threedsecure.js index b3385c7..40dfb48 100644 --- a/lib/threedsecure.js +++ b/lib/threedsecure.js @@ -1,24 +1,24 @@ -"use strict"; +'use strict'; module.exports = class ThreeDSecure { constructor() { this.embedded = true; - this.threeDIndicator = "1"; + this.threeDIndicator = '1'; } static get CONTINUE_ON_FAILURE() { - return "continue"; + return 'continue'; } static get DECLINE_ON_FAILURE() { - return "decline"; + return 'decline'; } static fromJSON(json) { let threeds = new ThreeDSecure(); for (let property in json) { - if (json.hasOwnProperty(property)) { + if (Object.prototype.hasOwnProperty.call(json, property)) { threeds[property] = json[property]; } } diff --git a/lib/transaction.js b/lib/transaction.js index e5a62b2..4a5d3a4 100644 --- a/lib/transaction.js +++ b/lib/transaction.js @@ -1,13 +1,13 @@ -"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"); +'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 { @@ -26,11 +26,11 @@ module.exports = class Transaction { } static get CREDIT() { - return "credit"; + return 'credit'; } static get DEBIT() { - return "debit"; + return 'debit'; } static get ORIGIN_EREDE() { @@ -49,7 +49,7 @@ module.exports = class Transaction { let transaction = new Transaction(json.amount, json.reference); for (let property in json) { - if (!json.hasOwnProperty(property)) { + if (Object.prototype.hasOwnProperty.call(json, property)) { continue; } @@ -58,49 +58,49 @@ module.exports = class Transaction { 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; + 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; } } @@ -204,7 +204,7 @@ module.exports = class Transaction { */ autoCapture(capture = true) { if (!capture && this.kind === Transaction.DEBIT) { - throw new Error("Debit transactions will always be captured"); + throw new Error('Debit transactions will always be captured'); } this.capture = capture; @@ -247,7 +247,7 @@ module.exports = class Transaction { * @param threeDIndicator * @returns {module.Transaction} */ - setThreeDSecure(onFailure = ThreeDSecure.DECLINE_ON_FAILURE, embed = true, directoryServerTransactionId = "", threeDIndicator = "1") { + setThreeDSecure(onFailure = ThreeDSecure.DECLINE_ON_FAILURE, embed = true, directoryServerTransactionId = '', threeDIndicator = '1') { this.threeDSecure = new ThreeDSecure(); this.threeDSecure.onFailure = onFailure; this.threeDSecure.embedded = embed; diff --git a/lib/url.js b/lib/url.js index 614f5bf..0d5084e 100644 --- a/lib/url.js +++ b/lib/url.js @@ -1,8 +1,8 @@ -"use strict"; +'use strict'; -const CALLBACK = "callback"; -const THREE_D_SECURE_FAILURE = "threeDSecureFailure"; -const THREE_D_SECURE_SUCCESS = "threeDSecureSuccess"; +const CALLBACK = 'callback'; +const THREE_D_SECURE_FAILURE = 'threeDSecureFailure'; +const THREE_D_SECURE_SUCCESS = 'threeDSecureSuccess'; module.exports = class Url { constructor(url, kind = Url.CALLBACK) { @@ -26,7 +26,7 @@ module.exports = class Url { let url = new Url(); for (let property in json) { - if (json.hasOwnProperty(property)) { + if (Object.prototype.hasOwnProperty.call(json, property)) { url[property] = json[property]; } } diff --git a/package.json b/package.json index bb01758..28b661f 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,10 @@ "type": "git", "url": "https://github.com/DevelopersRede/erede-node.git" }, + "scripts": { + "lint": "./node_modules/.bin/eslint ./", + "lint-fix": "./node_modules/.bin/eslint ./ --fix" + }, "bugs": { "url": "https://github.com/DevelopersRede/erede-node/issues" }, @@ -22,8 +26,11 @@ }, "main": "./lib/erede.js", "dependencies": { - "semver": "^5.0.3", - "bl": "^3.0.0" + "bl": "^4.0.2", + "semver": "^7.3.2" + }, + "devDependencies": { + "eslint": "^7.1.0" }, "config": { "blanket": { @@ -32,4 +39,4 @@ } }, "readmeFilename": "README.md" -} +} \ No newline at end of file