-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from samselikoff/override-service
Allow Stripe service to be overridden
- Loading branch information
Showing
13 changed files
with
273 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`); | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}) | ||
|
||
}); |
Oops, something went wrong.