@salla.sa/applepay 1.0.28 → 1.0.30

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@salla.sa/applepay",
3
- "version": "1.0.28",
3
+ "version": "1.0.30",
4
4
  "description": "Salla Apple Pay",
5
5
  "main": "dist/app.js",
6
6
  "scripts": {
package/src/ApplePay.js CHANGED
@@ -3,7 +3,6 @@ import DetectOS from './DetectOS';
3
3
 
4
4
  window.Salla = window.Salla || {};
5
5
  window.Salla.Payments = window.Salla.Payments || {};
6
-
7
6
  /**
8
7
  * Full Example
9
8
  *
@@ -38,7 +37,7 @@ window.Salla.Payments = window.Salla.Payments || {};
38
37
  *
39
38
  * @type {{init: Window.Salla.Payments.ApplePay.init, initDefault: Window.Salla.Payments.ApplePay.initDefault, onCancel: Window.Salla.Payments.ApplePay.onCancel, onPaymentAuthorized: Window.Salla.Payments.ApplePay.onPaymentAuthorized, getApplePaySessionVersion: (function(): number), startSession: (function(*): undefined), abortSession: Window.Salla.Payments.ApplePay.abortSession, onValidateMerchant: Window.Salla.Payments.ApplePay.onValidateMerchant}}
40
39
  */
41
- window.Salla.Payments.ApplePay = function() {
40
+ window.Salla.Payments.ApplePay = function () {
42
41
 
43
42
  const session = null;
44
43
  const detail = null;
@@ -49,21 +48,24 @@ window.Salla.Payments.ApplePay = function() {
49
48
  let id;
50
49
 
51
50
  return {
52
- init: function() {
51
+ init: function () {
52
+ window.isLegacyTheme = window.$ && ['theme_1', 'theme_2', 'theme_3', 'theme_4', 'theme_5', 'theme_6', 'default'].includes(Salla?.theme?.name);
53
53
  // init the helpers
54
54
  document.removeEventListener('payments::apple-pay.start-transaction', window.Salla.Payments.ApplePay.startSession);
55
55
  Salla.event.addEventListener('payments::apple-pay.start-transaction', window.Salla.Payments.ApplePay.startSession);
56
56
 
57
- if (!(window.ApplePaySession && ApplePaySession.canMakePayments())) {
58
- $('[data-show-if-apple-pay-supported]').hide();
57
+ if (window.ApplePaySession && ApplePaySession.canMakePayments()) {
58
+ return;
59
59
  }
60
+
61
+ document.querySelectorAll('data-show-if-apple-pay-supported').forEach(btn => btn.style.display = 'none');
60
62
  },
61
63
 
62
- initDefault: function() {
64
+ initDefault: function () {
63
65
  let _this = window.Salla.Payments.ApplePay;
64
66
 
65
67
  if (!_this.detail.onError) {
66
- _this.detail.onError = function(message) {
68
+ _this.detail.onError = function (message) {
67
69
  laravel.alert(message);
68
70
  };
69
71
  }
@@ -89,7 +91,7 @@ window.Salla.Payments.ApplePay = function() {
89
91
  }
90
92
  },
91
93
 
92
- startSession: function(event) {
94
+ startSession: function (event) {
93
95
  console.log('start session : payments::apple-pay.start-transaction');
94
96
 
95
97
  let _this = window.Salla.Payments.ApplePay;
@@ -114,20 +116,20 @@ window.Salla.Payments.ApplePay = function() {
114
116
 
115
117
 
116
118
  _this.total = {
117
- type: 'final',
118
- label: label,
119
+ type : 'final',
120
+ label : label,
119
121
  amount: _this.detail.amount
120
122
  };
121
123
 
122
124
  _this.request = {
123
- countryCode: 'SA',
124
- currencyCode: _this.detail.currency || 'SAR',
125
+ countryCode : 'SA',
126
+ currencyCode : _this.detail.currency || 'SAR',
125
127
  requiredShippingContactFields: _this.detail.requiredShippingContactFields ? _this.detail.requiredShippingContactFields : [],
126
- merchantCapabilities: ['supports3DS'],
127
- supportedNetworks: _this.detail.supportedNetworks || supportedNetworks,
128
- total: _this.total,
129
- shippingContact: _this.detail.shippingContact ? _this.detail.shippingContact : {},
130
- shippingMethods: _this.detail.shippingMethods && _this.detail.shippingMethods.length ? _this.mappingShippingMethods(event.detail.shippingMethods) : []
128
+ merchantCapabilities : ['supports3DS'],
129
+ supportedNetworks : _this.detail.supportedNetworks || supportedNetworks,
130
+ total : _this.total,
131
+ shippingContact : _this.detail.shippingContact ? _this.detail.shippingContact : {},
132
+ shippingMethods : _this.detail.shippingMethods && _this.detail.shippingMethods.length ? _this.mappingShippingMethods(event.detail.shippingMethods) : []
131
133
  };
132
134
 
133
135
  console.log('apple pay payment request:', _this.request);
@@ -191,7 +193,7 @@ window.Salla.Payments.ApplePay = function() {
191
193
  Salla.event.createAndDispatch('payments::apple-pay.validate-merchant.init', event);
192
194
 
193
195
  Http.post(_this.detail.validateMerchant.url.replace('{id}', _this.id), {
194
- amount: _this.detail.amount,
196
+ amount : _this.detail.amount,
195
197
  validation_url: event.validationURL,
196
198
  payment_method: 'apple_pay'
197
199
  }, ({data}) => {
@@ -205,7 +207,7 @@ window.Salla.Payments.ApplePay = function() {
205
207
  if (response.data) {
206
208
  _this.id = response.data.id || response.data.data.id;
207
209
  }
208
-
210
+
209
211
  _this.session.completeMerchantValidation(data.data);
210
212
 
211
213
  }).catch((response) => {
@@ -248,58 +250,58 @@ window.Salla.Payments.ApplePay = function() {
248
250
  return new Promise((resolve, reject) => {
249
251
  // Create New Address
250
252
  Http.post(_this.detail.shippingContactSelected.url.replace('{id}', _this.id),
251
- {
252
- 'country': event.shippingContact.country,
253
- 'city': event.shippingContact.locality,
254
- 'local': event.shippingContact.subLocality,
255
- 'description': event.shippingContact.subAdministrativeArea,
256
- 'street': event.shippingContact.administrativeArea,
257
- 'country_code': event.shippingContact.countryCode,
258
- 'postal_code': event.shippingContact.postalCode
259
- },
260
- ({data}) => {
261
- if (typeof _this.detail.shippingContactSelected.onSuccess === 'function') {
262
- _this.detail.shippingContactSelected.onSuccess(data);
263
- }
264
-
265
- _this.address_id = data.data.address_id;
266
- _this.shipping_methods = data.data.shipping_methods;
267
-
268
- if (!_this.shipping_methods || (_this.shipping_methods && !_this.shipping_methods.length)) {
269
- reject('لايوجد شركات شحن لهذا العنوان');
270
- }
271
-
272
- resolve(data);
273
-
274
- }, ({response}) => {
275
- console.log(response);
276
- // _this.abortSession();
277
- if (typeof _this.detail.shippingContactSelected.onFailed === 'function') {
278
- _this.detail.shippingContactSelected.onFailed(response);
279
- }
280
- _this.session.completeShippingContactSelection({
281
- 'newTotal': _this.total,
282
- 'errors': [new window.ApplePayError('shippingContactInvalid', 'locality', response.data.error.message)]
283
- });
253
+ {
254
+ 'country' : event.shippingContact.country,
255
+ 'city' : event.shippingContact.locality,
256
+ 'local' : event.shippingContact.subLocality,
257
+ 'description' : event.shippingContact.subAdministrativeArea,
258
+ 'street' : event.shippingContact.administrativeArea,
259
+ 'country_code': event.shippingContact.countryCode,
260
+ 'postal_code' : event.shippingContact.postalCode
261
+ },
262
+ ({data}) => {
263
+ if (typeof _this.detail.shippingContactSelected.onSuccess === 'function') {
264
+ _this.detail.shippingContactSelected.onSuccess(data);
265
+ }
266
+
267
+ _this.address_id = data.data.address_id;
268
+ _this.shipping_methods = data.data.shipping_methods;
269
+
270
+ if (!_this.shipping_methods || (_this.shipping_methods && !_this.shipping_methods.length)) {
271
+ reject('لايوجد شركات شحن لهذا العنوان');
272
+ }
273
+
274
+ resolve(data);
275
+
276
+ }, ({response}) => {
277
+ console.log(response);
278
+ // _this.abortSession();
279
+ if (typeof _this.detail.shippingContactSelected.onFailed === 'function') {
280
+ _this.detail.shippingContactSelected.onFailed(response);
281
+ }
282
+ _this.session.completeShippingContactSelection({
283
+ 'newTotal': _this.total,
284
+ 'errors' : [new window.ApplePayError('shippingContactInvalid', 'locality', response.data.error.message)]
284
285
  });
286
+ });
285
287
 
286
288
  }).then(() => {
287
289
  _this.selectApplePayShippingMethod(_this.shipping_methods[0]['ship_id'], _this.shipping_methods[0]['private_ship_id']);
288
290
  }).then(() => {
289
291
  console.log('shipping contact selected success');
290
292
  _this.session.completeShippingContactSelection(
291
- {
292
- 'newTotal': _this.total,
293
- 'newShippingMethods': _this.mappingShippingMethods(_this.shipping_methods)
294
- }
293
+ {
294
+ 'newTotal' : _this.total,
295
+ 'newShippingMethods': _this.mappingShippingMethods(_this.shipping_methods)
296
+ }
295
297
  );
296
- }).catch(function(error) {
298
+ }).catch(function (error) {
297
299
  console.log(error);
298
300
  _this.session.completeShippingContactSelection(
299
- {
300
- 'newTotal': _this.total,
301
- 'errors': [new window.ApplePayError('shippingContactInvalid', 'locality', error)]
302
- });
301
+ {
302
+ 'newTotal': _this.total,
303
+ 'errors' : [new window.ApplePayError('shippingContactInvalid', 'locality', error)]
304
+ });
303
305
  //_this.abortSession();
304
306
  });
305
307
  },
@@ -317,15 +319,15 @@ window.Salla.Payments.ApplePay = function() {
317
319
  return new Promise((resolve, reject) => {
318
320
  let shipping_ids = event.shippingMethod.identifier.split(',');
319
321
  _this.selectApplePayShippingMethod(
320
- shipping_ids[0],
321
- typeof shipping_ids[1] === 'undefined' ? null : shipping_ids[1]
322
+ shipping_ids[0],
323
+ typeof shipping_ids[1] === 'undefined' ? null : shipping_ids[1]
322
324
  );
323
325
  resolve();
324
326
 
325
327
  }).then(() => {
326
328
  _this.session.completeShippingMethodSelection({'newTotal': _this.total});
327
329
 
328
- }).catch(function(error) {
330
+ }).catch(function (error) {
329
331
  console.log(error);
330
332
  _this.abortSession();
331
333
  });
@@ -367,12 +369,32 @@ window.Salla.Payments.ApplePay = function() {
367
369
  recalculateTotal: () => {
368
370
  console.log('Recalculate Total');
369
371
  let _this = window.Salla.Payments.ApplePay;
372
+ const url = _this.detail.recalculateTotal.url.replace('{id}', _this.id);
373
+
374
+ if (!window.isLegacyTheme) {
375
+ //twilight
376
+ try {
377
+ let data = salla.api.payment.recalculateTotal(url);
378
+ _this.total = Object.assign({}, _this.total, {
379
+ amount: data.data.initial_data.cart.total
380
+ });
370
381
 
382
+ let total = _this.total;
383
+ _this.request = Object.assign({}, _this.request, {total});
384
+ } catch (error) {
385
+ console.error(error);
386
+ // expected output: ReferenceError: nonExistentFunction is not defined
387
+ // Note - error messages will vary depending on browser
388
+ }
389
+ return;
390
+ }
391
+
392
+ //we are sure that this is called in a legacy theme
371
393
  $.ajax({
372
- url: _this.detail.recalculateTotal.url.replace('{id}', _this.id),
373
- method: 'GET',
374
- async: false,
375
- success: function(data) {
394
+ url : url,
395
+ method : 'GET',
396
+ async : false,
397
+ success: function (data) {
376
398
  _this.total = Object.assign({}, _this.total, {
377
399
  amount: data.data.initial_data.cart.total
378
400
  });
@@ -381,7 +403,7 @@ window.Salla.Payments.ApplePay = function() {
381
403
  _this.request = Object.assign({}, _this.request, {total});
382
404
  console.log('Recalculate Total Success', total);
383
405
  },
384
- error: function(e) {
406
+ error : function (e) {
385
407
  console.log('Recalculate Total Failed');
386
408
  console.log(response);
387
409
  }
@@ -391,24 +413,42 @@ window.Salla.Payments.ApplePay = function() {
391
413
  selectApplePayShippingMethod: (company_id, private_company_id) => {
392
414
  let _this = window.Salla.Payments.ApplePay;
393
415
  console.log('select shipping method ', 'company_id : ' + company_id, 'private_company_id: ' + private_company_id);
416
+ let url = _this.detail.shippingMethodSelected.url.replace('{id}', _this.id)
417
+ let payload = {
418
+ address_id : _this.address_id,
419
+ company_id : company_id,
420
+ private_company_id: private_company_id,
421
+ payment_method : 'apple_pay'
422
+ };
394
423
 
424
+ if (!window.isLegacyTheme) {
425
+ try {
426
+ let data = salla.api.payment.selectShippingMethod(url, payload)
427
+ if (typeof _this.detail.shippingMethodSelected.onSuccess === 'function') {
428
+ _this.detail.shippingMethodSelected.onSuccess(data);
429
+ }
430
+ _this.recalculateTotal();
431
+ } catch (error) {
432
+ if (typeof _this.detail.shippingMethodSelected.onFailed === 'function') {
433
+ _this.detail.shippingMethodSelected.onFailed(error);
434
+ }
435
+ }
436
+ return;
437
+ }
438
+
439
+ //we are sure that this is called in a legacy theme
395
440
  $.ajax({
396
- url: _this.detail.shippingMethodSelected.url.replace('{id}', _this.id),
397
- method: 'POST',
398
- data: {
399
- address_id: _this.address_id,
400
- company_id: company_id,
401
- private_company_id: private_company_id,
402
- payment_method: 'apple_pay'
403
- },
404
- async: false,
405
- success: function(data) {
441
+ url : url,
442
+ method : 'POST',
443
+ data : payload,
444
+ async : false,
445
+ success: function (data) {
406
446
  if (typeof _this.detail.shippingMethodSelected.onSuccess === 'function') {
407
447
  _this.detail.shippingMethodSelected.onSuccess(data);
408
448
  }
409
449
  _this.recalculateTotal();
410
450
  },
411
- error: function(response) {
451
+ error : function (response) {
412
452
  // _this.abortSession();
413
453
  if (typeof _this.detail.shippingMethodSelected.onFailed === 'function') {
414
454
  _this.detail.shippingMethodSelected.onFailed(response);
@@ -419,16 +459,16 @@ window.Salla.Payments.ApplePay = function() {
419
459
 
420
460
  mappingShippingMethods: (methods) => {
421
461
 
422
- return methods.map(function(method) {
462
+ return methods.map(function (method) {
423
463
  let identifier = method.ship_id.toString();
424
464
 
425
465
  if (method.private_ship_id) {
426
466
  identifier += ',' + method.private_ship_id.toString();
427
467
  }
428
468
  return {
429
- 'label': method.shipping_title,
430
- 'amount': method.enable_free_shipping ? 0 : method.ship_cost,
431
- 'detail': '',
469
+ 'label' : method.shipping_title,
470
+ 'amount' : method.enable_free_shipping ? 0 : method.ship_cost,
471
+ 'detail' : '',
432
472
  'identifier': identifier
433
473
  };
434
474
  });
@@ -1,3 +0,0 @@
1
- - match:
2
- dependency_type: all
3
- update_type: "semver:patch" # includes patch updates!