Socket
Socket
Sign inDemoInstall

creditcards-tokenizer

Package Overview
Dependencies
Maintainers
2
Versions
904
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

creditcards-tokenizer - npm Package Compare versions

Comparing version 1.2.7 to 1.2.8

5

package.json
{
"name": "creditcards-tokenizer",
"version": "1.2.7",
"version": "1.2.8",
"description": "Wix Restaurants credit-cards tokenizer",

@@ -38,3 +38,4 @@ "main": "dist/index.js",

"webpack-dev-server": "^1.10.1",
"xhr2": "^0.1.3"
"xhr2": "^0.1.3",
"q": "^1.4.1"
},

@@ -41,0 +42,0 @@ "dependencies": {},

79

src/CommonProtocolClient.js

@@ -1,47 +0,50 @@

"use strict"
'use strict'
import Q from 'q'
export class CommonProtocolClient {
constructor({XMLHttpRequest, endpointUrl, timeout}) {
this.XMLHttpRequest = XMLHttpRequest
this.endpointUrl = endpointUrl
this.timeout = timeout || 0
this._XMLHttpRequest = XMLHttpRequest
this._endpointUrl = endpointUrl
this._timeout = timeout || 0
}
doRequest(resource, request) {
let This = this
return new Promise(function(resolve, reject) {
let xhr = new This.XMLHttpRequest()
xhr.ontimeout = function() {
reject({
code: "timeout",
description: "request timed out"
let deferred = Q.defer()
let xhr = new this._XMLHttpRequest()
xhr.ontimeout = () => {
deferred.reject({
code: 'timeout',
description: 'request timed out'
})
}
xhr.onerror = () => {
deferred.reject({
code: 'network_down',
description: 'network is down'
})
}
xhr.onload = () => {
try {
let response = JSON.parse(xhr.response)
if (response.error) {
deferred.reject(response.error)
} else {
deferred.resolve(response.value)
}
} catch (e) {
deferred.reject({
code: 'protocol',
description: 'unexpected response format'
})
}
xhr.onerror = function() {
reject({
code: "network_down",
description: "network is down"
})
}
xhr.onload = function() {
try {
let response = JSON.parse(xhr.response)
if (response.error) {
reject(response.error)
} else {
resolve(response.value)
}
} catch (e) {
reject({
code: "protocol",
description: "unexpected response format"
})
}
}
xhr.open("POST", This.endpointUrl + resource, true)
xhr.timeout = This.timeout
xhr.setRequestHeader("Content-Type", "application/json")
xhr.send(JSON.stringify(request))
})
}
xhr.open('POST', `${this._endpointUrl}${resource}`, true)
xhr.timeout = this._timeout
xhr.setRequestHeader('Content-Type', 'application/json')
xhr.send(JSON.stringify(request))
return deferred.promise
}
}

@@ -1,4 +0,4 @@

"use strict"
'use strict'
import {CommonProtocolClient} from "./CommonProtocolClient.js"
import {CommonProtocolClient} from './CommonProtocolClient.js'

@@ -9,3 +9,3 @@ export class CreditcardsTokenizer {

XMLHttpRequest: XMLHttpRequest,
endpointUrl: endpointUrl || "https://pay.wix.com/cards/",
endpointUrl: endpointUrl || 'https://pay.wix.com/cards/',
timeout: timeout || 0

@@ -15,7 +15,7 @@ })

tokenize({card}) {
return this.client.doRequest("tokenize", {card})
return this.client.doRequest('tokenize', {card})
}
intransit({permanentToken, additionalInfo}) {
return this.client.doRequest("intransit", {permanentToken, additionalInfo})
return this.client.doRequest('intransit', {permanentToken, additionalInfo})
}
}

@@ -1,27 +0,27 @@

"use strict"
'use strict'
import http from "http"
import _ from "lodash"
import http from 'http'
import _ from 'lodash'
export class CommonProtocolDriver {
constructor({port}) {
this.server = http.createServer(this._handler.bind(this))
this.port = port
this._server = http.createServer(this._handler.bind(this))
this._port = port
this.reset()
}
start() {
this.server.listen(this.port, "127.0.0.1")
this._server.listen(this._port, '127.0.0.1')
}
stop() {
this.server.close()
this._server.close()
}
reset() {
this.rules = {}
this._rules = {}
}
addRule({resource, request, response, delay, useRawResponse}) {
delay = delay || 0
let resourceRules = this.rules[resource]
let resourceRules = this._rules[resource]
if (!resourceRules) {
resourceRules = []
this.rules[resource] = resourceRules
this._rules[resource] = resourceRules
}

@@ -32,9 +32,9 @@ resourceRules.push({request, response, delay, useRawResponse})

let This = this
let body = ""
req.on('data', function (data) {
let body = ''
req.on('data', (data) => {
body += data
})
req.on('end', function () {
req.on('end', () => {
let request = JSON.parse(body)
let rule = _.find(This.rules[req.url], function(rule) {
let rule = _.find(this._rules[req.url], (rule) => {
return _.isEqual(rule.request, request)

@@ -44,3 +44,3 @@ })

if (rule) {
_.delay(function() {
_.delay(() => {
res.writeHead(200, {'Content-Type': rule.useRawResponse ? 'text/html' : 'application/json'})

@@ -47,0 +47,0 @@ res.end(rule.useRawResponse ? rule.response : JSON.stringify(rule.response))

@@ -1,9 +0,9 @@

"use strict"
'use strict'
import {CreditcardsTokenizer} from "../src/CreditcardsTokenizer.js"
import {CreditcardsTokenizerDriver} from "./CreditcardsTokenizerDriver.js"
import {expect, assert} from "chai"
import {XMLHttpRequest} from "xhr2"
import {CreditcardsTokenizer} from '../src/CreditcardsTokenizer.js'
import {CreditcardsTokenizerDriver} from './CreditcardsTokenizerDriver.js'
import {expect, assert} from 'chai'
import {XMLHttpRequest} from 'xhr2'
describe("CreditcardsTokenizer", function() {
describe('CreditcardsTokenizer', () => {
let tokenizerServicePort = 10000

@@ -13,4 +13,4 @@ let driver = new CreditcardsTokenizerDriver({

})
let endpointUrl = "http://localhost:" + tokenizerServicePort + "/"
let invalidEndpointUrl = "http://thisisanonexistentdomain.thisdoesntexist/"
let endpointUrl = `http://localhost:${tokenizerServicePort}/`
let invalidEndpointUrl = 'http://thisisanonexistentdomain.thisdoesntexist/'

@@ -20,3 +20,3 @@ let tokenizer = new CreditcardsTokenizer({XMLHttpRequest, endpointUrl})

let card = {
number: "4580458045804580",
number: '4580458045804580',
expiration: {

@@ -28,5 +28,5 @@ year: 2020,

let intransitToken = {
token: "580a3a7b-ec10-45f7-8997-d1d1f84cd754",
token: '580a3a7b-ec10-45f7-8997-d1d1f84cd754',
creditCard: {
lastDigits: "4580",
lastDigits: '4580',
expiration: {

@@ -36,9 +36,9 @@ year: 2020,

},
network: "visa"
network: 'visa'
}
}
let permanentToken = {
token: "12345678-90ab-cdef-1234-567890abcdef",
token: '12345678-90ab-cdef-1234-567890abcdef',
creditCard: {
lastDigits: "4580",
lastDigits: '4580',
expiration: {

@@ -48,18 +48,18 @@ year: 2020,

},
network: "visa"
network: 'visa'
}
}
let additionalInfo = {
csc: "123",
csc: '123',
publicFields: {
holderId: "1234567890",
holderName: "Chuck Norris",
billingAddress: "123 main st",
billingPostalCode: "90210"
holderId: '1234567890',
holderName: 'Chuck Norris',
billingAddress: '123 main st',
billingPostalCode: '90210'
}
}
let intransitTokenWithAdditionalFields = {
token: "663a3def-276a-78fc-13ab-9db1f54c7754",
token: '663a3def-276a-78fc-13ab-9db1f54c7754',
creditCard: {
lastDigits: "4580",
lastDigits: '4580',
expiration: {

@@ -69,3 +69,3 @@ year: 2020,

},
network: "visa",
network: 'visa',
additionalFields: {

@@ -78,22 +78,22 @@ publicFields: additionalInfo.publicFields

let someError = {
code: "someCode",
description: "someDescription"
code: 'someCode',
description: 'someDescription'
}
before(function() {
before(() => {
driver.start()
})
after(function() {
after(() => {
driver.stop()
})
beforeEach(function() {
beforeEach(() => {
driver.reset()
})
describe("tokenize", function() {
it ('creates in-transit tokens from valid cards', function() {
describe('tokenize', () => {
it ('creates in-transit tokens from valid cards', () => {
driver.addRule({
resource: "/tokenize",
resource: '/tokenize',
request: {card},

@@ -105,16 +105,16 @@ response: {

return tokenizer.tokenize({card}).then(function(intransitToken) {
return tokenizer.tokenize({card}).then((intransitToken) => {
expect(intransitToken.token).to.not.be.empty
expect(intransitToken.creditCard.lastDigits).to.be.equal(card.number.slice(-4))
expect(intransitToken.creditCard.expiration).to.deep.equal(card.expiration)
expect(intransitToken.creditCard.network).to.be.equal("visa")
expect(intransitToken.creditCard.network).to.be.equal('visa')
expect(intransitToken.creditCard.additionalFields).to.not.exist
}, function(error) {
assert.ok(false, "Tokenizing a valid card returned " + JSON.stringify(error))
}, (error) => {
assert.ok(false, `Tokenizing a valid card returned ${JSON.stringify(error)}`)
})
})
it ('gracefully fails on invalid cards', function() {
it ('gracefully fails on invalid cards', () => {
driver.addRule({
resource: "/tokenize",
resource: '/tokenize',
request: {card},

@@ -126,6 +126,6 @@ response: {

return tokenizer.tokenize({card}).then(function(intransitToken) {
return tokenizer.tokenize({card}).then((intransitToken) => {
// Unexpected success
assert.ok(false, "Tokenizing an invalid card returned " + JSON.stringify(intransitToken))
}, function(error) {
assert.ok(false, `Tokenizing an invalid card returned ${JSON.stringify(intransitToken)}`)
}, (error) => {
expect(error).to.deep.equal(someError)

@@ -135,3 +135,3 @@ })

it ('gracefully fails on timeout', function() {
it ('gracefully fails on timeout', () => {
let tokenizerWithTimeout = new CreditcardsTokenizer({

@@ -144,3 +144,3 @@ XMLHttpRequest: XMLHttpRequest,

driver.addRule({
resource: "/tokenize",
resource: '/tokenize',
request: {card},

@@ -153,7 +153,7 @@ response: {

return tokenizerWithTimeout.tokenize({card}).then(function(intransitToken) {
return tokenizerWithTimeout.tokenize({card}).then((intransitToken) => {
// Unexpected success
assert.ok(false, "Tokenizing should have timed out, but returned " + JSON.stringify(intransitToken))
}, function(error) {
expect(error.code).to.equal("timeout")
assert.ok(false, `Tokenizing should have timed out, but returned ${JSON.stringify(intransitToken)}`)
}, (error) => {
expect(error.code).to.equal('timeout')
expect(error.description).to.not.be.empty

@@ -163,3 +163,3 @@ })

it ('gracefully fails when network is down', function() {
it ('gracefully fails when network is down', () => {
let tokenizerWithInvalidEndpointUrl = new CreditcardsTokenizer({

@@ -170,7 +170,7 @@ XMLHttpRequest: XMLHttpRequest,

return tokenizerWithInvalidEndpointUrl.tokenize({card}).then(function(intransitToken) {
return tokenizerWithInvalidEndpointUrl.tokenize({card}).then((intransitToken) => {
// Unexpected success
assert.ok(false, "Network should be down, but request returned " + JSON.stringify(intransitToken))
}, function(error) {
expect(error.code).to.equal("network_down")
assert.ok(false, `Network should be down, but request returned ${JSON.stringify(intransitToken)}`)
}, (error) => {
expect(error.code).to.equal('network_down')
expect(error.description).to.not.be.empty

@@ -180,15 +180,15 @@ })

it ('gracefully fails on protocol error', function() {
it ('gracefully fails on protocol error', () => {
driver.addRule({
resource: "/tokenize",
resource: '/tokenize',
request: {card},
response: "<html><head><title>Error 500</title></head></html>",
response: '<html><head><title>Error 500</title></head></html>',
useRawResponse: true
})
return tokenizer.tokenize({card}).then(function(intransitToken) {
return tokenizer.tokenize({card}).then((intransitToken) => {
// Unexpected success
assert.ok(false, "Expected protocol error, but request returned " + JSON.stringify(intransitToken))
}, function(error) {
expect(error.code).to.equal("protocol")
assert.ok(false, `Expected protocol error, but request returned ${JSON.stringify(intransitToken)}`)
}, (error) => {
expect(error.code).to.equal('protocol')
expect(error.description).to.not.be.empty

@@ -199,6 +199,6 @@ })

describe("intransit", function() {
it ('creates in-transit tokens from valid permanent tokens', function() {
describe('intransit', () => {
it ('creates in-transit tokens from valid permanent tokens', () => {
driver.addRule({
resource: "/intransit",
resource: '/intransit',
request: {permanentToken, additionalInfo},

@@ -210,3 +210,3 @@ response: {

return tokenizer.intransit({permanentToken, additionalInfo}).then(function(intransitToken) {
return tokenizer.intransit({permanentToken, additionalInfo}).then((intransitToken) => {
expect(intransitToken.token).to.not.be.empty

@@ -219,4 +219,4 @@ expect(intransitToken.creditCard.lastDigits).to.be.equal(permanentToken.creditCard.lastDigits)

expect(intransitToken.creditCard.additionalFields.publicFields).to.deep.equal(additionalInfo.publicFields)
}, function(error) {
assert.ok(false, "Tokenizing a valid card returned " + JSON.stringify(error))
}, (error) => {
assert.ok(false, `Tokenizing a valid card returned ${JSON.stringify(error)}`)
})

@@ -226,5 +226,5 @@

it ('gracefully fails on invalid permanent tokens', function() {
it ('gracefully fails on invalid permanent tokens', () => {
driver.addRule({
resource: "/intransit",
resource: '/intransit',
request: {permanentToken, additionalInfo},

@@ -236,6 +236,6 @@ response: {

return tokenizer.intransit({permanentToken, additionalInfo}).then(function(intransitToken) {
return tokenizer.intransit({permanentToken, additionalInfo}).then((intransitToken) => {
// Unexpected success
assert.ok(false, "Tokenizing an invalid permanent token returned " + JSON.stringify(intransitToken))
}, function(error) {
assert.ok(false, `Tokenizing an invalid permanent token returned ${JSON.stringify(intransitToken)}`)
}, (error) => {
expect(error).to.deep.equal(someError)

@@ -245,3 +245,3 @@ })

it ('gracefully fails on timeout', function() {
it ('gracefully fails on timeout', () => {
let tokenizerWithTimeout = new CreditcardsTokenizer({

@@ -254,3 +254,3 @@ XMLHttpRequest: XMLHttpRequest,

driver.addRule({
resource: "/intransit",
resource: '/intransit',
request: {permanentToken, additionalInfo},

@@ -263,7 +263,7 @@ response: {

return tokenizerWithTimeout.intransit({permanentToken, additionalInfo}).then(function(intransitToken) {
return tokenizerWithTimeout.intransit({permanentToken, additionalInfo}).then((intransitToken) => {
// Unexpected success
assert.ok(false, "Tokenizing a permanent token should have timed out, but returned " + JSON.stringify(intransitToken))
}, function(error) {
expect(error.code).to.equal("timeout")
assert.ok(false, `Tokenizing a permanent token should have timed out, but returned ${JSON.stringify(intransitToken)}`)
}, (error) => {
expect(error.code).to.equal('timeout')
expect(error.description).to.not.be.empty

@@ -273,3 +273,3 @@ })

it ('gracefully fails when network is down', function() {
it ('gracefully fails when network is down', () => {
let tokenizerWithInvalidEndpointUrl = new CreditcardsTokenizer({

@@ -280,7 +280,7 @@ XMLHttpRequest: XMLHttpRequest,

return tokenizerWithInvalidEndpointUrl.intransit({permanentToken, additionalInfo}).then(function(intransitToken) {
return tokenizerWithInvalidEndpointUrl.intransit({permanentToken, additionalInfo}).then((intransitToken) => {
// Unexpected success
assert.ok(false, "Network should be down, but request returned " + JSON.stringify(intransitToken))
}, function(error) {
expect(error.code).to.equal("network_down")
assert.ok(false, `Network should be down, but request returned ${JSON.stringify(intransitToken)}`)
}, (error) => {
expect(error.code).to.equal('network_down')
expect(error.description).to.not.be.empty

@@ -290,15 +290,15 @@ })

it ('gracefully fails on protocol error', function() {
it ('gracefully fails on protocol error', () => {
driver.addRule({
resource: "/intransit",
resource: '/intransit',
request: {permanentToken, additionalInfo},
response: "<html><head><title>Error 500</title></head></html>",
response: '<html><head><title>Error 500</title></head></html>',
useRawResponse: true
})
return tokenizer.intransit({permanentToken, additionalInfo}).then(function(intransitToken) {
return tokenizer.intransit({permanentToken, additionalInfo}).then((intransitToken) => {
// Unexpected success
assert.ok(false, "Expected protocol error, but request returned " + JSON.stringify(intransitToken))
}, function(error) {
expect(error.code).to.equal("protocol")
assert.ok(false, `Expected protocol error, but request returned ${JSON.stringify(intransitToken)}`)
}, (error) => {
expect(error.code).to.equal('protocol')
expect(error.description).to.not.be.empty

@@ -305,0 +305,0 @@ })

@@ -1,4 +0,4 @@

"use strict"
'use strict'
import {CommonProtocolDriver} from "./CommonProtocolDriver.js"
import {CommonProtocolDriver} from './CommonProtocolDriver.js'

@@ -5,0 +5,0 @@ export class CreditcardsTokenizerDriver {

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc