Skip to content

Commit 95a9804

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

File tree

4 files changed

+41
-21
lines changed

4 files changed

+41
-21
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: 10 additions & 5 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,20 +31,24 @@ 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');
39-
let mock = this.get('mock');
4041

41-
if (!lazyLoad || mock) {
42+
if (!lazyLoad) {
4243
this.configure();
4344
}
4445
},
4546

46-
load() {
47+
load(publishableKey = null) {
48+
if (publishableKey) {
49+
this.set('publishableKey', publishableKey);
50+
}
51+
4752
let lazyLoad = this.get('lazyLoad');
4853
let mock = this.get('mock');
4954
let shouldLoad = lazyLoad && !mock
@@ -63,7 +68,7 @@ export default Service.extend({
6368
let publishableKey = this.get('publishableKey');
6469

6570
if (!publishableKey) {
66-
throw new Error("stripev3: Missing Stripe key, please set `ENV.stripe.publishableKey` in config.environment.js");
71+
throw new EmberError("stripev3: Missing Stripe key, please set `ENV.stripe.publishableKey` in config.environment.js");
6772
}
6873

6974
let stripe = new Stripe(publishableKey);

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)