Skip to content

Commit

Permalink
Merge pull request #41 from samselikoff/override-service
Browse files Browse the repository at this point in the history
Allow Stripe service to be overridden
  • Loading branch information
buritica committed May 3, 2016
2 parents 9b8a31e + 009c973 commit 60fd0d7
Show file tree
Hide file tree
Showing 13 changed files with 273 additions and 176 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ install:
- npm install -g bower
- npm install
- bower install
- mkdir travis-phantomjs
- wget https://s3.amazonaws.com/travis-phantomjs/phantomjs-2.0.0-ubuntu-12.04.tar.bz2 -O $PWD/travis-phantomjs/phantomjs-2.0.0-ubuntu-12.04.tar.bz2
- tar -xvf $PWD/travis-phantomjs/phantomjs-2.0.0-ubuntu-12.04.tar.bz2 -C $PWD/travis-phantomjs
- export PATH=$PWD/travis-phantomjs:$PATH

script:
- ember try $EMBER_TRY_SCENARIO test
96 changes: 96 additions & 0 deletions addon/services/stripe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/* global Stripe */
import Ember from 'ember';

export default Ember.Service.extend({
init() {
this._super(...arguments);

this.card = {
createToken: this._createCardToken.bind(this)
};

this.bankAccount = {
createToken: this._createBankAccountToken.bind(this)
};

this._checkForAndAddCardFn('cardType', Stripe.card.cardType);
this._checkForAndAddCardFn('validateCardNumber', Stripe.card.validateCardNumber);
this._checkForAndAddCardFn('validateCVC', Stripe.card.validateCVC);
this._checkForAndAddCardFn('validateExpiry', Stripe.card.validateExpiry);
},

/**
* Creates a creditCard token using Stripe.js API, exposed as `card.createToken`
* @param {ojbect} card CreditCard
* @return {promise} Returns a promise that holds response, see stripe.js docs for details
* status is not being returned at the moment but it can be logged
*/
_createCardToken(card) {
this.debug('card.createToken:', card);

return new Ember.RSVP.Promise((resolve, reject) => {
Stripe.card.createToken(card, (status, response) => {

this.debug('card.createToken handler - status %s, response:', status, response);

if (response.error) {
reject(response);
} else {
resolve(response);
}
});
});
},

/**
* Creates a BankAccout token using Stripe.js API, exposed as `bankAccount.createToken`
* @param {ojbect} bankAccount
* @return {promise} Returns a promise that holds response, see stripe.js docs for details
* Status is not being returned at the moment but it can be logged
*
*/
_createBankAccountToken(bankAccount) {
this.debug('bankAccount.createToken:', bankAccount);

return new Ember.RSVP.Promise((resolve, reject) => {
Stripe.bankAccount.createToken(bankAccount, (status, response) => {

this.debug('bankAccount.createToken handler - status %s, response:', status, response);

if (response.error) {
reject(response);
} else {
resolve(response);
}
});
});
},

/**
* Uses Ember.Logger.info to output service information if LOG_STRIPE_SERVICE is
* set
*
* notes:
* - proxies all arguments to Ember.Logger.info
* - pre-pends StripeService to all messages
*/
debug() {
let config = this.get('config');
let debuggingEnabled = (config && typeof config.LOG_STRIPE_SERVICE !== 'undefined') ? config.LOG_STRIPE_SERVICE : false;

if (debuggingEnabled) {
let args = Array.prototype.slice.call(arguments);
args[0] = `StripeService: ${args[0]}`;
Ember.Logger.info.apply(null, args);
}
},

_checkForAndAddCardFn(name, fn) {
if (Ember.isEqual(Ember.typeOf(Stripe.card[name]), 'function')) {
this.card[name] = fn;
} else {
this.card[name] = Ember.K;
Ember.Logger.error(`ember-cli-stripe: ${name} on Stripe.card is no longer available`);
}
}
});
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
/* global Stripe */
import Ember from 'ember';
import config from '../config/environment';
var debug = config.LOG_STRIPE_SERVICE;

export function initialize() {
if (debug) {
const application = arguments[1] || arguments[0];
application.register('config:ember-stripe-service', config, { instantiate: false });
application.inject('service:stripe', 'config', 'config:ember-stripe-service');

if (config.LOG_STRIPE_SERVICE) {
Ember.Logger.info('StripeService: initialize');
}

Expand All @@ -13,9 +16,10 @@ export function initialize() {
}

Stripe.setPublishableKey(config.stripe.publishableKey);

}

export default {
name: 'stripe',
name: 'ember-stripe-stripe',
initialize: initialize
};
99 changes: 1 addition & 98 deletions app/services/stripe.js
Original file line number Diff line number Diff line change
@@ -1,98 +1 @@
/* global Stripe */
import env from '../config/environment';
import Ember from 'ember';

/**
* Uses Ember.Logger.info to output service information if LOG_STRIPE_SERVICE is
* set
*
* notes:
* - proxies all arguments to Ember.Logger.info
* - pre-pends StripeService to all messages
*/
function debug() {
var debuggingEnabled = (typeof env.LOG_STRIPE_SERVICE !== 'undefined');

if (!debuggingEnabled) {
return false;
}

var args = Array.prototype.slice.call(arguments);
args[0] = `StripeService: ${args[0]}`;
Ember.Logger.info.apply(null, args);
}

/**
* Creates a creditCard token using Stripe.js API, exposed as `card.createToken`
* @param {ojbect} card CreditCard
* @return {promise} Returns a promise that holds response, see stripe.js docs for details
* status is not being returned at the moment but it can be logged
*/
function createCardToken (card) {
debug('card.createToken:', card);

return new Ember.RSVP.Promise(function (resolve, reject) {
Stripe.card.createToken(card, function (status, response) {

debug('card.createToken handler - status %s, response:', status, response);

if (response.error) {
return Ember.run(null, reject, response);
}

Ember.run(null, resolve, response);

});
});
}

/**
* Creates a BankAccout token using Stripe.js API, exposed as `bankAccount.createToken`
* @param {ojbect} bankAccount
* @return {promise} Returns a promise that holds response, see stripe.js docs for details
* Status is not being returned at the moment but it can be logged
*
*/
function createBankAccountToken(bankAccount) {
debug('bankAccount.createToken:', bankAccount);

return new Ember.RSVP.Promise(function (resolve, reject) {
Stripe.bankAccount.createToken(bankAccount, function (status, response) {

debug('bankAccount.createToken handler - status %s, response:', status, response);

if (response.error) {
return Ember.run(null, reject, response);
}

Ember.run(null, resolve, response);
});
});
}

/**
* Expose module
*/
export default Ember.Service.extend({
init() {
this._super(...arguments);
this._checkForAndAddCardFn('cardType', Stripe.card.cardType);
this._checkForAndAddCardFn('validateCardNumber', Stripe.card.validateCardNumber);
this._checkForAndAddCardFn('validateCVC', Stripe.card.validateCVC);
this._checkForAndAddCardFn('validateExpiry', Stripe.card.validateExpiry);
},
card: {
createToken: createCardToken
},
bankAccount: {
createToken: createBankAccountToken,
},
_checkForAndAddCardFn(name, fn) {
if (Ember.isEqual(Ember.typeOf(Stripe.card[name]), 'function')) {
this.card[name] = fn;
} else {
this.card[name] = Ember.K;
Ember.Logger.error(`ember-cli-stripe: ${name} on Stripe.card is no longer available`);
}
}
});
export { default } from 'ember-stripe-service/services/stripe';
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
'use strict';

module.exports = {
name: 'stripe-service',
name: 'ember-stripe-service',
contentFor: function(type) {
// we use body since Stripe must be available before
if (type === 'body') {
return '<script type="text/javascript" src="https://js.stripe.com/v2/"></script>';
}
},

isDevelopingAddon: function() {
return true;
}
};
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
{
"name": "Alex Urbano",
"email": "alex.urbano@ride.com"
},
{
"name": "Sam Selikoff",
"email": "sam.selikoff@gmail.com"
}
],
"license": "MIT",
Expand Down
37 changes: 37 additions & 0 deletions tests/acceptance/initializer-logging-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { module, test } from 'qunit';
import Ember from 'ember';
import sinon from 'sinon';
import config from 'dummy/config/environment';
import startApp from '../helpers/start-app';
import destroyApp from '../helpers/destroy-app';

module('Acceptance | Initializer logging', {
beforeEach() {
this.info = sinon.stub(Ember.Logger, 'info');
this._original_LOG_STRIPE_SERVICE = config.LOG_STRIPE_SERVICE;
},
afterEach() {
this.info.restore();
config.LOG_STRIPE_SERVICE = this._original_LOG_STRIPE_SERVICE;
}
});

test('it logs on app boot when LOG_STRIPE_SERVICE is true', function(assert) {
config.LOG_STRIPE_SERVICE = true;

let application = startApp();

assert.ok(this.info.called);

destroyApp(application);
});

test('it doesn\'t log on app boot when LOG_STRIPE_SERVICE is false', function(assert) {
config.LOG_STRIPE_SERVICE = false;

let application = startApp();

assert.ok(this.info.notCalled);

destroyApp(application);
});
16 changes: 16 additions & 0 deletions tests/acceptance/publishable-key-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { module, test } from 'qunit';
import config from 'dummy/config/environment';
import startApp from '../helpers/start-app';

module('Acceptance | Publishable Key');

test('it throws an error if config.stripe.publishableKey is not set', function(assert) {
let originalKey = config.stripe.publishableKey;
config.stripe.publishableKey = undefined;

assert.throws(function() {
startApp();
}, /Missing Stripe key/);

config.stripe.publishableKey = originalKey;
});
55 changes: 55 additions & 0 deletions tests/acceptance/service-logging-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* global Stripe */
import { test } from 'qunit';
import Ember from 'ember';
import sinon from 'sinon';
import moduleForAcceptance from '../../tests/helpers/module-for-acceptance';
import config from 'dummy/config/environment';

moduleForAcceptance('Acceptance | Logging', {
beforeEach() {
this.cc = {
number: 4242424242424242,
exp_year: 2018,
exp_month: 10,
cvc: 123,
address_zip: 12345
};

this.info = sinon.stub(Ember.Logger, 'info');

this.createToken = sinon.stub(Stripe.card, 'createToken', function(card, cb) {
let response = {
id: 'my id'
};
cb(200, response);
});

this.service = this.application.__container__.lookup('service:stripe');
this._original_LOG_STRIPE_SERVICE = config.LOG_STRIPE_SERVICE;
this._original_stripePublishableKey = config.stripe.publishableKey;
},

afterEach() {
this.createToken.restore();
this.info.restore();
config.LOG_STRIPE_SERVICE = this._original_LOG_STRIPE_SERVICE;
config.stripe.publishableKey = this._original_stripePublishableKey;
}
});


test('createToken logs when LOG_STRIPE_SERVICE is set in env config', function(assert) {
config.LOG_STRIPE_SERVICE = true;

return this.service.card.createToken(this.cc).then(() => {
assert.ok(this.info.called);
});
});

test('createToken doesn\'t log when LOG_STRIPE_SERVICE is set to false in env config', function(assert) {
config.LOG_STRIPE_SERVICE = false;

return this.service.card.createToken(this.cc).then(() => {
assert.ok(this.info.notCalled);
});
});
13 changes: 13 additions & 0 deletions tests/dummy/app/controllers/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Ember from 'ember';

export default Ember.Controller.extend({

stripe: Ember.inject.service(),

doSomething: Ember.on('init', function() {
let stripe = this.get('stripe');

stripe.card.createToken();
})

});
Loading

0 comments on commit 60fd0d7

Please sign in to comment.