| @@ -2,12 +2,16 @@ | ||
| 2 | 2 | (function($){ |
| 3 | 3 | |
| 4 | 4 | var $apple_pay_button = $('apple-pay-button') |
| 5 | 5 | var session = null; |
| 6 | + var is_checkout = apple_pay_params.is_checkout; | |
| 7 | + var is_order_pay = apple_pay_params.is_order_pay; | |
| 8 | + var $form = is_order_pay ? $('form#order_review') : $('form.woocommerce-checkout'); | |
| 6 | 9 | var apple_pay = { |
| 10 | + load_order_total: false, | |
| 7 | 11 | init: function () { |
| 8 | 12 | $apple_pay_button = $('apple-pay-button') |
| 9 | - $apple_pay_button.on( | |
| 13 | + $apple_pay_button.off('click', apple_pay.ProcessCheckout).on( | |
| 10 | 14 | 'click', |
| 11 | 15 | apple_pay.ProcessCheckout |
| 12 | 16 | ) |
| 13 | 17 | }, |
| @@ -16,16 +20,23 @@ | ||
| 16 | 20 | e.stopImmediatePropagation() |
| 17 | 21 | apple_pay.CreateSession() |
| 18 | 22 | apple_pay.CancelOrder() |
| 19 | 23 | apple_pay.PaymentCompleted() |
| 20 | - $('form.woocommerce-checkout').block({ message: null, overlayCSS: { background: '#fff', opacity: 0.6 } }) | |
| 21 | - $.post( | |
| 22 | - apple_pay_params.ajax_url_payplug_create_order, | |
| 23 | - $('form.woocommerce-checkout').serialize() | |
| 24 | - ).done(apple_pay.OrdernPaymentCreated) | |
| 24 | + $form.block({ message: null, overlayCSS: { background: '#fff', opacity: 0.6 } }) | |
| 25 | + var ajaxUrl = is_order_pay | |
| 26 | + ? apple_pay_params.ajax_url_payplug_apple_pay_create_order_pay | |
| 27 | + : apple_pay_params.ajax_url_payplug_create_order; | |
| 28 | + var postData = is_order_pay | |
| 29 | + ? { | |
| 30 | + order_id: apple_pay_params.order_pay_id, | |
| 31 | + order_key: apple_pay_params.order_pay_key, | |
| 32 | + 'woocommerce-process-checkout-nonce': apple_pay_params.wp_nonce | |
| 33 | + } | |
| 34 | + : $form.serialize(); | |
| 35 | + $.post(ajaxUrl, postData).done(apple_pay.OrdernPaymentCreated) | |
| 25 | 36 | }, |
| 26 | 37 | OrdernPaymentCreated: function (response) { |
| 27 | - $('form.woocommerce-checkout').unblock() | |
| 38 | + $form.unblock() | |
| 28 | 39 | if ('success' !== response.result) { |
| 29 | 40 | var error_messages = response.messages || '' |
| 30 | 41 | apple_pay.SubmitError(error_messages) |
| 31 | 42 | return; |
| @@ -38,11 +49,11 @@ | ||
| 38 | 49 | $('.woocommerce-NoticeGroup-checkout, .woocommerce-error, .woocommerce-message').remove() |
| 39 | 50 | $('<div></div>') |
| 40 | 51 | .addClass('woocommerce-NoticeGroup woocommerce-NoticeGroup-checkout') |
| 41 | 52 | .html(parsedHtml) |
| 42 | - .prependTo($('form.woocommerce-checkout')) | |
| 43 | - $('form.woocommerce-checkout').removeClass('processing').unblock() | |
| 44 | - $('form.woocommerce-checkout').find('.input-text, select, input:checkbox').trigger('validate').blur() | |
| 53 | + .prependTo($form) | |
| 54 | + $form.removeClass('processing').unblock() | |
| 55 | + $form.find('.input-text, select, input:checkbox').trigger('validate').blur() | |
| 45 | 56 | apple_pay.ScrollToNotices() |
| 46 | 57 | $(document.body).trigger('checkout_error') |
| 47 | 58 | }, |
| 48 | 59 | ScrollToNotices: function () { |
| @@ -61,11 +72,16 @@ | ||
| 61 | 72 | "merchantCapabilities": [ |
| 62 | 73 | "supports3DS" |
| 63 | 74 | ], |
| 64 | 75 | "supportedNetworks": [ |
| 76 | + "cartesBancaires", | |
| 65 | 77 | "visa", |
| 66 | 78 | "masterCard" |
| 67 | 79 | ], |
| 80 | + "supportedTypes": [ | |
| 81 | + "debit", | |
| 82 | + "credit" | |
| 83 | + ], | |
| 68 | 84 | "total": { |
| 69 | 85 | "label": "Apple Pay", |
| 70 | 86 | "type": "final", |
| 71 | 87 | "amount": apple_pay_params.total |
| @@ -73,9 +89,9 @@ | ||
| 73 | 89 | 'applicationData': btoa(JSON.stringify({ |
| 74 | 90 | 'apple_pay_domain': apple_pay_params.apple_pay_domain |
| 75 | 91 | })) |
| 76 | 92 | } |
| 77 | - session = new ApplePaySession(3, request) | |
| 93 | + session = new ApplePaySession(4, request) | |
| 78 | 94 | }, |
| 79 | 95 | BeginSession: function (response) { |
| 80 | 96 | session.payment_id = response.payment_id |
| 81 | 97 | session.order_id = response.order_id |
| @@ -88,9 +104,9 @@ | ||
| 88 | 104 | session.onvalidatemerchant = async event => { |
| 89 | 105 | try { |
| 90 | 106 | session.completeMerchantValidation(merchant_session) |
| 91 | 107 | } catch (err) { |
| 92 | - alert(err) | |
| 108 | + apple_pay.CancelOrder(); | |
| 93 | 109 | } |
| 94 | 110 | } |
| 95 | 111 | }, |
| 96 | 112 | CancelOrder: function () { |
| @@ -120,28 +136,66 @@ | ||
| 120 | 136 | $('apple-pay-button').removeClass("isDisabled") |
| 121 | 137 | }, |
| 122 | 138 | }) |
| 123 | 139 | } |
| 140 | + }, | |
| 141 | + | |
| 142 | + getOrderTotals(){ | |
| 143 | + // Avoid updating shipping costs when loading page | |
| 144 | + if(!apple_pay.load_order_total){ | |
| 145 | + apple_pay.load_order_total = true; | |
| 146 | + return false; | |
| 147 | + } | |
| 148 | + $('apple-pay-button').addClass("isDisabled") | |
| 149 | + jQuery.post( | |
| 150 | + apple_pay_params.ajax_url_applepay_get_order_totals | |
| 151 | + ).done(function(results){ | |
| 152 | + if(results.success){ | |
| 153 | + apple_pay_params.total = results.data; | |
| 154 | + } else { | |
| 155 | + window.location.reload(); | |
| 156 | + return false; | |
| 157 | + } | |
| 158 | + | |
| 159 | + $('apple-pay-button').removeClass("isDisabled") | |
| 160 | + }).fail( function() { | |
| 161 | + window.location.reload(); | |
| 162 | + return false; | |
| 163 | + }) | |
| 124 | 164 | } |
| 125 | 165 | } |
| 126 | 166 | |
| 127 | 167 | var applePaycontroller = function(){ |
| 168 | + // On the order-pay page, the submit button only has id="place_order" (no | |
| 169 | + // name="woocommerce_checkout_place_order"), so both selectors are needed to | |
| 170 | + // disable it on both the regular checkout and the order-pay page. | |
| 128 | 171 | if(jQuery("[name=payment_method]:checked").val() === "apple_pay"){ |
| 129 | - jQuery("[name=woocommerce_checkout_place_order]").prop("disabled", true); | |
| 172 | + jQuery("#place_order, [name=woocommerce_checkout_place_order]").prop("disabled", true); | |
| 130 | 173 | |
| 131 | 174 | //enable buttons |
| 132 | 175 | apple_pay.init(); |
| 133 | 176 | }else{ |
| 134 | - jQuery("[name=woocommerce_checkout_place_order]").prop("disabled", false); | |
| 177 | + jQuery("#place_order, [name=woocommerce_checkout_place_order]").prop("disabled", false); | |
| 135 | 178 | } |
| 136 | 179 | } |
| 137 | 180 | |
| 138 | - jQuery(document).on("change click", "[name=payment_method]", applePaycontroller); | |
| 181 | + if( is_checkout ){ | |
| 182 | + jQuery(document).on("change click", "[name=payment_method]", applePaycontroller); | |
| 139 | 183 | |
| 140 | - $( document ).ajaxComplete(function() { | |
| 141 | - applePaycontroller(); | |
| 142 | - }); | |
| 184 | + $( document ).ajaxComplete(function() { | |
| 185 | + applePaycontroller(); | |
| 186 | + }); | |
| 143 | 187 | |
| 144 | - jQuery("[name=payment_method]").prop("checked", false); | |
| 188 | + jQuery("[name=payment_method]").prop("checked", false); | |
| 189 | + | |
| 190 | + //GET ORDER TOTALS ON SHIPPING METHOD SELECTION | |
| 191 | + jQuery( 'body' ).on( 'updated_checkout', function() { | |
| 192 | + apple_pay.getOrderTotals(); | |
| 193 | + }) | |
| 194 | + } | |
| 195 | + | |
| 196 | + | |
| 145 | 197 | })(jQuery) |
| 198 | + | |
| 199 | + | |
| 146 | 200 | |
| 147 | 201 | |