| 1 |
/* global window, apple_pay_params */ |
| 2 |
(function($){ |
| 3 |
|
| 4 |
var $apple_pay_button = $('apple-pay-button') |
| 5 |
var session = null; |
| 6 |
var apple_pay = { |
| 7 |
init: function () { |
| 8 |
$apple_pay_button = $('apple-pay-button') |
| 9 |
$apple_pay_button.on( |
| 10 |
'click', |
| 11 |
apple_pay.ProcessCheckout |
| 12 |
) |
| 13 |
}, |
| 14 |
ProcessCheckout: function (e) { |
| 15 |
e.preventDefault() |
| 16 |
e.stopImmediatePropagation() |
| 17 |
apple_pay.CreateSession() |
| 18 |
apple_pay.CancelOrder() |
| 19 |
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) |
| 25 |
}, |
| 26 |
OrdernPaymentCreated: function (response) { |
| 27 |
$('form.woocommerce-checkout').unblock() |
| 28 |
if ('success' !== response.result) { |
| 29 |
var error_messages = response.messages || '' |
| 30 |
apple_pay.SubmitError(error_messages) |
| 31 |
return; |
| 32 |
} |
| 33 |
$('.woocommerce-NoticeGroup-checkout, .woocommerce-error, .woocommerce-message').remove() |
| 34 |
apple_pay.BeginSession(response) |
| 35 |
}, |
| 36 |
SubmitError: function (error_message) { |
| 37 |
var parsedHtml = $.parseHTML(error_message, document, false); |
| 38 |
$('.woocommerce-NoticeGroup-checkout, .woocommerce-error, .woocommerce-message').remove() |
| 39 |
$('<div></div>') |
| 40 |
.addClass('woocommerce-NoticeGroup woocommerce-NoticeGroup-checkout') |
| 41 |
.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() |
| 45 |
apple_pay.ScrollToNotices() |
| 46 |
$(document.body).trigger('checkout_error') |
| 47 |
}, |
| 48 |
ScrollToNotices: function () { |
| 49 |
var scrollElement = $('.woocommerce-NoticeGroup-updateOrderReview, .woocommerce-NoticeGroup-checkout') |
| 50 |
if (!scrollElement.length) { |
| 51 |
scrollElement = $('.form.checkout') |
| 52 |
} |
| 53 |
$('html, body').animate({ |
| 54 |
scrollTop: (scrollElement.offset().top - 100) |
| 55 |
}, 500) |
| 56 |
}, |
| 57 |
CreateSession: function () { |
| 58 |
const request = { |
| 59 |
"countryCode": apple_pay_params.countryCode, |
| 60 |
"currencyCode": apple_pay_params.currencyCode, |
| 61 |
"merchantCapabilities": [ |
| 62 |
"supports3DS" |
| 63 |
], |
| 64 |
"supportedNetworks": [ |
| 65 |
"visa", |
| 66 |
"masterCard" |
| 67 |
], |
| 68 |
"total": { |
| 69 |
"label": "Apple Pay", |
| 70 |
"type": "final", |
| 71 |
"amount": apple_pay_params.total |
| 72 |
}, |
| 73 |
'applicationData': btoa(JSON.stringify({ |
| 74 |
'apple_pay_domain': apple_pay_params.apple_pay_domain |
| 75 |
})) |
| 76 |
} |
| 77 |
session = new ApplePaySession(3, request) |
| 78 |
}, |
| 79 |
BeginSession: function (response) { |
| 80 |
session.payment_id = response.payment_id |
| 81 |
session.order_id = response.order_id |
| 82 |
session.cancel_url = response.cancel_url |
| 83 |
session.return_url = response.return_url |
| 84 |
apple_pay.MerchantValidated(session, response.merchant_session) |
| 85 |
session.begin() |
| 86 |
}, |
| 87 |
MerchantValidated: function(session, merchant_session) { |
| 88 |
session.onvalidatemerchant = async event => { |
| 89 |
try { |
| 90 |
session.completeMerchantValidation(merchant_session) |
| 91 |
} catch (err) { |
| 92 |
alert(err) |
| 93 |
} |
| 94 |
} |
| 95 |
}, |
| 96 |
CancelOrder: function () { |
| 97 |
session.oncancel = event => { |
| 98 |
$('apple-pay-button').addClass("isDisabled") |
| 99 |
window.location = session.cancel_url |
| 100 |
} |
| 101 |
}, |
| 102 |
PaymentCompleted: function () { |
| 103 |
session.onpaymentauthorized = event => { |
| 104 |
$('apple-pay-button').addClass("isDisabled") |
| 105 |
jQuery.ajax({ |
| 106 |
url: apple_pay_params.ajax_url_applepay_update_payment, |
| 107 |
type: 'post', |
| 108 |
data: {'action': 'applepay_update_payment', 'post_type': 'POST', |
| 109 |
'payment_id': session.payment_id, 'payment_token': event.payment.token, 'order_id': session.order_id}, |
| 110 |
dataType: 'json', |
| 111 |
success:function(res) { |
| 112 |
var apple_pay_Session_status = ApplePaySession.STATUS_SUCCESS; |
| 113 |
if (res.data.result !== true) { |
| 114 |
apple_pay_Session_status = ApplePaySession.STATUS_FAILURE; |
| 115 |
} |
| 116 |
session.completePayment({"status": apple_pay_Session_status}) |
| 117 |
window.location = session.return_url |
| 118 |
}, |
| 119 |
error: function(err){ |
| 120 |
$('apple-pay-button').removeClass("isDisabled") |
| 121 |
}, |
| 122 |
}) |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
var applePaycontroller = function(){ |
| 128 |
if(jQuery("[name=payment_method]:checked").val() === "apple_pay"){ |
| 129 |
jQuery("[name=woocommerce_checkout_place_order]").prop("disabled", true); |
| 130 |
|
| 131 |
//enable buttons |
| 132 |
apple_pay.init(); |
| 133 |
}else{ |
| 134 |
jQuery("[name=woocommerce_checkout_place_order]").prop("disabled", false); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
jQuery(document).on("change click", "[name=payment_method]", applePaycontroller); |
| 139 |
|
| 140 |
$( document ).ajaxComplete(function() { |
| 141 |
applePaycontroller(); |
| 142 |
}); |
| 143 |
|
| 144 |
jQuery("[name=payment_method]").prop("checked", false); |
| 145 |
})(jQuery) |
| 146 |
|
| 147 |
|
| 148 |
|