| 1 |
/* global payplug_checkout_params */ |
| 2 |
|
| 3 |
(function ($) { |
| 4 |
|
| 5 |
/** |
| 6 |
* For now we need to redefine the `_closeIframe` from the Payplug object |
| 7 |
* to trigger an event when it run. This allow us to redirect the user to |
| 8 |
* the cancelURL manually. |
| 9 |
*/ |
| 10 |
if (typeof Payplug != 'undefined') { |
| 11 |
Payplug._closeIframe = function (callback) { |
| 12 |
var node = document.getElementById("payplug-spinner"); |
| 13 |
if (node) { |
| 14 |
node.style.display = "none"; |
| 15 |
node.parentNode.removeChild(node); |
| 16 |
} |
| 17 |
node = document.getElementById("wrapper-payplug-iframe"); |
| 18 |
if (node) { |
| 19 |
this._fadeOut(node, function () { |
| 20 |
if (callback) { |
| 21 |
callback(); |
| 22 |
} |
| 23 |
}); |
| 24 |
} |
| 25 |
// Hard Remove iframe |
| 26 |
node.parentNode.removeChild(node); |
| 27 |
node = document.getElementById("iframe-payplug-close"); |
| 28 |
if (node && node.parentNode) { |
| 29 |
node.parentNode.removeChild(node); |
| 30 |
} |
| 31 |
|
| 32 |
$(document).trigger('payplugIframeClosed'); |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
var payplug_checkout = { |
| 37 |
init: function () { |
| 38 |
if ($('form.woocommerce-checkout').length) { |
| 39 |
this.$form = $('form.woocommerce-checkout'); |
| 40 |
this.$form.on( |
| 41 |
'submit', |
| 42 |
this.onSubmit |
| 43 |
) |
| 44 |
} |
| 45 |
|
| 46 |
if ($('form#order_review').length) { |
| 47 |
this.$form = $('form#order_review'); |
| 48 |
this.$form.on( |
| 49 |
'submit', |
| 50 |
this.onSubmit |
| 51 |
) |
| 52 |
} |
| 53 |
|
| 54 |
$(document).on('payplugIframeClosed', this.handleClosedIframe); |
| 55 |
}, |
| 56 |
onSubmit: function (e) { |
| 57 |
if (!payplug_checkout.isPayplugChosen()) { |
| 58 |
return; |
| 59 |
} |
| 60 |
|
| 61 |
// Use standard checkout process if a payment token has been |
| 62 |
// choose by a user. |
| 63 |
if (payplug_checkout.isPaymentTokenSelected()) { |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
//Prevent submit and stop all other listeners from being triggered. |
| 68 |
e.preventDefault(); |
| 69 |
e.stopImmediatePropagation(); |
| 70 |
|
| 71 |
payplug_checkout.$form.block({message: null, overlayCSS: {background: '#fff', opacity: 0.6}}); |
| 72 |
|
| 73 |
$.post( |
| 74 |
payplug_checkout_params.ajax_url, |
| 75 |
payplug_checkout.$form.serialize() |
| 76 |
).done(payplug_checkout.openModal); |
| 77 |
}, |
| 78 |
openModal: function (response) { |
| 79 |
payplug_checkout.$form.unblock(); |
| 80 |
|
| 81 |
if ('success' !== response.result) { |
| 82 |
var error_messages = response.messages || ''; |
| 83 |
payplug_checkout.submit_error(error_messages); |
| 84 |
return; |
| 85 |
} |
| 86 |
|
| 87 |
payplug_checkout.cancelUrl = response.cancel || false; |
| 88 |
Payplug.showPayment(response.redirect); |
| 89 |
}, |
| 90 |
handleClosedIframe: function () { |
| 91 |
if (payplug_checkout.cancelUrl) { |
| 92 |
window.location.href = payplug_checkout.cancelUrl; |
| 93 |
} |
| 94 |
}, |
| 95 |
isPayplugChosen: function () { |
| 96 |
return $('#payment_method_payplug').is(':checked'); |
| 97 |
}, |
| 98 |
isPaymentTokenSelected: function () { |
| 99 |
var token = $('input[name=wc-payplug-payment-token]:checked'); |
| 100 |
return token.length > 0 && 'new' !== token.val(); |
| 101 |
}, |
| 102 |
submit_error: function (error_message) { |
| 103 |
$('.woocommerce-NoticeGroup-checkout, .woocommerce-error, .woocommerce-message').remove(); |
| 104 |
payplug_checkout.$form.prepend('<div class="woocommerce-NoticeGroup woocommerce-NoticeGroup-checkout">' + error_message + '</div>'); |
| 105 |
payplug_checkout.$form.removeClass('processing').unblock(); |
| 106 |
payplug_checkout.$form.find('.input-text, select, input:checkbox').trigger('validate').blur(); |
| 107 |
payplug_checkout.scroll_to_notices(); |
| 108 |
$(document.body).trigger('checkout_error'); |
| 109 |
}, |
| 110 |
scroll_to_notices: function () { |
| 111 |
var scrollElement = $('.woocommerce-NoticeGroup-updateOrderReview, .woocommerce-NoticeGroup-checkout'); |
| 112 |
|
| 113 |
if (!scrollElement.length) { |
| 114 |
scrollElement = $('.form.checkout'); |
| 115 |
} |
| 116 |
|
| 117 |
$('html, body').animate({ |
| 118 |
scrollTop: (scrollElement.offset().top - 100) |
| 119 |
}, 500); |
| 120 |
} |
| 121 |
}; |
| 122 |
|
| 123 |
payplug_checkout.init(); |
| 124 |
})(jQuery); |