diff --git a/lib/recurly/fraud.js b/lib/recurly/fraud.js index e2c6d872e..94592924b 100644 --- a/lib/recurly/fraud.js +++ b/lib/recurly/fraud.js @@ -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'; @@ -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)); @@ -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 @@ -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) => { @@ -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 }); - } - } }