Skip to content

Commit 9739409

Browse files
pablobmlindyhopchris
authored andcommitted
Add functions missing from the Stripe object (#50)
Add functions missing from https://stripe.com/docs/stripe-js/reference#the-stripe-object
1 parent 8c5a508 commit 9739409

File tree

4 files changed

+177
-14
lines changed

4 files changed

+177
-14
lines changed

README.md

+15-6
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,21 @@ A simple Ember wrapper for [Stripe Elements](https://stripe.com/docs/elements).
2121

2222
- Inject `<script src="https://js.stripe.com/v3/"></script>` into your application's `<body>`
2323
- Initialize `Stripe` with your publishable key
24-
- Inject a `stripev3` service into your controllers so you can use:
25-
- [`stripe.createToken(element[, options])`](https://stripe.com/docs/elements/reference#stripe-create-token)
26-
- [`stripe.createSource(element[, options])`](https://stripe.com/docs/elements/reference#stripe-create-source)
27-
- [`stripe.retrieveSource(source)`](https://stripe.com/docs/elements/reference#stripe-retrieve-source)
28-
- [`stripe.paymentRequest(options)`](https://stripe.com/docs/stripe-js/reference#stripe-payment-request)
29-
- [`stripe.elements([options])`](https://stripe.com/docs/elements/reference#stripe-elements), if for some reason you need to
24+
- Inject a `stripev3` service into your controllers so you can use the functions usually available on the `stripe` object (see https://stripe.com/docs/stripe-js/reference#the-stripe-object):
25+
- `stripe.elements()`
26+
- `stripe.createToken()`
27+
- `stripe.createSource()`
28+
- `stripe.createPaymentMethod()`
29+
- `stripe.retrieveSource()`
30+
- `stripe.paymentRequest()`
31+
- `stripe.redirectToCheckout()`
32+
- `stripe.retrievePaymentIntent()`
33+
- `stripe.handleCardPayment()`
34+
- `stripe.handleCardAction()`
35+
- `stripe.confirmPaymentIntent()`
36+
- `stripe.handleCardSetup()`
37+
- `stripe.retrieveSetupIntent()`
38+
- `stripe.confirmSetupIntent()`
3039
- Simple, configurable Ember components like `{{stripe-card}}` (demoed in the gif above)
3140

3241
## Installation

addon/services/stripev3.js

+23-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
11
/* global Stripe */
22
import Service from '@ember/service';
3-
import { setProperties } from '@ember/object';
3+
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';
77

8+
// As listed at https://stripe.com/docs/stripe-js/reference#the-stripe-object
9+
const STRIPE_FUNCTIONS = [
10+
'elements',
11+
'createToken',
12+
'createSource',
13+
'createPaymentMethod',
14+
'retrieveSource',
15+
'paymentRequest',
16+
'redirectToCheckout',
17+
'retrievePaymentIntent',
18+
'handleCardPayment',
19+
'handleCardAction',
20+
'confirmPaymentIntent',
21+
'handleCardSetup',
22+
'retrieveSetupIntent',
23+
'confirmSetupIntent'
24+
];
25+
826
export default Service.extend({
927
config: null,
1028
didConfigure: false,
@@ -44,8 +62,10 @@ export default Service.extend({
4462
if (!didConfigure) {
4563
let publishableKey = this.get('publishableKey');
4664

47-
let { elements, createToken, createSource, retrieveSource, paymentRequest, createPaymentMethod } = new Stripe(publishableKey);
48-
setProperties(this, { elements, createToken, createSource, retrieveSource, paymentRequest, createPaymentMethod });
65+
66+
let stripe = new Stripe(publishableKey);
67+
let functions = getProperties(stripe, STRIPE_FUNCTIONS);
68+
setProperties(this, functions);
4969

5070
this.set('didConfigure', true);
5171
}

addon/utils/stripe-mock.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,18 @@ StripeMock.prototype.elements = function() {
1313
}
1414
};
1515
}
16-
StripeMock.prototype.createToken = function() {}
17-
StripeMock.prototype.createSource = function() {}
18-
StripeMock.prototype.retrieveSource = function() {}
19-
StripeMock.prototype.paymentRequest = function() {}
16+
StripeMock.prototype.createToken = function() {};
17+
StripeMock.prototype.createSource = function() {};
18+
StripeMock.prototype.createPaymentMethod = function() {};
19+
StripeMock.prototype.retrieveSource = function() {};
20+
StripeMock.prototype.paymentRequest = function() {};
21+
StripeMock.prototype.redirectToCheckout = function() {};
22+
StripeMock.prototype.retrievePaymentIntent = function() {};
23+
StripeMock.prototype.handleCardPayment = function() {};
24+
StripeMock.prototype.handleCardAction = function() {};
25+
StripeMock.prototype.confirmPaymentIntent = function() {};
26+
StripeMock.prototype.handleCardSetup = function() {};
27+
StripeMock.prototype.retrieveSetupIntent = function() {};
28+
StripeMock.prototype.confirmSetupIntent = function() {};
2029

2130
export default StripeMock;

tests/unit/services/stripev3-test.js

+126-1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,20 @@ module('Unit | Service | stripev3', function(hooks) {
5959
createSource.restore();
6060
});
6161

62+
test('makes Stripe.createPaymentMethod available on the service', function(assert) {
63+
assert.expect(1);
64+
65+
let service = this.subject;
66+
let mockOptions = { locale: 'en' };
67+
68+
let createPaymentMethod = sinon.stub(service, 'createPaymentMethod').callsFake(function(options) {
69+
assert.deepEqual(options, mockOptions, 'called with mock options');
70+
});
71+
72+
createPaymentMethod(mockOptions);
73+
createPaymentMethod.restore();
74+
});
75+
6276
test('makes Stripe.retrieveSource available on the service', function(assert) {
6377
assert.expect(1);
6478

@@ -86,5 +100,116 @@ module('Unit | Service | stripev3', function(hooks) {
86100
paymentRequest(mockOptions);
87101
paymentRequest.restore();
88102
});
89-
});
90103

104+
test('makes Stripe.redirectToCheckout available on the service', function(assert) {
105+
assert.expect(1);
106+
107+
let service = this.subject;
108+
let mockOptions = { locale: 'en' };
109+
110+
let redirectToCheckout = sinon.stub(service, 'redirectToCheckout').callsFake(function(options) {
111+
assert.deepEqual(options, mockOptions, 'called with mock options');
112+
});
113+
114+
redirectToCheckout(mockOptions);
115+
redirectToCheckout.restore();
116+
});
117+
118+
test('makes Stripe.retrievePaymentIntent available on the service', function(assert) {
119+
assert.expect(1);
120+
121+
let service = this.subject;
122+
let mockOptions = { locale: 'en' };
123+
124+
let retrievePaymentIntent = sinon.stub(service, 'retrievePaymentIntent').callsFake(function(options) {
125+
assert.deepEqual(options, mockOptions, 'called with mock options');
126+
});
127+
128+
retrievePaymentIntent(mockOptions);
129+
retrievePaymentIntent.restore();
130+
});
131+
132+
test('makes Stripe.handleCardPayment available on the service', function(assert) {
133+
assert.expect(1);
134+
135+
let service = this.subject;
136+
let mockOptions = { locale: 'en' };
137+
138+
let handleCardPayment = sinon.stub(service, 'handleCardPayment').callsFake(function(options) {
139+
assert.deepEqual(options, mockOptions, 'called with mock options');
140+
});
141+
142+
handleCardPayment(mockOptions);
143+
handleCardPayment.restore();
144+
});
145+
146+
test('makes Stripe.handleCardAction available on the service', function(assert) {
147+
assert.expect(1);
148+
149+
let service = this.subject;
150+
let mockOptions = { locale: 'en' };
151+
152+
let handleCardAction = sinon.stub(service, 'handleCardAction').callsFake(function(options) {
153+
assert.deepEqual(options, mockOptions, 'called with mock options');
154+
});
155+
156+
handleCardAction(mockOptions);
157+
handleCardAction.restore();
158+
});
159+
160+
test('makes Stripe.confirmPaymentIntent available on the service', function(assert) {
161+
assert.expect(1);
162+
163+
let service = this.subject;
164+
let mockOptions = { locale: 'en' };
165+
166+
let confirmPaymentIntent = sinon.stub(service, 'confirmPaymentIntent').callsFake(function(options) {
167+
assert.deepEqual(options, mockOptions, 'called with mock options');
168+
});
169+
170+
confirmPaymentIntent(mockOptions);
171+
confirmPaymentIntent.restore();
172+
});
173+
174+
test('makes Stripe.handleCardSetup available on the service', function(assert) {
175+
assert.expect(1);
176+
177+
let service = this.subject;
178+
let mockOptions = { locale: 'en' };
179+
180+
let handleCardSetup = sinon.stub(service, 'handleCardSetup').callsFake(function(options) {
181+
assert.deepEqual(options, mockOptions, 'called with mock options');
182+
});
183+
184+
handleCardSetup(mockOptions);
185+
handleCardSetup.restore();
186+
});
187+
188+
test('makes Stripe.retrieveSetupIntent available on the service', function(assert) {
189+
assert.expect(1);
190+
191+
let service = this.subject;
192+
let mockOptions = { locale: 'en' };
193+
194+
let retrieveSetupIntent = sinon.stub(service, 'retrieveSetupIntent').callsFake(function(options) {
195+
assert.deepEqual(options, mockOptions, 'called with mock options');
196+
});
197+
198+
retrieveSetupIntent(mockOptions);
199+
retrieveSetupIntent.restore();
200+
});
201+
202+
test('makes Stripe.confirmSetupIntent available on the service', function(assert) {
203+
assert.expect(1);
204+
205+
let service = this.subject;
206+
let mockOptions = { locale: 'en' };
207+
208+
let confirmSetupIntent = sinon.stub(service, 'confirmSetupIntent').callsFake(function(options) {
209+
assert.deepEqual(options, mockOptions, 'called with mock options');
210+
});
211+
212+
confirmSetupIntent(mockOptions);
213+
confirmSetupIntent.restore();
214+
});
215+
});

0 commit comments

Comments
 (0)