PluginProbe
PayPlug for WooCommerce (Official) / 1.0.17
PayPlug for WooCommerce (Official) v1.0.17
3.1.0 3.0.0 2.18.0 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 1.0.22 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.1.0 1.10.0 1.10.1 1.2.1 1.2.10 1.2.11 1.2.2 1.2.3 1.2.4 1.2.5 All 102 releases
payplug / assets / js / payplug-checkout.js

payplug-checkout.js in PayPlug for WooCommerce (Official) 1.0.17, at assets/js/payplug-checkout.js

128 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 var parsedHtml = $.parseHTML(error_message, document, false);
104 $('.woocommerce-NoticeGroup-checkout, .woocommerce-error, .woocommerce-message').remove();
105 $('<div></div>')
106 .addClass('woocommerce-NoticeGroup woocommerce-NoticeGroup-checkout')
107 .html( parsedHtml )
108 .prependTo( payplug_checkout.$form );
109 payplug_checkout.$form.removeClass('processing').unblock();
110 payplug_checkout.$form.find('.input-text, select, input:checkbox').trigger('validate').blur();
111 payplug_checkout.scroll_to_notices();
112 $(document.body).trigger('checkout_error');
113 },
114 scroll_to_notices: function () {
115 var scrollElement = $('.woocommerce-NoticeGroup-updateOrderReview, .woocommerce-NoticeGroup-checkout');
116
117 if (!scrollElement.length) {
118 scrollElement = $('.form.checkout');
119 }
120
121 $('html, body').animate({
122 scrollTop: (scrollElement.offset().top - 100)
123 }, 500);
124 }
125 };
126
127 payplug_checkout.init();
128 })(jQuery);