| 1 |
/* global window, payplug_checkout_params */ |
| 2 |
|
| 3 |
(function ($) { |
| 4 |
|
| 5 |
var payplug_checkout = { |
| 6 |
init: function () { |
| 7 |
if ($('form.woocommerce-checkout').length) { |
| 8 |
this.$form = $('form.woocommerce-checkout'); |
| 9 |
this.$form.on( |
| 10 |
'submit', |
| 11 |
this.onSubmit |
| 12 |
) |
| 13 |
} |
| 14 |
|
| 15 |
if ($('form#order_review').length) { |
| 16 |
this.$form = $('form#order_review'); |
| 17 |
this.$form.on( |
| 18 |
'submit', |
| 19 |
this.onSubmit |
| 20 |
) |
| 21 |
} |
| 22 |
}, |
| 23 |
onSubmit: function (e) { |
| 24 |
if (!payplug_checkout.isPayplugChosen()) { |
| 25 |
return; |
| 26 |
} |
| 27 |
|
| 28 |
// Use standard checkout process if a payment token has been |
| 29 |
// choose by a user. |
| 30 |
if (payplug_checkout.isPaymentTokenSelected()) { |
| 31 |
return; |
| 32 |
} |
| 33 |
|
| 34 |
//Prevent submit and stop all other listeners from being triggered. |
| 35 |
e.preventDefault(); |
| 36 |
e.stopImmediatePropagation(); |
| 37 |
|
| 38 |
payplug_checkout.$form.block({message: null, overlayCSS: {background: '#fff', opacity: 0.6}}); |
| 39 |
|
| 40 |
$.post( |
| 41 |
payplug_checkout_params.ajax_url, |
| 42 |
payplug_checkout.$form.serialize() |
| 43 |
).done(payplug_checkout.openModal); |
| 44 |
}, |
| 45 |
openModal: function (response) { |
| 46 |
payplug_checkout.$form.unblock(); |
| 47 |
|
| 48 |
if ('success' !== response.result) { |
| 49 |
var error_messages = response.messages || ''; |
| 50 |
payplug_checkout.submit_error(error_messages); |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
// Set the cancel URL use by PayPlug js when the user close the embedded payment form. |
| 55 |
window.redirection_url = response.cancel || false; |
| 56 |
|
| 57 |
Payplug.showPayment(response.redirect); |
| 58 |
}, |
| 59 |
isPayplugChosen: function () { |
| 60 |
return $('#payment_method_payplug').is(':checked'); |
| 61 |
}, |
| 62 |
isPaymentTokenSelected: function () { |
| 63 |
var token = $('input[name=wc-payplug-payment-token]:checked'); |
| 64 |
return token.length > 0 && 'new' !== token.val(); |
| 65 |
}, |
| 66 |
submit_error: function (error_message) { |
| 67 |
var parsedHtml = $.parseHTML(error_message, document, false); |
| 68 |
$('.woocommerce-NoticeGroup-checkout, .woocommerce-error, .woocommerce-message').remove(); |
| 69 |
$('<div></div>') |
| 70 |
.addClass('woocommerce-NoticeGroup woocommerce-NoticeGroup-checkout') |
| 71 |
.html( parsedHtml ) |
| 72 |
.prependTo( payplug_checkout.$form ); |
| 73 |
payplug_checkout.$form.removeClass('processing').unblock(); |
| 74 |
payplug_checkout.$form.find('.input-text, select, input:checkbox').trigger('validate').blur(); |
| 75 |
payplug_checkout.scroll_to_notices(); |
| 76 |
$(document.body).trigger('checkout_error'); |
| 77 |
}, |
| 78 |
scroll_to_notices: function () { |
| 79 |
var scrollElement = $('.woocommerce-NoticeGroup-updateOrderReview, .woocommerce-NoticeGroup-checkout'); |
| 80 |
|
| 81 |
if (!scrollElement.length) { |
| 82 |
scrollElement = $('.form.checkout'); |
| 83 |
} |
| 84 |
|
| 85 |
$('html, body').animate({ |
| 86 |
scrollTop: (scrollElement.offset().top - 100) |
| 87 |
}, 500); |
| 88 |
} |
| 89 |
}; |
| 90 |
|
| 91 |
payplug_checkout.init(); |
| 92 |
})(jQuery); |
| 93 |
|