Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent multiple 3ds instances #847

Merged
merged 4 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/recurly/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,12 @@ const ERRORS = [
to the cause property for more detail.`,
classification: 'internal'
},
{
code: '3ds-multiple-instances',
message: `More than one instance of threeDSecure was initialized. Make sure you remove the previous instance before
initializing a new one.`,
classification: 'merchant'
},
{
code: '3ds-auth-error',
message: `We were unable to authenticate your payment method. Please choose a different payment
Expand Down
5 changes: 5 additions & 0 deletions lib/recurly/risk/three-d-secure/three-d-secure.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ export class ThreeDSecure extends RiskConcern {
}

constructor ({ risk, actionTokenId, challengeWindowSize }) {
const existingConcern = risk.concerns.find((concern) => concern instanceof ThreeDSecure);
if (existingConcern) {
throw errors('3ds-multiple-instances', { name: 'ThreeDSecure', expect: 'to be the only concern' });
}

super({ risk });

this.actionTokenId = actionTokenId;
Expand Down
16 changes: 14 additions & 2 deletions test/unit/risk/three-d-secure.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('ThreeDSecure', function () {
beforeEach(function (done) {
const actionTokenId = this.actionTokenId = 'action-token-test';
const recurly = this.recurly = initRecurly();
const risk = this.risk = { add: sinon.stub(), remove: sinon.stub(), recurly };
const risk = this.risk = { add: sinon.stub(), remove: sinon.stub(),concerns: [], recurly };
const sandbox = this.sandbox = sinon.createSandbox();

// Neuter the third party lib loaders
Expand Down Expand Up @@ -51,7 +51,7 @@ describe('ThreeDSecure', function () {
describe('factory', function () {
beforeEach(function () {
const { sandbox, recurly } = this;
this.riskStub = { add: sandbox.stub(), recurly };
this.riskStub = { add: sandbox.stub(), recurly, concerns: [] };
});

it('returns a ThreeDSecure instance', function () {
Expand Down Expand Up @@ -138,6 +138,18 @@ describe('ThreeDSecure', function () {
));
});

describe('when a threeDSecure instance already exists', function () {
it('throws and errror and prevents another one from being added', function () {
const { risk, actionTokenId, threeDSecure } = this;
risk.concerns.push(threeDSecure);

assert.throws(() => {
new ThreeDSecure({ risk, actionTokenId });
}, /More than one instance of threeDSecure was initialized. Make sure you remove the previous instance before initializing a new one./);
assert(risk.add.calledOnce);
});
});

describe('recurly', function () {
it('references the risk recurly instance', function () {
const { threeDSecure, risk } = this;
Expand Down
Loading