| @@ -1,135 +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 | |
| 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 | - if(popupLoaded) { | |
| 31 | - showarrow() | |
| 32 | - } | |
| 33 | - } | |
| 34 | - }) | |
| 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; | |
| 35 | 8 | } |
| 36 | - showarrow = function () { | |
| 37 | - arrow.show() | |
| 38 | - arrow.position({ | |
| 39 | - my: "left top-43", | |
| 40 | - at: "right+10 bottom", | |
| 41 | - of: showpopup, | |
| 42 | - }) | |
| 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; | |
| 43 | 18 | } |
| 44 | - calculTotals = function () { | |
| 45 | - var price = oneyData.data('price') | |
| 46 | - return qtyInput.length ? totalsProduct * price : price | |
| 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'); | |
| 47 | 28 | } |
| 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) | |
| 29 | + } | |
| 30 | + | |
| 31 | + function showSimulationPopin(container) { | |
| 32 | + if (container.hasClass('disabled') || typeof loadOneyWidget !== 'function') { | |
| 33 | + return; | |
| 53 | 34 | } |
| 54 | - checkOneyError = function () { | |
| 55 | - if(oneyData.hasClass('disabled')) { | |
| 56 | - popupLoaded = true | |
| 57 | - popup.html('').append($(oneyError)).append($(arrow)) | |
| 58 | - popup.addClass('loaded') | |
| 59 | - popup.find('.payplug-lds-roller').hide() | |
| 60 | - popup.find('#oney-popup-error .oney-error').hide() | |
| 61 | - totalsProduct >= maxOneyQty ? | |
| 62 | - popup.find('#oney-popup-error .oney-error.qty').show() : | |
| 63 | - popup.find('#oney-popup-error .oney-error.range').show() | |
| 64 | - } else { | |
| 65 | - arrow.hide() | |
| 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; | |
| 66 | 47 | } |
| 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; | |
| 67 | 62 | } |
| 68 | - qtyInput.unbind() | |
| 69 | - qtyInput.on('change', function () { | |
| 70 | - totalsProduct = $(this).val() | |
| 71 | - popupLoaded = false | |
| 72 | - if (isInOneyRange() && totalsProduct < maxOneyQty) { | |
| 73 | - oneyData.removeClass('disabled') | |
| 74 | - popup.removeClass('disabled').removeClass('loaded') | |
| 75 | - popup.html('').append($(arrow)) | |
| 76 | - arrow.hide() | |
| 77 | - } else { | |
| 78 | - checkOneyError() | |
| 79 | - oneyData.addClass('disabled') | |
| 80 | - popup.addClass('disabled') | |
| 63 | + | |
| 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'; | |
| 81 | 77 | } |
| 82 | - }) | |
| 83 | - showpopuponey.unbind() | |
| 84 | - showpopuponey.on('click', function () { | |
| 85 | - showpopupF(true) | |
| 86 | - if (isInOneyRange() && totalsProduct < maxOneyQty) { | |
| 87 | - if (popupLoaded) { | |
| 88 | - return | |
| 89 | - } | |
| 90 | - popup.html('').append($(loading)) | |
| 91 | - $.post( | |
| 92 | - payplug_config.ajax_url, | |
| 93 | - { | |
| 94 | - 'action': payplug_config.ajax_action, | |
| 95 | - 'price': calculTotals() | |
| 96 | - }, function (response) { | |
| 97 | - if (response.data.popup) { | |
| 98 | - popupLoaded = true | |
| 99 | - popup.addClass('loaded') | |
| 100 | - popup.html(response.data.popup) | |
| 101 | - showpopupF() | |
| 102 | - } | |
| 103 | - } | |
| 104 | - ) | |
| 105 | - } else { | |
| 106 | - checkOneyError() | |
| 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 | + }, | |
| 89 | + }); | |
| 90 | + | |
| 91 | + loadOneyWidget(function () { | |
| 92 | + if (typeof oneyMerchantApp === 'undefined') { | |
| 93 | + hideLoading(); | |
| 94 | + | |
| 95 | + return; | |
| 107 | 96 | } |
| 108 | - }) | |
| 109 | - showpopuponey.on('mouseenter', function () { | |
| 110 | - showpopupF() | |
| 111 | - }) | |
| 112 | - showpopuponey.on('mouseleave', function () { | |
| 113 | - if (popupLoaded) { | |
| 114 | - popup.hide() | |
| 115 | - } | |
| 116 | - }) | |
| 117 | - $(document).on('scroll', function () { | |
| 118 | - if (!$.browser.mobile) { | |
| 119 | - popup.hide() | |
| 120 | - } | |
| 121 | - }) | |
| 97 | + oneyMerchantApp.loadCheckoutSection({options: options}); | |
| 98 | + checkoutSectionsLoaded[gatewayId] = placeholder; | |
| 99 | + }); | |
| 122 | 100 | } |
| 123 | - $(document).on('ready', function () { | |
| 124 | - initevent() | |
| 125 | - if (is_cart) { | |
| 126 | - $(document).ajaxSuccess(function (event, request, settings) { | |
| 127 | - if (settings.data.includes(payplug_config.ajax_action)) { | |
| 128 | - return | |
| 129 | - } | |
| 130 | - initevent() | |
| 131 | - popupLoaded = false | |
| 132 | - }) | |
| 101 | + | |
| 102 | + function maybeLoadSelectedCheckoutSection() { | |
| 103 | + var selected = $('input[name="payment_method"]:checked').val(); | |
| 104 | + if (selected && selected.indexOf('oney_') === 0) { | |
| 105 | + loadCheckoutSection(selected); | |
| 133 | 106 | } |
| 134 | - }) | |
| 135 | -})(jQuery) | |
| 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')); | |
| 115 | + }); | |
| 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); | |