card-connect-api
Advanced tools
Comparing version 1.0.1 to 1.0.2
const httpism = require("httpism"); | ||
const errorTypeForCode = require("./errors"); | ||
function CommandCancelledError() { | ||
this.name = 'CommandCancelledError'; | ||
this.message = 'Command cancelled'; | ||
this.stack = (new Error()).stack; | ||
} | ||
CommandCancelledError.prototype = new Error; | ||
function errorTypeForCode(code) { | ||
if (code === 8) { | ||
return CommandCancelledError | ||
} | ||
} | ||
class CardConnectApi { | ||
@@ -25,56 +13,40 @@ constructor({ baseUrl, merchantId, authorizationHeader }) { | ||
async listTerminals() { | ||
const response = await this._client.post("listTerminals", { | ||
return this._post("listTerminals", { | ||
merchantId: this._merchantId | ||
}); | ||
return response.terminals; | ||
}).then(response => response.terminals); | ||
} | ||
async connectTerminal({ hsn, force }) { | ||
const response = await this._client.post("connect", { | ||
return this._post("connect", { | ||
merchantId: this._merchantId, | ||
hsn, | ||
force | ||
}).then(response => { | ||
this._sessionKey = response.headers["x-cardconnect-sessionkey"].split( | ||
";" | ||
)[0]; | ||
return response.statusCode === 200 ? true : false; | ||
}); | ||
this._sessionKey = response.headers["x-cardconnect-sessionkey"].split( | ||
";" | ||
)[0]; | ||
return { | ||
connected: response.statusCode === 200 ? true : false | ||
}; | ||
} | ||
async sendMessage({ hsn, text }) { | ||
const response = await this._client.post("display", { | ||
return this._post("display", { | ||
merchantId: this._merchantId, | ||
hsn, | ||
text | ||
}).then(response => { | ||
return response.statusCode === 200 ? true : false; | ||
}); | ||
return { | ||
delivered: response.statusCode === 200 ? true : false | ||
}; | ||
} | ||
async readCard({ hsn, amount }) { | ||
try { | ||
const response = await this._client.post("readCard", { | ||
merchantId: this._merchantId, | ||
hsn, | ||
amount | ||
}); | ||
return response; | ||
} catch (e) { | ||
const errorType = errorTypeForCode(e.body.errorCode) | ||
if (errorType) { | ||
throw new errorType() | ||
} | ||
throw e; | ||
} | ||
return this._post("readCard", { | ||
merchantId: this._merchantId, | ||
hsn, | ||
amount | ||
}); | ||
} | ||
async ping({ hsn }) { | ||
return this._client.post("ping", { | ||
return this._post("ping", { | ||
merchantId: this._merchantId, | ||
@@ -89,2 +61,9 @@ hsn | ||
_post(path, options) { | ||
return this._client.post(path, options).catch(e => { | ||
const errorType = errorTypeForCode(e.body.errorCode); | ||
throw { error: new errorType() }; | ||
}); | ||
} | ||
_createClient() { | ||
@@ -91,0 +70,0 @@ const defaultHeaders = { |
{ | ||
"name": "card-connect-api", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Node Wrapper for CardConnectApi", | ||
@@ -5,0 +5,0 @@ "main": "CardConnectApi.js", |
22
test.js
@@ -19,3 +19,3 @@ const assert = require("assert"); | ||
it("should connect to a terminal", async () => { | ||
const response = await this.api.connectTerminal({ | ||
const connected = await this.api.connectTerminal({ | ||
hsn: process.env.TESTABLE_TERMINAL, | ||
@@ -25,3 +25,3 @@ force: true | ||
assert(response.connected); | ||
assert(connected); | ||
}); | ||
@@ -34,3 +34,3 @@ | ||
}); | ||
const response = await this.api.sendMessage({ | ||
const delivered = await this.api.sendMessage({ | ||
hsn: process.env.TESTABLE_TERMINAL, | ||
@@ -40,3 +40,3 @@ text: "hacked bro" | ||
assert(response.delivered); | ||
assert(delivered); | ||
}); | ||
@@ -49,8 +49,8 @@ | ||
}); | ||
const response = await this.api.sendMessage({ | ||
const cleared = await this.api.sendMessage({ | ||
hsn: process.env.TESTABLE_TERMINAL, | ||
text: '' | ||
text: "" | ||
}); | ||
assert(response.delivered); | ||
assert(cleared); | ||
}); | ||
@@ -76,3 +76,3 @@ | ||
console.log('> PRESS CANCEL ON THE TERMINAL NOW <') | ||
console.log("> PRESS CANCEL ON THE TERMINAL NOW <"); | ||
@@ -84,4 +84,4 @@ try { | ||
}); | ||
} catch(e) { | ||
assert.equal(e.message, 'Command cancelled') | ||
} catch (e) { | ||
assert.equal(e.error.message, "User cancelled"); | ||
} | ||
@@ -96,3 +96,3 @@ }); | ||
console.log('> SWIPE YOUR CARD ON THE TERMINAL NOW <') | ||
console.log("> SWIPE YOUR CARD ON THE TERMINAL NOW <"); | ||
@@ -99,0 +99,0 @@ const response = await this.api.readCard({ |
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
17415
6
191
1
99