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