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