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