1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
| var stripe = null;
var cardElement = null;
var app = {
/*
* init is where we initialize everything
*/
init: function() {
this.addValidation();
this.bootstrap();
this.tableSearch();
this.paymentPage();
this.checkNotification();
},
/******************************************************
*
******************************************************/
/*
* setup our payment page
*/
paymentPage: function() {
if($('.publishable-key').val() != ''){
stripe=Stripe($('.publishable-key').val());
var elements = stripe.elements();
cardElement=elements.create('card');
cardElement.mount('#card-element');
}
$('input:text:visible:first').focus();
// show hide the recurring notice
$('input[name="payment_type"]').on('change', function() {
var type = $('input[name="payment_type"]:checked').val();
if ( type == 'recurring' ) {
$('.recurring-alert').show();
var enableSubscriptions = $('.enable-subscriptions').val();
if ( enableSubscriptions == 'stripe_only' ) {
$('input[name="payment_method"][value="creditcard"]').click();
$('input[name="payment_method"][value="paypal"]').prop('disabled', true);
}
if ( enableSubscriptions == 'paypal_only' ) {
$('input[name="payment_method"][value="paypal"]').click();
$('input[name="payment_method"][value="creditcard"]').prop('disabled', true);
}
} else {
$('.recurring-alert').hide();
$('input[name="payment_method"]').prop('disabled', false);
}
});
// show hide the proper payment details
$('input[name="payment_method"]').on('change', function() {
var method = $('input[name="payment_method"]:checked').val();
$('.stripe-length-text, .paypal-length-text').hide();
if ( method == 'paypal' ) {
$('.paypal-content').show();
$('.creditcard-content').hide();
$('.paypal-length-text').show();
} else {
$('.paypal-content').hide();
$('.creditcard-content').show();
$('.stripe-length-text').show();
}
});
// show hide the recurring notice
$('input[name="name"]').on('blur', function() {
$('input[data-stripe="name"]').val($(this).val());
});
// show proper card type image on keyup
$('.card-number').on('keyup', function() {
var number = $(this).val();
var type = app.getCardType(number);
$('.card-type-image').removeClass('visa mastercard amex discover none').addClass(type);
});
var updateTotal = function(total) {
if ( !isNaN(total) ) {
$('.submit-button .total span').html(total);
$('.submit-button .total').css('display', 'block');
} else {
$('.submit-button .total').css('display', 'none');
}
paymentIntent();
}
var paymentIntent = function(){
console.log("Payment intent called");
let vamount=0.00;
if($('select[name="item_id"]').length > 0){
vamount=parseFloat($('select[name="item_id"] option:selected').attr('data-price')).toFixed(2);
console.log(vamount);
}
if($('#order_form input[name="amount"]').length > 0){
vamount=parseFloat($('input[name="amount"]').val()).toFixed(2);
console.log("coiso tal");
}
if($('#invoice-amount').length > 0){
vamount=parseFloat($('#invoice-amount').text()).toFixed(2);
console.log("tal");
}
vamount=Math.round(vamount*100);
if(isNaN(vamount)){
vamount=0.00;
}
console.log(vamount);
let type=$('input[name=payment_type]:checked').val();
let url=$('#order_form').attr('action');
let data={
amount:vamount
};
if($("#payment_intentsk").val() !== ''){
data.payment_intentsk = $("#payment_intentsk").val();
}
$.post(url+"?action=create_intent", data , function(data, status){
if(status==="success"){
$("#payment_intentsk").val(data.message.client_secret);
}
});
}
if($('#invoice-amount').length > 0){
paymentIntent();
}
// show total amount on button
$('select[name="item_id"]').on('change', function() {
var total = parseFloat($('select[name="item_id"] option:selected').attr('data-price')).toFixed(2);
updateTotal(total);
});
$('input[name="amount"]').on('blur', function() {
var total = parseFloat($(this).val()).toFixed(2);
updateTotal(total);
});
// update paypal form on paypal button click
$('.paypal-button').on('click', function(e) {
e.preventDefault();
if ( $('#order_form').valid() ) {
// set button to loading
$('.paypal-button').button('loading');
// get amount and description
var amount = 0;
var description = '';
if ( $('#order_form select[name="item_id"]').length ) {
amount = $('#order_form select[name="item_id"] option:selected').attr('data-price');
description = $('#order_form select[name="item_id"] option:selected').attr('data-name');
} else if ( $('#order_form input[name="amount"]').length ) {
amount = $('#order_form input[name="amount"]').val();
description = $('#order_form textarea[name="description"]').val();
description = description ? description : 'PayPal Payment';
}
// serialize our form data
var formData = $('#order_form').serialize();
// remove undeeded data from string
formData = formData.replace(/(&?amount=[^&]*|&?description=[^&]*|&?action=[^&]*|&?payment_type=[^&]*|&?payment_method=[^&]*)/g, '');
formData = formData.replace(/^&/, '');
// add form data to custom input field
$('.paypal-form input[name="custom"]').val(formData);
// only update values if we dont' have invoice
if ( !$('input[name="invoice_id"]').length ) {
if ( $('input[name="payment_type"]:checked').val() == 'recurring' ) {
$('#paypal_form_recurring input[name="a3"]').val(amount);
$('#paypal_form_recurring input[name="item_name"]').val(description);
} else {
$('#paypal_form_one_time input[name="amount"]').val(amount);
$('#paypal_form_one_time input[name="item_name"]').val(description);
}
}
// submit proper form now
if ( $('input[name="payment_type"]:checked').val() == 'recurring' ) {
$('#paypal_form_recurring').submit();
} else {
$('#paypal_form_one_time').submit();
}
}
});
// show success message on paypal success return
if ( $.jGet('status') == 'paypal_success' ) {
app.response = 'Your PayPal payment has been received, you should receive a confirmation email shortly.';
$('.submit-button').button('complete');
setTimeout(function() {
$('.submit-button').prop('disabled', true).removeClass('btn-primary').addClass('btn-default colorsuccess');
app.showSuccess();
}, 10);
}
if ( $.jGet('status') == 'paypal_subscription_success' ) {
app.response = 'Your PayPal subscription has been created successfully, you should receive a confirmation email shortly.';
$('.submit-button').button('complete');
setTimeout(function() {
$('.submit-button').prop('disabled', true).removeClass('btn-primary').addClass('btn-default colorsuccess');
app.showSuccess();
}, 10);
}
},
/*
* setup our bootstrap functionality
*/
bootstrap: function() {
$('[data-toggle="tooltip"]').tooltip({html: true});
$('[data-toggle="popover"]').popover();
if ( $('.nav-tabs').length ) {
if ( $.jGet('tab') ) {
$('.nav-tabs a[href="#' + $.jGet('tab') + '"]').tab('show');
} else {
$('.nav-tabs a:first').tab('show');
if ( $('.nav-tabs.hash-tabs').length ) {
window.location.hash = '#tab=' + $('.nav-tabs.hash-tabs a:first').attr('href').substr(1);
}
}
$('.hash-tabs a[data-toggle="tab"]').off('shown.bs.tab').on('shown.bs.tab', function(e) {
window.location.hash = '#tab=' + e.target.hash.substr(1);
});
}
// show last settings pane if it's set
if ( localStorage.activePill ) {
$('.nav-pill-control > li').removeClass('active');
$('.nav-pill-control > li > a[href="' + localStorage.activePill + '"]').parent().addClass('active');
$('.nav-pill-pane').hide();
$(localStorage.activePill).show();
}
$('[data-hide]').on('click', function() {
if ( $(this).parent().hasClass('modal-header') ) {
$('#' + $(this).attr('data-hide')).modal('hide')
} else {
$(this).closest('.' + $(this).attr('data-hide')).hide();
}
});
$('#add_item, #create_invoice').on('shown.bs.modal', function(e) {
$(this).find('input:text:first').focus();
});
$('#edit_item').on('show.bs.modal', function(e) {
var $tr = $(e.relatedTarget).closest('tr');
$('#edit_item input[name="id"]').val($tr.attr('data-item-id'));
$('#edit_item input[name="name"]').val($tr.attr('data-item-name'));
$('#edit_item input[name="price"]').val($tr.attr('data-item-price'));
});
$('.datepicker').datepicker();
$('.confirm-click').on('click', function(e) {
e.preventDefault();
$this = $(this);
var text = $this.attr('data-text') ? $this.attr('data-text') : 'Yes, delete it!';
swal({
title: 'Are you sure?',
text: 'This action cannot be undone.',
type: 'warning',
showCancelButton: true,
confirmButtonClass: 'btn-danger',
confirmButtonText: text,
closeOnConfirm: false
}, function() {
window.location = $this.attr('href');
});
});
$('.nav-pill-control a').on('click', function(e) {
e.preventDefault();
$('.nav-pill-control > li').removeClass('active');
$(this).parent().addClass('active');
$('.nav-pill-pane').hide();
$($(this).attr('href')).show();
localStorage.activePill = $(this).attr('href');
});
$('.maxlength[maxlength]').maxlength({
alwaysShow: false,
threshold: 20,
showCharsTyped: true,
placement: 'bottom',
warningClass: 'label label-success',
limitReachedClass: 'label label-danger',
separator: ' of ',
validate: true
});
},
/*
* table search
*/
tableSearch: function() {
// live filter searching
$('input.filter').on('keyup', function() {
var $table = $(this).closest('.tab-pane').find('table');
var rex = new RegExp($(this).val(), 'i');
$table.find('tbody tr').hide();
$table.find('tbody tr').filter(function() {
return rex.test($(this).text());
}).show();
if ( $table.find('tbody tr:visible').length === 0 ) {
$table.find('tbody').next('tfoot').show();
} else {
$table.find('tbody').next('tfoot').hide();
}
});
},
/*
* Add our jquery form validation here
*/
addValidation: function() {
$('form.validate').each(function() {
$(this).validate({
errorClass: 'validate-error control-label',
validClass: 'validate-valid control-label',
ignore: 'select:hidden:not(.selectpicker), input:hidden, textarea:hidden',
errorPlacement: function(error, element) {
if ( element.is('input:checkbox') || element.is('input:radio') ) {
var lastElement = $('[name="' + element.attr('name') + '"]:last');
lastElement = element.parent().hasClass('icheck') ? lastElement.closest('label') : lastElement;
error.insertAfter(lastElement.parent().is('label') ? lastElement.parent() : lastElement);
} else {
if ( element.closest('.input-group').length == 1 ) {
error.insertAfter(element.closest('.input-group'));
} else {
error.insertAfter(element);
}
}
},
highlight: function(element, errorClass, validClass) {
if ( $(element).closest('.form-group').length == 1 ) {
$(element).closest('.form-group').addClass('has-error');
} else {
$(element).addClass('validate-error');
}
},
unhighlight: function(element, errorClass, validClass) {
if ( $(element).closest('.form-group').length == 1 ) {
$(element).closest('.form-group').removeClass('has-error');
} else {
$(element).removeClass('validate-error');
}
},
/*onkeyup: function(element, event) {
if ( !$(element).hasClass('check-email') ) {
$(element).valid();
}
},*/
onfocusout: function(element, event) {
if ( !$(element).hasClass('check-email') && $(element).attr('aria-invalid') ) {
$(element).valid();
}
},
invalidHandler: function(event, validator) {
// this fires if the form didn't pass validatation
},
submitHandler: function(form,event) {
event.preventDefault();
app.submitForm(form);
}
});
});
},
/*
* handle a form submission
*/
submitForm: function(form) {
$form = $(form);
// prevent disabled form from submission
if ( $form.hasClass('disabled') ) {
return false;
}
// check for paypal method
if ( $('input[name="payment_method"]:checked').val() == 'paypal' ) {
$('.paypal-button').click();
return false;
}
// set our button to loading state
$button = $form.find('.btn[data-loading-text]:visible:last');
$button.button('loading');
if ( $form.attr('id') == 'order_form' ) {
// hide errors first
$('.error-alert').hide();
let ptype=$('input[name=payment_type]:checked').val();
if(ptype==='recurring'){
stripe.createToken(cardElement).then(function(result) {
if (result.error) {
// Inform the customer that there was an error.
app.response = result.error.message;
app.showError();
$button.button('reset');
return false;
} else {
// Send the token to your server.
console.log(result);
$form.ajaxSubmit({
data: {
token:result.token,
source:result.token.id
},
beforeSubmit: function() {
},
error: function(jqXHR, textStatus, errorThrown) {
// set our response and show error
app.response = jqXHR.responseText;
app.showError();
// reset the button state now
$button.button('reset');
},
success: function(data) {
// set the global response value
app.response = data.message;
// reset the button state now
$button.button('reset');
if ( data.status ) {
// disable the current form and button
$('#order_form').addClass('disabled');
$button.button('complete');
setTimeout(function() {
$button.prop('disabled', true).removeClass('btn-primary').addClass('btn-default colorsuccess');
}, 10);
$('input[name="payment_method"]').prop('disabled', true);
app.showSuccess();
} else {
app.showError();
}
}
});
}
});
}
else {
let clientSecret=$("#payment_intentsk").val();
stripe.handleCardPayment(
clientSecret, cardElement, {
payment_method_data: {
billing_details: {name: $("input[name='name']").val()}
}
}
).then(function(result) {
if (result.error) {
app.response = result.error.message;
app.showError();
$button.button('reset');
return false;
} else {
$form.ajaxSubmit({
data: {
paymentintentid: result.paymentIntent.id
},
beforeSubmit: function() {
},
error: function(jqXHR, textStatus, errorThrown) {
// set our response and show error
app.response = jqXHR.responseText;
app.showError();
// reset the button state now
$button.button('reset');
},
success: function(data) {
// set the global response value
app.response = data.message;
// reset the button state now
$button.button('reset');
if ( data.status ) {
// disable the current form and button
$('#order_form').addClass('disabled');
$button.button('complete');
setTimeout(function() {
$button.prop('disabled', true).removeClass('btn-primary').addClass('btn-default colorsuccess');
}, 10);
$('input[name="payment_method"]').prop('disabled', true);
app.showSuccess();
} else {
app.showError();
}
}
});
}
});
}
} else if ( $form.attr('id') == 'install_form' ) {
// submit form now
$form.ajaxSubmit({
beforeSubmit: function() {
},
error: function(jqXHR, textStatus, errorThrown) {
// set our response and show error
app.response = jqXHR.responseText;
app.showError();
// reset the button state now
$button.button('reset');
},
success: function(data) {
if ( data.status ) {
$form.slideUp(function() {
$('.install-success').slideDown();
});
} else {
// reset button
$button.button('reset');
// show message
app.response = data.message;
app.showError();
}
}
});
} else {
form.submit();
}
return false;
},
showSuccess: function() {
swal({
title: 'Thank You!',
text: app.response,
type: 'success',
showCancelButton: true,
confirmButtonClass: 'btn-primary',
confirmButtonText: 'Make Another Payment',
cancelButtonText: 'Close',
closeOnConfirm: false,
closeOnCancel: true
},
function(isConfirm) {
if ( isConfirm ) {
url = document.URL.replace(/(\?|#).*/, '');
window.location = url;
}
});
},
showError: function() {
swal({
title: 'Oh Snap!',
text: app.response,
type: 'error',
showCancelButton: false,
confirmButtonClass: 'btn-default',
confirmButtonText: 'Close',
cancelButtonText: 'Close',
closeOnConfirm: true,
closeOnCancel: true
});
},
getCardType: function(number) {
var re = new RegExp('^4[0-9]');
if (number.match(re) != null) {
return 'visa';
}
re = new RegExp('^3[47][0-9]');
if (number.match(re) != null) {
return 'amex';
}
re = new RegExp('^5[1-5][0-9]');
if (number.match(re) != null) {
return 'mastercard';
}
re = new RegExp('^6(?:011|5[0-9]{2})[0-9]');
if (number.match(re) != null) {
return 'discover';
}
return 'none';
},
checkNotification: function() {
// check api for notifications if we're on admin page
if ( $('.notification-header').length && checkNotification ) {
$.ajax({
url: 'http://api.devinlewis.com/payment-system-notification',
data: {source: 'customer'},
dataType: 'jsonp',
jsonp: 'notificationCallback'
});
}
// disable the notification
$('.disable-notification').on('click', function(e) {
e.preventDefault();
$.ajax({
url: 'process.php?action=disable_notification'
});
$('.notification-header').remove();
});
},
response: ''
};
/*
* jsonp function to handle api callback
*/
var notificationCallback = function(data) {
if ( data.display ) {
if ( data.prevent_disable ) {
$('.disable-notification').hide();
}
$('.notification-alert').addClass(data.alert);
$('.notification-message').html(data.message);
$('.notification-header').show();
}
};
/*
* launch everything on document ready
*/
$(function() {
app.init();
}); |
Partager