swell-js 3.14.0 → 3.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api.js +26 -18
- package/dist/currency.js +19 -21
- package/dist/payment.js +5 -3
- package/dist/utils/index.js +3 -0
- package/package.json +1 -1
- package/src/api.js +14 -3
- package/src/api.test.js +2 -1
- package/src/currency.js +28 -14
- package/src/payment.js +12 -2
- package/src/utils/index.js +2 -0
- package/src/utils/stripe.js +1 -1
- package/test/page/containers/payment/stripe.js +2 -6
package/dist/api.js
CHANGED
|
@@ -177,17 +177,25 @@ function _request() {
|
|
|
177
177
|
reqBody = JSON.stringify(reqData);
|
|
178
178
|
}
|
|
179
179
|
|
|
180
|
-
reqHeaders =
|
|
180
|
+
reqHeaders = {
|
|
181
|
+
Accept: 'application/json',
|
|
181
182
|
'Content-Type': 'application/json',
|
|
182
183
|
Authorization: "Basic ".concat(base64Encode(String(allOptions.key)))
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
'X-
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
if (session) {
|
|
187
|
+
reqHeaders['X-Session'] = session;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (locale) {
|
|
191
|
+
reqHeaders['X-Locale'] = locale;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if (currency) {
|
|
195
|
+
reqHeaders['X-Currency'] = currency;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
_context.next = 21;
|
|
191
199
|
return fetch(reqUrl, {
|
|
192
200
|
method: reqMethod,
|
|
193
201
|
headers: reqHeaders,
|
|
@@ -196,7 +204,7 @@ function _request() {
|
|
|
196
204
|
mode: 'cors'
|
|
197
205
|
});
|
|
198
206
|
|
|
199
|
-
case
|
|
207
|
+
case 21:
|
|
200
208
|
response = _context.sent;
|
|
201
209
|
responseSession = response.headers.get('X-Session');
|
|
202
210
|
|
|
@@ -204,14 +212,14 @@ function _request() {
|
|
|
204
212
|
setCookie('swell-session', responseSession);
|
|
205
213
|
}
|
|
206
214
|
|
|
207
|
-
_context.next =
|
|
215
|
+
_context.next = 26;
|
|
208
216
|
return response.json();
|
|
209
217
|
|
|
210
|
-
case
|
|
218
|
+
case 26:
|
|
211
219
|
result = _context.sent;
|
|
212
220
|
|
|
213
221
|
if (!(result && result.error)) {
|
|
214
|
-
_context.next =
|
|
222
|
+
_context.next = 35;
|
|
215
223
|
break;
|
|
216
224
|
}
|
|
217
225
|
|
|
@@ -221,9 +229,9 @@ function _request() {
|
|
|
221
229
|
err.param = result.error.param;
|
|
222
230
|
throw err;
|
|
223
231
|
|
|
224
|
-
case
|
|
232
|
+
case 35:
|
|
225
233
|
if (response.ok) {
|
|
226
|
-
_context.next =
|
|
234
|
+
_context.next = 39;
|
|
227
235
|
break;
|
|
228
236
|
}
|
|
229
237
|
|
|
@@ -231,10 +239,10 @@ function _request() {
|
|
|
231
239
|
_err.code = 'connection_error';
|
|
232
240
|
throw _err;
|
|
233
241
|
|
|
234
|
-
case
|
|
242
|
+
case 39:
|
|
235
243
|
return _context.abrupt("return", options.useCamelCase ? toCamel(result) : result);
|
|
236
244
|
|
|
237
|
-
case
|
|
245
|
+
case 40:
|
|
238
246
|
case "end":
|
|
239
247
|
return _context.stop();
|
|
240
248
|
}
|
|
@@ -246,7 +254,7 @@ function _request() {
|
|
|
246
254
|
|
|
247
255
|
if (typeof window !== 'undefined') {
|
|
248
256
|
window.swell = {
|
|
249
|
-
version: '3.
|
|
257
|
+
version: '3.15.0'
|
|
250
258
|
};
|
|
251
259
|
}
|
|
252
260
|
|
package/dist/currency.js
CHANGED
|
@@ -10,7 +10,8 @@ var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/
|
|
|
10
10
|
|
|
11
11
|
var _require = require('./utils'),
|
|
12
12
|
get = _require.get,
|
|
13
|
-
find = _require.find
|
|
13
|
+
find = _require.find,
|
|
14
|
+
round = _require.round;
|
|
14
15
|
|
|
15
16
|
var _require2 = require('./cookie'),
|
|
16
17
|
getCookie = _require2.getCookie,
|
|
@@ -33,16 +34,11 @@ function methods(request, opt) {
|
|
|
33
34
|
switch (_context.prev = _context.next) {
|
|
34
35
|
case 0:
|
|
35
36
|
this.set(currency);
|
|
36
|
-
|
|
37
|
-
_context.next = 4;
|
|
38
|
-
return request('put', '/session', {
|
|
37
|
+
return _context.abrupt("return", request('put', '/session', {
|
|
39
38
|
currency: currency
|
|
40
|
-
});
|
|
39
|
+
}));
|
|
41
40
|
|
|
42
|
-
case
|
|
43
|
-
return _context.abrupt("return", _context.sent);
|
|
44
|
-
|
|
45
|
-
case 5:
|
|
41
|
+
case 2:
|
|
46
42
|
case "end":
|
|
47
43
|
return _context.stop();
|
|
48
44
|
}
|
|
@@ -57,13 +53,11 @@ function methods(request, opt) {
|
|
|
57
53
|
return select;
|
|
58
54
|
}(),
|
|
59
55
|
selected: function selected() {
|
|
60
|
-
if (this.code) {
|
|
61
|
-
|
|
56
|
+
if (!this.code) {
|
|
57
|
+
this.set(getCookie('swell-currency') || opt.api.settings.get('store.currency'));
|
|
62
58
|
}
|
|
63
59
|
|
|
64
|
-
|
|
65
|
-
var cookieCurrency = getCookie('swell-currency');
|
|
66
|
-
return cookieCurrency || storeCurrency;
|
|
60
|
+
return this.code;
|
|
67
61
|
},
|
|
68
62
|
get: function get() {
|
|
69
63
|
if (!this.code) {
|
|
@@ -85,6 +79,7 @@ function methods(request, opt) {
|
|
|
85
79
|
code: code
|
|
86
80
|
};
|
|
87
81
|
this.locale = String(opt.api.settings.get('store.locale', (typeof navigator === "undefined" ? "undefined" : (0, _typeof2["default"])(navigator)) === 'object' ? navigator.language : 'en-US'));
|
|
82
|
+
setCookie('swell-currency', code);
|
|
88
83
|
return this.state;
|
|
89
84
|
},
|
|
90
85
|
format: function format(amount) {
|
|
@@ -143,13 +138,20 @@ function methods(request, opt) {
|
|
|
143
138
|
var code = _ref.code,
|
|
144
139
|
locale = _ref.locale,
|
|
145
140
|
decimals = _ref.decimals;
|
|
141
|
+
locale = String(locale || '').replace('_', '-');
|
|
146
142
|
var key = [code, locale, decimals].join('|');
|
|
147
143
|
|
|
148
144
|
if (FORMATTERS[key]) {
|
|
149
145
|
return FORMATTERS[key];
|
|
150
146
|
}
|
|
151
147
|
|
|
152
|
-
var
|
|
148
|
+
var formatLocales = [];
|
|
149
|
+
|
|
150
|
+
if (locale) {
|
|
151
|
+
formatLocales.push(locale);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
formatLocales.push('en-US');
|
|
153
155
|
var formatDecimals = typeof decimals === 'number' ? decimals : undefined;
|
|
154
156
|
var props = {
|
|
155
157
|
style: 'currency',
|
|
@@ -161,7 +163,7 @@ function methods(request, opt) {
|
|
|
161
163
|
|
|
162
164
|
try {
|
|
163
165
|
try {
|
|
164
|
-
FORMATTERS[key] = new Intl.NumberFormat(
|
|
166
|
+
FORMATTERS[key] = new Intl.NumberFormat(formatLocales, props);
|
|
165
167
|
} catch (err) {
|
|
166
168
|
if (err.message.indexOf('Invalid language tag') >= 0) {
|
|
167
169
|
FORMATTERS[key] = new Intl.NumberFormat('en-US', props);
|
|
@@ -203,11 +205,7 @@ function methods(request, opt) {
|
|
|
203
205
|
|
|
204
206
|
return this.round(roundValue, scale);
|
|
205
207
|
},
|
|
206
|
-
round:
|
|
207
|
-
var scale = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
|
|
208
|
-
// TODO: this is unrealiable (but only used for display)
|
|
209
|
-
return Number(Number(value).toFixed(scale));
|
|
210
|
-
}
|
|
208
|
+
round: round
|
|
211
209
|
};
|
|
212
210
|
}
|
|
213
211
|
|
package/dist/payment.js
CHANGED
|
@@ -985,7 +985,7 @@ function _paymentTokenize() {
|
|
|
985
985
|
|
|
986
986
|
case 17:
|
|
987
987
|
currency = toLower(get(cart, 'currency', 'usd'));
|
|
988
|
-
amount = stripeAmountByCurrency(currency, totalDue);
|
|
988
|
+
amount = stripeAmountByCurrency(currency, cart.trial ? cart.trial_initial_capture_total + cart.trial_auth_total : totalDue);
|
|
989
989
|
stripeCustomer = get(cart, 'account.stripe_customer');
|
|
990
990
|
_context14.t0 = toSnake;
|
|
991
991
|
_context14.next = 23;
|
|
@@ -1035,9 +1035,11 @@ function _paymentTokenize() {
|
|
|
1035
1035
|
method: 'card',
|
|
1036
1036
|
card: paymentMethod,
|
|
1037
1037
|
intent: {
|
|
1038
|
-
stripe: {
|
|
1038
|
+
stripe: _objectSpread({
|
|
1039
1039
|
id: paymentIntent.id
|
|
1040
|
-
}
|
|
1040
|
+
}, cart.trial && {
|
|
1041
|
+
auth_amount: cart.trial_auth_total
|
|
1042
|
+
})
|
|
1041
1043
|
}
|
|
1042
1044
|
}
|
|
1043
1045
|
}).then(onSuccess)["catch"](onError);
|
package/dist/utils/index.js
CHANGED
|
@@ -18,6 +18,8 @@ var uniq = require('lodash/uniq');
|
|
|
18
18
|
|
|
19
19
|
var find = require('lodash/find');
|
|
20
20
|
|
|
21
|
+
var round = require('lodash/round');
|
|
22
|
+
|
|
21
23
|
var findIndex = require('lodash/findIndex');
|
|
22
24
|
|
|
23
25
|
var camelCase = require('lodash/camelCase');
|
|
@@ -313,6 +315,7 @@ module.exports = {
|
|
|
313
315
|
get: get,
|
|
314
316
|
uniq: uniq,
|
|
315
317
|
find: find,
|
|
318
|
+
round: round,
|
|
316
319
|
findIndex: findIndex,
|
|
317
320
|
camelCase: camelCase,
|
|
318
321
|
cloneDeep: cloneDeep,
|
package/package.json
CHANGED
package/src/api.js
CHANGED
|
@@ -135,13 +135,23 @@ async function request(method, url, id = undefined, data = undefined, opt = unde
|
|
|
135
135
|
}
|
|
136
136
|
|
|
137
137
|
const reqHeaders = {
|
|
138
|
+
Accept: 'application/json',
|
|
138
139
|
'Content-Type': 'application/json',
|
|
139
140
|
Authorization: `Basic ${base64Encode(String(allOptions.key))}`,
|
|
140
|
-
...(session ? { 'X-Session': session } : {}),
|
|
141
|
-
...(locale ? { 'X-Locale': locale } : {}),
|
|
142
|
-
...(currency ? { 'X-Currency': currency } : {}),
|
|
143
141
|
};
|
|
144
142
|
|
|
143
|
+
if (session) {
|
|
144
|
+
reqHeaders['X-Session'] = session;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (locale) {
|
|
148
|
+
reqHeaders['X-Locale'] = locale;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (currency) {
|
|
152
|
+
reqHeaders['X-Currency'] = currency;
|
|
153
|
+
}
|
|
154
|
+
|
|
145
155
|
const response = await fetch(reqUrl, {
|
|
146
156
|
method: reqMethod,
|
|
147
157
|
headers: reqHeaders,
|
|
@@ -149,6 +159,7 @@ async function request(method, url, id = undefined, data = undefined, opt = unde
|
|
|
149
159
|
credentials: 'include',
|
|
150
160
|
mode: 'cors',
|
|
151
161
|
});
|
|
162
|
+
|
|
152
163
|
const responseSession = response.headers.get('X-Session');
|
|
153
164
|
|
|
154
165
|
if (typeof responseSession === 'string' && session !== responseSession) {
|
package/src/api.test.js
CHANGED
|
@@ -59,7 +59,8 @@ describe('api', () => {
|
|
|
59
59
|
|
|
60
60
|
expect(fetch.mock.calls.length).toEqual(1);
|
|
61
61
|
expect(fetch.mock.calls[0][1]).toHaveProperty('headers', {
|
|
62
|
-
|
|
62
|
+
Accept: 'application/json',
|
|
63
|
+
'Content-Type': 'application/json',
|
|
63
64
|
Authorization: `Basic ${Buffer.from('pk_test').toString('base64')}`,
|
|
64
65
|
});
|
|
65
66
|
});
|
package/src/currency.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const { get, find } = require('./utils');
|
|
1
|
+
const { get, find, round } = require('./utils');
|
|
2
2
|
const { getCookie, setCookie } = require('./cookie');
|
|
3
3
|
|
|
4
4
|
const FORMATTERS = {};
|
|
@@ -15,17 +15,19 @@ function methods(request, opt) {
|
|
|
15
15
|
|
|
16
16
|
async select(currency) {
|
|
17
17
|
this.set(currency);
|
|
18
|
-
|
|
19
|
-
return
|
|
18
|
+
|
|
19
|
+
return request('put', '/session', { currency });
|
|
20
20
|
},
|
|
21
21
|
|
|
22
22
|
selected() {
|
|
23
|
-
if (this.code) {
|
|
24
|
-
|
|
23
|
+
if (!this.code) {
|
|
24
|
+
this.set(
|
|
25
|
+
getCookie('swell-currency') ||
|
|
26
|
+
opt.api.settings.get('store.currency')
|
|
27
|
+
);
|
|
25
28
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
return cookieCurrency || storeCurrency;
|
|
29
|
+
|
|
30
|
+
return this.code;
|
|
29
31
|
},
|
|
30
32
|
|
|
31
33
|
get() {
|
|
@@ -41,12 +43,16 @@ function methods(request, opt) {
|
|
|
41
43
|
set(code = 'USD') {
|
|
42
44
|
this.code = code;
|
|
43
45
|
this.state = find(this.list(), { code }) || { code };
|
|
46
|
+
|
|
44
47
|
this.locale = String(
|
|
45
48
|
opt.api.settings.get(
|
|
46
49
|
'store.locale',
|
|
47
50
|
typeof navigator === 'object' ? navigator.language : 'en-US',
|
|
48
51
|
),
|
|
49
52
|
);
|
|
53
|
+
|
|
54
|
+
setCookie('swell-currency', code);
|
|
55
|
+
|
|
50
56
|
return this.state;
|
|
51
57
|
},
|
|
52
58
|
|
|
@@ -96,13 +102,24 @@ function methods(request, opt) {
|
|
|
96
102
|
},
|
|
97
103
|
|
|
98
104
|
formatter({ code, locale, decimals }) {
|
|
105
|
+
locale = String(locale || '').replace('_', '-');
|
|
106
|
+
|
|
99
107
|
const key = [code, locale, decimals].join('|');
|
|
108
|
+
|
|
100
109
|
if (FORMATTERS[key]) {
|
|
101
110
|
return FORMATTERS[key];
|
|
102
111
|
}
|
|
103
112
|
|
|
104
|
-
const
|
|
113
|
+
const formatLocales = [];
|
|
114
|
+
|
|
115
|
+
if (locale) {
|
|
116
|
+
formatLocales.push(locale);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
formatLocales.push('en-US');
|
|
120
|
+
|
|
105
121
|
const formatDecimals = typeof decimals === 'number' ? decimals : undefined;
|
|
122
|
+
|
|
106
123
|
const props = {
|
|
107
124
|
style: 'currency',
|
|
108
125
|
currency: code,
|
|
@@ -113,7 +130,7 @@ function methods(request, opt) {
|
|
|
113
130
|
|
|
114
131
|
try {
|
|
115
132
|
try {
|
|
116
|
-
FORMATTERS[key] = new Intl.NumberFormat(
|
|
133
|
+
FORMATTERS[key] = new Intl.NumberFormat(formatLocales, props);
|
|
117
134
|
} catch (err) {
|
|
118
135
|
if (err.message.indexOf('Invalid language tag') >= 0) {
|
|
119
136
|
FORMATTERS[key] = new Intl.NumberFormat('en-US', props);
|
|
@@ -166,10 +183,7 @@ function methods(request, opt) {
|
|
|
166
183
|
return this.round(roundValue, scale);
|
|
167
184
|
},
|
|
168
185
|
|
|
169
|
-
round
|
|
170
|
-
// TODO: this is unrealiable (but only used for display)
|
|
171
|
-
return Number(Number(value).toFixed(scale));
|
|
172
|
-
},
|
|
186
|
+
round,
|
|
173
187
|
};
|
|
174
188
|
}
|
|
175
189
|
|
package/src/payment.js
CHANGED
|
@@ -441,6 +441,8 @@ async function paymentTokenize(request, params, payMethods, cart) {
|
|
|
441
441
|
if (paymentMethod.error) {
|
|
442
442
|
return onError(paymentMethod.error);
|
|
443
443
|
} else if (totalDue < 1) {
|
|
444
|
+
// should save payment method data when payment amount is less than 1
|
|
445
|
+
// https://stripe.com/docs/currencies#minimum-and-maximum-charge-amounts
|
|
444
446
|
return cartApi
|
|
445
447
|
.methods(request, options)
|
|
446
448
|
.update({
|
|
@@ -454,7 +456,10 @@ async function paymentTokenize(request, params, payMethods, cart) {
|
|
|
454
456
|
}
|
|
455
457
|
|
|
456
458
|
const currency = toLower(get(cart, 'currency', 'usd'));
|
|
457
|
-
const amount = stripeAmountByCurrency(
|
|
459
|
+
const amount = stripeAmountByCurrency(
|
|
460
|
+
currency,
|
|
461
|
+
cart.trial ? cart.trial_initial_capture_total + cart.trial_auth_total : totalDue,
|
|
462
|
+
);
|
|
458
463
|
const stripeCustomer = get(cart, 'account.stripe_customer');
|
|
459
464
|
const intent = toSnake(
|
|
460
465
|
await methods(request)
|
|
@@ -483,7 +488,12 @@ async function paymentTokenize(request, params, payMethods, cart) {
|
|
|
483
488
|
method: 'card',
|
|
484
489
|
card: paymentMethod,
|
|
485
490
|
intent: {
|
|
486
|
-
stripe: {
|
|
491
|
+
stripe: {
|
|
492
|
+
id: paymentIntent.id,
|
|
493
|
+
...(cart.trial && {
|
|
494
|
+
auth_amount: cart.trial_auth_total,
|
|
495
|
+
}),
|
|
496
|
+
},
|
|
487
497
|
},
|
|
488
498
|
},
|
|
489
499
|
})
|
package/src/utils/index.js
CHANGED
|
@@ -3,6 +3,7 @@ const set = require('lodash/set');
|
|
|
3
3
|
const get = require('lodash/get');
|
|
4
4
|
const uniq = require('lodash/uniq');
|
|
5
5
|
const find = require('lodash/find');
|
|
6
|
+
const round = require('lodash/round');
|
|
6
7
|
const findIndex = require('lodash/findIndex');
|
|
7
8
|
const camelCase = require('lodash/camelCase');
|
|
8
9
|
const snakeCase = require('lodash/snakeCase');
|
|
@@ -257,6 +258,7 @@ module.exports = {
|
|
|
257
258
|
get,
|
|
258
259
|
uniq,
|
|
259
260
|
find,
|
|
261
|
+
round,
|
|
260
262
|
findIndex,
|
|
261
263
|
camelCase,
|
|
262
264
|
cloneDeep,
|
package/src/utils/stripe.js
CHANGED
|
@@ -262,7 +262,7 @@ function stripeAmountByCurrency(currency, amount) {
|
|
|
262
262
|
'MGA', // Malagasy Ariary
|
|
263
263
|
'RWF', // Rwandan Franc
|
|
264
264
|
'VUV', // Vanuatu Vatu
|
|
265
|
-
'XOF' // West African Cfa Franc
|
|
265
|
+
'XOF', // West African Cfa Franc
|
|
266
266
|
];
|
|
267
267
|
if (zeroDecimalCurrencies.includes(currency.toUpperCase())) {
|
|
268
268
|
return amount;
|
|
@@ -53,10 +53,6 @@ class Stripe extends React.Component {
|
|
|
53
53
|
this.renderStripeElements();
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
-
componentDidUpdate() {
|
|
57
|
-
this.renderStripeElements();
|
|
58
|
-
}
|
|
59
|
-
|
|
60
56
|
renderStripeElements() {
|
|
61
57
|
const { type, font } = this.state;
|
|
62
58
|
const options = {
|
|
@@ -124,11 +120,11 @@ class Stripe extends React.Component {
|
|
|
124
120
|
}
|
|
125
121
|
|
|
126
122
|
onChangeType(event, type) {
|
|
127
|
-
this.setState({ type });
|
|
123
|
+
this.setState({ type }, this.renderStripeElements);
|
|
128
124
|
}
|
|
129
125
|
|
|
130
126
|
onChangeFont(event, font) {
|
|
131
|
-
this.setState({ font });
|
|
127
|
+
this.setState({ font }, this.renderStripeElements);
|
|
132
128
|
}
|
|
133
129
|
|
|
134
130
|
render() {
|