Skip to content

Commit

Permalink
Merge pull request #363 from recurly/bt-fraud
Browse files Browse the repository at this point in the history
Adds support for Braintree deviceData
  • Loading branch information
snodgrass23 authored Jul 6, 2017
2 parents 1a98749 + 6d8b2c1 commit 1d0e8fa
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
3 changes: 2 additions & 1 deletion lib/recurly.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ const defaults = {
cors: true,
fraud: {
kount: { dataCollector: false },
litle: { sessionId: null }
litle: { sessionId: undefined },
braintree: { deviceData: undefined }
},
api: 'https://api.recurly.com/js/v1',
fields: {
Expand Down
28 changes: 20 additions & 8 deletions lib/recurly/fraud.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,28 @@ export class Fraud extends Emitter {
this.recurly = recurly;
this.selector = this.recurly.config.fields.number.selector;
this.dataCollectorInitiated = false;
this.form = null;
this.form = undefined;

recurly.ready(this.dataCollector.bind(this));
if (this.shouldCollectData) {
recurly.ready(this.dataCollector.bind(this));
}
}

get shouldCollectData () {
return !!this.recurly.config.fraud.kount.dataCollector;
}

/**
* gets the fraud params present including injected form fields
* as well as grabbing litle session id if configured
*/
params (data) {
if (!data) return null;

var fraudParams = [];
let fraudParams = [];

if (this.recurly.config.fraud.kount.dataCollector && data['fraud_session_id']) {
fraudParams.push({
processor: 'kount',
session_id: data['fraud_session_id']
session_id: data.fraud_session_id
});
}

Expand All @@ -40,6 +44,13 @@ export class Fraud extends Emitter {
});
}

if (this.recurly.config.fraud.braintree.deviceData) {
fraudParams.push({
processor: 'braintree',
session_id: this.recurly.config.fraud.braintree.deviceData
});
}

return fraudParams;
}

Expand All @@ -48,7 +59,8 @@ export class Fraud extends Emitter {
* Recurly server and injects them into the payment form
*/
dataCollector () {
if (!this.recurly.config.fraud.kount.dataCollector || this.dataCollectorInitiated) return;
if (this.dataCollectorInitiated) return;

this.dataCollectorInitiated = true;

this.getForm();
Expand All @@ -65,7 +77,7 @@ export class Fraud extends Emitter {
}

addNodes (contentString) {
var tempContainer = document.createElement("div");
var tempContainer = document.createElement('div');
tempContainer.innerHTML = contentString;

each(tempContainer.childNodes, node => this.form.appendChild(node));
Expand Down
18 changes: 10 additions & 8 deletions lib/recurly/token.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import each from 'component-each';
import type from 'component-type';
import {normalize} from '../util/normalize';
import {uuid} from '../util/uuid';
import errors from '../errors';

const debug = require('debug')('recurly:token');
Expand All @@ -14,7 +15,7 @@ module.exports = token;
* @private
*/

var fields = [
const FIELDS = [
'first_name',
'last_name',
'address1',
Expand Down Expand Up @@ -54,8 +55,8 @@ var fields = [
function token (options, done) {
debug('token');

var data = normalize(fields, options, { parseCard: true });
var inputs = data.values;
let data = normalize(FIELDS, options, { parseCard: true });
let inputs = data.values;

if (!this.configured) {
throw errors('not-configured');
Expand All @@ -68,14 +69,15 @@ function token (options, done) {
if (this.config.parent) {
inputs.fraud = this.fraud.params(inputs);

if (this.hostedFields.errors.length) {
if (this.hostedFields.errors.length > 0) {
throw this.hostedFields.errors[0];
}
const id = Math.floor(Math.random() * 1e10);

const id = uuid();
this.bus.send('token:init', { id, inputs });
this.once(`token:done:${id}`, msg => complete(msg.err, msg.token));
} else {
var userErrors = validate.call(this, inputs);
let userErrors = validate.call(this, inputs);
if (userErrors.length) {
return done(errors('validation', { fields: userErrors }));
}
Expand All @@ -100,7 +102,7 @@ function token (options, done) {
*/

function validate (input) {
var errors = [];
let errors = [];

if (!this.validate.cardNumber(input.number)) {
errors.push('number');
Expand All @@ -123,7 +125,7 @@ function validate (input) {
}

each(this.config.required, function(field) {
if (!input[field] && ~fields.indexOf(field)) {
if (!input[field] && ~FIELDS.indexOf(field)) {
errors.push(field);
}
});
Expand Down

0 comments on commit 1d0e8fa

Please sign in to comment.