| 1 |
/* global window, apple_pay_params */ |
| 2 |
(function($){ |
| 3 |
|
| 4 |
var $apple_pay_button = $('apple-pay-button') |
| 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'); |
| 9 |
var apple_pay = { |
| 10 |
load_order_total: false, |
| 11 |
init: function () { |
| 12 |
$apple_pay_button = $('apple-pay-button') |
| 13 |
$apple_pay_button.off('click', apple_pay.ProcessCheckout).on( |
| 14 |
'click', |
| 15 |
apple_pay.ProcessCheckout |
| 16 |
) |
| 17 |
}, |
| 18 |
ProcessCheckout: function (e) { |
| 19 |
e.preventDefault() |
| 20 |
e.stopImmediatePropagation() |
| 21 |
apple_pay.CreateSession() |
| 22 |
apple_pay.CancelOrder() |
| 23 |
apple_pay.PaymentCompleted() |
| 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) |
| 36 |
}, |
| 37 |
OrdernPaymentCreated: function (response) { |
| 38 |
$form.unblock() |
| 39 |
if ('success' !== response.result) { |
| 40 |
var error_messages = response.messages || '' |
| 41 |
apple_pay.SubmitError(error_messages) |
| 42 |
return; |
| 43 |
} |
| 44 |
$('.woocommerce-NoticeGroup-checkout, .woocommerce-error, .woocommerce-message').remove() |
| 45 |
apple_pay.BeginSession(response) |
| 46 |
}, |
| 47 |
SubmitError: function (error_message) { |
| 48 |
var parsedHtml = $.parseHTML(error_message, document, false); |
| 49 |
$('.woocommerce-NoticeGroup-checkout, .woocommerce-error, .woocommerce-message').remove() |
| 50 |
$('<div></div>') |
| 51 |
.addClass('woocommerce-NoticeGroup woocommerce-NoticeGroup-checkout') |
| 52 |
.html(parsedHtml) |
| 53 |
.prependTo($form) |
| 54 |
$form.removeClass('processing').unblock() |
| 55 |
$form.find('.input-text, select, input:checkbox').trigger('validate').blur() |
| 56 |
apple_pay.ScrollToNotices() |
| 57 |
$(document.body).trigger('checkout_error') |
| 58 |
}, |
| 59 |
ScrollToNotices: function () { |
| 60 |
var scrollElement = $('.woocommerce-NoticeGroup-updateOrderReview, .woocommerce-NoticeGroup-checkout') |
| 61 |
if (!scrollElement.length) { |
| 62 |
scrollElement = $('.form.checkout') |
| 63 |
} |
| 64 |
$('html, body').animate({ |
| 65 |
scrollTop: (scrollElement.offset().top - 100) |
| 66 |
}, 500) |
| 67 |
}, |
| 68 |
CreateSession: function () { |
| 69 |
const request = { |
| 70 |
"countryCode": apple_pay_params.countryCode, |
| 71 |
"currencyCode": apple_pay_params.currencyCode, |
| 72 |
"merchantCapabilities": [ |
| 73 |
"supports3DS" |
| 74 |
], |
| 75 |
"supportedNetworks": [ |
| 76 |
"cartesBancaires", |
| 77 |
"visa", |
| 78 |
"masterCard" |
| 79 |
], |
| 80 |
"supportedTypes": [ |
| 81 |
"debit", |
| 82 |
"credit" |
| 83 |
], |
| 84 |
"total": { |
| 85 |
"label": "Apple Pay", |
| 86 |
"type": "final", |
| 87 |
"amount": apple_pay_params.total |
| 88 |
}, |
| 89 |
'applicationData': btoa(JSON.stringify({ |
| 90 |
'apple_pay_domain': apple_pay_params.apple_pay_domain |
| 91 |
})) |
| 92 |
} |
| 93 |
session = new ApplePaySession(4, request) |
| 94 |
}, |
| 95 |
BeginSession: function (response) { |
| 96 |
session.payment_id = response.payment_id |
| 97 |
session.order_id = response.order_id |
| 98 |
session.cancel_url = response.cancel_url |
| 99 |
session.return_url = response.return_url |
| 100 |
apple_pay.MerchantValidated(session, response.merchant_session) |
| 101 |
session.begin() |
| 102 |
}, |
| 103 |
MerchantValidated: function(session, merchant_session) { |
| 104 |
session.onvalidatemerchant = async event => { |
| 105 |
try { |
| 106 |
session.completeMerchantValidation(merchant_session) |
| 107 |
} catch (err) { |
| 108 |
apple_pay.CancelOrder(); |
| 109 |
} |
| 110 |
} |
| 111 |
}, |
| 112 |
CancelOrder: function () { |
| 113 |
session.oncancel = event => { |
| 114 |
$('apple-pay-button').addClass("isDisabled") |
| 115 |
window.location = session.cancel_url |
| 116 |
} |
| 117 |
}, |
| 118 |
PaymentCompleted: function () { |
| 119 |
session.onpaymentauthorized = event => { |
| 120 |
$('apple-pay-button').addClass("isDisabled") |
| 121 |
jQuery.ajax({ |
| 122 |
url: apple_pay_params.ajax_url_applepay_update_payment, |
| 123 |
type: 'post', |
| 124 |
data: {'action': 'applepay_update_payment', 'post_type': 'POST', |
| 125 |
'payment_id': session.payment_id, 'payment_token': event.payment.token, 'order_id': session.order_id}, |
| 126 |
dataType: 'json', |
| 127 |
success:function(res) { |
| 128 |
var apple_pay_Session_status = ApplePaySession.STATUS_SUCCESS; |
| 129 |
if (res.data.result !== true) { |
| 130 |
apple_pay_Session_status = ApplePaySession.STATUS_FAILURE; |
| 131 |
} |
| 132 |
session.completePayment({"status": apple_pay_Session_status}) |
| 133 |
window.location = session.return_url |
| 134 |
}, |
| 135 |
error: function(err){ |
| 136 |
$('apple-pay-button').removeClass("isDisabled") |
| 137 |
}, |
| 138 |
}) |
| 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 |
}) |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 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. |
| 171 |
if(jQuery("[name=payment_method]:checked").val() === "apple_pay"){ |
| 172 |
jQuery("#place_order, [name=woocommerce_checkout_place_order]").prop("disabled", true); |
| 173 |
|
| 174 |
//enable buttons |
| 175 |
apple_pay.init(); |
| 176 |
}else{ |
| 177 |
jQuery("#place_order, [name=woocommerce_checkout_place_order]").prop("disabled", false); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
if( is_checkout ){ |
| 182 |
jQuery(document).on("change click", "[name=payment_method]", applePaycontroller); |
| 183 |
|
| 184 |
$( document ).ajaxComplete(function() { |
| 185 |
applePaycontroller(); |
| 186 |
}); |
| 187 |
|
| 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 |
|
| 197 |
})(jQuery) |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|