Skip to content

Commit bd395a7

Browse files
committed
Accept publishableKey on load() when lazy loading
1 parent f83be95 commit bd395a7

File tree

4 files changed

+39
-20
lines changed

4 files changed

+39
-20
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'
@@ -89,6 +90,12 @@ export default Route.extend({
8990
});
9091
```
9192

93+
You can also pass `publishableKey` to the `load` function.
94+
95+
```js
96+
this.get('stripe').load('pk_thisIsATestKey');
97+
```
98+
9299
## Components
93100

94101
### Basics

addon/services/stripev3.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,24 @@ export default Service.extend({
3030

3131
lazyLoad: readOnly('config.lazyLoad'),
3232
mock: readOnly('config.mock'),
33-
publishableKey: readOnly('config.publishableKey'),
33+
publishableKey: null,
3434

3535
init() {
3636
this._super(...arguments);
37+
this.set('publishableKey', this.get('config.publishableKey'))
3738

3839
let lazyLoad = this.get('lazyLoad');
39-
let mock = this.get('mock');
4040

41-
if (!lazyLoad || mock) {
41+
if (!lazyLoad) {
4242
this.configure();
4343
}
4444
},
4545

46-
load() {
46+
load(publishableKey = null) {
47+
if (publishableKey) {
48+
this.set('publishableKey', publishableKey);
49+
}
50+
4751
let lazyLoad = this.get('lazyLoad');
4852
let mock = this.get('mock');
4953
let shouldLoad = lazyLoad && !mock

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.subject = 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)