/
angelleye-in-context-checkout.js
575 lines (566 loc) · 31.3 KB
/
angelleye-in-context-checkout.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
jQuery(function ($) {
if (typeof angelleye_in_content_param === 'undefined') {
return false;
}
display_smart_button_on_cart_checkout();
display_smart_button_on_min_cart();
display_smart_button_on_product_page();
display_smart_button_on_wsc_cart();
function is_funding_icon_should_show_in_content() {
var disallowed_funding_methods = angelleye_in_content_param.disallowed_funding_methods;
if (disallowed_funding_methods === null) {
disallowed_funding_methods = [];
}
if ($.inArray('card', disallowed_funding_methods) > -1) {
return false;
} else {
return true;
}
}
function display_smart_button_on_product_page() {
if ($('.angelleye_button_single').length > 0) {
$('.angelleye_button_single').empty();
window.paypalCheckoutReady = function () {
allowed_funding_methods_single_array = $.parseJSON(angelleye_in_content_param.allowed_funding_methods);
disallowed_funding_methods_single_array = $.parseJSON(angelleye_in_content_param.disallowed_funding_methods);
if (angelleye_in_content_param.is_paypal_credit_enable == "no") {
disallowed_funding_methods_single_array.push("credit");
}
var get_attributes = function () {
var select = $('.variations_form').find('.variations select'),
data = {},
count = 0,
chosen = 0;
select.each(function () {
var attribute_name = $(this).data('attribute_name') || $(this).attr('name');
var value = $(this).val() || '';
if (value.length > 0) {
chosen++;
}
count++;
data[ attribute_name ] = value;
});
return {
'count': count,
'chosenCount': chosen,
'data': data
};
};
angelleye_cart_style_object = {size: angelleye_in_content_param.button_size,
color: angelleye_in_content_param.button_color,
shape: angelleye_in_content_param.button_shape,
label: angelleye_in_content_param.button_label,
layout: angelleye_in_content_param.button_layout,
tagline: (angelleye_in_content_param.button_tagline === "true") ? true : false
};
if (angelleye_in_content_param.button_layout === 'horizontal' && is_funding_icon_should_show_in_content() === true && angelleye_in_content_param.button_label !== 'credit' && angelleye_in_content_param.button_fundingicons === "true") {
angelleye_cart_style_object['fundingicons'] = (angelleye_in_content_param.button_fundingicons === "true") ? true : false;
}
$('.angelleye_button_single').empty();
paypal.Button.render({
env: angelleye_in_content_param.environment,
style: angelleye_cart_style_object,
locale: angelleye_in_content_param.locale,
commit: (angelleye_in_content_param.zcommit === "false") ? false : true,
funding: {
allowed: allowed_funding_methods_single_array,
disallowed: disallowed_funding_methods_single_array
},
payment: function (data, actions) {
var data_param = {
'nonce': angelleye_in_content_param.generate_cart_nonce,
'qty': $('.quantity .qty').val(),
'attributes': $('.variations_form').length ? JSON.stringify(get_attributes().data) : [],
'is_cc': '',
'product_id': $("input[name=add-to-cart]").val(),
'variation_id': $("input[name=variation_id]").val(),
'request_from': 'JSv4',
'express_checkout': 'true'
};
var angelleye_action;
angelleye_action = angelleye_in_content_param.add_to_cart_ajaxurl;
if ($("#wc-paypal_express-new-payment-method").is(':checked')) {
angelleye_action = angelleye_action + '&ec_save_to_account=true';
}
return paypal.request.post(angelleye_action, data_param).then(function (data) {
var params = {
request_from: 'JSv4'
};
return paypal.request.post(data.url, params).then(function (res) {
return res.token;
});
});
},
onAuthorize: function (data, actions) {
var params = {
paymentToken: data.paymentToken,
payerID: data.payerID,
token: data.paymentToken,
request_from: 'JSv4'
};
paypal.request.post(data.returnUrl, params).then(function (res) {
data.returnUrl = res.url;
actions.redirect();
});
},
onCancel: function (data, actions) {
window.location.href = angelleye_in_content_param.cancel_page;
},
onClick: function () {
if (angelleye_in_content_param.enable_google_analytics_click === 'yes') {
if (typeof ga != 'undefined' && $.isFunction(ga)) {
ga('send', {
hitType: 'event',
eventCategory: 'Checkout',
eventAction: 'button_click'
});
}
}
},
onError: function (err, actions) {
console.log("err");
if ($('.angelleye_button_single').length) {
window.location.href = angelleye_in_content_param.cancel_page;
}
}
}, '.angelleye_button_single');
};
}
}
function display_smart_button_on_cart_checkout() {
window.paypalCheckoutReady = function () {
var angelleye_button_selector = [];
var allowed_funding_methods_var = [];
var disallowed_funding_methods_var = [];
if (angelleye_in_content_param.is_checkout == 'yes' && angelleye_in_content_param.is_display_on_checkout == 'yes') {
angelleye_button_selector.push(".angelleye_smart_button_checkout_top");
}
if (angelleye_in_content_param.is_cart == 'yes') {
if (angelleye_in_content_param.cart_button_possition == 'both') {
angelleye_button_selector.push(".angelleye_smart_button_top", ".angelleye_smart_button_bottom");
} else if (angelleye_in_content_param.cart_button_possition == 'bottom') {
angelleye_button_selector.push(".angelleye_smart_button_bottom");
} else if (angelleye_in_content_param.cart_button_possition == 'top') {
angelleye_button_selector.push(".angelleye_smart_button_top");
}
}
disallowed_funding_methods_var = $.parseJSON(angelleye_in_content_param.disallowed_funding_methods);
allowed_funding_methods_var = $.parseJSON(angelleye_in_content_param.allowed_funding_methods);
if (angelleye_in_content_param.is_paypal_credit_enable == "no") {
disallowed_funding_methods_var.push("credit");
}
angelleye_cart_style_object = {size: angelleye_in_content_param.button_size,
color: angelleye_in_content_param.button_color,
shape: angelleye_in_content_param.button_shape,
label: angelleye_in_content_param.button_label,
layout: angelleye_in_content_param.button_layout,
tagline: (angelleye_in_content_param.button_tagline === "true") ? true : false
};
angelleye_button_selector.forEach(function (selector) {
$(selector).html("");
disallowed_funding_methods_var = $.grep(disallowed_funding_methods_var, function (value) {
return value !== 'venmo';
});
if (selector.length > 0 && $(selector).length > 0) {
if (angelleye_in_content_param.button_layout === 'horizontal' && is_funding_icon_should_show_in_content() === true && angelleye_in_content_param.button_label !== 'credit') {
if (angelleye_in_content_param.button_fundingicons === 'true') {
angelleye_cart_style_object['fundingicons'] = (angelleye_in_content_param.button_fundingicons === "true") ? true : false;
}
}
paypal.Button.render({
env: angelleye_in_content_param.environment,
style: angelleye_cart_style_object,
locale: angelleye_in_content_param.locale,
commit: (angelleye_in_content_param.zcommit === "false") ? false : true,
funding: {
allowed: allowed_funding_methods_var,
disallowed: disallowed_funding_methods_var
},
payment: function () {
var data_param = {
'request_from': 'JSv4'
};
var angelleye_action;
angelleye_action = angelleye_in_content_param.set_express_checkout;
if ($("#wc-paypal_express-new-payment-method").is(':checked')) {
angelleye_action = angelleye_action + '&ec_save_to_account=true';
} else if ($("#wc-paypal_express-new-payment-method_bottom").is(':checked')) {
angelleye_action = angelleye_action + '&ec_save_to_account=true';
}
return paypal.request.post(angelleye_action, data_param).then(function (data) {
return data.token;
});
},
onAuthorize: function (data, actions) {
var params = {
paymentToken: data.paymentToken,
payerID: data.payerID,
token: data.paymentToken,
request_from: 'JSv4'
};
paypal.request.post(data.returnUrl, params).then(function (res) {
data.returnUrl = res.url;
actions.redirect();
});
},
onCancel: function (data, actions) {
window.location.href = angelleye_in_content_param.cancel_page;
},
onClick: function () {
if (angelleye_in_content_param.enable_google_analytics_click === 'yes') {
if (typeof ga != 'undefined' && $.isFunction(ga)) {
ga('send', {
hitType: 'event',
eventCategory: 'Checkout',
eventAction: 'paypal_button_click'
});
}
}
},
onError: function (err, actions) {
window.location.href = angelleye_in_content_param.cancel_page;
}
}, selector);
}
if(selector === "angelleye_smart_button_checkout_top") {
return false;
}
});
};
}
function display_smart_button_on_min_cart() {
window.paypalCheckoutReady = function () {
var angelleye_button_selector = [];
var allowed_funding_methods_var = [];
var disallowed_funding_methods_var = [];
angelleye_button_selector.push(".angelleye_smart_button_mini");
disallowed_funding_methods_var = $.parseJSON(angelleye_in_content_param.mini_cart_disallowed_funding_methods);
allowed_funding_methods_var = $.parseJSON(angelleye_in_content_param.mini_cart_allowed_funding_methods);
if (angelleye_in_content_param.is_paypal_credit_enable == "no") {
disallowed_funding_methods_var.push("credit");
}
angelleye_cart_style_object = {size: angelleye_in_content_param.mini_cart_button_size,
color: angelleye_in_content_param.button_color,
shape: angelleye_in_content_param.button_shape,
label: angelleye_in_content_param.button_label,
layout: angelleye_in_content_param.mini_cart_button_layout,
tagline: (angelleye_in_content_param.button_tagline === "true") ? true : false
};
angelleye_button_selector.forEach(function (selector) {
$(selector).html("");
disallowed_funding_methods_var = $.grep(disallowed_funding_methods_var, function (value) {
return value !== 'venmo';
});
if (selector.length > 0 && $(selector).length > 0) {
angelleye_cart_style_object['size'] = 'responsive';
if (angelleye_in_content_param.button_layout === 'horizontal' && is_funding_icon_should_show_in_content() === true && angelleye_in_content_param.button_label !== 'credit') {
if (angelleye_in_content_param.button_fundingicons === 'true') {
angelleye_cart_style_object['fundingicons'] = (angelleye_in_content_param.button_fundingicons === "true") ? true : false;
}
}
paypal.Button.render({
env: angelleye_in_content_param.environment,
style: angelleye_cart_style_object,
locale: angelleye_in_content_param.locale,
commit: (angelleye_in_content_param.zcommit === "false") ? false : true,
funding: {
allowed: allowed_funding_methods_var,
disallowed: disallowed_funding_methods_var
},
payment: function () {
var data_param = {
request_from: 'JSv4'
};
var angelleye_action;
angelleye_action = angelleye_in_content_param.set_express_checkout;
if ($("#wc-paypal_express-new-payment-method").is(':checked')) {
angelleye_action = angelleye_action + '&ec_save_to_account=true';
}
return paypal.request.post(angelleye_action, data_param).then(function (data) {
return data.token;
});
},
onAuthorize: function (data, actions) {
var params = {
paymentToken: data.paymentToken,
payerID: data.payerID,
token: data.paymentToken,
request_from: 'JSv4'
};
paypal.request.post(data.returnUrl, params).then(function (res) {
data.returnUrl = res.url;
actions.redirect();
});
},
onCancel: function (data, actions) {
window.location.href = angelleye_in_content_param.cancel_page;
},
onClick: function () {
if (angelleye_in_content_param.enable_google_analytics_click === 'yes') {
if (typeof ga != 'undefined' && $.isFunction(ga)) {
ga('send', {
hitType: 'event',
eventCategory: 'Checkout',
eventAction: 'paypal_button_click'
});
}
}
},
onError: function (err, actions) {
window.location.href = angelleye_in_content_param.cancel_page;
}
}, selector);
}
});
};
}
function display_smart_button_on_wsc_cart() {
window.paypalCheckoutReady = function () {
var angelleye_button_selector = [];
var allowed_funding_methods_var = [];
var disallowed_funding_methods_var = [];
angelleye_button_selector.push(".angelleye_smart_button_wsc");
disallowed_funding_methods_var = $.parseJSON(angelleye_in_content_param.wsc_cart_disallowed_funding_methods);
allowed_funding_methods_var = $.parseJSON(angelleye_in_content_param.wsc_cart_allowed_funding_methods);
if (angelleye_in_content_param.is_paypal_credit_enable == "no") {
disallowed_funding_methods_var.push("credit");
}
angelleye_cart_style_object = {size: angelleye_in_content_param.wsc_cart_button_size,
color: angelleye_in_content_param.button_color,
shape: angelleye_in_content_param.button_shape,
label: angelleye_in_content_param.button_label,
layout: angelleye_in_content_param.wsc_cart_button_layout,
tagline: (angelleye_in_content_param.button_tagline === "true") ? true : false
};
angelleye_button_selector.forEach(function (selector) {
$(selector).html("");
disallowed_funding_methods_var = $.grep(disallowed_funding_methods_var, function (value) {
return value !== 'venmo';
});
if (selector.length > 0 && $(selector).length > 0) {
angelleye_cart_style_object['size'] = 'responsive';
if (angelleye_in_content_param.button_layout === 'horizontal' && is_funding_icon_should_show_in_content() === true && angelleye_in_content_param.button_label !== 'credit') {
if (angelleye_in_content_param.button_fundingicons === 'true') {
angelleye_cart_style_object['fundingicons'] = (angelleye_in_content_param.button_fundingicons === "true") ? true : false;
}
}
paypal.Button.render({
env: angelleye_in_content_param.environment,
style: angelleye_cart_style_object,
locale: angelleye_in_content_param.locale,
commit: (angelleye_in_content_param.zcommit === "false") ? false : true,
funding: {
allowed: allowed_funding_methods_var,
disallowed: disallowed_funding_methods_var
},
payment: function () {
var data_param = {
request_from: 'JSv4'
};
var angelleye_action;
angelleye_action = angelleye_in_content_param.set_express_checkout;
if ($("#wc-paypal_express-new-payment-method").is(':checked')) {
angelleye_action = angelleye_action + '&ec_save_to_account=true';
}
return paypal.request.post(angelleye_action, data_param).then(function (data) {
return data.token;
});
},
onAuthorize: function (data, actions) {
var params = {
paymentToken: data.paymentToken,
payerID: data.payerID,
token: data.paymentToken,
request_from: 'JSv4'
};
paypal.request.post(data.returnUrl, params).then(function (res) {
data.returnUrl = res.url;
actions.redirect();
});
},
onCancel: function (data, actions) {
window.location.href = angelleye_in_content_param.cancel_page;
},
onClick: function () {
if (angelleye_in_content_param.enable_google_analytics_click === 'yes') {
if (typeof ga != 'undefined' && $.isFunction(ga)) {
ga('send', {
hitType: 'event',
eventCategory: 'Checkout',
eventAction: 'paypal_button_click'
});
}
}
},
onError: function (err, actions) {
window.location.href = angelleye_in_content_param.cancel_page;
}
}, selector);
}
});
};
}
function display_smart_button_checkout_bottom() {
window.paypalCheckoutReady = function () {
var angelleye_button_selector = [];
var allowed_funding_methods_var = [];
var disallowed_funding_methods_var = [];
angelleye_button_selector.push(".angelleye_smart_button_checkout_bottom");
disallowed_funding_methods_var = $.parseJSON(angelleye_in_content_param.disallowed_funding_methods);
allowed_funding_methods_var = $.parseJSON(angelleye_in_content_param.allowed_funding_methods);
if (angelleye_in_content_param.is_paypal_credit_enable == "no") {
disallowed_funding_methods_var.push("credit");
}
angelleye_cart_style_object = {size: angelleye_in_content_param.button_size,
color: angelleye_in_content_param.button_color,
shape: angelleye_in_content_param.button_shape,
label: angelleye_in_content_param.button_label,
layout: angelleye_in_content_param.button_layout,
tagline: (angelleye_in_content_param.button_tagline === "true") ? true : false
};
angelleye_button_selector.forEach(function (selector) {
$(selector).html("");
disallowed_funding_methods_var = $.grep(disallowed_funding_methods_var, function (value) {
return value !== 'venmo';
});
if (selector.length > 0 && $(selector).length > 0) {
if (angelleye_in_content_param.button_layout === 'horizontal' && is_funding_icon_should_show_in_content() === true && angelleye_in_content_param.button_label !== 'credit') {
if (angelleye_in_content_param.button_fundingicons === 'true') {
angelleye_cart_style_object['fundingicons'] = (angelleye_in_content_param.button_fundingicons === "true") ? true : false;
}
}
paypal.Button.render({
env: angelleye_in_content_param.environment,
style: angelleye_cart_style_object,
locale: angelleye_in_content_param.locale,
commit: (angelleye_in_content_param.zcommit === "false") ? false : true,
funding: {
allowed: allowed_funding_methods_var,
disallowed: disallowed_funding_methods_var
},
payment: function () {
$('.woocommerce').block({message: null, overlayCSS: {background: '#fff', opacity: 0.6}});
var data = $( selector ).closest( 'form' )
.add( $( '<input type="hidden" name="request_from" /> ' )
.attr( 'value', 'JSv4' )
)
.add( $( '<input type="hidden" name="from_checkout" /> ' )
.attr( 'value', 'yes' )
)
.serialize();
return paypal.request( {
method: 'post',
url: angelleye_in_content_param.set_express_checkout,
body: data,
} ).then( function( response ) {
return response.token;
} );
},
onAuthorize: function (data, actions) {
var params = {
paymentToken: data.paymentToken,
payerID: data.payerID,
token: data.paymentToken,
request_from: 'JSv4'
};
paypal.request.post(data.returnUrl, params).then(function (res) {
data.returnUrl = res.url;
actions.redirect();
});
},
onCancel: function (data, actions) {
$('.woocommerce').unblock();
window.location.href = angelleye_in_content_param.cancel_page;
},
onClick: function () {
if (angelleye_in_content_param.enable_google_analytics_click === 'yes') {
if (typeof ga != 'undefined' && $.isFunction(ga)) {
ga('send', {
hitType: 'event',
eventCategory: 'Checkout',
eventAction: 'paypal_button_click'
});
}
}
},
onError: function (err, actions) {
$('.woocommerce').unblock();
window.location.href = angelleye_in_content_param.cancel_page;
}
}, selector);
}
if(selector === "angelleye_smart_button_checkout_bottom") {
return false;
}
});
};
}
$(document.body).on('cart_totals_refreshed updated_shipping_method wc_fragments_refreshed updated_checkout updated_wc_div updated_cart_totals wc_fragments_loaded', function (event) {
display_smart_button_on_cart_checkout();
});
if(angelleye_in_content_param.checkout_page_disable_smart_button === "no") {
$(document.body).on('updated_shipping_method wc_fragments_refreshed updated_checkout', function (event) {
display_smart_button_checkout_bottom();
});
}
$( document.body ).on( 'wc_fragments_loaded wc_fragments_refreshed', function() {
var $button = $( '.angelleye_smart_button_mini' );
if ( $button.length ) {
$button.empty();
display_smart_button_on_min_cart();
}
var $single = $('.angelleye_button_single');
if ( $single.length ) {
$single.empty();
display_smart_button_on_product_page();
}
var $button_wsc = $( '.angelleye_smart_button_wsc' );
if ( $button_wsc.length ) {
$button_wsc.empty();
display_smart_button_on_wsc_cart();
}
} );
if(angelleye_in_content_param.checkout_page_disable_smart_button === "no") {
$(document.body).on('updated_checkout wc-credit-card-form-init update_checkout', function (event) {
angelleye_manage_smart_button();
});
function angelleye_manage_smart_button() {
var is_checked = $('#payment_method_paypal_express').is(':checked');
if($('input[name="wc-paypal_express-payment-token"]:checked').length > 0) {
if (is_checked && $('input[name="wc-paypal_express-payment-token"]').length && $('input[name="wc-paypal_express-payment-token"]:checked').val() === 'new') {
$( '#place_order' ).hide();
$( '.angelleye_smart_button_checkout_bottom' ).show();
} else if(is_checked && $('input[name="wc-paypal_express-payment-token"]').length && $('input[name="wc-paypal_express-payment-token"]:checked').val() !== 'new') {
$( '#place_order' ).show();
$( '.angelleye_smart_button_checkout_bottom' ).hide();
} else if(is_checked) {
$( '.angelleye_smart_button_checkout_bottom' ).show();
$( '#place_order' ).hide();
} else {
$( '.angelleye_smart_button_checkout_bottom' ).hide();
$( '#place_order' ).show();
}
} else {
if(is_checked) {
$( '.angelleye_smart_button_checkout_bottom' ).show();
$( '#place_order' ).hide();
} else {
$( '.angelleye_smart_button_checkout_bottom' ).hide();
$( '#place_order' ).show();
}
}
}
$( 'form.checkout' ).on( 'click', 'input[name="payment_method"]', function() {
angelleye_manage_smart_button();
} );
$( 'form.checkout' ).on( 'click', 'input[name="wc-paypal_express-payment-token"]', function() {
if ($(this).val() === 'new') {
$( '#place_order' ).hide();
$( '.angelleye_smart_button_checkout_bottom' ).show();
} else if($(this).val() !== 'new') {
$( '#place_order' ).show();
$( '.angelleye_smart_button_checkout_bottom' ).hide();
}
});
}
});