File "applepay.min.js"
Full Path: /home/stylijtl/public_html/wp-content/plugins/wpforms-lite/assets/js/integrations/paypal-commerce/payment-methods/applepay.min.js
File size: 3.51 KB
MIME-type: text/plain
Charset: utf-8
((r,a,e)=>{let n=[];class i extends WPFormsPaypalCommercePaymentHandler.PaymentHandler{constructor(){super(),this.session=null}async initPaymentButton(){var t=this.getPayPalSDK();if(void 0!==t&&void 0!==t.Applepay){var a=this.$form.find(".wpforms-paypal-commerce-applepay-button");if(a.length)try{let e=await this.isAvailable(t.Applepay());var o=r.createElement("apple-pay-button");o.setAttribute("buttonstyle",this.formOptions.applepay.buttonColor),o.setAttribute("type","plain"),o.setAttribute("locale",wpforms_paypal_commerce.locale||"en"),o.addEventListener("click",()=>{this.validateBeforePayment().then(()=>{this.startApplePaySession(e)}).catch(()=>{})}),a.removeClass("wpforms-hidden"),a[0].appendChild(o)}catch(e){console.error("Apple Pay availability check error:",e)}}}async isAvailable(e){if(!e)throw new Error("Empty Apple Pay instance provided");if(void 0===a.ApplePaySession)throw new Error("This device does not support Apple Pay");if(!a.ApplePaySession.canMakePayments())throw new Error("This device is not capable of making Apple Pay payments");e=await e.config();if(e.isEligible)return e;throw new Error("Merchant is not eligible for Apple Pay")}buildPaymentRequest(e,t){var a=wpforms.getCurrency();return{countryCode:e.countryCode,merchantCapabilities:e.merchantCapabilities,supportedNetworks:e.supportedNetworks,currencyCode:a.code,requiredBillingContactFields:["postalAddress"],total:{label:wpforms_paypal_commerce.total_price_label,type:"final",amount:t}}}startApplePaySession(e){var t=String(wpforms.amountTotalCalc(this.$form)),e=this.buildPaymentRequest(e,t);this.session=new a.ApplePaySession(4,e),this.session.onvalidatemerchant=e=>{this.onValidateMerchant(e)},this.session.onpaymentauthorized=e=>{this.onPaymentAuthorized(e)},this.session.oncancel=()=>{console.log("Apple Pay payment was canceled")},this.session.begin()}onValidateMerchant(e){var t=this.getPayPalSDK().Applepay(),a=this.getFormOptions();t.validateMerchant({validationUrl:e.validationURL,displayName:a.merchant_name||"My Store"}).then(e=>{this.session.completeMerchantValidation(e.merchantSession)}).catch(e=>{console.error("Merchant validation failed:",e),this.session.abort()})}onPaymentAuthorized(e){this.processPayment(e.payment).then(()=>{this.session.completePayment(ApplePaySession.STATUS_SUCCESS)}).catch(()=>{this.session.completePayment(ApplePaySession.STATUS_FAILURE)})}async processPayment(e){try{var t=await WPFormsPaypalCommerce.createSingleOrder(this.$form,"apple_pay");return await this.confirmPayment(t,e),WPFormsPaypalCommerce.submitPaymentForm(this.$form,t,this.type,"apple_pay"),Promise.resolve()}catch(e){throw console.error("Apple Pay payment processing error:",e),this.showError(wpforms_paypal_commerce.i18n.on_error),e}}async confirmPayment(e,t){if("APPROVED"!==(await this.getPayPalSDK().Applepay().confirmOrder({orderId:e,token:t.token,billingContact:t.billingContact,shippingContact:t.shippingContact}))?.approveApplePayPayment?.status)throw new Error("Payment not approved");return Promise.resolve()}static checkApplePayAPI(){return void 0!==a.ApplePaySession}}let t={init(){e(r).on("wpformsPayPalCommerceLoadedSDK",t.initFromQueue),e(r).on("wpformsPayPalCommerceInit",t.queueOrInit)},queueOrInit(e,t,a,o){var r;"single"===o&&((r=new i).init(t,wpforms_paypal_commerce.payment_options[a],o),i.checkApplePayAPI()?r.initPaymentButton():n.push(r))},initFromQueue(e,{handle:t}){"wpforms-paypal-commerce-apple-pay-sdk"===t&&(i.checkApplePayAPI()?WPFormsPaypalCommercePaymentHandler.processQueue(n):console.error("Apple Pay SDK not loaded"))}};t.init()})(document,window,jQuery);