Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down
16 changes: 12 additions & 4 deletions addon/services/stripev3.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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
Expand All @@ -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);
Expand Down
5 changes: 0 additions & 5 deletions app/initializers/ember-stripe-elements.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import EmberError from '@ember/error';
import config from '../config/environment';
import StripeMock from 'ember-stripe-elements/utils/stripe-mock';

Expand All @@ -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;
}
Expand Down
16 changes: 0 additions & 16 deletions tests/acceptance/publishable-key-test.js

This file was deleted.

24 changes: 24 additions & 0 deletions tests/unit/services/stripev3-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
});
});