Skip to content

Commit

Permalink
PayPal Complete using PayPal method
Browse files Browse the repository at this point in the history
  • Loading branch information
douglaslise committed May 9, 2023
1 parent 9dc68c3 commit 35af7b4
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 6 deletions.
3 changes: 3 additions & 0 deletions lib/recurly/paypal/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Emitter from 'component-emitter';
import { DirectStrategy } from './strategy/direct';
import { BraintreeStrategy } from './strategy/braintree';
import { CompleteStrategy } from './strategy/complete';

const debug = require('debug')('recurly:paypal');

Expand Down Expand Up @@ -36,6 +37,8 @@ class PayPal extends Emitter {
if (options.braintree) {
this.strategy = new BraintreeStrategy(options);
this.strategy.onFail(this.fallback.bind(this));
} else if (options.payPalComplete) {
this.strategy = new CompleteStrategy(options);
} else {
this.strategy = new DirectStrategy(options);
}
Expand Down
32 changes: 32 additions & 0 deletions lib/recurly/paypal/strategy/complete.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { PayPalStrategy } from './index';

// const debug = require('debug')('recurly:paypal:strategy:complete');

const PAYPAL_COMPLETE_START_PATH = '/paypal_commerce/start';

/**
* PayPal Complete strategy
*/
export class CompleteStrategy extends PayPalStrategy {
constructor (...args) {
super(...args);
this.emit('ready');
}

start () {
const frame = this.frame = this.recurly.Frame({ path: PAYPAL_COMPLETE_START_PATH });
frame.once('done', token => this.emit('token', token));
frame.once('close', () => this.emit('cancel'));
frame.once('error', cause => {
if (cause.code === 'paypal-cancel') this.emit('cancel');
this.error('paypal-tokenize-error', { cause });
});
}

destroy () {
if (this.frame) {
this.frame.destroy();
}
this.off();
}
}
23 changes: 17 additions & 6 deletions test/unit/paypal/paypal.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import assert from 'assert';
import after from 'lodash.after';
import { PayPal } from '../../../lib/recurly/paypal';
import { DirectStrategy } from '../../../lib/recurly/paypal/strategy/direct';
import { BraintreeStrategy } from '../../../lib/recurly/paypal/strategy/braintree';
import {
initRecurly,
stubBraintree,
stubWindowOpen
} from '../support/helpers';
import { CompleteStrategy } from '../../../lib/recurly/paypal/strategy/complete';

describe(`Recurly.PayPal`, function () {
describe('Recurly.PayPal', function () {
stubWindowOpen();

beforeEach(function () {
Expand Down Expand Up @@ -54,7 +53,7 @@ describe(`Recurly.PayPal`, function () {
this.paypal.ready(() => {
assert(this.paypal.strategy instanceof DirectStrategy);
done();
})
});
});

it('falls back to direct PayPal flow', function (done) {
Expand All @@ -67,12 +66,24 @@ describe(`Recurly.PayPal`, function () {
});
});

describe('when configured using PayPal Complete', function () {
const validOpts = { payPalComplete: true };

beforeEach(function () {
this.paypal = this.recurly.PayPal(validOpts);
});

it('uses PayPal Complete strategy', function () {
assert(this.paypal.strategy instanceof CompleteStrategy);
});
});

describe('destroy', function () {
it('deletes the strategy and removes listeners', function () {
this.sandbox.spy(this.paypal, 'off');
this.paypal.destroy();
assert.equal(this.paypal.strategy, undefined);
assert(this.paypal.off.calledOnce);
})
})
});
});
});
27 changes: 27 additions & 0 deletions test/unit/paypal/strategy/complete.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import assert from 'assert';

import {
initRecurly,
stubWindowOpen
} from '../../support/helpers';

describe('CompleteStrategy', function () {
const validOpts = { payPalComplete: true };

stubWindowOpen();

beforeEach(function () {
this.sandbox = sinon.createSandbox();
this.recurly = initRecurly();
this.paypal = this.recurly.PayPal(validOpts);
});

describe('start', function () {
it('opens iframe with PayPal Complete start path', function () {
this.sandbox.spy(this.recurly, 'Frame');
this.paypal.start();
console.log({ calls: this.recurly.Frame.getCalls() });
assert(this.recurly.Frame.calledWith({ path: '/paypal_commerce/start' }));
});
});
});

0 comments on commit 35af7b4

Please sign in to comment.