diff --git a/README.md b/README.md index 35dc180..ca53fd4 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,7 @@ $ ember install ember-stripe-elements You must set your [publishable key](https://support.stripe.com/questions/where-do-i-find-my-api-keys) in `config/environment.js`. + ```js ENV.stripe = { publishableKey: 'pk_thisIsATestKey' @@ -89,6 +90,12 @@ export default Route.extend({ }); ``` +You can also pass `publishableKey` to the `load` function. + +```js +this.get('stripe').load('pk_thisIsATestKey'); +``` + ## Components ### Basics diff --git a/addon/services/stripev3.js b/addon/services/stripev3.js index df0a01e..67e1654 100644 --- a/addon/services/stripev3.js +++ b/addon/services/stripev3.js @@ -4,6 +4,7 @@ import { getProperties, setProperties } from '@ember/object'; import { readOnly } from '@ember/object/computed'; import { resolve } from 'rsvp'; import loadScript from 'ember-stripe-elements/utils/load-script'; +import EmberError from '@ember/error'; // As listed at https://stripe.com/docs/stripe-js/reference#the-stripe-object const STRIPE_FUNCTIONS = [ @@ -30,20 +31,24 @@ export default Service.extend({ lazyLoad: readOnly('config.lazyLoad'), mock: readOnly('config.mock'), - publishableKey: readOnly('config.publishableKey'), + publishableKey: null, init() { this._super(...arguments); + this.set('publishableKey', this.get('config.publishableKey')) let lazyLoad = this.get('lazyLoad'); - let mock = this.get('mock'); - if (!lazyLoad || mock) { + if (!lazyLoad) { this.configure(); } }, - load() { + load(publishableKey = null) { + if (publishableKey) { + this.set('publishableKey', publishableKey); + } + let lazyLoad = this.get('lazyLoad'); let mock = this.get('mock'); let shouldLoad = lazyLoad && !mock @@ -62,6 +67,9 @@ export default Service.extend({ if (!didConfigure) { let publishableKey = this.get('publishableKey'); + if (!publishableKey) { + throw new EmberError("stripev3: Missing Stripe key, please set `ENV.stripe.publishableKey` in config.environment.js"); + } let stripe = new Stripe(publishableKey); let functions = getProperties(stripe, STRIPE_FUNCTIONS); diff --git a/app/initializers/ember-stripe-elements.js b/app/initializers/ember-stripe-elements.js index bf72b5e..4f27973 100644 --- a/app/initializers/ember-stripe-elements.js +++ b/app/initializers/ember-stripe-elements.js @@ -1,4 +1,3 @@ -import EmberError from '@ember/error'; import config from '../config/environment'; import StripeMock from 'ember-stripe-elements/utils/stripe-mock'; @@ -9,10 +8,6 @@ export function initialize() { application.register('config:stripe', stripeConfig, { instantiate: false }); application.inject('service:stripev3', 'config', 'config:stripe'); - if (!stripeConfig.publishableKey) { - throw new EmberError("stripev3: Missing Stripe key, please set `ENV.stripe.publishableKey` in config.environment.js"); - } - if (typeof FastBoot !== 'undefined' || stripeConfig.mock) { window.Stripe = StripeMock; } diff --git a/tests/acceptance/publishable-key-test.js b/tests/acceptance/publishable-key-test.js deleted file mode 100644 index a3e5807..0000000 --- a/tests/acceptance/publishable-key-test.js +++ /dev/null @@ -1,16 +0,0 @@ -import { module, test } from 'ember-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.expectAssertion(function() { - startApp(); - }, /Missing Stripe key/); - - config.stripe.publishableKey = originalKey; -}); diff --git a/tests/unit/services/stripev3-test.js b/tests/unit/services/stripev3-test.js index 1207720..ccedb59 100644 --- a/tests/unit/services/stripev3-test.js +++ b/tests/unit/services/stripev3-test.js @@ -212,4 +212,28 @@ module('Unit | Service | stripev3', function(hooks) { confirmSetupIntent(mockOptions); confirmSetupIntent.restore(); }); + + test('it throws an error if config.stripe.publishableKey is not set', function(assert) { + assert.expectAssertion(() => { + this.owner.factoryFor('service:stripev3').create({ + config: { + mock: true, + publishableKey: null + } + }); + }, /Missing Stripe key/); + }); + + test('it does not throw when publishableKey is provided by load method', async function(assert) { + this.subject = this.owner.factoryFor('service:stripev3').create({ + config: { + mock: true, + lazyLoad: true, + publishableKey: null + } + }); + + await this.subject.load('some-key'); + assert.ok(this.subject.get('didConfigure'), 'should have configured') + }); });