document.addEventListener("DOMContentLoaded", () => { if(typeof ApplePaySession === 'undefined'){ return; } // eslint-disable-next-line no-undef if(ApplePaySession?.supportsVersion(4) && ApplePaySession?.canMakePayments()) { init_applepay_checkout().catch(console.error); } }); async function init_applepay( ){ // eslint-disable-next-line no-undef if(ApplePaySession?.supportsVersion(4) && ApplePaySession?.canMakePayments()) { init_applepay_checkout().catch(console.error); } } async function init_applepay_checkout() { const applepay = paypal.Applepay(); const { isEligible, countryCode, currencyCode, merchantCapabilities, supportedNetworks, } = await applepay.config(); if (!isEligible) { throw new Error("applepay is not eligible"); } document.getElementById("applepay-button-container").innerHTML = ''; document.getElementById("btn-appl").addEventListener("click", applepay_button_click); async function applepay_button_click() { //console.log({ merchantCapabilities, currencyCode, supportedNetworks }) /* const paymentRequest = { countryCode, currencyCode: 'USD', merchantCapabilities, supportedNetworks, requiredBillingContactFields: [ "name", "phone", "email", "postalAddress", ], requiredShippingContactFields: [], total: { label: "Demo (Card is not charged)", amount: "10.00", type: "final", }, }; */ paymentRequest = { countryCode: 'US', currencyCode: 'USD', merchantCapabilities, supportedNetworks, requiredBillingContactFields: [ "name", "phone", "email", "postalAddress", ], requiredShippingContactFields: [] }; //console.log(typeof paymentRequest); if(apple_pay_transaction_info && apple_pay_transaction_info != ''){ paymentRequest = $.extend(paymentRequest, apple_pay_transaction_info); } //console.log(paymentRequest); // eslint-disable-next-line no-undef let session = new ApplePaySession(4, paymentRequest); session.onvalidatemerchant = (event) => { applepay.validateMerchant({ validationUrl: event.validationURL, }).then((payload) => { session.completeMerchantValidation(payload.merchantSession); }).catch((err) => { console.error(err); session.abort(); }); }; session.onpaymentmethodselected = () => { session.completePaymentMethodSelection({ newTotal: paymentRequest.total, }); }; session.onpaymentauthorized = async (event) => { try { /* Create Order on the Server Side */ const orderResponse = await fetch('/paypal_checkout.php',{ method:'POST', body: JSON.stringify({ rt: 'create', paymentSource: 'applepay', }), headers : { 'Content-Type': 'application/json' } }) if(!orderResponse.ok) { msg = encodeURI('An error occurred while trying to process your transaction (code: ap-ocr).'); window.location = '/cart/checkout?cart_action=checkout&msg=' + msg; } const { id } = await orderResponse.json() //console.log({ id }) /** * Confirm Payment */ await applepay.confirmOrder({ orderId: id, token: event.payment.token, billingContact: event.payment.billingContact, shippingContact: event.payment.shippingContact }); /* * Capture order (must currently be made on server) */ await fetch('/paypal_checkout.php', { method: 'POST', body: JSON.stringify({ rt: 'capture', paypal_order_id: id, }), }); session.completePayment({ status: window.ApplePaySession.STATUS_SUCCESS, }); window.location = '/cart/thank-you?cart_action=paypal_confirm'; } catch (err) { console.error(err); session.completePayment({ status: window.ApplePaySession.STATUS_FAILURE, }); msg = encodeURI('An error occurred while trying to process your transaction (code: ap-oca).'); window.location = '/cart/checkout?cart_action=checkout&msg=' + msg; } }; session.oncancel = () => { console.log("Apple Pay Cancelled !!") } session.begin(); } }