Skip to content

Commit

Permalink
Return methodType and encryptAsObject (#12)
Browse files Browse the repository at this point in the history
* Return methodType='paymentMethodCipher'

* Provide both encrypt and encryptToObject
  • Loading branch information
sHesl committed Aug 15, 2018
1 parent 753e0b5 commit 1a16b60
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
3 changes: 2 additions & 1 deletion package.json
@@ -1,10 +1,11 @@
{
"name": "ravelinjs",
"version": "0.0.8",
"version": "0.0.9",
"description": "Ravelin Browser Card Encryption Library.",
"main": "ravelin.js",
"scripts": {
"minify": "uglifyjs --support-ie8 -o ravelin.min.js ravelin.js",
"minify-latest": "uglifyjs --support-ie8 -o ravelin.min.js ravelin.js && cp ravelin.min.js ravelin-$(node -e 'console.log(require(\"./package.json\").version)').min.js",
"test": "mocha -C test/test.js",
"test-appium": "npm run minify && wdio ./test/appium.conf.js",
"test-crossbrowser": "npm run minify && wdio ./test/crossbrowser.conf.js",
Expand Down
27 changes: 20 additions & 7 deletions ravelin.js
Expand Up @@ -9,7 +9,7 @@
}
}(typeof self !== 'undefined' ? self : this, function () {

var version = '0.0.8';
var version = '0.0.9';

var RSAKey = (function(){
// prng4.js - uses Arcfour as a PRNG
Expand Down Expand Up @@ -1033,14 +1033,14 @@
}

/**
* encrypt takes the encrypted card details and prepares them to be sent
* to Ravelin.
* encryptAsObject takes the encrypted card details and prepares them to be sent
* to Ravelin, with the resulting payload returned as an object.
*
* @param {Object} details An object containing properties pan, month, year,
* month, and nameOnCard (optional).
* @return {String} The encrypted paylaod to be sent to Ravelin.
* @return {Object} The encrypted payload to be sent to Ravelin.
*/
RavelinJS.prototype.encrypt = function(details) {
RavelinJS.prototype.encryptAsObject = function(details) {
if (!this.rsaKey) {
throw new Error("RavelinJS Key has not been set");
}
Expand Down Expand Up @@ -1091,14 +1091,27 @@
var aesResult = aesEncrypt(JSON.stringify(details));
var rsaResultB64 = rsaEncrypt(this.rsaKey, aesResult.aesKeyB64, aesResult.ivB64);

return JSON.stringify({
return {
methodType: 'paymentMethodCipher',
cardCiphertext: aesResult.ciphertextB64,
aesKeyCiphertext: rsaResultB64,
algorithm: "RSA_WITH_AES_256_GCM",
ravelinjsVersion: version,
});
};
};

/**
* encrypt takes the encrypted card details and prepares them to be sent
* to Ravelin, with the resulting payload returned as a string.
*
* @param {Object} details An object containing properties pan, month, year,
* month, and nameOnCard (optional).
* @return {String} The encrypted payload to be sent to Ravelin.
*/
RavelinJS.prototype.encrypt = function(details) {
return JSON.stringify(this.encryptAsObject(details));
}

if ((typeof window !== 'undefined' && window.addEventListener) || (typeof document !== 'undefined' && document.attachEvent)) {
sjcl.random.startCollectors();
}
Expand Down
22 changes: 21 additions & 1 deletion test/test.js
Expand Up @@ -57,10 +57,11 @@ describe('ravelinjs', function() {
function validCipher(c) {
c = JSON.parse(c);
return (
c.methodType == 'paymentMethodCipher' &&
c.cardCiphertext != "" && c.cardCiphertext.length > 10 &&
c.aesKeyCiphertext != "" && c.aesKeyCiphertext.length > 10 &&
c.algorithm == "RSA_WITH_AES_256_GCM" &&
c.ravelinjsVersion == "0.0.8"
c.ravelinjsVersion == "0.0.9"
);
}

Expand All @@ -82,6 +83,25 @@ describe('ravelinjs', function() {
year: "20",
})).to.satisfy(validCipher);
});

it('can return payload as object', function() {
ravelin.setRSAKey(dummyRSAKey);
function validCipher(c) {
return (
c.methodType == 'paymentMethodCipher' &&
c.cardCiphertext != "" && c.cardCiphertext.length > 10 &&
c.aesKeyCiphertext != "" && c.aesKeyCiphertext.length > 10 &&
c.algorithm == "RSA_WITH_AES_256_GCM" &&
c.ravelinjsVersion == "0.0.9"
);
}

expect(ravelin.encryptAsObject({
pan: '4111 1111 1111 1111',
month: 10,
year: 2020,
})).to.satisfy(validCipher);
});
});

});

0 comments on commit 1a16b60

Please sign in to comment.