Skip to content

Commit

Permalink
WIP changes
Browse files Browse the repository at this point in the history
  • Loading branch information
gilv93 committed Aug 27, 2024
1 parent 1ea4ec5 commit 96cccce
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 16 deletions.
7 changes: 6 additions & 1 deletion lib/recurly.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ const DEFAULTS = {
},
report: false,
risk: {
threeDSecure: { preflightDeviceDataCollector: true }
threeDSecure: {
preflightDeviceDataCollector: true,
proactive: {
enabled: false,
}
}
},
api: DEFAULT_API_URL,
fields: {
Expand Down
6 changes: 2 additions & 4 deletions lib/recurly/risk/three-d-secure/strategy/braintree.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ export default class BraintreeStrategy extends ThreeDSecureStrategy {
}

get braintreeClientToken () {
return this.actionToken.three_d_secure.params.client_token;
return this.actionToken.three_d_secure.params.client_token
}

get nonce () {
return this.actionToken.three_d_secure.params.payment_method_nonce;
}

get amount () {
return this.actionToken.transaction.amount;
return this.actionToken.transaction?.amount || this.actionToken.three_d_secure.amount;
}

get billingInfo () {
Expand All @@ -54,9 +54,7 @@ export default class BraintreeStrategy extends ThreeDSecureStrategy {

this.whenReady(() => {
debug('Attempting to load braintree');

const { braintree, braintreeClientToken, amount, nonce, bin, billingInfo } = this;

const verifyCardOptions = {
amount: amount,
nonce: nonce,
Expand Down
4 changes: 2 additions & 2 deletions lib/recurly/risk/three-d-secure/strategy/strategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ export default class ThreeDSecureStrategy extends ReadinessEmitter {
static preflight () {}
static PREFLIGHT_TIMEOUT = 30000;

constructor ({ threeDSecure, actionToken }) {
constructor ({ threeDSecure, actionToken, proactiveToken }) {
super();
this.threeDSecure = threeDSecure;
this.actionToken = actionToken;
this.actionToken = actionToken || proactiveToken;
}

get strategyName () {
Expand Down
38 changes: 31 additions & 7 deletions lib/recurly/risk/three-d-secure/three-d-secure.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export class ThreeDSecure extends RiskConcern {
}, Promise.resolve([]));
}

constructor ({ risk, actionTokenId, challengeWindowSize }) {
constructor ({ risk, actionTokenId, challengeWindowSize, proactiveTokenId }) {
const existingConcern = risk.concerns.find((concern) => concern instanceof ThreeDSecure);
if (existingConcern) {
throw errors('3ds-multiple-instances', { name: 'ThreeDSecure', expect: 'to be the only concern' });
Expand All @@ -117,25 +117,28 @@ export class ThreeDSecure extends RiskConcern {
super({ risk });

this.actionTokenId = actionTokenId;
this.proactiveTokenId = proactiveTokenId;

this.validateChallengeWindowSize(challengeWindowSize);
this.challengeWindowSize = challengeWindowSize || this.constructor.CHALLENGE_WINDOW_SIZE_DEFAULT;

if (!actionTokenId) {
if (!actionTokenId && !proactiveTokenId) {
throw errors('invalid-option', { name: 'actionTokenId', expect: 'a three_d_secure_action_token_id' });
}

this.recurly.request.get({ route: `/tokens/${actionTokenId}` })
this.recurly.request.get({ route: `/tokens/${this.resolveToken()}` })
.catch(err => this.error(err))
.then(token => {
assertIsActionToken(token);
this.strategy = this.getStrategyForActionToken(token);
this.strategy.on('done', (...args) => this.onStrategyDone(...args));
if (this.resolveToken() == this.actionTokenId) {
this.resolveActionToken(token);
} else {
this.resolveProactiveToken(token);
}
this.markReady();
})
.catch(err => this.error(err));

this.report('create', { actionTokenId });
this.report('create', { actionTokenId, proactiveTokenId });
this.whenReady(() => this.report('ready', { strategy: this.strategy.strategyName }));
}

Expand Down Expand Up @@ -169,6 +172,11 @@ export class ThreeDSecure extends RiskConcern {
return new strategy({ threeDSecure: this, actionToken });
}

getStrategyForProactiveToken(token) {
const strategy = ThreeDSecure.getStrategyForGatewayType(token.three_d_secure.gateway.type);
return new strategy({ threeDSecure: this, proactiveToken: token });
}

/**
* Creates a ThreeDSecureActionResultToken from action results
*
Expand All @@ -181,6 +189,7 @@ export class ThreeDSecure extends RiskConcern {
const data = {
type: 'three_d_secure_action_result',
three_d_secure_action_token_id: this.actionTokenId,
proactive_three_d_secure_token_id: this.proactiveTokenId,
results
};
debug('submitting results for tokenization', data);
Expand Down Expand Up @@ -216,6 +225,21 @@ export class ThreeDSecure extends RiskConcern {
throw new Error(`Invalid challengeWindowSize. Expected any of ${validWindowSizes}, got ${challengeWindowSize}`);
}
}

resolveToken () {
return this.actionTokenId || this.proactiveTokenId;
}

resolveActionToken(token) {
assertIsActionToken(token);
this.strategy = this.getStrategyForActionToken(token);
this.strategy.on('done', (...args) => this.onStrategyDone(...args));
}

resolveProactiveToken(token) {
this.strategy = this.getStrategyForProactiveToken(token);
this.strategy.on('done', (...args) => this.onStrategyDone(...args));
}
}

function assertIsActionToken (token) {
Expand Down
7 changes: 5 additions & 2 deletions lib/recurly/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,11 @@ function token (customerData, bus, done) {

const { number, month, year } = inputs;
Risk.preflight({ recurly: this, number, month, year })
.then(results => inputs.risk = results)
.then(() => this.request.post({ route: '/token', data: inputs, done: complete }))
.then(results => {
inputs.risk = results
inputs.proactive = this.config.risk.threeDSecure.proactive
})
.then(() => this.request.post({ route: '/tokens', data: inputs, done: complete }))
.done();
}

Expand Down
23 changes: 23 additions & 0 deletions test/unit/recurly.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,29 @@ describe('Recurly', function () {
});
});
});

describe.only('when proactive3ds', function () {
describe('is set to true', function() {
it('returns true', function () {
const recurly = initRecurly({
risk: {
threeDSecure: {
proactive: {
enabled: true
}
}
}
});
assert.strictEqual(recurly.config.risk.threeDSecure.proactive.enabled, true);
});
});
describe('is not set', function() {
it('returns false', function () {
const recurly = initRecurly({});
assert.strictEqual(recurly.config.risk.threeDSecure.proactive.enabled, false);
});
})
});
});

describe('destroy', function () {
Expand Down
3 changes: 3 additions & 0 deletions types/lib/configure.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ export type RecurlyOptions = {
risk?: {
threeDSecure?: {
preflightDeviceDataCollector?: boolean;
proactive?: {
enabled: true;
}
}
};

Expand Down

0 comments on commit 96cccce

Please sign in to comment.