swissChili | f0cbdc3 | 2023-01-05 17:21:38 -0500 | [diff] [blame] | 1 | jQuery(function(i){"use strict";var o,p=Stripe(wc_stripe_payment_request_params.stripe.key,{locale:wc_stripe_payment_request_params.stripe.locale}),u={getAjaxURL:function(t){return wc_stripe_payment_request_params.ajax_url.toString().replace("%%endpoint%%","wc_stripe_"+t)},getCartDetails:function(){var t={security:wc_stripe_payment_request_params.nonce.payment};i.ajax({type:"POST",data:t,url:u.getAjaxURL("get_cart_details"),success:function(t){u.startPaymentRequest(t)}})},getAttributes:function(){var t=i(".variations_form").find(".variations select"),n={},a=0,r=0;return t.each(function(){var t=i(this).data("attribute_name")||i(this).attr("name"),e=i(this).val()||"";0<e.length&&r++,a++,n[t]=e}),{count:a,chosenCount:r,data:n}},processSource:function(t,e){e=u.getOrderData(t,e);return i.ajax({type:"POST",data:e,dataType:"json",url:u.getAjaxURL("create_order")})},getOrderData:function(t,e){var n=t.source,a=n.owner.email,r=n.owner.phone,s=n.owner.address,i=n.owner.name,o=t.shippingAddress,e={_wpnonce:wc_stripe_payment_request_params.nonce.checkout,billing_first_name:null!==i?i.split(" ").slice(0,1).join(" "):"",billing_last_name:null!==i?i.split(" ").slice(1).join(" "):"",billing_company:"",billing_email:null!==a?a:t.payerEmail,billing_phone:null!==r?r:t.payerPhone&&t.payerPhone.replace("/[() -]/g",""),billing_country:null!==s?s.country:"",billing_address_1:null!==s?s.line1:"",billing_address_2:null!==s?s.line2:"",billing_city:null!==s?s.city:"",billing_state:null!==s?s.state:"",billing_postcode:null!==s?s.postal_code:"",shipping_first_name:"",shipping_last_name:"",shipping_company:"",shipping_country:"",shipping_address_1:"",shipping_address_2:"",shipping_city:"",shipping_state:"",shipping_postcode:"",shipping_method:[null===t.shippingOption?null:t.shippingOption.id],order_comments:"",payment_method:"stripe",ship_to_different_address:1,terms:1,stripe_source:n.id,payment_request_type:e};return o&&(e.shipping_first_name=o.recipient.split(" ").slice(0,1).join(" "),e.shipping_last_name=o.recipient.split(" ").slice(1).join(" "),e.shipping_company=o.organization,e.shipping_country=o.country,e.shipping_address_1=void 0===o.addressLine[0]?"":o.addressLine[0],e.shipping_address_2=void 0===o.addressLine[1]?"":o.addressLine[1],e.shipping_city=o.city,e.shipping_state=o.region,e.shipping_postcode=o.postalCode),e},getErrorMessageHTML:function(t){return i('<div class="woocommerce-error" />').text(t)},displayErrorMessage:function(t){var e;i(".woocommerce-error").remove(),wc_stripe_payment_request_params.is_product_page?((e=i(".product").first()).before(t),i("html, body").animate({scrollTop:e.prev(".woocommerce-error").offset().top},600)):((e=i(".shop_table.cart").closest("form")).before(t),i("html, body").animate({scrollTop:e.prev(".woocommerce-error").offset().top},600))},abortPayment:function(t,e){t.complete("fail"),u.displayErrorMessage(e)},completePayment:function(t,e){u.block(),t.complete("success"),window.location=e},block:function(){i.blockUI({message:null,overlayCSS:{background:"#fff",opacity:.6}})},updateShippingOptions:function(t,e){e={security:wc_stripe_payment_request_params.nonce.shipping,country:e.country,state:e.region,postcode:e.postalCode,city:e.city,address:void 0===e.addressLine[0]?"":e.addressLine[0],address_2:void 0===e.addressLine[1]?"":e.addressLine[1],payment_request_type:o,is_product_page:wc_stripe_payment_request_params.is_product_page};return i.ajax({type:"POST",data:e,url:u.getAjaxURL("get_shipping_options")})},updateShippingDetails:function(t,e){e={security:wc_stripe_payment_request_params.nonce.update_shipping,shipping_method:[e.id],payment_request_type:o,is_product_page:wc_stripe_payment_request_params.is_product_page};return i.ajax({type:"POST",data:e,url:u.getAjaxURL("update_shipping_method")})},addToCart:function(){var t=i(".single_add_to_cart_button").val();i(".single_variation_wrap").length&&(t=i(".single_variation_wrap").find('input[name="product_id"]').val());var a={security:wc_stripe_payment_request_params.nonce.add_to_cart,product_id:t,qty:i(".quantity .qty").val(),attributes:i(".variations_form").length?u.getAttributes().data:[]},t=i("form.cart").serializeArray();return i.each(t,function(t,e){var n;/^addon-/.test(e.name)&&(/\[\]$/.test(e.name)?(n=e.name.substring(0,e.name.length-2),a[n]?a[n].push(e.value):a[n]=[e.value]):a[e.name]=e.value)}),i.ajax({type:"POST",data:a,url:u.getAjaxURL("add_to_cart")})},clearCart:function(){var t={security:wc_stripe_payment_request_params.nonce.clear_cart};return i.ajax({type:"POST",data:t,url:u.getAjaxURL("clear_cart"),success:function(t){}})},getRequestOptionsFromLocal:function(){return{total:wc_stripe_payment_request_params.product.total,currency:wc_stripe_payment_request_params.checkout.currency_code,country:wc_stripe_payment_request_params.checkout.country_code,requestPayerName:!0,requestPayerEmail:!0,requestPayerPhone:wc_stripe_payment_request_params.checkout.needs_payer_phone,requestShipping:wc_stripe_payment_request_params.product.requestShipping,displayItems:wc_stripe_payment_request_params.product.displayItems}},startPaymentRequest:function(t){var e,n=wc_stripe_payment_request_params.is_product_page?e=u.getRequestOptionsFromLocal():(e={total:t.order_data.total,currency:t.order_data.currency,country:t.order_data.country_code,requestPayerName:!0,requestPayerEmail:!0,requestPayerPhone:wc_stripe_payment_request_params.checkout.needs_payer_phone,requestShipping:!!t.shipping_required,displayItems:t.order_data.displayItems},t.order_data);"PR"===e.country&&(e.country="US");try{var a=p.paymentRequest(e),r=p.elements({locale:wc_stripe_payment_request_params.button.locale}),s=u.createPaymentRequestButton(r,a);a.canMakePayment().then(function(t){t&&(o=t.applePay?"apple_pay":t.googlePay?"google_pay":"payment_request_api",u.attachPaymentRequestButtonEventListeners(s,a),u.showPaymentRequestButton(s))}),a.on("shippingaddresschange",function(e){i.when(u.updateShippingOptions(n,e.shippingAddress)).then(function(t){e.updateWith({status:t.result,shippingOptions:t.shipping_options,total:t.total,displayItems:t.displayItems})})}),a.on("shippingoptionchange",function(e){i.when(u.updateShippingDetails(n,e.shippingOption)).then(function(t){"success"===t.result&&e.updateWith({status:"success",total:t.total,displayItems:t.displayItems}),"fail"===t.result&&e.updateWith({status:"fail"})})}),a.on("source",function(e){"no"===wc_stripe_payment_request_params.stripe.allow_prepaid_card&&"prepaid"===e.source.card.funding?u.abortPayment(e,u.getErrorMessageHTML(wc_stripe_payment_request_params.i18n.no_prepaid_card)):i.when(u.processSource(e,o)).then(function(t){"success"===t.result?u.completePayment(e,t.redirect):u.abortPayment(e,t.messages)})})}catch(t){console.error(t)}},getSelectedProductData:function(){var t=i(".single_add_to_cart_button").val();i(".single_variation_wrap").length&&(t=i(".single_variation_wrap").find('input[name="product_id"]').val());var e=(i("#product-addons-total").data("price_data")||[]).reduce(function(t,e){return t+e.cost},0),e={security:wc_stripe_payment_request_params.nonce.get_selected_product_data,product_id:t,qty:i(".quantity .qty").val(),attributes:i(".variations_form").length?u.getAttributes().data:[],addon_value:e};return i.ajax({type:"POST",data:e,url:u.getAjaxURL("get_selected_product_data")})},debounce:function(a,r,s){var i;return function(){var t=this,e=arguments,n=s&&!i;clearTimeout(i),i=setTimeout(function(){i=null,s||r.apply(t,e)},a),n&&r.apply(t,e)}},createPaymentRequestButton:function(t,e){var n;if(wc_stripe_payment_request_params.button.is_custom&&(n=i(wc_stripe_payment_request_params.button.css_selector)).length)return n.data("isCustom",!0),n;if(wc_stripe_payment_request_params.button.is_branded){if(u.shouldUseGooglePayBrand())return(n=u.createGooglePayButton()).data("isBranded",!0),n;wc_stripe_payment_request_params.button.type="long"===wc_stripe_payment_request_params.button.branded_type?"buy":"default"}return t.create("paymentRequestButton",{paymentRequest:e,style:{paymentRequestButton:{type:wc_stripe_payment_request_params.button.type,theme:wc_stripe_payment_request_params.button.theme,height:wc_stripe_payment_request_params.button.height+"px"}}})},isCustomPaymentRequestButton:function(t){return t&&"function"==typeof t.data&&t.data("isCustom")},isBrandedPaymentRequestButton:function(t){return t&&"function"==typeof t.data&&t.data("isBranded")},shouldUseGooglePayBrand:function(){var t=window.navigator.userAgent.toLowerCase(),e=/chrome/.test(t)&&!/edge|edg|opr|brave\//.test(t)&&"Google Inc."===window.navigator.vendor,t=e&&window.navigator.brave;return e&&!t},createGooglePayButton:function(){var t=wc_stripe_payment_request_params.button.theme,e=wc_stripe_payment_request_params.button.branded_type,n=wc_stripe_payment_request_params.button.locale,a=wc_stripe_payment_request_params.button.height,r="dark"===(t=["dark","light","light-outline"].includes(t)?t:"light")?"dark":"light",e=["short","long"].includes(e)?e:"long",s=i('<button type="button" id="wc-stripe-branded-button" aria-label="Google Pay" class="gpay-button"></button>');return s.css("height",a+"px"),s.addClass(t+" "+e),"long"===e&&function(t,e,n){t.css("background-image","url("+e+")");var a=document.createElement("img");a.onerror=function(){t.css("background-image","url("+n+")")},a.src=e}(s,"https://www.gstatic.com/instantbuy/svg/"+r+"/"+n+".svg","https://www.gstatic.com/instantbuy/svg/"+r+"/en.svg"),s},attachPaymentRequestButtonEventListeners:function(t,e){t.on("click",function(t){i("body").addClass("woocommerce-stripe-prb-clicked")}),wc_stripe_payment_request_params.is_product_page?u.attachProductPageEventListeners(t,e):u.attachCartPageEventListeners(t,e)},attachProductPageEventListeners:function(e,n){var a=[],r=i(".single_add_to_cart_button");e.on("click",function(t){return wc_stripe_payment_request_params.login_confirmation?(t.preventDefault(),void s(o)):r.is(".disabled")?(t.preventDefault(),void(r.is(".wc-variation-is-unavailable")?window.alert(wc_add_to_cart_variation_params.i18n_unavailable_text):r.is(".wc-variation-selection-needed")&&window.alert(wc_add_to_cart_variation_params.i18n_make_a_selection_text))):0<a.length?(t.preventDefault(),void window.alert(a)):(u.addToCart(),void((u.isCustomPaymentRequestButton(e)||u.isBrandedPaymentRequestButton(e))&&(t.preventDefault(),n.show())))}),i(document.body).on("wc_stripe_unblock_payment_request_button wc_stripe_enable_payment_request_button",function(){u.unblockPaymentRequestButton()}),i(document.body).on("wc_stripe_block_payment_request_button",function(){u.blockPaymentRequestButton("wc_request_button_is_blocked")}),i(document.body).on("wc_stripe_disable_payment_request_button",function(){u.blockPaymentRequestButton("wc_request_button_is_disabled")}),i(document.body).on("woocommerce_variation_has_changed",function(){i(document.body).trigger("wc_stripe_block_payment_request_button"),i.when(u.getSelectedProductData()).then(function(t){i.when(n.update({total:t.total,displayItems:t.displayItems})).then(function(){i(document.body).trigger("wc_stripe_unblock_payment_request_button")})})}),i(".quantity").on("input",".qty",function(){i(document.body).trigger("wc_stripe_block_payment_request_button")}),i(".quantity").on("input",".qty",u.debounce(250,function(){i(document.body).trigger("wc_stripe_block_payment_request_button"),a=[],i.when(u.getSelectedProductData()).then(function(t){t.error?(a=[t.error],i(document.body).trigger("wc_stripe_unblock_payment_request_button")):i.when(n.update({total:t.total,displayItems:t.displayItems})).then(function(){i(document.body).trigger("wc_stripe_unblock_payment_request_button")})})})),i(".variations_form").length&&i(".variations_form").on("found_variation.wc-variation-form",function(t,e){e.is_in_stock?u.unhidePaymentRequestButton():u.hidePaymentRequestButton()})},attachCartPageEventListeners:function(e,n){e.on("click",function(t){return wc_stripe_payment_request_params.login_confirmation?(t.preventDefault(),void s(o)):void((u.isCustomPaymentRequestButton(e)||u.isBrandedPaymentRequestButton(e))&&(t.preventDefault(),n.show()))})},showPaymentRequestButton:function(t){u.isCustomPaymentRequestButton(t)?(t.addClass("is-active"),i("#wc-stripe-payment-request-wrapper, #wc-stripe-payment-request-button-separator").show()):u.isBrandedPaymentRequestButton(t)?(i("#wc-stripe-payment-request-wrapper, #wc-stripe-payment-request-button-separator").show(),i("#wc-stripe-payment-request-button").html(t)):i("#wc-stripe-payment-request-button").length&&(i("#wc-stripe-payment-request-wrapper, #wc-stripe-payment-request-button-separator").show(),t.mount("#wc-stripe-payment-request-button"))},hidePaymentRequestButton:function(){i("#wc-stripe-payment-request-wrapper, #wc-stripe-payment-request-button-separator").hide()},unhidePaymentRequestButton:function(){const t=i("#wc-stripe-payment-request-wrapper"),e=i("#wc-stripe-payment-request-button-separator");(t.is(":hidden")||e.is(":hidden"))&&(t.show(),e.show())},blockPaymentRequestButton:function(t){i("#wc-stripe-payment-request-button").data("blockUI.isBlocked")||i("#wc-stripe-payment-request-button").addClass(t).block({message:null})},unblockPaymentRequestButton:function(){i("#wc-stripe-payment-request-button").removeClass(["wc_request_button_is_blocked","wc_request_button_is_disabled"]).unblock()},init:function(){wc_stripe_payment_request_params.is_product_page?u.startPaymentRequest(""):u.getCartDetails()}};function s(t){var e;wc_stripe_payment_request_params.login_confirmation&&(e=wc_stripe_payment_request_params.login_confirmation.message,e=(e="payment_request_api"!==t?e.replace(/\*\*.*?\*\*/,"apple_pay"===t?"Apple Pay":"Google Pay"):e).replace(/\*\*/g,""),confirm(e)&&(window.location.href=wc_stripe_payment_request_params.login_confirmation.redirect_url))}u.init(),i(document.body).on("updated_cart_totals",function(){u.init()}),i(document.body).on("updated_checkout",function(){u.init()})}); |