| @@ -122,8 +122,45 @@ | ||
| 122 | 122 | settingsDiv.addClass('hidden'); |
| 123 | 123 | } |
| 124 | 124 | }); |
| 125 | 125 | |
| 126 | + // Offline payment methods the merchant adds (ACH, wire, …). Adding or | |
| 127 | + // removing one registers a gateway, so the page reloads to show it. | |
| 128 | + var offlineNonce = (typeof easyInvoiceSettings !== 'undefined' && easyInvoiceSettings.nonce) ? easyInvoiceSettings.nonce : ''; | |
| 129 | + var offlineAjax = (typeof easyInvoiceSettings !== 'undefined' && easyInvoiceSettings.ajaxurl) ? easyInvoiceSettings.ajaxurl : (window.ajaxurl || ''); | |
| 130 | + var toast = function (type, message) { | |
| 131 | + if (typeof EasyInvoiceToast !== 'undefined' && EasyInvoiceToast[type]) { EasyInvoiceToast[type](message); } else if (message) { window.alert(message); } | |
| 132 | + }; | |
| 133 | + $('#ei-offline-add-btn').on('click', function () { | |
| 134 | + var $btn = $(this), title = $.trim($('#ei-offline-add-title').val() || ''); | |
| 135 | + if (!title) { toast('error', 'Give the payment method a name.'); $('#ei-offline-add-title').focus(); return; } | |
| 136 | + $btn.prop('disabled', true); | |
| 137 | + $.post(offlineAjax, { action: 'easy_invoice_add_offline_method', nonce: offlineNonce, title: title }) | |
| 138 | + .done(function (r) { | |
| 139 | + if (r && r.success) { | |
| 140 | + toast('success', r.data && r.data.message ? r.data.message : 'Payment method added.'); | |
| 141 | + var url = new URL(window.location.href); url.searchParams.set('section', 'payment'); url.hash = ''; | |
| 142 | + setTimeout(function () { window.location.href = url.toString() + '#gateway-enable-' + (r.data && r.data.id ? r.data.id : ''); }, 600); | |
| 143 | + } else { | |
| 144 | + toast('error', r && r.data && r.data.message ? r.data.message : 'The payment method could not be added.'); | |
| 145 | + $btn.prop('disabled', false); | |
| 146 | + } | |
| 147 | + }) | |
| 148 | + .fail(function () { toast('error', 'The payment method could not be added.'); $btn.prop('disabled', false); }); | |
| 149 | + }); | |
| 150 | + $('#ei-offline-add-title').on('keydown', function (e) { if (e.key === 'Enter') { e.preventDefault(); $('#ei-offline-add-btn').trigger('click'); } }); | |
| 151 | + $('#settings-form').on('click', '.ei-offline-remove', function () { | |
| 152 | + var $btn = $(this), id = $btn.data('method-id'), name = $btn.data('method-title') || id; | |
| 153 | + if (!window.confirm('Remove "' + name + '" from the payment methods you offer? Payments already recorded keep it.')) { return; } | |
| 154 | + $btn.prop('disabled', true); | |
| 155 | + $.post(offlineAjax, { action: 'easy_invoice_remove_offline_method', nonce: offlineNonce, id: id }) | |
| 156 | + .done(function (r) { | |
| 157 | + if (r && r.success) { toast('success', r.data && r.data.message ? r.data.message : 'Payment method removed.'); setTimeout(function () { window.location.reload(); }, 600); } | |
| 158 | + else { toast('error', r && r.data && r.data.message ? r.data.message : 'The payment method could not be removed.'); $btn.prop('disabled', false); } | |
| 159 | + }) | |
| 160 | + .fail(function () { toast('error', 'The payment method could not be removed.'); $btn.prop('disabled', false); }); | |
| 161 | + }); | |
| 162 | + | |
| 126 | 163 | // Initialize gateway settings visibility on page load |
| 127 | 164 | $('.gateway-enable-checkbox').each(function() { |
| 128 | 165 | var settingsDiv = $(this).closest('.gateway-item').find('.gateway-settings'); |
| 129 | 166 | if ($(this).is(':checked')) { |