| 1 |
;(function ($) { |
| 2 |
"use strict"; |
| 3 |
|
| 4 |
$(document).on("click",".better-payment-paypal-bt",function(e) { |
| 5 |
const id = $("#" + this.form.id), |
| 6 |
$form = $(id), |
| 7 |
paymentInfo = $(this).data("paypal-info"), |
| 8 |
fields = $form.serializeObject(); |
| 9 |
|
| 10 |
let data = validateBetterPaymentForm(paymentInfo, fields, this.form); |
| 11 |
|
| 12 |
if(typeof data.status !== 'undefined' && !data.status){ |
| 13 |
e.preventDefault(); |
| 14 |
toastr.error( data.errors.length ? data.errors[0] : "Something went wrong!" ); |
| 15 |
this.disabled = false; |
| 16 |
|
| 17 |
return false; |
| 18 |
} |
| 19 |
}); |
| 20 |
|
| 21 |
$(document).on("click",".better-payment-stripe-bt",function(e) { |
| 22 |
e.preventDefault(); |
| 23 |
|
| 24 |
const id = $("#" + this.form.id), |
| 25 |
$form = $(id), |
| 26 |
$this = $(this), |
| 27 |
setting_data = $form.data('better-payment'), |
| 28 |
fields = $form.serializeObject(); |
| 29 |
|
| 30 |
let data = validateBetterPaymentForm('', fields, this.form, false); |
| 31 |
|
| 32 |
if(typeof data.status !== 'undefined' && !data.status){ |
| 33 |
e.preventDefault(); |
| 34 |
toastr.error( data.errors.length ? data.errors[0] : "Something went wrong!" ); |
| 35 |
this.disabled = false; |
| 36 |
|
| 37 |
return false; |
| 38 |
} |
| 39 |
|
| 40 |
$(this).html('Submitting <span class="elementor-control-spinner"> <i class="eicon-spinner eicon-animation-spin"></i> </span>') |
| 41 |
|
| 42 |
$.ajax({ |
| 43 |
url: betterPayment.ajax_url, |
| 44 |
type: "post", |
| 45 |
data: { |
| 46 |
action: "better_payment_stripe_get_token", |
| 47 |
security: betterPayment.nonce, |
| 48 |
fields: fields, |
| 49 |
setting_data: setting_data, |
| 50 |
}, |
| 51 |
|
| 52 |
success: function (response) { |
| 53 |
if (typeof response.data.stripe_data != 'undefined') { |
| 54 |
$this.html('Redirecting <span class="elementor-control-spinner"> <i class="eicon-spinner eicon-animation-spin"></i> </span>') |
| 55 |
var stripe = Stripe(response.data.stripe_public_key); |
| 56 |
stripe.redirectToCheckout({sessionId: response.data.stripe_data}).then(function (t) { |
| 57 |
}) |
| 58 |
} else { |
| 59 |
$this.html('Stripe'); |
| 60 |
toastr.error(response.data); |
| 61 |
} |
| 62 |
}, |
| 63 |
error: function () { |
| 64 |
console.log('Something went wrong!'); |
| 65 |
}, |
| 66 |
}); |
| 67 |
}); |
| 68 |
|
| 69 |
function validateBetterPaymentForm(paymentInfo, fields, thisForm, isPaypal = true){ |
| 70 |
let fieldsToExclude = getExcludedFields(), |
| 71 |
data = {}; |
| 72 |
|
| 73 |
data.status = true; |
| 74 |
data.errors = []; |
| 75 |
|
| 76 |
let validatedData = validateForm(thisForm, fields, fieldsToExclude); |
| 77 |
|
| 78 |
data.status = typeof validatedData.status !== "undefined" ? validatedData.status : data.status; |
| 79 |
data.errors = typeof validatedData.errors !== "undefined" && validatedData.errors.constructor === Array ? data.errors.concat(validatedData.errors) : data.errors; |
| 80 |
|
| 81 |
if (isPaypal && !paymentInfo.business_email) { |
| 82 |
data.errors.push("Business Email is required"); |
| 83 |
data.status = false; |
| 84 |
} |
| 85 |
|
| 86 |
if (typeof fields.payment_amount === 'undefined' || fields.payment_amount === '') { |
| 87 |
data.errors.push("Payment Amount field is required!"); |
| 88 |
data.status = false; |
| 89 |
} |
| 90 |
|
| 91 |
if (typeof fields.payment_amount !== 'undefined' && parseFloat(fields.payment_amount) < 1) { |
| 92 |
data.errors.push("Minimum amount is 1!"); |
| 93 |
data.status = false; |
| 94 |
} |
| 95 |
|
| 96 |
return data; |
| 97 |
} |
| 98 |
|
| 99 |
$(document).on("change",".bp-custom-payment-amount",function(e) { |
| 100 |
if (typeof this.form.id == 'undefined') { |
| 101 |
toastr.error('Something went wrong!'); |
| 102 |
} |
| 103 |
|
| 104 |
const radioInput = document.querySelectorAll("#" + this.form.id + ' .bp-payment-amount-wrap input[type="radio"]'); |
| 105 |
radioInput.forEach((radio) => { |
| 106 |
radio.checked = false |
| 107 |
}); |
| 108 |
$('.bp-transaction-details-amount-text').text(this.value); |
| 109 |
}); |
| 110 |
|
| 111 |
$(document).on("click",".bp-form_pay-radio",function(e) { |
| 112 |
if (typeof this.form.id == 'undefined') { |
| 113 |
toastr.error('Something went wrong!'); |
| 114 |
} |
| 115 |
const $this = $(this) |
| 116 |
let amount = parseFloat($this.val()); |
| 117 |
|
| 118 |
if (amount == '') { |
| 119 |
return false; |
| 120 |
} |
| 121 |
$("#" + this.form.id + ' .bp-custom-payment-amount').val(amount); |
| 122 |
$('.bp-transaction-details-amount-text').text(amount); |
| 123 |
}); |
| 124 |
|
| 125 |
$(window).on('elementor/frontend/init', () => { |
| 126 |
|
| 127 |
const Better_Payment_Stripe = elementorModules.frontend.handlers.Base.extend({ |
| 128 |
getDefaultSettings: function getDefaultSettings() { |
| 129 |
return { |
| 130 |
selectors: { |
| 131 |
form: '.elementor-form' |
| 132 |
} |
| 133 |
}; |
| 134 |
}, |
| 135 |
|
| 136 |
getDefaultElements: function getDefaultElements() { |
| 137 |
let selectors = this.getSettings('selectors'), |
| 138 |
elements = {}; |
| 139 |
elements.$form = this.$element.find(selectors.form); |
| 140 |
return elements; |
| 141 |
}, |
| 142 |
|
| 143 |
bindEvents: function bindEvents() { |
| 144 |
this.elements.$form.on('submit_success', this.handleFormAction); |
| 145 |
}, |
| 146 |
|
| 147 |
handleFormAction: function handleFormAction(event, res) { |
| 148 |
if (typeof res.data.better_stripe_data != 'undefined') { |
| 149 |
let stripe = Stripe(res.data.better_stripe_data.stripe_public_key); |
| 150 |
stripe.redirectToCheckout({sessionId: res.data.better_stripe_data.stripe_data}).then(function (t) { |
| 151 |
}); |
| 152 |
} |
| 153 |
}, |
| 154 |
}); |
| 155 |
|
| 156 |
const Better_Payment_Handler = ($element) => { |
| 157 |
elementorFrontend.elementsHandler.addHandler(Better_Payment_Stripe, { |
| 158 |
$element, |
| 159 |
}); |
| 160 |
}; |
| 161 |
elementorFrontend.hooks.addAction('frontend/element_ready/form.default', Better_Payment_Handler); |
| 162 |
|
| 163 |
if(typeof elementor != 'undefined'){ |
| 164 |
|
| 165 |
elementor.hooks.addAction( 'panel/open_editor/widget/form', function( panel, model, view ) { |
| 166 |
|
| 167 |
elementor.hooks.addFilter('elementor_pro/forms/content_template/item', function(item, i, settings){ |
| 168 |
let bp_field_payment_amount = 'payment_amount'; |
| 169 |
let better_payment_payment_amount_enable_settings = ''; |
| 170 |
|
| 171 |
if(typeof settings.better_payment_payment_amount_enable !== 'undefined'){ |
| 172 |
better_payment_payment_amount_enable_settings = settings.better_payment_payment_amount_enable; |
| 173 |
} |
| 174 |
console,l |
| 175 |
if(typeof item.field_type !== 'undefined' && item.field_type == bp_field_payment_amount && better_payment_payment_amount_enable_settings === 'yes' ){ |
| 176 |
let bp_field_label = item.field_label; |
| 177 |
let bp_field_placeholder = typeof item.bp_placeholder !== 'undefined' ? item.bp_placeholder : ''; |
| 178 |
let better_payment_form_currency = 'USD'; |
| 179 |
|
| 180 |
if(settings.submit_actions.includes('paypal')){ |
| 181 |
better_payment_form_currency = (typeof settings.better_payment_form_paypal_currency !== 'undefined') ? settings.better_payment_form_paypal_currency : better_payment_form_currency; |
| 182 |
}else if(settings.submit_actions.includes('stripe')){ |
| 183 |
better_payment_form_currency = (typeof settings.better_payment_form_stripe_currency !== 'undefined') ? settings.better_payment_form_stripe_currency : better_payment_form_currency; |
| 184 |
} |
| 185 |
|
| 186 |
let better_payment_form_currency_symbol = getCurrencySymbols(better_payment_form_currency); |
| 187 |
|
| 188 |
let bp_payment_amount_label = `<label for="form-field-${bp_field_payment_amount}" class="elementor-field-label">${bp_field_label}</label>`; |
| 189 |
let bp_payment_amount_input = `<input type="number" name="form_fields[${bp_field_payment_amount}]" id="form-field-${bp_field_payment_amount}" class="elementor-field elementor-size-sm elementor-field-textual bp-elementor-field-textual-amount" min="${item.bp_field_min}" max="${item.bp_field_max}" placeholder="${bp_field_placeholder}" >`; |
| 190 |
|
| 191 |
let payment_amount_input_group = ` <div class="bp-input-group mb-2"> |
| 192 |
<div class="bp-input-group-prepend"> |
| 193 |
<div class="bp-input-group-text" title="${better_payment_form_currency}">${better_payment_form_currency_symbol}</div> |
| 194 |
</div> |
| 195 |
${bp_payment_amount_input} |
| 196 |
</div> |
| 197 |
`; |
| 198 |
|
| 199 |
item.field_type = 'html'; |
| 200 |
item.field_html = bp_payment_amount_label + payment_amount_input_group; |
| 201 |
} |
| 202 |
|
| 203 |
return item; |
| 204 |
}, 10, 3 ); |
| 205 |
|
| 206 |
} ); |
| 207 |
} |
| 208 |
|
| 209 |
}); |
| 210 |
|
| 211 |
$.fn.serializeObject = function () { |
| 212 |
var objInit = {}; |
| 213 |
var a = this.serializeArray(); |
| 214 |
$.each(a, function () { |
| 215 |
if (objInit[this.name]) { |
| 216 |
if (!objInit[this.name].push) { |
| 217 |
objInit[this.name] = [objInit[this.name]]; |
| 218 |
} |
| 219 |
objInit[this.name].push(this.value || ''); |
| 220 |
} else { |
| 221 |
objInit[this.name] = this.value || ''; |
| 222 |
} |
| 223 |
}); |
| 224 |
return objInit; |
| 225 |
}; |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
//Better Payment Functions Starts |
| 230 |
function getExcludedFields(){ |
| 231 |
return [ |
| 232 |
'better_payment_page_id', |
| 233 |
'better_payment_widget_id', |
| 234 |
'return', |
| 235 |
'action', |
| 236 |
'security', |
| 237 |
'cancel_return', |
| 238 |
'payment_amount', |
| 239 |
]; |
| 240 |
} |
| 241 |
|
| 242 |
function getCurrencySymbols( currency = 'USD' ) { |
| 243 |
let currencySymbol = '$'; |
| 244 |
let currencyList = { |
| 245 |
'AUD': "$", |
| 246 |
'CAD': "$", |
| 247 |
'CZK': "Kč", |
| 248 |
'DKK': "kr", |
| 249 |
'EUR': "€", |
| 250 |
'HKD': "$", |
| 251 |
'HUF': "ft", |
| 252 |
'ILS': "₪", |
| 253 |
'JPY': "¥", |
| 254 |
'MXN': "$", |
| 255 |
'NOK': "kr", |
| 256 |
'NZD': "$", |
| 257 |
'PHP': "₱", |
| 258 |
'PLN': "zł", |
| 259 |
'GBP': "£", |
| 260 |
'RUB': "₽", |
| 261 |
'SGD': "$", |
| 262 |
'SEK': "kr", |
| 263 |
'CHF': "CHF", |
| 264 |
'TWD': "$", |
| 265 |
'THB': "฿", |
| 266 |
'TRY': "₺", |
| 267 |
'USD': "$", |
| 268 |
}; |
| 269 |
|
| 270 |
if(currency in currencyList){ |
| 271 |
currencySymbol = currencyList[currency]; |
| 272 |
} |
| 273 |
|
| 274 |
return currencySymbol; |
| 275 |
} |
| 276 |
//Better Payment Functions Ends |
| 277 |
|
| 278 |
//Helper Functions Starts |
| 279 |
function validateEmail (email) { |
| 280 |
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email) |
| 281 |
} |
| 282 |
|
| 283 |
function validateForm(formSelector, fields, fieldsToExclude){ |
| 284 |
let data = {}; |
| 285 |
|
| 286 |
data.status = true; |
| 287 |
data.errors = []; |
| 288 |
|
| 289 |
for (var key in fields) { |
| 290 |
if (fields.hasOwnProperty(key)) { |
| 291 |
|
| 292 |
if (fieldsToExclude.indexOf(key) === -1) { |
| 293 |
let validatedData = validateField(formSelector[key], fields[key]); |
| 294 |
if(!validatedData.status){ |
| 295 |
data.errors.push(typeof validatedData.error !== 'undefined' ? validatedData.error : `${key} field is required!`); |
| 296 |
data.status = false; |
| 297 |
return data; |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
return data; |
| 305 |
} |
| 306 |
|
| 307 |
function validateField(fieldSelector, fieldValue, errorMessage=''){ |
| 308 |
let fieldRequired = isFieldRequired(fieldSelector); |
| 309 |
let data = {}; |
| 310 |
|
| 311 |
data.error = ''; |
| 312 |
data.status = true; |
| 313 |
|
| 314 |
if(fieldSelector.length > 1){ |
| 315 |
// Multiple Fields/Nodes |
| 316 |
return data; |
| 317 |
} |
| 318 |
|
| 319 |
if(fieldRequired){ |
| 320 |
let itemPlaceholder = fieldSelector.placeholder; |
| 321 |
itemPlaceholder = itemPlaceholder.replace(' *',''); |
| 322 |
|
| 323 |
let error = errorMessage ? errorMessage : `${itemPlaceholder} field is required!`; |
| 324 |
if( typeof fieldValue === 'undefined' || fieldValue === ''){ |
| 325 |
data.error = error; |
| 326 |
data.status = false; |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
return data; |
| 331 |
} |
| 332 |
|
| 333 |
function isFieldRequired(fieldSelector){ |
| 334 |
let fieldRequired = false; |
| 335 |
|
| 336 |
if(fieldSelector.length > 1){ |
| 337 |
//Multiple Fields/Nodes |
| 338 |
return true; |
| 339 |
} |
| 340 |
|
| 341 |
//if required attribute has value rather than null or '' |
| 342 |
fieldRequired = !( fieldSelector.getAttribute('required') === null || fieldSelector.getAttribute('required') === '' ); |
| 343 |
return fieldRequired; |
| 344 |
} |
| 345 |
|
| 346 |
function toasterOptions() { |
| 347 |
toastr.options = { |
| 348 |
"timeOut": "2000", |
| 349 |
toastClass: "font-size-md", |
| 350 |
maxOpened: 1 |
| 351 |
}; |
| 352 |
}; |
| 353 |
toasterOptions(); |
| 354 |
//Helper Functions Ends |
| 355 |
|
| 356 |
})(jQuery); |