diff --git a/lib/recurly/risk/risk.js b/lib/recurly/risk/risk.js index 49fcdd83..91c744e2 100644 --- a/lib/recurly/risk/risk.js +++ b/lib/recurly/risk/risk.js @@ -68,12 +68,15 @@ export class Risk { debug('received preflight instructions', preflights); return ThreeDSecure.preflight({ recurly, number, month, year, cvv, preflights }); }) - .then(results => results.filter(maybeErr => { - if (maybeErr.code === 'risk-preflight-timeout') { - debug('timeout encountered', maybeErr); - return false; - } - return true; + .then(({ tokenType, risk }) => ({ + risk: risk.filter(maybeErr => { + if (maybeErr.code === 'risk-preflight-timeout') { + debug('timeout encountered', maybeErr); + return false; + } + return true; + }), + tokenType })); } diff --git a/lib/recurly/risk/three-d-secure/strategy/braintree.js b/lib/recurly/risk/three-d-secure/strategy/braintree.js index 175b2148..782963fd 100644 --- a/lib/recurly/risk/three-d-secure/strategy/braintree.js +++ b/lib/recurly/risk/three-d-secure/strategy/braintree.js @@ -11,7 +11,7 @@ export default class BraintreeStrategy extends ThreeDSecureStrategy { } static preflight ({ recurly, number, month, year, cvv }) { - const { enabled, gatewayCode } = recurly.config.risk.threeDSecure.proactive; + const { enabled, gatewayCode, amount } = recurly.config.risk.threeDSecure.proactive; debug('performing preflight for', { gatewayCode }); @@ -32,9 +32,13 @@ export default class BraintreeStrategy extends ThreeDSecureStrategy { // resolve with relevant data instead of session_id return recurly.request.post({ route: '/risk/authentications', data }) .then(({ paymentMethodNonce, clientToken, bin }) => ({ - payment_method_nonce: paymentMethodNonce, - client_token: clientToken, - bin, + results: { + payment_method_nonce: paymentMethodNonce, + client_token: clientToken, + bin, + amount: amount + }, + tokenType: 'three_d_secure_proactive_action' })); } diff --git a/lib/recurly/risk/three-d-secure/strategy/cybersource.js b/lib/recurly/risk/three-d-secure/strategy/cybersource.js index 3c22f163..eaad4839 100644 --- a/lib/recurly/risk/three-d-secure/strategy/cybersource.js +++ b/lib/recurly/risk/three-d-secure/strategy/cybersource.js @@ -44,7 +44,7 @@ export default class CybersourceStrategy extends ThreeDSecureStrategy { const body = JSON.parse(data); if (body.MessageType === 'profile.completed') { debug('received device data session id', body); - resolve({ session_id: body.SessionId }); + resolve({ results: { session_id: body.SessionId }}); frame.destroy(); recurly.bus.off('raw-message', listener); } diff --git a/lib/recurly/risk/three-d-secure/strategy/worldpay.js b/lib/recurly/risk/three-d-secure/strategy/worldpay.js index 38f95886..52d1fbc6 100644 --- a/lib/recurly/risk/three-d-secure/strategy/worldpay.js +++ b/lib/recurly/risk/three-d-secure/strategy/worldpay.js @@ -44,7 +44,7 @@ export default class WorldpayStrategy extends ThreeDSecureStrategy { const body = JSON.parse(data); if (body.MessageType === 'profile.completed') { debug('received device data session id', body); - resolve({ session_id: body.SessionId }); + resolve({ results: { session_id: body.SessionId }}); recurly.bus.off('raw-message', listener); frame.destroy(); } diff --git a/lib/recurly/risk/three-d-secure/three-d-secure.js b/lib/recurly/risk/three-d-secure/three-d-secure.js index 825d0224..ba3efd48 100644 --- a/lib/recurly/risk/three-d-secure/three-d-secure.js +++ b/lib/recurly/risk/three-d-secure/three-d-secure.js @@ -102,15 +102,29 @@ export class ThreeDSecure extends RiskConcern { static preflight ({ recurly, number, month, year, cvv, preflights }) { return preflights.reduce((preflight, result) => { return preflight.then((finishedPreflights) => { - const { type } = result.gateway; + const { type: gatewayType } = result.gateway; const { gateway_code } = result.params; - const strategy = ThreeDSecure.getStrategyForGatewayType(type); + const strategy = ThreeDSecure.getStrategyForGatewayType(gatewayType); return strategy.preflight({ recurly, number, month, year, cvv, ...result.params }) - .then(results => { - return finishedPreflights.concat([{ processor: type, gateway_code, results }]); + .then(({ results, tokenType }) => { + // return finishedPreflights.concat([{ processor: type, gateway_code, results}]); + return { + tokenType: finishedPreflights.tokenType || tokenType, + // risk: { + // processor: gatewayType, + // gateway_code, + // risk + // // finishedPreflights.risk.concat(risk) + // } + risk: finishedPreflights.risk.concat({ + processor: gatewayType, + gateway_code, + results + }) + } }); }); - }, Promise.resolve([])); + }, Promise.resolve({risk: []})); } constructor ({ risk, actionTokenId, challengeWindowSize }) { @@ -225,10 +239,10 @@ export class ThreeDSecure extends RiskConcern { } function assertIsActionToken (token) { - if (VALID_ACTION_TOKEN_TYPES.includes(token?.type)) return; + if (ThreeDSecure.VALID_ACTION_TOKEN_TYPES.includes(token?.type)) return; throw errors('invalid-option', { name: 'actionTokenId', - expect: `a token of type: ${VALID_ACTION_TOKEN_TYPES.join(',')}` + expect: `a token of type: ${ThreeDSecure.VALID_ACTION_TOKEN_TYPES.join(',')}` }); } diff --git a/lib/recurly/token.js b/lib/recurly/token.js index 393c3e4a..e6959c88 100644 --- a/lib/recurly/token.js +++ b/lib/recurly/token.js @@ -174,7 +174,10 @@ function token (customerData, bus, done) { const { number, month, year, cvv } = inputs; Risk.preflight({ recurly: this, number, month, year, cvv }) - .then(results => inputs.risk = results) + .then(({ risk, tokenType }) => { + inputs.risk = risk + if (tokenType) inputs.type = tokenType + }) .then(() => this.request.post({ route: '/token', data: inputs, done: complete })) .done(); }