Skip to content

Commit

Permalink
Merge pull request #6 from unravelin/tracking-object
Browse files Browse the repository at this point in the history
Expose additional functions
  • Loading branch information
sHesl authored Jul 18, 2018
2 parents 2f6e30d + 2c6f505 commit cb7fd8d
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 30 deletions.
17 changes: 17 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# EditorConfig is awesome: https://EditorConfig.org

# top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

# Matches multiple files with brace expansion notation
# Set default charset
[*.{js,py}]
indent_style = space
indent_size = 2
charset = utf-8
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ravelinjs",
"version": "0.0.5",
"version": "0.0.6",
"description": "Ravelin Browser Card Encryption Library.",
"main": "ravelin.js",
"scripts": {
Expand Down
102 changes: 93 additions & 9 deletions ravelin.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
}
}(typeof self !== 'undefined' ? self : this, function () {

var version = '0.0.5';
var version = '0.0.6';

var RSAKey = (function(){
// prng4.js - uses Arcfour as a PRNG
Expand Down Expand Up @@ -893,6 +893,10 @@
return aesKeyAndIVCiphertextBase64;
}

/**
* RavelinJS provides card encryption and wraps Ravelin's tracking library.
* @param {String} key The RSA Key to be provided to setRSAKey
*/
function RavelinJS(key) {
if (key) this.setRSAKey(key);
}
Expand All @@ -902,41 +906,113 @@
* track invokes the Ravelin client-side tracking script. You must have set
* the public API key in advance of calling track, so that it can submit the
* data directly to Ravelin. Its execution is asynchronous.
*
* See https://developer.ravelin.com/v2/#tracking
*
* @param {String} eventName A description of what has occurred.
* @param {Object} meta Any additional metadata you wish to use to describe the page.
*/
RavelinJS.prototype.track = function() {
this._ravelin('track');
this._ravelin(['track'].concat(Array.prototype.slice.call(arguments, 0)));
}

/**
* trackPage logs the page view. Call this from as many pages as possible.
*
* @param {Object} meta Any additional metadata you wish to use to describe the page.
*/
RavelinJS.prototype.trackPage = function() {
this._ravelin('trackPage');
this._ravelin(['trackPage'].concat(Array.prototype.slice.call(arguments, 0)));
}

/**
* trackLogout informs Ravelin of logout events and resets the associated customerId and tempCustomerId.
*
* @param {Object} meta Any additional metadata you wish to use to describe the event.
*/
RavelinJS.prototype.trackLogout = function() {
this._ravelin(['trackLogout'].concat(Array.prototype.slice.call(arguments, 0)));
}

/**
* trackFingerprint sends device information back to Ravelin.
* trackFingerprint sends device information back to Ravelin. Invoke from
* the checkout page of your payment flow.
*/
RavelinJS.prototype.trackFingerprint = function() {
this._ravelin('fingerprint');
this._ravelin(['fingerprint']);
}

RavelinJS.prototype.setPublicAPIKey = function(apiKey) {
this._ravelin('setApiKey', this.apiKey = apiKey)
/**
* setPublicAPIKey sets the API Key used to authenticate with Ravelin. It
* should be called before anything else.
*
* @param {String} apiKey
*/
RavelinJS.prototype.setPublicAPIKey = function() {
this.apiKey = arguments[0];
this._ravelin(['setApiKey'].concat(Array.prototype.slice.call(arguments, 0)));
}

RavelinJS.prototype._ravelin = function() {
/**
* setCookieDomain configures where Ravelin will store any cookies on your
* domain. Set as high as possible, e.g. ".mysite.com" rather than
* ".www.uk.mysite.com".
*
* @param {String} domain
*/
RavelinJS.prototype.setCookieDomain = function() {
this._ravelin(['setCookieDomain'].concat(Array.prototype.slice.call(arguments, 0)));
}

/**
* Set the customerId submitted with requests. This is used to associate device activity
* with a specific user.
*
* @param {String} customerId
*/
RavelinJS.prototype.setCustomerId = function() {
this._ravelin(['setCustomerId'].concat(Array.prototype.slice.call(arguments, 0)));
}

/**
* Set the tempCustomerId submitted with requests. This is used as a temporary association between device/
* session data and a user, and should be followed with a v2/login request to Ravelin as soon as a
* customerId is available
*
* @param {String} customerId
*/
RavelinJS.prototype.setTempCustomerId = function() {
this._ravelin(['setTempCustomerId'].concat(Array.prototype.slice.call(arguments, 0)));
}

/**
* Set the orderId submitted with requests. This is used to associate session-activity
* with a specific user.
*
* @param {String} orderId
*/
RavelinJS.prototype.setOrderId = function() {
this._ravelin(['setOrderId'].concat(Array.prototype.slice.call(arguments, 0)));
}

RavelinJS.prototype._ravelin = function(args) {
if (typeof this.apiKey !== 'string') {
throw new Error("No tracking API key set. See RavelinJS.setPublicAPIKey");
}
if (!window.ravelin) {
// https://developer.ravelin.com/v2/#device-tracking.
(function(r,a,v,e,l,i,n){r[l]=r[l]||function(){(r[l].q=r[l].q||[]).push(arguments)};i=a.createElement(v);i.async=i.defer=1;i.src=e;a.body.appendChild(i)})(window, document, 'script', 'https://cdn.ravelin.net/js/rvn-beta.min.js', 'ravelin');
}
window.ravelin.apply(window, arguments)

window.ravelin.apply(window, args);
}
}

/**
* setKey configures RavelinJS with the given public key, it the format that
* Ravelin provides it.
*
* @param {String} rawPubKey The public RSA key provided by Ravelin, used to encrypt card details.
*/
RavelinJS.prototype.setRSAKey = function(rawPubKey) {
if (typeof rawPubKey !== "string") {
Expand All @@ -952,6 +1028,14 @@
this.rsaKey.setPublic(split[1], split[0]);
}

/**
* encrypt takes the encrypted card details and prepares them to be sent
* to Ravelin.
*
* @param {Object} details An object containing properties pan, month, year,
* month, and nameOnCard (optional).
* @return {String} The encrypted paylaod to be sent to Ravelin.
*/
RavelinJS.prototype.encrypt = function(details) {
if (!this.rsaKey) {
throw new Error("RavelinJS Key has not been set");
Expand Down
30 changes: 15 additions & 15 deletions test/crossbrowser.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ exports.config = Object.assign(base, {
version: 'latest',
platform: 'Windows 10',
},
{
build: 'ravelinjs 1.0',
name: 'osx chromeLatest',
browserName: 'Chrome',
version: 'latest',
platform: 'Mac OSX 10.12',
},
// {
// build: 'ravelinjs 1.0',
// name: 'osx chromeLatest',
// browserName: 'Chrome',
// version: 'latest',
// platform: 'Mac OSX 10.12',
// },
{
build: 'ravelinjs 1.0',
name: 'win8 chrome18',
Expand All @@ -51,13 +51,13 @@ exports.config = Object.assign(base, {
// version: '11',
// platform: 'Mac OSX 10.13',
// },
{
build: 'ravelinjs 1.0',
name: 'osx safari10',
browserName: 'Safari',
version: '10',
platform: 'Mac OSX 10.12',
},
// {
// build: 'ravelinjs 1.0',
// name: 'osx safari10',
// browserName: 'Safari',
// version: '10',
// platform: 'Mac OSX 10.12',
// },
{
build: 'ravelinjs 1.0',
name: 'osx safari6',
Expand Down Expand Up @@ -185,4 +185,4 @@ exports.config.onPrepares.push(function(config, capabilities) {

exports.config.onCompletes.push(function () {
cbt.stop();
});
});
7 changes: 4 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('ravelinjs', function() {

it('validates no unknown attributes are present', function() {
ravelin.setRSAKey(dummyRSAKey);

const err = "RavelinJS validation: encrypt only allows properties pan, year, month, nameOnCard";
expect(() => ravelin.encrypt({pan: "4111 1111 1111 1111", month: 1, year: "18", "cvv": "123"})).to.throw(err);
});
Expand All @@ -60,7 +60,7 @@ describe('ravelinjs', function() {
c.cardCiphertext != "" && c.cardCiphertext.length > 10 &&
c.aesKeyCiphertext != "" && c.aesKeyCiphertext.length > 10 &&
c.algorithm == "RSA_WITH_AES_256_GCM" &&
c.ravelinjsVersion == "0.0.5"
c.ravelinjsVersion == "0.0.6"
);
}

Expand All @@ -83,4 +83,5 @@ describe('ravelinjs', function() {
})).to.satisfy(validCipher);
});
});
});

});

0 comments on commit cb7fd8d

Please sign in to comment.