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 +106 -133 1.10.0 → 3.1.0 View file →
@@ -1,159 +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 qtyInput = $('input[name=quantity]')
9 - var popup = $("#oney-popup")
10 - var loading = popup.find('.payplug-lds-roller')
11 - var oneyError = popup.find('#oney-popup-error')
12 - var totalsProduct = oneyData.data('total-products')
13 - var maxOneyQty = oneyData.data('max-oney-qty')
14 - is_cart = oneyData.data('is-cart')
2 + function calculTotals(container) {
3 + var price = parseFloat(container.data('price'));
4 + var qtyInput = $('input[name=quantity]');
15 5
16 - function _showPopup(show) {
17 - if (!oneyData.hasClass('disabled') && !popupLoaded && !show) {
18 - return
19 - }
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 - }
31 - })
6 + if (qtyInput.length) {
7 + return parseFloat(qtyInput.val()) * price;
32 8 }
33 9
34 - function _hidePopup() {
35 - popup.hide();
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;
36 18 }
37 19
38 - function _calculTotals() {
39 - var price = oneyData.data('price')
40 - return qtyInput.length ? totalsProduct * price : price
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');
41 28 }
29 + }
42 30
43 - function _isInOneyRange() {
44 - var totalPrice = _calculTotals();
45 - var minOney = oneyData.data('min-oney');
46 - var maxOney = oneyData.data('max-oney');
47 - return (totalPrice >= minOney && totalPrice <= maxOney);
48 - };
31 + function showSimulationPopin(container) {
32 + if (container.hasClass('disabled') || typeof loadOneyWidget !== 'function') {
33 + return;
34 + }
49 35
50 - function _checkOneyError() {
51 - if (oneyData.hasClass('disabled')) {
52 - popupLoaded = true
53 - popup.html('').append($(oneyError))
54 - popup.addClass('loaded')
55 - popup.find('.payplug-lds-roller').hide()
56 - popup.find('#oney-popup-error .oney-error').hide()
57 - totalsProduct >= maxOneyQty ?
58 - popup.find('#oney-popup-error .oney-error.qty').show() :
59 - popup.find('#oney-popup-error .oney-error.range').show()
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;
60 47 }
61 - };
48 + oneyMerchantApp.loadSimulationPopin({options: options});
49 + });
50 + }
62 51
63 - function _bindCloseOneyPopup() {
64 - $(document).unbind('mouseup')
65 - $(document).mouseup(function (e) {
66 - // if the target of the click isn't the container nor a descendant of the container
67 - if (!popup.is(e.target) && popup.has(e.target).length === 0) {
68 - _hidePopup();
69 - }
70 - });
52 + var checkoutSectionsLoaded = {};
71 53
72 - $('#oney-popup-close').unbind();
73 - $('#oney-popup-close').on('click', function () {
74 - _hidePopup();
75 - });
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;
76 62 }
77 63
78 - _bindCloseOneyPopup();
79 - qtyInput.unbind();
80 - qtyInput.on('change', function () {
81 - totalsProduct = $(this).val()
82 - popupLoaded = false
83 - if (_isInOneyRange() && totalsProduct < maxOneyQty) {
84 - oneyData.removeClass('disabled')
85 - popup.removeClass('disabled').removeClass('loaded')
86 - popup.html('')
87 - } else {
88 - _checkOneyError()
89 - oneyData.addClass('disabled')
90 - popup.addClass('disabled')
64 + if (checkoutSectionsLoaded[gatewayId] === placeholder || typeof loadOneyWidget !== 'function') {
65 + return;
66 + }
67 +
68 + var configEl = document.getElementById('oney-checkout-config-' + gatewayId);
69 + if (!configEl) {
70 + return;
71 + }
72 +
73 + var loading = document.getElementById('oney-checkout-loading-' + gatewayId);
74 + var hideLoading = function () {
75 + if (loading) {
76 + loading.style.display = 'none';
91 77 }
78 + };
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 + },
92 89 });
93 - showpopuponey.unbind();
94 - showpopuponey.on('click', function () {
95 - _showPopup(true);
96 - if (_isInOneyRange() && totalsProduct < maxOneyQty) {
97 - if (popupLoaded) {
98 - return
99 - }
100 - popup.html('').append($(loading))
101 - $.post(
102 - payplug_config.ajax_url,
103 - {
104 - 'action': payplug_config.ajax_action,
105 - 'price': _calculTotals()
106 - }, function (response) {
107 - if (response.data.popup) {
108 - popupLoaded = true;
109 - popup.addClass('loaded');
110 - popup.html(response.data.popup);
111 - _showPopup();
112 - _bindCloseOneyPopup()
113 - }
114 - }
115 - );
116 - } else {
117 - _checkOneyError()
118 - }
119 - });
120 90
121 - $(window).on('scroll', function () {
122 - if(popup.is(':visible')) {
123 - _showPopup()
91 + loadOneyWidget(function () {
92 + if (typeof oneyMerchantApp === 'undefined') {
93 + hideLoading();
94 +
95 + return;
124 96 }
97 + oneyMerchantApp.loadCheckoutSection({options: options});
98 + checkoutSectionsLoaded[gatewayId] = placeholder;
125 99 });
100 + }
126 101
127 - $(document).ready(function () {
128 - if (!(is_cart)) {
129 - totalsProduct = qtyInput.val()
130 - popupLoaded = false
131 - if (_isInOneyRange() && totalsProduct < maxOneyQty) {
132 - oneyData.removeClass('disabled')
133 - popup.removeClass('disabled').removeClass('loaded')
134 - popup.html('')
135 - } else {
136 - _checkOneyError()
137 - oneyData.addClass('disabled')
138 - popup.addClass('disabled')
139 - }
140 - }
141 - });
142 - };
102 + function maybeLoadSelectedCheckoutSection() {
103 + var selected = $('input[name="payment_method"]:checked').val();
104 + if (selected && selected.indexOf('oney_') === 0) {
105 + loadCheckoutSection(selected);
106 + }
107 + }
143 108
144 - $(function() {
145 - _initevent();
146 - if (is_cart) {
147 - $(document).ajaxSuccess(function (event, request, settings) {
148 - if (settings.data.includes(payplug_config.ajax_action)) {
149 - return;
150 - }
151 - _initevent();
152 - popupLoaded = false;
153 - });
154 - }
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'));
115 + });
155 116
117 + $(document).on('change', 'input[name=quantity]', function () {
118 + $('.payplug-oney').each(function () {
119 + refreshEligibility($(this));
120 + });
156 121 });
157 122
123 + $(function () {
124 + $('.payplug-oney').each(function () {
125 + refreshEligibility($(this));
126 + });
127 + });
158 128
129 + $(document).on('change', 'input[name="payment_method"]', maybeLoadSelectedCheckoutSection);
130 + $(document.body).on('updated_checkout', maybeLoadSelectedCheckoutSection);
131 + $(maybeLoadSelectedCheckoutSection);
159 132 })(jQuery);