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