| 1 |
(function ($) { |
| 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; |
| 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; |
| 62 |
} |
| 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'; |
| 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 |
}, |
| 89 |
}); |
| 90 |
|
| 91 |
loadOneyWidget(function () { |
| 92 |
if (typeof oneyMerchantApp === 'undefined') { |
| 93 |
hideLoading(); |
| 94 |
|
| 95 |
return; |
| 96 |
} |
| 97 |
oneyMerchantApp.loadCheckoutSection({options: options}); |
| 98 |
checkoutSectionsLoaded[gatewayId] = placeholder; |
| 99 |
}); |
| 100 |
} |
| 101 |
|
| 102 |
function maybeLoadSelectedCheckoutSection() { |
| 103 |
var selected = $('input[name="payment_method"]:checked').val(); |
| 104 |
if (selected && selected.indexOf('oney_') === 0) { |
| 105 |
loadCheckoutSection(selected); |
| 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')); |
| 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); |
| 133 |
|