| 1 |
; (function ($) { |
| 2 |
$(document).ready(function () { |
| 3 |
'use strict' |
| 4 |
|
| 5 |
//Settings Page Starts |
| 6 |
$('.bp-tabs .tab__link').on('click', function (e) { |
| 7 |
e.preventDefault(); |
| 8 |
$('.bp-tabs .tab__link').removeClass('active'); |
| 9 |
$(this).addClass('active'); |
| 10 |
|
| 11 |
var dataId = $(this).data('id'); |
| 12 |
|
| 13 |
$('.bp-tabs .tab__content__item').removeClass('show').fadeOut(); |
| 14 |
$('#' + dataId).addClass('show').fadeIn(); |
| 15 |
|
| 16 |
}); |
| 17 |
|
| 18 |
$(document).on('click', '.sidebar__menu a', function (e) { |
| 19 |
e.preventDefault(); |
| 20 |
|
| 21 |
let sidebar_link_class = 'sidebar__link'; |
| 22 |
if ($(this).hasClass('sidebar__link_submenu')) { |
| 23 |
sidebar_link_class = 'sidebar__link_submenu'; |
| 24 |
} |
| 25 |
|
| 26 |
$('.sidebar__menu .' + sidebar_link_class).removeClass('active'); |
| 27 |
$(this).addClass('active'); |
| 28 |
|
| 29 |
var dataId = $(this).data('id'); |
| 30 |
$('.content__area__body .sidebar__tab__content').removeClass('show').fadeOut(); |
| 31 |
$('#' + dataId).addClass('show').fadeIn(); |
| 32 |
|
| 33 |
//Auto active first submenu item |
| 34 |
if ($(this).hasClass('sidebar__link') && (dataId === 'admin-email' || dataId === 'paypal')) { |
| 35 |
$(this).siblings('ul').find('li a').removeClass('active') |
| 36 |
$(this).siblings('ul').find('li:first-child a').addClass('active'); |
| 37 |
} |
| 38 |
}); |
| 39 |
|
| 40 |
$(document).on('click', ".sidebar__item .sidebar__link, .email-additional-headers", function (e) { |
| 41 |
e.preventDefault(); |
| 42 |
let $this = $(this); |
| 43 |
let bpTargetSelector = $(this).hasClass('email-additional-headers') ? '.email-additional-headers-content' : '.sub__menu'; |
| 44 |
|
| 45 |
if ($this.siblings(bpTargetSelector).hasClass('show')) { |
| 46 |
$this.siblings(bpTargetSelector).removeClass('show').slideUp(); |
| 47 |
} else { |
| 48 |
$(bpTargetSelector + '.show').slideUp().removeClass('show'); |
| 49 |
$this.siblings(bpTargetSelector).addClass('show').slideDown(); |
| 50 |
} |
| 51 |
}); |
| 52 |
|
| 53 |
//Stripe toggle button |
| 54 |
$(document).on('change', '.betterpayment-settings-payment-stripe input[name="betterpayment_settings_payment_stripe_live_mode"]', function (e) { |
| 55 |
e.preventDefault(); |
| 56 |
let bpAdminSettingsPaymentStripe = $(this).attr('data-targettest'); |
| 57 |
|
| 58 |
if ($(this).is(':checked')) { |
| 59 |
bpAdminSettingsPaymentStripe = $(this).attr('data-targetlive'); |
| 60 |
} |
| 61 |
|
| 62 |
$('.bp-stripe-key').removeClass('bp-d-block').addClass('bp-d-none'); |
| 63 |
$(`.${bpAdminSettingsPaymentStripe}`).removeClass('bp-d-none').addClass('bp-d-block'); |
| 64 |
}); |
| 65 |
|
| 66 |
//Settings Save |
| 67 |
$(document).on("click", ".betterpayment-admin-settings-button", function (e) { |
| 68 |
e.preventDefault(); |
| 69 |
let bpAdminSettingsForm = $(this).parents("#betterpayment-admin-settings-form"); |
| 70 |
bpAdminSettingsSave(this, bpAdminSettingsForm); |
| 71 |
}); |
| 72 |
|
| 73 |
function bpAdminSettingsSave(button, form) { |
| 74 |
let bpAdminSettingsSaveBtn = $(button), |
| 75 |
nonce = betterpaymentObj.nonce, |
| 76 |
formData = $(form).serializeArray(); |
| 77 |
|
| 78 |
let bpIsFormValidated = bpValidateFormFields(formData); |
| 79 |
|
| 80 |
//Reset button |
| 81 |
$('.betterpayment-settings-reset').val(0); |
| 82 |
if ($(button).hasClass('betterpayment-reset-button')) { |
| 83 |
$('.betterpayment-settings-reset').val(1); |
| 84 |
} |
| 85 |
|
| 86 |
if (!bpIsFormValidated) { |
| 87 |
return false; |
| 88 |
} |
| 89 |
|
| 90 |
let bpIsResetButton = $(button).hasClass('betterpayment-reset-button'); |
| 91 |
|
| 92 |
$.ajax({ |
| 93 |
type: "POST", |
| 94 |
url: ajaxurl, |
| 95 |
data: { |
| 96 |
action: "betterpayment_settings_action", |
| 97 |
nonce: nonce, |
| 98 |
form_data: formData, |
| 99 |
reset_button: bpIsResetButton, |
| 100 |
}, |
| 101 |
beforeSend: function () { |
| 102 |
bpAdminSettingsSaveBtn.addClass("is-loading"); |
| 103 |
}, |
| 104 |
success: function (res) { |
| 105 |
bpAdminSettingsSaveBtn.removeClass("is-loading"); |
| 106 |
if (res.data === "success") { |
| 107 |
toastr.success('Changes saved successfully!'); |
| 108 |
} else { |
| 109 |
toastr.error('Opps! something went wrong!'); |
| 110 |
} |
| 111 |
}, |
| 112 |
}); |
| 113 |
} |
| 114 |
|
| 115 |
function bpValidateFormFields(formFields, emailFields = []) { |
| 116 |
$(`#betterpayment-admin-settings-form input`).removeClass('is-danger'); |
| 117 |
if (!(emailFields.length)) { |
| 118 |
emailFields = [ |
| 119 |
'betterpayment_settings_general_email_to', |
| 120 |
'betterpayment_settings_general_email_from_email', |
| 121 |
'betterpayment_settings_general_email_reply_to', |
| 122 |
'betterpayment_settings_general_email_cc', |
| 123 |
'betterpayment_settings_general_email_bcc', |
| 124 |
'betterpayment_settings_general_email_to_customer', |
| 125 |
'betterpayment_settings_general_email_from_email_customer', |
| 126 |
'betterpayment_settings_general_email_reply_to_customer', |
| 127 |
'betterpayment_settings_general_email_cc_customer', |
| 128 |
'betterpayment_settings_general_email_bcc_customer', |
| 129 |
'betterpayment_settings_payment_paypal_email' |
| 130 |
]; |
| 131 |
} |
| 132 |
|
| 133 |
let formFieldName = '', |
| 134 |
formFieldValue = '', |
| 135 |
formField = '', |
| 136 |
isFormFieldValidated = false, |
| 137 |
errorMessageStr = '' |
| 138 |
; |
| 139 |
for (const property in formFields) { |
| 140 |
formField = formFields[property]; |
| 141 |
formFieldName = formField.name; |
| 142 |
formFieldValue = formField.value; |
| 143 |
|
| 144 |
if ((emailFields.indexOf(formFieldName) >= 0) && formFieldValue != '') { |
| 145 |
|
| 146 |
isFormFieldValidated = admin_better_email_validation(formFieldValue); |
| 147 |
|
| 148 |
if (!isFormFieldValidated) { |
| 149 |
if (formFieldName == 'betterpayment_settings_general_email_to') { |
| 150 |
errorMessageStr = 'Admin Email: To Email'; |
| 151 |
} else if (formFieldName == 'betterpayment_settings_general_email_from_email') { |
| 152 |
errorMessageStr = 'Admin Email: From Email'; |
| 153 |
} else if (formFieldName == 'betterpayment_settings_general_email_reply_to') { |
| 154 |
errorMessageStr = 'Admin Email: Reply-To'; |
| 155 |
} else if (formFieldName == 'betterpayment_settings_general_email_cc') { |
| 156 |
errorMessageStr = 'Admin Email: Cc'; |
| 157 |
} else if (formFieldName == 'betterpayment_settings_general_email_bcc') { |
| 158 |
errorMessageStr = 'Admin Email: Bcc'; |
| 159 |
} if (formFieldName == 'betterpayment_settings_general_email_to_customer') { |
| 160 |
errorMessageStr = 'Customer Email: To Email'; |
| 161 |
} else if (formFieldName == 'betterpayment_settings_general_email_from_email_customer') { |
| 162 |
errorMessageStr = 'Customer Email: From Email'; |
| 163 |
} else if (formFieldName == 'betterpayment_settings_general_email_reply_to_customer') { |
| 164 |
errorMessageStr = 'Customer Email: Reply-To'; |
| 165 |
} else if (formFieldName == 'betterpayment_settings_general_email_cc_customer') { |
| 166 |
errorMessageStr = 'Customer Email: Cc'; |
| 167 |
} else if (formFieldName == 'betterpayment_settings_general_email_bcc_customer') { |
| 168 |
errorMessageStr = 'Customer Email: Bcc'; |
| 169 |
} else if (formFieldName == 'betterpayment_settings_payment_paypal_email') { |
| 170 |
errorMessageStr = 'PayPal Business Email'; |
| 171 |
} |
| 172 |
|
| 173 |
toastr.error(`Invalid email address on ${errorMessageStr} field!`); |
| 174 |
$(`#betterpayment-admin-settings-form input[name="${formFieldName}"]`).addClass('is-danger'); |
| 175 |
return false; |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
return true; |
| 181 |
} |
| 182 |
|
| 183 |
function admin_better_email_validation(email) { |
| 184 |
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email) |
| 185 |
} |
| 186 |
//Settings Page Ends |
| 187 |
|
| 188 |
//Transaction Page Starts |
| 189 |
$(document).on("change", ".betterpayment .header__show_entries select", function (e) { |
| 190 |
e.preventDefault(); |
| 191 |
let perPageVal = $(this).val(); |
| 192 |
location = location.origin + location.pathname + location.search + '&paged=1&per_page=' + perPageVal + location.hash; |
| 193 |
}); |
| 194 |
|
| 195 |
$(document).on("click", ".betterpayment-transaction-edit, .betterpayment-transaction-email-resend", function (e) { |
| 196 |
e.preventDefault(); |
| 197 |
toastr.warning('Coming REAL Soon!'); |
| 198 |
}); |
| 199 |
//Pagination |
| 200 |
|
| 201 |
//Filter transactions |
| 202 |
$(document).on("click", ".betterpayment-transaction-filter", function (e) { |
| 203 |
e.preventDefault(); |
| 204 |
let paymentFromDate = $('.betterpayment .payment_date_from').val(); |
| 205 |
let paymentToDate = $('.betterpayment .payment_date_to').val(); |
| 206 |
if(paymentFromDate != '' && paymentToDate != ''){ |
| 207 |
if(paymentFromDate > paymentToDate){ |
| 208 |
toastr.error('From Date must be smaller than To Date!'); |
| 209 |
return false; |
| 210 |
} |
| 211 |
} |
| 212 |
let tableSelector = $('.betterpayment-admin-transactions-page .transaction__table'); |
| 213 |
|
| 214 |
let filterFormData = {}; |
| 215 |
filterFormData.search_text = $('.betterpayment .serch-text').val(); |
| 216 |
|
| 217 |
filterFormData.payment_date_from = $('.betterpayment .payment_date_from').val(); |
| 218 |
filterFormData.payment_date_to = $('.betterpayment .payment_date_to').val(); |
| 219 |
filterFormData.order_by = $('.betterpayment .order-by').val(); |
| 220 |
filterFormData.order = $('.betterpayment .order').val(); |
| 221 |
filterFormData.source = $('.betterpayment .source').val(); |
| 222 |
|
| 223 |
filterFormData.paged = $('.betterpayment .paged').val(); |
| 224 |
filterFormData.per_page = $('.betterpayment .per-page').val(); |
| 225 |
filterFormData.total_entry = $('.betterpayment .total-entry').val(); |
| 226 |
|
| 227 |
bpTransactionFilter(tableSelector, filterFormData); |
| 228 |
}); |
| 229 |
|
| 230 |
let elements = document.getElementsByClassName("bp-copy-clipboard"); |
| 231 |
Array.from(elements).forEach(function(element) { |
| 232 |
element.addEventListener('click', bpTransactionIdCopy); |
| 233 |
}); |
| 234 |
|
| 235 |
function bpTransactionIdCopy() { |
| 236 |
let bpTxnCounter = $(this).attr('data-bp_txn_counter'); |
| 237 |
let node = 'bp_copy_clipboard_input_'+bpTxnCounter; |
| 238 |
|
| 239 |
node = document.getElementById(node); |
| 240 |
|
| 241 |
if (document.body.createTextRange) { |
| 242 |
const range = document.body.createTextRange(); |
| 243 |
range.moveToElementText(node); |
| 244 |
range.select(); |
| 245 |
|
| 246 |
document.execCommand("copy"); |
| 247 |
|
| 248 |
} else if (window.getSelection) { |
| 249 |
|
| 250 |
const selection = window.getSelection(); |
| 251 |
const range = document.createRange(); |
| 252 |
range.selectNodeContents(node); |
| 253 |
selection.removeAllRanges(); |
| 254 |
selection.addRange(range); |
| 255 |
|
| 256 |
document.execCommand("copy"); |
| 257 |
selection.removeAllRanges(); |
| 258 |
|
| 259 |
} else { |
| 260 |
console.warn("Could not select text in node: Unsupported browser."); |
| 261 |
} |
| 262 |
$('.bp-copy-clipboard').attr('title', 'Copy').css('color', '#2a3256'); |
| 263 |
$(this).attr('title', 'Copied!').css('color', '#6e58f7'); |
| 264 |
} |
| 265 |
|
| 266 |
function bpTransactionFilter(tableSelector, filterFormData){ |
| 267 |
$.ajax({ |
| 268 |
type: "POST", |
| 269 |
url: ajaxurl, |
| 270 |
data: { |
| 271 |
action: "betterpayment-filter-transaction", |
| 272 |
nonce: betterpaymentObj.nonce, |
| 273 |
filterFormData: filterFormData, |
| 274 |
}, |
| 275 |
dataType: "html", |
| 276 |
beforeSend: function () { |
| 277 |
tableSelector.css('opacity', '.5'); |
| 278 |
}, |
| 279 |
success: function (resultHtml) { |
| 280 |
$('.betterpayment .transaction-table-wrapper').replaceWith(resultHtml); |
| 281 |
tableSelector.css('opacity', '1'); |
| 282 |
}, |
| 283 |
}); |
| 284 |
} |
| 285 |
//Transaction Page Ends |
| 286 |
|
| 287 |
//Helper Functions Starts |
| 288 |
function toasterOptions() { |
| 289 |
toastr.options = { |
| 290 |
"timeOut": "2000", |
| 291 |
"toastClass": "font-size-md", |
| 292 |
"positionClass": 'toast-top-center', |
| 293 |
"showMethod": "slideDown", |
| 294 |
"hideMethod": "slideUp", |
| 295 |
|
| 296 |
}; |
| 297 |
}; |
| 298 |
toasterOptions(); |
| 299 |
|
| 300 |
$(".bp-datepicker").datepicker({ |
| 301 |
dateFormat : "dd-mm-yy" |
| 302 |
}); |
| 303 |
//Helper Functions Ends |
| 304 |
|
| 305 |
}); |
| 306 |
})(jQuery); |