@salla.sa/applepay 1.0.29 → 2.11.70

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/src/ApplePay.js DELETED
@@ -1,440 +0,0 @@
1
- import Http from './http';
2
- import DetectOS from './DetectOS';
3
-
4
- window.Salla = window.Salla || {};
5
- window.Salla.Payments = window.Salla.Payments || {};
6
-
7
- /**
8
- * Full Example
9
- *
10
- * Salla.event.createAndDispatch('payments::apple-pay.start-transaction', {
11
- * amount: 1000,
12
- * validateMerchant: {
13
- * url: '{{ route('cp.marketplace.cart.pay', ['cart' => $cart]) }}',
14
- * // onFailed: (response) => {
15
- * // laravel.ajax.errorHandler(response);
16
- * // this.onCancel({}, response.data.error.message);
17
- * // },
18
- * // onSuccess: (response) => {
19
- * // laravel.ajax.successHandler(response);
20
- * // }
21
- * },
22
- * authorized: {
23
- * url: '{{ route('cp.marketplace.cart.confirm', ['cart' => $cart]) }}',
24
- * // onFailed: (response) => {
25
- * // laravel.ajax.errorHandler(response);
26
- * // this.onCancel({}, response.data.error.message);
27
- * // },
28
- * // onSuccess: (response) => {
29
- * // // nothing
30
- * // }
31
- * },
32
- * // onError: function (message) {
33
- * // laravel.alert(message);
34
- * // }
35
- * });
36
- *
37
- * You can hide the apple pay button easy with add data-show-if-apple-pay-supported to element like <div data-show-if-apple-pay-supported>
38
- *
39
- * @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
- */
41
- window.Salla.Payments.ApplePay = function() {
42
-
43
- const session = null;
44
- const detail = null;
45
- let address_id = null;
46
- let shipping_methods = [];
47
- let total;
48
- let request;
49
- let id;
50
-
51
- return {
52
- init: function() {
53
- // init the helpers
54
- document.removeEventListener('payments::apple-pay.start-transaction', window.Salla.Payments.ApplePay.startSession);
55
- Salla.event.addEventListener('payments::apple-pay.start-transaction', window.Salla.Payments.ApplePay.startSession);
56
-
57
- if (!(window.ApplePaySession && ApplePaySession.canMakePayments())) {
58
- $('[data-show-if-apple-pay-supported]').hide();
59
- }
60
- },
61
-
62
- initDefault: function() {
63
- let _this = window.Salla.Payments.ApplePay;
64
-
65
- if (!_this.detail.onError) {
66
- _this.detail.onError = function(message) {
67
- laravel.alert(message);
68
- };
69
- }
70
-
71
- if (!_this.detail.authorized.onFailed) {
72
- _this.detail.authorized.onFailed = (response) => {
73
- laravel.ajax.errorHandler(response);
74
- _this.onCancel({}, response.data.error.message);
75
- };
76
- }
77
-
78
- if (!_this.detail.validateMerchant.onFailed) {
79
- _this.detail.validateMerchant.onFailed = (response) => {
80
- laravel.ajax.errorHandler(response);
81
- _this.onCancel({}, response.data.error.message);
82
- };
83
- }
84
-
85
- if (!_this.detail.authorized.onSuccess) {
86
- _this.detail.authorized.onSuccess = (response) => {
87
- laravel.ajax.successHandler(response);
88
- };
89
- }
90
- },
91
-
92
- startSession: function(event) {
93
- console.log('start session : payments::apple-pay.start-transaction');
94
-
95
- let _this = window.Salla.Payments.ApplePay;
96
- _this.detail = event.detail || event;
97
- console.log(_this.detail);
98
-
99
- _this.initDefault();
100
-
101
- if (!(window.ApplePaySession && ApplePaySession.canMakePayments())) {
102
- console.log('error');
103
- return;
104
- }
105
-
106
- let label = window.location.hostname || 'Salla';
107
-
108
- let version = _this.getApplePaySessionVersion();
109
- let supportedNetworks = _this.detail.supportedNetworks || ['masterCard', 'visa'];
110
-
111
- if (version === 5) {
112
- supportedNetworks.push('mada');
113
- }
114
-
115
-
116
- _this.total = {
117
- type: 'final',
118
- label: label,
119
- amount: _this.detail.amount
120
- };
121
-
122
- _this.request = {
123
- countryCode: 'SA',
124
- currencyCode: _this.detail.currency || 'SAR',
125
- 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) : []
131
- };
132
-
133
- console.log('apple pay payment request:', _this.request);
134
-
135
- // https://developer.apple.com/documentation/apple_pay_on_the_web/applepaypaymentrequest
136
- _this.session = new ApplePaySession(version, _this.request);
137
-
138
- _this.session.onshippingcontactselected = _this.onShippingContactSelected;
139
- _this.session.onshippingmethodselected = _this.onShippingMethodSelected;
140
- _this.session.onvalidatemerchant = _this.onValidateMerchant;
141
- _this.session.onpaymentauthorized = _this.onPaymentAuthorized;
142
- _this.session.oncancel = _this.onCancel;
143
- _this.session.begin();
144
- },
145
-
146
- onCancel: (event = {}, message = null) => {
147
- window.Salla.Payments.ApplePay.detail.onError(message || 'فشل الدفع!');
148
- Salla.event.createAndDispatch('payments::apple-pay.canceled', event);
149
- },
150
-
151
- /**
152
- * Confirm payment after authorization.
153
- *
154
- * @param event
155
- */
156
- onPaymentAuthorized: (event) => {
157
- let _this = window.Salla.Payments.ApplePay;
158
- console.log(event.payment);
159
-
160
- Salla.event.createAndDispatch('payments::apple-pay.authorized.init', event);
161
- Http.post(_this.detail.authorized.url.replace('{id}', _this.id), {
162
- payment_method: 'apple_pay',
163
- applepay_token: JSON.stringify(event.payment)
164
- }, ({data}) => {
165
- Salla.event.createAndDispatch('payments::apple-pay.authorized.success', data);
166
-
167
- _this.session.completePayment(ApplePaySession.STATUS_SUCCESS);
168
-
169
- if (typeof _this.detail.authorized.onSuccess === 'function') {
170
- _this.detail.authorized.onSuccess(data);
171
- }
172
- }, ({response}) => {
173
-
174
- Salla.event.createAndDispatch('payments::apple-pay.authorized.failed', response);
175
-
176
- _this.session.completePayment(ApplePaySession.STATUS_FAILURE);
177
-
178
- if (typeof _this.detail.authorized.onFailed === 'function') {
179
- _this.detail.authorized.onFailed(response);
180
- }
181
- });
182
- },
183
-
184
- /**
185
- * Validate Submit.
186
- *
187
- * @param event
188
- */
189
- onValidateMerchant: (event) => {
190
- let _this = window.Salla.Payments.ApplePay;
191
- Salla.event.createAndDispatch('payments::apple-pay.validate-merchant.init', event);
192
-
193
- Http.post(_this.detail.validateMerchant.url.replace('{id}', _this.id), {
194
- amount: _this.detail.amount,
195
- validation_url: event.validationURL,
196
- payment_method: 'apple_pay'
197
- }, ({data}) => {
198
-
199
- Salla.event.createAndDispatch('payments::apple-pay.validate-merchant.success', data);
200
-
201
- if (typeof _this.detail.validateMerchant.onSuccess === 'function') {
202
- _this.detail.validateMerchant.onSuccess(data).then((response) => {
203
-
204
- //set cart id
205
- if (response.data) {
206
- _this.id = response.data.id || response.data.data.id;
207
- }
208
-
209
- _this.session.completeMerchantValidation(data.data);
210
-
211
- }).catch((response) => {
212
- _this.abortValidateMerchant(response);
213
- });
214
- } else {
215
- _this.session.completeMerchantValidation(data.data);
216
- }
217
-
218
- }, ({response}) => {
219
- _this.abortValidateMerchant(response);
220
- });
221
- },
222
-
223
- abortValidateMerchant: (response = null) => {
224
- let _this = window.Salla.Payments.ApplePay;
225
-
226
- // todo :: check if we still need it.
227
- // _this.session.completeMerchantValidation(ApplePaySession.STATUS_FAILURE);
228
- _this.abortSession();
229
- Salla.event.createAndDispatch('payments::apple-pay.validate-merchant.failed', response);
230
-
231
- if (typeof _this.detail.validateMerchant.onFailed === 'function') {
232
- _this.detail.validateMerchant.onFailed(response);
233
- }
234
- },
235
-
236
- /**
237
- * Select Shipping Contact
238
- *
239
- * @param event
240
- */
241
- onShippingContactSelected: (event) => {
242
- console.log(event.shippingContact);
243
-
244
- let _this = window.Salla.Payments.ApplePay;
245
- if (!_this.detail.requiredShippingContactFields) {
246
- return;
247
- }
248
- return new Promise((resolve, reject) => {
249
- // Create New Address
250
- 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
- });
284
- });
285
-
286
- }).then(() => {
287
- _this.selectApplePayShippingMethod(_this.shipping_methods[0]['ship_id'], _this.shipping_methods[0]['private_ship_id']);
288
- }).then(() => {
289
- console.log('shipping contact selected success');
290
- _this.session.completeShippingContactSelection(
291
- {
292
- 'newTotal': _this.total,
293
- 'newShippingMethods': _this.mappingShippingMethods(_this.shipping_methods)
294
- }
295
- );
296
- }).catch(function(error) {
297
- console.log(error);
298
- _this.session.completeShippingContactSelection(
299
- {
300
- 'newTotal': _this.total,
301
- 'errors': [new window.ApplePayError('shippingContactInvalid', 'locality', error)]
302
- });
303
- //_this.abortSession();
304
- });
305
- },
306
-
307
- /**
308
- * Select Shipping Method
309
- *
310
- * @param event
311
- *
312
- */
313
- onShippingMethodSelected: (event) => {
314
- let _this = window.Salla.Payments.ApplePay;
315
- console.log(event.shippingMethod);
316
-
317
- return new Promise((resolve, reject) => {
318
- let shipping_ids = event.shippingMethod.identifier.split(',');
319
- _this.selectApplePayShippingMethod(
320
- shipping_ids[0],
321
- typeof shipping_ids[1] === 'undefined' ? null : shipping_ids[1]
322
- );
323
- resolve();
324
-
325
- }).then(() => {
326
- _this.session.completeShippingMethodSelection({'newTotal': _this.total});
327
-
328
- }).catch(function(error) {
329
- console.log(error);
330
- _this.abortSession();
331
- });
332
- },
333
-
334
-
335
- abortSession: () => {
336
- let _this = window.Salla.Payments.ApplePay;
337
-
338
- if (_this.session) {
339
- _this.session.abort();
340
- }
341
- },
342
-
343
- getApplePaySessionVersion: () => {
344
- const userAgent = navigator.userAgent || navigator.vendor || window.opera;
345
-
346
- if (userAgent === 'sallapp') {
347
- return 5;
348
- }
349
-
350
- // can't handle custom user agent like sallapp
351
- let detection = DetectOS.init();
352
- let v = parseFloat(detection.os.version);
353
-
354
- if (detection.os.name === 'Macintosh') {
355
- if (v < 10.142) {
356
- return 1;
357
- }
358
- } else {
359
- if (v < 12.11) {
360
- return 1;
361
- }
362
- }
363
-
364
- return 5;
365
- },
366
-
367
- recalculateTotal: () => {
368
- console.log('Recalculate Total');
369
- let _this = window.Salla.Payments.ApplePay;
370
-
371
- $.ajax({
372
- url: _this.detail.recalculateTotal.url.replace('{id}', _this.id),
373
- method: 'GET',
374
- async: false,
375
- success: function(data) {
376
- _this.total = Object.assign({}, _this.total, {
377
- amount: data.data.initial_data.cart.total
378
- });
379
-
380
- let total = _this.total;
381
- _this.request = Object.assign({}, _this.request, {total});
382
- console.log('Recalculate Total Success', total);
383
- },
384
- error: function(e) {
385
- console.log('Recalculate Total Failed');
386
- console.log(response);
387
- }
388
- });
389
- },
390
-
391
- selectApplePayShippingMethod: (company_id, private_company_id) => {
392
- let _this = window.Salla.Payments.ApplePay;
393
- console.log('select shipping method ', 'company_id : ' + company_id, 'private_company_id: ' + private_company_id);
394
-
395
- $.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) {
406
- if (typeof _this.detail.shippingMethodSelected.onSuccess === 'function') {
407
- _this.detail.shippingMethodSelected.onSuccess(data);
408
- }
409
- _this.recalculateTotal();
410
- },
411
- error: function(response) {
412
- // _this.abortSession();
413
- if (typeof _this.detail.shippingMethodSelected.onFailed === 'function') {
414
- _this.detail.shippingMethodSelected.onFailed(response);
415
- }
416
- }
417
- });
418
- },
419
-
420
- mappingShippingMethods: (methods) => {
421
-
422
- return methods.map(function(method) {
423
- let identifier = method.ship_id.toString();
424
-
425
- if (method.private_ship_id) {
426
- identifier += ',' + method.private_ship_id.toString();
427
- }
428
- return {
429
- 'label': method.shipping_title,
430
- 'amount': method.enable_free_shipping ? 0 : method.ship_cost,
431
- 'detail': '',
432
- 'identifier': identifier
433
- };
434
- });
435
- }
436
- };
437
-
438
- }();
439
-
440
- window.Salla.Payments.ApplePay.init();