PluginProbe
PayPlug for WooCommerce (Official) / 3.1.0
PayPlug for WooCommerce (Official) v3.1.0
3.1.0 3.0.0 2.18.0 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 1.0.22 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.1.0 1.10.0 1.10.1 1.2.1 1.2.10 1.2.11 1.2.2 1.2.3 1.2.4 1.2.5 All 102 releases
← All changes | assets/js/payplug-oney.js +120 -126 1.2.4 → 3.1.0 View file →
@@ -1,138 +1,132 @@
1 1 (function ($) {
2 - var is_cart = false
3 - var popupLoaded = false
4 - var initevent = function () {
5 - var showpopup = $("#oney-show-popup")
6 - var oneyData = $("#oney-show-popup").closest('.payplug-oney')
7 - var showpopuponey = $("#oney-show-popup").closest('.payplug-oney-popup')
8 - var arrow = $("#oney-popup-arrow")
9 - var qtyInput = $('input[name=quantity]')
10 - var popup = $("#oney-popup")
11 - var loading = popup.find('.payplug-lds-roller')
12 - var oneyError = popup.find('#oney-popup-error')
13 - var totalsProduct = oneyData.data('total-products')
14 - var maxOneyQty = oneyData.data('max-oney-qty')
15 - is_cart = oneyData.data('is-cart')
16 - var showpopupF = function (show) {
17 - if(!oneyData.hasClass('disabled') && !popupLoaded && !show) {
18 - return
2 + function calculTotals(container) {
3 + var price = parseFloat(container.data('price'));
4 + var qtyInput = $('input[name=quantity]');
5 +
6 + if (qtyInput.length) {
7 + return parseFloat(qtyInput.val()) * price;
8 + }
9 +
10 + return price;
11 + }
12 +
13 + function refreshEligibility(container) {
14 + if (container.data('min-oney') === undefined || container.data('max-oney') === undefined) {
15 + // No thresholds means the badge is disabled for a reason quantity can't change
16 + // (e.g. country not allowed) - never re-enable it client-side.
17 + return;
18 + }
19 +
20 + var total = calculTotals(container);
21 + var min = parseFloat(container.data('min-oney'));
22 + var max = parseFloat(container.data('max-oney'));
23 +
24 + if (total >= min && total <= max) {
25 + container.removeClass('disabled');
26 + } else {
27 + container.addClass('disabled');
28 + }
29 + }
30 +
31 + function showSimulationPopin(container) {
32 + if (container.hasClass('disabled') || typeof loadOneyWidget !== 'function') {
33 + return;
34 + }
35 +
36 + var options = $.extend({}, window.payplug_oney_config, {
37 + payment_amount: calculTotals(container),
38 + filter_by: 'business_transaction_codes',
39 + errorCallback: function (status, response) {
40 + console.warn('Oney widget unavailable', status, response);
41 + },
42 + });
43 +
44 + loadOneyWidget(function () {
45 + if (typeof oneyMerchantApp === 'undefined') {
46 + return;
19 47 }
20 - popup.show(0, function () {
21 - if (!$.browser.mobile) {
22 - checkOneyError()
23 - var top = oneyData.hasClass('disabled') ? 50 : 110
24 - popup.css('position', 'fixed')
25 - popup.position({
26 - my: popupLoaded ? "left top-" + top : "left top-75",
27 - at: popupLoaded ? "right+40 bottom" : "right bottom",
28 - of: showpopup,
29 - })
30 - if(popupLoaded) {
31 - showarrow()
32 - }
33 - }
34 - })
48 + oneyMerchantApp.loadSimulationPopin({options: options});
49 + });
50 + }
51 +
52 + var checkoutSectionsLoaded = {};
53 +
54 + function loadCheckoutSection(gatewayId) {
55 + // The memo must reflect the placeholder currently in the DOM: WooCommerce replaces the
56 + // whole payment fragment on updated_checkout, so a previously mounted widget is gone.
57 + var placeholder = document.getElementById('oney-checkout-' + gatewayId);
58 + if (!placeholder) {
59 + delete checkoutSectionsLoaded[gatewayId];
60 +
61 + return;
35 62 }
36 - showarrow = function () {
37 - arrow.show()
38 - arrow.position({
39 - my: "left top-43",
40 - at: "right+10 bottom",
41 - of: showpopup,
42 - })
63 +
64 + if (checkoutSectionsLoaded[gatewayId] === placeholder || typeof loadOneyWidget !== 'function') {
65 + return;
43 66 }
44 - calculTotals = function () {
45 - var price = oneyData.data('price')
46 - return qtyInput.length ? totalsProduct * price : price
67 +
68 + var configEl = document.getElementById('oney-checkout-config-' + gatewayId);
69 + if (!configEl) {
70 + return;
47 71 }
48 - isInOneyRange = function () {
49 - var totalPrice = calculTotals();
50 - var minOney = oneyData.data('min-oney');
51 - var maxOney = oneyData.data('max-oney');
52 - return (totalPrice >= minOney && totalPrice <= maxOney);
53 - };
54 - checkOneyError = function () {
55 72
56 - if(oneyData.hasClass('disabled')) {
57 - popupLoaded = true
58 - popup.html('').append($(oneyError)).append($(arrow))
59 - popup.addClass('loaded')
60 - popup.find('.payplug-lds-roller').hide()
61 - popup.find('#oney-popup-error .oney-error').hide()
62 - totalsProduct >= maxOneyQty ?
63 - popup.find('#oney-popup-error .oney-error.qty').show() :
64 - popup.find('#oney-popup-error .oney-error.range').show()
65 - } else {
66 - arrow.hide()
73 + var loading = document.getElementById('oney-checkout-loading-' + gatewayId);
74 + var hideLoading = function () {
75 + if (loading) {
76 + loading.style.display = 'none';
67 77 }
68 78 };
69 - qtyInput.unbind()
70 - qtyInput.on('change', function () {
71 - totalsProduct = $(this).val()
72 - popupLoaded = false
73 - if (isInOneyRange() && totalsProduct < maxOneyQty) {
74 - oneyData.removeClass('disabled')
75 - popup.removeClass('disabled').removeClass('loaded')
76 - popup.html('').append($(arrow))
77 - arrow.hide()
78 - } else {
79 - checkOneyError()
80 - oneyData.addClass('disabled')
81 - popup.addClass('disabled')
79 +
80 + var config = JSON.parse(configEl.textContent);
81 + var options = $.extend({}, config, {
82 + filter_by: 'business_transaction_code',
83 + checkout_placeholder: '#oney-checkout-' + gatewayId,
84 + successCallback: hideLoading,
85 + errorCallback: function (status, response) {
86 + hideLoading();
87 + console.warn('Oney checkout widget unavailable', status, response);
88 + },
89 + });
90 +
91 + loadOneyWidget(function () {
92 + if (typeof oneyMerchantApp === 'undefined') {
93 + hideLoading();
94 +
95 + return;
82 96 }
83 - })
84 - showpopuponey.unbind();
85 - showpopuponey.on('click', function () {
86 - showpopupF(true);
87 - if (isInOneyRange() && totalsProduct < maxOneyQty) {
88 - if (popupLoaded) {
89 - return
90 - }
91 - popup.html('').append($(loading))
92 - $.post(
93 - payplug_config.ajax_url,
94 - {
95 - 'action': payplug_config.ajax_action,
96 - 'price': calculTotals()
97 - }, function (response) {
98 - if (response.data.popup) {
99 - popupLoaded = true;
100 - popup.addClass('loaded');
101 - popup.html(response.data.popup);
102 - showpopupF();
103 -
104 - var closepopup = $('#oney-popup-close');
105 - closepopup.on('click', function () {
106 - popup.hide();
107 - });
108 -
109 - $(document).mouseup(function(e) {
110 - // if the target of the click isn't the container nor a descendant of the container
111 - if (!popup.is(e.target) && popup.has(e.target).length === 0) {
112 - popup.hide();
113 - }
114 - });
115 - }
116 - }
117 - );
118 - } else {
119 - checkOneyError()
120 - }
97 + oneyMerchantApp.loadCheckoutSection({options: options});
98 + checkoutSectionsLoaded[gatewayId] = placeholder;
121 99 });
122 - };
123 -
124 - $(document).on('ready', function () {
125 - initevent();
126 - if (is_cart) {
127 - $(document).ajaxSuccess(function (event, request, settings) {
128 - if (settings.data.includes(payplug_config.ajax_action)) {
129 - return;
130 - }
131 - initevent();
132 - popupLoaded = false;
133 - });
100 + }
101 +
102 + function maybeLoadSelectedCheckoutSection() {
103 + var selected = $('input[name="payment_method"]:checked').val();
104 + if (selected && selected.indexOf('oney_') === 0) {
105 + loadCheckoutSection(selected);
134 106 }
107 + }
108 +
109 + // Bound to the whole popup wrapper (logo + "?"), not just #oney-show-popup itself: the
110 + // pre-migration implementation made the entire wrapper clickable (see the old
111 + // showpopuponey = $('#oney-show-popup').closest('.payplug-oney-popup') pattern), and users
112 + // click the Oney logo as often as the small "?" icon next to it.
113 + $(document).on('click', '.payplug-oney-popup', function () {
114 + showSimulationPopin($(this).closest('.payplug-oney'));
135 115 });
136 -
137 -
138 -})(jQuery);
116 +
117 + $(document).on('change', 'input[name=quantity]', function () {
118 + $('.payplug-oney').each(function () {
119 + refreshEligibility($(this));
120 + });
121 + });
122 +
123 + $(function () {
124 + $('.payplug-oney').each(function () {
125 + refreshEligibility($(this));
126 + });
127 + });
128 +
129 + $(document).on('change', 'input[name="payment_method"]', maybeLoadSelectedCheckoutSection);
130 + $(document.body).on('updated_checkout', maybeLoadSelectedCheckoutSection);
131 + $(maybeLoadSelectedCheckoutSection);
132 +})(jQuery);