Skip to content

Commit

Permalink
Merge pull request #470 from recurly/fraud-for-card-field
Browse files Browse the repository at this point in the history
Fixes Kount fraud detection when using Card field
  • Loading branch information
snodgrass23 authored Jul 31, 2018
2 parents c1356a9 + 60da93d commit 6842487
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions lib/recurly/fraud.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import each from 'component-each';
import find from 'component-find';
import Emitter from 'component-emitter';
import dom from '../util/dom';
import errors from '../errors';
Expand All @@ -10,9 +11,7 @@ export class Fraud extends Emitter {
super();

this.recurly = recurly;
this.selector = this.recurly.config.fields.number.selector;
this.dataCollectorInitiated = false;
this.form = undefined;

if (this.shouldCollectData) {
recurly.ready(this.dataCollector.bind(this));
Expand All @@ -23,6 +22,23 @@ export class Fraud extends Emitter {
return !!this.recurly.config.fraud.kount.dataCollector;
}

get form () {
if (this._form) return this._form;
const fields = this.recurly.config.fields;
const selectors = Object.keys(fields).map(name => fields[name].selector).filter(Boolean);
let form;
find(selectors, selector => {
const node = dom.findNodeInParents(window.document.querySelector(selector), 'form');
if (dom.element(node)) form = node;
});

if (form) {
return this._form = form;
} else {
throw errors('fraud-data-collector-missing-form', { selectors });
}
}

/**
* gets the fraud params present including injected form fields
* as well as grabbing litle session id if configured
Expand Down Expand Up @@ -63,8 +79,6 @@ export class Fraud extends Emitter {

this.dataCollectorInitiated = true;

this.getForm();

this.recurly.request.get({
route: '/fraud_data_collector',
done: (error, response) => {
Expand All @@ -85,12 +99,4 @@ export class Fraud extends Emitter {

each(tempContainer.childNodes, node => this.form.appendChild(node));
}

getForm () {
this.form = dom.findNodeInParents(window.document.querySelector(this.selector), 'form');

if (!dom.element(this.form)) {
throw errors('fraud-data-collector-missing-form', { selector: this.selector });
}
}
}

0 comments on commit 6842487

Please sign in to comment.