| 1 |
jQuery(document).ready(function ($) { |
| 2 |
const { id, url, code_setting } = hyperpay_data; |
| 3 |
|
| 4 |
const currencies_ids = $(`#woocommerce_${id}_currencies_ids_field`); |
| 5 |
const currencies_option = currencies_ids.data("currencies"); |
| 6 |
const currencies_list = currencies_ids.data("currencies_list"); |
| 7 |
const add_currencies_button = $(`<div style='margin-left:auto;' class='button-primary woocommerce-save-button'>Add currency</div>`) |
| 8 |
const currencies_group = $( |
| 9 |
`<div style='width:400px;display:flex;flex-direction:column;gap:10px'> |
| 10 |
<div id='currencies_container'></div> |
| 11 |
</div>` |
| 12 |
) |
| 13 |
|
| 14 |
const genInputs = (key = null, index = 0) => { |
| 15 |
let options = '' |
| 16 |
currencies_list.forEach(i => { |
| 17 |
options += `<option ${(key && key == i) ? 'selected' : ''} value='${i}'>${i}</option>` |
| 18 |
}) |
| 19 |
|
| 20 |
return $( |
| 21 |
`<div style='display:flex;margin-top:10px;flex-wrap:wrap'> |
| 22 |
<select name='currencies_ids[${index}][name]' style='width:100px'> |
| 23 |
<option selected disabled>Currency</option> |
| 24 |
${options} |
| 25 |
</select> |
| 26 |
<input name='currencies_ids[${index}][value]' value='${(key && currencies_option[key]) ? currencies_option[key] : ''}' |
| 27 |
style='width:300px' type='text' placeholder='Entity ID'/> |
| 28 |
<small style='display:block;margin-left:105px'>Leave it blank and save to remove</small> |
| 29 |
</div>`) |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
Object.keys(currencies_ids.data("currencies")).forEach((key,index) => { |
| 34 |
currencies_group.find('#currencies_container').append(genInputs(key, index)) |
| 35 |
}) |
| 36 |
|
| 37 |
currencies_group.append(add_currencies_button) |
| 38 |
currencies_ids.parent().append(currencies_group) |
| 39 |
|
| 40 |
add_currencies_button.on('click', function () { |
| 41 |
let index = $("#currencies_container").children().length |
| 42 |
$('#currencies_container').append(genInputs(null,index)) |
| 43 |
}) |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
// Convert TextArea for custom css field on admin settings |
| 48 |
wp.codeEditor.initialize($(`#woocommerce_${id}_custom_style`), code_setting); |
| 49 |
|
| 50 |
/** |
| 51 |
* Make Validation before submit settings via send request |
| 52 |
* to prepare checkout to make sure the access token and entity id is valid |
| 53 |
* @if return success response @then => resume submitting @else print error msg and preventDefault |
| 54 |
* |
| 55 |
* @returns {boolean} if valid or not |
| 56 |
* |
| 57 |
*/ |
| 58 |
const from = $("#mainform"); |
| 59 |
|
| 60 |
from.on('submit', function (event) { |
| 61 |
|
| 62 |
$('.input-error-msg').remove(); |
| 63 |
|
| 64 |
|
| 65 |
let valid = false |
| 66 |
|
| 67 |
const accesstoken = $(this).find(`input[name='woocommerce_${id}_accesstoken']`); |
| 68 |
const entityId = $(this).find(`input[name='woocommerce_${id}_entityId']`); |
| 69 |
|
| 70 |
const dataToSend = { |
| 71 |
entityId: entityId.val(), |
| 72 |
}; |
| 73 |
|
| 74 |
$.ajax({ |
| 75 |
type: 'POST', |
| 76 |
async: false, |
| 77 |
headers: { |
| 78 |
Authorization: `Bearer ${accesstoken.val()}` |
| 79 |
}, |
| 80 |
url: url, |
| 81 |
data: dataToSend, |
| 82 |
dataType: "json", |
| 83 |
success: function () { |
| 84 |
valid = true |
| 85 |
}, |
| 86 |
error: function (resultData) { |
| 87 |
valid = false; |
| 88 |
accesstoken.addClass('input-error').after(`<p class='input-error-msg'>${resultData.responseJSON.result.description}</p>`); |
| 89 |
entityId.addClass('input-error').after(`<p class='input-error-msg'>${resultData.responseJSON.result.description}</p>`); |
| 90 |
$('html, body').animate({ |
| 91 |
scrollTop: accesstoken.offset().top - 200 |
| 92 |
}, 200); |
| 93 |
}, |
| 94 |
|
| 95 |
}); |
| 96 |
return valid; |
| 97 |
}) |
| 98 |
|
| 99 |
}); |