@salla.sa/applepay 2.13.101 → 2.13.103

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.
Files changed (3) hide show
  1. package/package.json +2 -2
  2. package/src/index.js +7 -102
  3. package/src/utils.js +137 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@salla.sa/applepay",
3
- "version": "2.13.101",
3
+ "version": "2.13.103",
4
4
  "description": "Salla Apple Pay light",
5
5
  "main": "dist/app.js",
6
6
  "scripts": {
@@ -26,5 +26,5 @@
26
26
  "dependencies": {
27
27
  "axios": "^0.23.0"
28
28
  },
29
- "gitHead": "e09dc44f0a3fe0aba76ab19a269377841da7bff4"
29
+ "gitHead": "2011314e946caa20b07530ab034a8956b9f3a648"
30
30
  }
package/src/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import Http from './http';
2
2
  import DetectOS from './DetectOS';
3
+ import { mutateShipmentAddress } from './utils';
3
4
 
4
5
  window.Salla = window.Salla || {};
5
6
  window.Salla.Payments = window.Salla.Payments || {};
@@ -204,9 +205,12 @@ window.SallaApplePay = {
204
205
  *
205
206
  * @param event
206
207
  */
207
- onPaymentAuthorized: (event) => {
208
+ onPaymentAuthorized: async (event) => {
208
209
  salla.logger.log('🍏 Pay: onPaymentAuthorized', event.payment);
209
210
 
211
+ // Update the payment address
212
+ await mutateShipmentAddress(SallaApplePay, event.payment.shippingContact, true);
213
+
210
214
  Salla.event.dispatch('payments::apple-pay.authorized.init', event);
211
215
  Http.post(SallaApplePay.detail.authorized.url.replace('{id}', SallaApplePay.id), {
212
216
  payment_method: 'apple_pay',
@@ -295,107 +299,8 @@ window.SallaApplePay = {
295
299
  onShippingContactSelected: async (event) => {
296
300
  salla.logger.log('🍏 Pay: onShippingContactSelected', event.shippingContact);
297
301
 
298
- if (!SallaApplePay.detail.requiredShippingContactFields) {
299
- return;
300
- }
301
-
302
- // Create New Address
303
- return Http.post(SallaApplePay.detail.shippingContactSelected.url.replace('{id}', SallaApplePay.id),
304
- {
305
- 'country': event.shippingContact.country,
306
- 'city': event.shippingContact.locality,
307
- 'local': event.shippingContact.subLocality,
308
- 'description': event.shippingContact.subAdministrativeArea,
309
- 'street': event.shippingContact.administrativeArea,
310
- 'country_code': event.shippingContact.countryCode,
311
- 'postal_code': event.shippingContact.postalCode
312
- },
313
- async ({data}) => {
314
- if (typeof SallaApplePay.detail.shippingContactSelected.onSuccess === 'function') {
315
- SallaApplePay.detail.shippingContactSelected.onSuccess(data);
316
- }
317
-
318
- SallaApplePay.address_id = data.data.address_id;
319
- SallaApplePay.shipping_methods = data.data.shipping_methods;
320
-
321
- if (!SallaApplePay.shipping_methods || (SallaApplePay.shipping_methods && !SallaApplePay.shipping_methods.length)) {
322
- salla.logger.warn('🍏 Pay: We dont found any supported methods', data);
323
-
324
- return SallaApplePay.session.completeShippingContactSelection({
325
- status: SallaApplePay.session.STATUS_INVALID_SHIPPING_POSTAL_ADDRESS,
326
- errors: [
327
- new window.ApplePayError('addressUnserviceable')
328
- ]
329
- });
330
- }
331
-
332
- try {
333
- await SallaApplePay.selectApplePayShippingMethod(SallaApplePay.shipping_methods[0]['ship_id'], SallaApplePay.shipping_methods[0]['private_ship_id']);
334
- } catch (error) {
335
- salla.logger.warn('Failed set the shipping details to api', error);
336
-
337
- return SallaApplePay.session.completeShippingContactSelection({
338
- status: SallaApplePay.session.STATUS_INVALID_SHIPPING_POSTAL_ADDRESS,
339
- errors: [
340
- new window.ApplePayError('addressUnserviceable')
341
- ]
342
- });
343
- }
344
-
345
- try {
346
- await SallaApplePay.recalculateTotal();
347
- } catch (error) {
348
- salla.logger.warn('🍏 Pay: Failed recalculate total', error);
349
-
350
- return SallaApplePay.session.completeShippingContactSelection({
351
- status: SallaApplePay.session.STATUS_INVALID_SHIPPING_POSTAL_ADDRESS,
352
- errors: [
353
- new window.ApplePayError('addressUnserviceable')
354
- ]
355
- });
356
- }
357
-
358
- SallaApplePay.session.completeShippingContactSelection({
359
- newTotal: SallaApplePay.prepareTotal(),
360
- newLineItems: SallaApplePay.prepareLineItems(),
361
- newShippingMethods: SallaApplePay.mappingShippingMethods(SallaApplePay.shipping_methods)
362
- });
363
-
364
- }, ({response}) => {
365
- salla.logger.warn('🍏 Pay: Failed add address via api', response);
366
-
367
- if (typeof SallaApplePay.detail.shippingContactSelected.onFailed === 'function') {
368
- SallaApplePay.detail.shippingContactSelected.onFailed(response);
369
- }
370
-
371
- // parse 422 errors
372
- let fields = response?.data?.error?.fields;
373
-
374
- let errors = [];
375
-
376
- if (fields?.country_code) {
377
- errors.push(new window.ApplePayError('shippingContactInvalid', 'countryCode', fields?.country_code[0]))
378
- }
379
-
380
- if (fields?.city) {
381
- errors.push(new window.ApplePayError('shippingContactInvalid', 'locality', fields?.city[0]))
382
- }
383
-
384
- if (fields?.country) {
385
- errors.push(new window.ApplePayError('shippingContactInvalid', 'country', fields?.country[0]))
386
- }
387
-
388
- if (errors.length === 0 && response?.data?.error?.message) {
389
- errors.push(new window.ApplePayError('shippingContactInvalid', 'locality', response?.data?.error?.message))
390
- }
391
-
392
- SallaApplePay.session.completeShippingContactSelection({
393
- newTotal: SallaApplePay.prepareTotal(),
394
- newLineItems: SallaApplePay.prepareLineItems(),
395
- status: SallaApplePay.session.STATUS_INVALID_SHIPPING_POSTAL_ADDRESS,
396
- errors: errors
397
- });
398
- });
302
+ // create address for shipping calculation
303
+ mutateShipmentAddress(SallaApplePay, event.shippingContact);
399
304
  },
400
305
 
401
306
  /**
package/src/utils.js ADDED
@@ -0,0 +1,137 @@
1
+ import http from "./http";
2
+
3
+ /**
4
+ * @typedef {Object} ApplePayPaymentContact
5
+ * @property {string} phoneNumber
6
+ * @property {string} emailAddress
7
+ * @property {string} givenName
8
+ * @property {string} familyName
9
+ * @property {string} [phoneticGivenName]
10
+ * @property {string} [phoneticFamilyName]
11
+ * @property {string[]} addressLines
12
+ * @property {string} [subLocality]
13
+ * @property {string} locality
14
+ * @property {string} postalCode
15
+ * @property {string} [subAdministrativeArea]
16
+ * @property {string} administrativeArea
17
+ * @property {string} country
18
+ * @property {string} countryCode
19
+ */
20
+
21
+
22
+ /**
23
+ *
24
+ * @param {SallaApplePay} SallaApplePay
25
+ * @param {boolean} isAuthorized
26
+ * @param {ApplePayPaymentContact} shippingContact
27
+ *
28
+ */
29
+ export function mutateShipmentAddress(SallaApplePay, shippingContact, isAuthorized = false) {
30
+ console.log('mutateShipmentAddress called', shippingContact, isAuthorized);
31
+
32
+ if (!SallaApplePay.detail.requiredShippingContactFields) {
33
+ return;
34
+ }
35
+
36
+ return http.post(
37
+ SallaApplePay.detail.shippingContactSelected.url.replace('{id}', SallaApplePay.id),
38
+ {
39
+ 'country': shippingContact.country,
40
+ 'city': shippingContact.locality,
41
+ 'local': shippingContact.subLocality,
42
+ 'description': shippingContact.subAdministrativeArea,
43
+ 'street': shippingContact.addressLines?.join(", ") || shippingContact.administrativeArea,
44
+ 'country_code': shippingContact.countryCode,
45
+ 'postal_code': shippingContact.postalCode,
46
+ 'is_authorized': isAuthorized
47
+ },
48
+ async ({ data }) => {
49
+ if (isAuthorized) { return }
50
+ if (typeof SallaApplePay.detail.shippingContactSelected.onSuccess === 'function') {
51
+ SallaApplePay.detail.shippingContactSelected.onSuccess(data);
52
+ }
53
+
54
+ SallaApplePay.address_id = data.data.address_id;
55
+ SallaApplePay.shipping_methods = data.data.shipping_methods;
56
+
57
+ if (!SallaApplePay.shipping_methods || (SallaApplePay.shipping_methods && !SallaApplePay.shipping_methods.length)) {
58
+ salla.logger.warn('🍏 Pay: We dont found any supported methods', data);
59
+
60
+ return SallaApplePay.session.completeShippingContactSelection({
61
+ status: SallaApplePay.session.STATUS_INVALID_SHIPPING_POSTAL_ADDRESS,
62
+ errors: [
63
+ new window.ApplePayError('addressUnserviceable')
64
+ ]
65
+ });
66
+ }
67
+
68
+ try {
69
+ await SallaApplePay.selectApplePayShippingMethod(SallaApplePay.shipping_methods[0]['ship_id'], SallaApplePay.shipping_methods[0]['private_ship_id']);
70
+ } catch (error) {
71
+ salla.logger.warn('Failed set the shipping details to api', error);
72
+
73
+ return SallaApplePay.session.completeShippingContactSelection({
74
+ status: SallaApplePay.session.STATUS_INVALID_SHIPPING_POSTAL_ADDRESS,
75
+ errors: [
76
+ new window.ApplePayError('addressUnserviceable')
77
+ ]
78
+ });
79
+ }
80
+
81
+ try {
82
+ await SallaApplePay.recalculateTotal();
83
+ } catch (error) {
84
+ salla.logger.warn('🍏 Pay: Failed recalculate total', error);
85
+
86
+ return SallaApplePay.session.completeShippingContactSelection({
87
+ status: SallaApplePay.session.STATUS_INVALID_SHIPPING_POSTAL_ADDRESS,
88
+ errors: [
89
+ new window.ApplePayError('addressUnserviceable')
90
+ ]
91
+ });
92
+ }
93
+
94
+ SallaApplePay.session.completeShippingContactSelection({
95
+ newTotal: SallaApplePay.prepareTotal(),
96
+ newLineItems: SallaApplePay.prepareLineItems(),
97
+ newShippingMethods: SallaApplePay.mappingShippingMethods(SallaApplePay.shipping_methods)
98
+ });
99
+
100
+ },
101
+ ({ response }) => {
102
+ salla.logger.warn('🍏 Pay: Failed add address via api', response);
103
+
104
+ if (typeof SallaApplePay.detail.shippingContactSelected.onFailed === 'function') {
105
+ SallaApplePay.detail.shippingContactSelected.onFailed(response);
106
+ }
107
+
108
+ // parse 422 errors
109
+ let fields = response?.data?.error?.fields;
110
+
111
+ let errors = [];
112
+
113
+ if (fields?.country_code) {
114
+ errors.push(new window.ApplePayError('shippingContactInvalid', 'countryCode', fields?.country_code[0]))
115
+ }
116
+
117
+ if (fields?.city) {
118
+ errors.push(new window.ApplePayError('shippingContactInvalid', 'locality', fields?.city[0]))
119
+ }
120
+
121
+ if (fields?.country) {
122
+ errors.push(new window.ApplePayError('shippingContactInvalid', 'country', fields?.country[0]))
123
+ }
124
+
125
+ if (errors.length === 0 && response?.data?.error?.message) {
126
+ errors.push(new window.ApplePayError('shippingContactInvalid', 'locality', response?.data?.error?.message))
127
+ }
128
+
129
+ SallaApplePay.session.completeShippingContactSelection({
130
+ newTotal: SallaApplePay.prepareTotal(),
131
+ newLineItems: SallaApplePay.prepareLineItems(),
132
+ status: SallaApplePay.session.STATUS_INVALID_SHIPPING_POSTAL_ADDRESS,
133
+ errors: errors
134
+ });
135
+ }
136
+ );
137
+ }