-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathstripev3.js
More file actions
86 lines (70 loc) · 2.12 KB
/
stripev3.js
File metadata and controls
86 lines (70 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/* global Stripe */
import Service from '@ember/service';
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 = [
'elements',
'createToken',
'createSource',
'createPaymentMethod',
'retrieveSource',
'paymentRequest',
'redirectToCheckout',
'retrievePaymentIntent',
'handleCardPayment',
'handleCardAction',
'confirmPaymentIntent',
'handleCardSetup',
'retrieveSetupIntent',
'confirmSetupIntent'
];
export default Service.extend({
config: null,
didConfigure: false,
didLoad: false,
lazyLoad: readOnly('config.lazyLoad'),
mock: readOnly('config.mock'),
publishableKey: null,
init() {
this._super(...arguments);
this.set('publishableKey', this.get('config.publishableKey'))
let lazyLoad = this.get('lazyLoad');
if (!lazyLoad) {
this.configure();
}
},
unload() {
this.set('didConfigure', false);
this.set('didLoad', false);
},
load(publishableKey = null, params = null) {
if (publishableKey) {
this.set('publishableKey', publishableKey);
}
let lazyLoad = this.get('lazyLoad');
let mock = this.get('mock');
let shouldLoad = lazyLoad && !mock
let doLoad = shouldLoad ? loadScript("https://js.stripe.com/v3/") : resolve();
return doLoad.then(() => {
this.configure(params);
this.set('didLoad', true);
});
},
configure(params) {
let didConfigure = this.get('didConfigure');
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, params);
let functions = getProperties(stripe, STRIPE_FUNCTIONS);
setProperties(this, functions);
this.set('didConfigure', true);
}
}
});