mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 10:55:00 +08:00
2 lines
No EOL
5.6 KiB
JavaScript
2 lines
No EOL
5.6 KiB
JavaScript
!function(e){var t={};function r(n){if(t[n])return t[n].exports;var o=t[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)r.d(n,o,function(t){return e[t]}.bind(null,o));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=0)}([function(e,t,r){"use strict";r.r(t);var n=class{constructor(e){this.wrapper=e}render(e){paypal.Buttons(e).render(this.wrapper)}hideButtons(){document.querySelector(this.wrapper).style.display="none"}showButtons(){document.querySelector(this.wrapper).style.display="block"}};var o=class{constructor(e,t,r){this.element=e,this.showCallback=t,this.hideCallback=r,this.observer=null}init(){this.observer=new MutationObserver(()=>{this.element.classList.contains("disabled")?this.hideCallback():this.showCallback()}),this.observer.observe(this.element,{attributes:!0})}disconnect(){this.observer.disconnect()}};var a=class{constructor(e,t,r){this.id=e,this.quantity=t,this.variations=r}data(){return{id:this.id,quantity:this.quantity,variations:this.variations}}};var i=class{constructor(e,t,r,n,o,a){this.config=e,this.updateCart=t,this.showButtonCallback=r,this.hideButtonCallback=n,this.formElement=o,this.errorHandler=a}configuration(){if(this.hasVariations()){new o(this.formElement.querySelector(".single_add_to_cart_button"),this.showButtonCallback,this.hideButtonCallback).init()}return{createOrder:this.createOrder(),onApprove:(e,t)=>t.redirect(this.config.redirect),onError:e=>{this.errorHandler.message(e)}}}createOrder(){var e=null;e=this.isGroupedProduct()?()=>{const e=[];return this.formElement.querySelectorAll('input[type="number"]').forEach(t=>{if(!t.value)return;const r=t.getAttribute("name").match(/quantity\[([\d]*)\]/);if(2!==r.length)return;const n=parseInt(r[1]),o=parseInt(t.value);e.push(new a(n,o,null))}),e}:()=>{const e=document.querySelector('[name="add-to-cart"]').value,t=document.querySelector('[name="quantity"]').value,r=this.variations();return[new a(e,t,r)]};return(t,r)=>{this.errorHandler.clear();return this.updateCart.update(e=>fetch(this.config.ajax.create_order.endpoint,{method:"POST",body:JSON.stringify({nonce:this.config.ajax.create_order.nonce,purchase_units:e})}).then((function(e){return e.json()})).then((function(e){if(e.success)return e.data.id})),e())}}variations(){if(!this.hasVariations())return null;return[...this.formElement.querySelectorAll("[name^='attribute_']")].map(e=>({value:e.value,name:e.name}))}hasVariations(){return this.formElement.classList.contains("variations_form")}isGroupedProduct(){return this.formElement.classList.contains("grouped_form")}};var s=class{constructor(e,t){this.endpoint=e,this.nonce=t}update(e,t){return new Promise((r,n)=>{fetch(this.endpoint,{method:"POST",body:JSON.stringify({nonce:this.nonce,products:t})}).then(e=>e.json()).then(t=>{if(!t.success)return void n(t.data);const o=e(t.data);r(o)})})}};var c=class{constructor(){this.wrapper=document.querySelector(".woocommerce-notices-wrapper")}message(e){this.wrapper.classList.add("woocommerce-error"),this.wrapper.innerText=this.sanitize(e)}sanitize(e){const t=document.createElement("textarea");return t.innerHTML=e,t.value}clear(){this.wrapper.classList.contains("woocommerce-error")&&(this.wrapper.classList.remove("woocommerce-error"),this.wrapper.innerText="")}};var u=class{constructor(e,t){this.config=e,this.errorHandler=t}configuration(){return{createOrder:(e,t)=>fetch(this.config.ajax.create_order.endpoint,{method:"POST",body:JSON.stringify({nonce:this.config.ajax.create_order.nonce,purchase_units:[]})}).then((function(e){return e.json()})).then((function(e){if(e.success)return e.data.id})),onApprove:(e,t)=>t.redirect(this.config.redirect),onError:e=>{this.errorHandler.message(e)}}}};document.addEventListener("DOMContentLoaded",()=>{const e=document.createElement("script");e.setAttribute("src",PayPalCommerceGateway.button.url),e.addEventListener("load",e=>{(()=>{const e=PayPalCommerceGateway.context,t=new c,r=new u(PayPalCommerceGateway,t);if(document.querySelector(PayPalCommerceGateway.button.mini_cart_wrapper)){new n(PayPalCommerceGateway.button.mini_cart_wrapper).render(r.configuration())}if(jQuery(document.body).on("wc_fragments_loaded wc_fragments_refreshed",()=>{if(!document.querySelector(PayPalCommerceGateway.button.mini_cart_wrapper))return;new n(PayPalCommerceGateway.button.mini_cart_wrapper).render(r.configuration())}),!document.querySelector(PayPalCommerceGateway.button.wrapper))return;const o=new n(PayPalCommerceGateway.button.wrapper);let a=null;if("product"===e){if(!document.querySelector("form.cart"))return;const e=new s(PayPalCommerceGateway.ajax.change_cart.endpoint,PayPalCommerceGateway.ajax.change_cart.nonce);a=new i(PayPalCommerceGateway,e,o.showButtons.bind(o),o.hideButtons.bind(o),document.querySelector("form.cart"),t)}"cart"===e&&(a=r,jQuery(document.body).on("updated_cart_totals updated_checkout",()=>{o.render(a.configuration())})),a?o.render(a.configuration()):console.error("No context for button found.")})()}),document.body.append(e)})}]);
|
|
//# sourceMappingURL=button.js.map
|