Skip to content

Commit aab8c7b

Browse files
Merge pull request #59 from josemarluedke/lazy-config
Lazy publishableKey config
2 parents c764599 + 95a9804 commit aab8c7b

File tree

5 files changed

+42
-23
lines changed

5 files changed

+42
-23
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ $ ember install ember-stripe-elements
5656

5757
You must set your [publishable key](https://support.stripe.com/questions/where-do-i-find-my-api-keys) in `config/environment.js`.
5858

59+
5960
```js
6061
ENV.stripe = {
6162
publishableKey: 'pk_thisIsATestKey'
@@ -107,6 +108,12 @@ export default Route.extend({
107108
});
108109
```
109110

111+
You can also pass `publishableKey` to the `load` function.
112+
113+
```js
114+
this.get('stripe').load('pk_thisIsATestKey');
115+
```
116+
110117
## Components
111118

112119
### Basics

addon/services/stripev3.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { getProperties, setProperties } from '@ember/object';
44
import { readOnly } from '@ember/object/computed';
55
import { resolve } from 'rsvp';
66
import loadScript from 'ember-stripe-elements/utils/load-script';
7+
import EmberError from '@ember/error';
78

89
// As listed at https://stripe.com/docs/stripe-js/reference#the-stripe-object
910
const STRIPE_FUNCTIONS = [
@@ -30,10 +31,11 @@ export default Service.extend({
3031

3132
lazyLoad: readOnly('config.lazyLoad'),
3233
mock: readOnly('config.mock'),
33-
publishableKey: readOnly('config.publishableKey'),
34+
publishableKey: null,
3435

3536
init() {
3637
this._super(...arguments);
38+
this.set('publishableKey', this.get('config.publishableKey'))
3739

3840
let lazyLoad = this.get('lazyLoad');
3941

@@ -42,7 +44,11 @@ export default Service.extend({
4244
}
4345
},
4446

45-
load() {
47+
load(publishableKey = null) {
48+
if (publishableKey) {
49+
this.set('publishableKey', publishableKey);
50+
}
51+
4652
let lazyLoad = this.get('lazyLoad');
4753
let mock = this.get('mock');
4854
let shouldLoad = lazyLoad && !mock
@@ -61,6 +67,9 @@ export default Service.extend({
6167
if (!didConfigure) {
6268
let publishableKey = this.get('publishableKey');
6369

70+
if (!publishableKey) {
71+
throw new EmberError("stripev3: Missing Stripe key, please set `ENV.stripe.publishableKey` in config.environment.js");
72+
}
6473

6574
let stripe = new Stripe(publishableKey);
6675
let functions = getProperties(stripe, STRIPE_FUNCTIONS);

app/initializers/ember-stripe-elements.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import EmberError from '@ember/error';
21
import config from '../config/environment';
32
import StripeMock from 'ember-stripe-elements/utils/stripe-mock';
43

@@ -9,10 +8,6 @@ export function initialize() {
98
application.register('config:stripe', stripeConfig, { instantiate: false });
109
application.inject('service:stripev3', 'config', 'config:stripe');
1110

12-
if (!stripeConfig.publishableKey) {
13-
throw new EmberError("stripev3: Missing Stripe key, please set `ENV.stripe.publishableKey` in config.environment.js");
14-
}
15-
1611
if (typeof FastBoot !== 'undefined' || stripeConfig.mock) {
1712
window.Stripe = StripeMock;
1813
}

tests/acceptance/publishable-key-test.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

tests/unit/services/stripev3-test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,4 +212,28 @@ module('Unit | Service | stripev3', function(hooks) {
212212
confirmSetupIntent(mockOptions);
213213
confirmSetupIntent.restore();
214214
});
215+
216+
test('it throws an error if config.stripe.publishableKey is not set', function(assert) {
217+
assert.expectAssertion(() => {
218+
this.owner.factoryFor('service:stripev3').create({
219+
config: {
220+
mock: true,
221+
publishableKey: null
222+
}
223+
});
224+
}, /Missing Stripe key/);
225+
});
226+
227+
test('it does not throw when publishableKey is provided by load method', async function(assert) {
228+
this.subject = this.owner.factoryFor('service:stripev3').create({
229+
config: {
230+
mock: true,
231+
lazyLoad: true,
232+
publishableKey: null
233+
}
234+
});
235+
236+
await this.subject.load('some-key');
237+
assert.ok(this.subject.get('didConfigure'), 'should have configured')
238+
});
215239
});

0 commit comments

Comments
 (0)