mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 10:55:00 +08:00
2 lines
No EOL
7.8 KiB
JavaScript
2 lines
No EOL
7.8 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(){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 o=e=>(t,r)=>fetch(e.config.ajax.approve_order.endpoint,{method:"POST",body:JSON.stringify({nonce:e.config.ajax.approve_order.nonce,order_id:t.orderID})}).then(e=>e.json()).then(t=>{t.success&&(location.href=e.config.redirect)});var a=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:o(this),onError:e=>{this.errorHandler.message(e)}}}};var i=e=>(t,r)=>fetch(e.config.ajax.approve_order.endpoint,{method:"POST",body:JSON.stringify({nonce:e.config.ajax.approve_order.nonce,order_id:t.orderID})}).then(e=>e.json()).then(e=>{e.success&&document.querySelector("#place_order").click()});var s=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:i(this),onError:e=>{this.errorHandler.message(e)}}}};var c=class{constructor(e,t,r){this.gateway=e,this.renderer=t,this.configurator=r}init(){this.shouldRender()&&(jQuery(document.body).on("wc_fragments_loaded wc_fragments_refreshed",()=>{renderer.render(this.configurator.configuration())}),this.renderer.render(this.gateway.button.mini_cart_wrapper,this.configurator.configuration()))}shouldRender(){return null!==document.querySelector(this.gateway.button.mini_cart_wrapper)}};var u=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 d=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 h=class{constructor(e,t,r){this.element=e,this.showCallback=t,this.hideCallback=r,this.observer=null}init(){const e=()=>{this.element.classList.contains("disabled")?this.hideCallback():this.showCallback()};this.observer=new MutationObserver(e),this.observer.observe(this.element,{attributes:!0}),e()}disconnect(){this.observer.disconnect()}};var l=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 h(this.formElement.querySelector(".single_add_to_cart_button"),this.showButtonCallback,this.hideButtonCallback).init()}return{createOrder:this.createOrder(),onApprove:o(this),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 u(n,o,null))}),e}:()=>{const e=document.querySelector('[name="add-to-cart"]').value,t=document.querySelector('[name="quantity"]').value,r=this.variations();return[new u(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 p=class{constructor(e,t){this.gateway=e,this.renderer=t}init(){if(!this.shouldRender())return;const e=new n,t=new d(this.gateway.ajax.change_cart.endpoint,this.gateway.ajax.change_cart.nonce),r=this.gateway.button.wrapper,o=new l(this.gateway,t,()=>{this.renderer.showButtons(r)},()=>{this.renderer.hideButtons(r)},document.querySelector("form.cart"),e);this.renderer.render(r,o.configuration())}shouldRender(){return null!==document.querySelector("form.cart")&&null!==document.querySelector(this.gateway.button.wrapper)}};var f=class{constructor(e,t,r){this.gateway=e,this.renderer=t,this.configurator=r}init(){this.shouldRender()&&(jQuery(document.body).on("updated_cart_totals updated_checkout",()=>{this.renderer.render(this.configurator.configuration())}),this.renderer.render(this.gateway.button.wrapper,this.configurator.configuration()))}shouldRender(){return null!==document.querySelector(this.gateway.button.wrapper)}};var m=class{constructor(e,t,r){this.gateway=e,this.renderer=t,this.configurator=r}init(){if(!this.shouldRender())return;const e=this.gateway.button.wrapper,t=()=>{"ppcp-gateway"!==jQuery('input[name="payment_method"]:checked').val()?(this.renderer.hideButtons(e),jQuery("#place_order").show()):(this.renderer.showButtons(e),jQuery("#place_order").hide())};jQuery(document.body).on("updated_checkout",()=>{this.renderer.render(e,this.configurator.configuration()),t()}),jQuery(document.body).on("payment_method_selected",()=>{t()}),this.renderer.render(e,this.configurator.configuration())}shouldRender(){return!document.querySelector(this.gateway.button.cancel_wrapper)&&null!==document.querySelector(this.gateway.button.wrapper)}};var y=class{render(e,t){this.isAlreadyRendered(e)||paypal.Buttons(t).render(e)}isAlreadyRendered(e){return document.querySelector(e).hasChildNodes()}hideButtons(e){document.querySelector(e).style.display="none"}showButtons(e){document.querySelector(e).style.display="block"}};document.addEventListener("DOMContentLoaded",()=>{const e=document.createElement("script");e.setAttribute("src",PayPalCommerceGateway.button.url),e.addEventListener("load",e=>{(()=>{const e=new y,t=new n,r=new a(PayPalCommerceGateway,t),o=new s(PayPalCommerceGateway,t),i=PayPalCommerceGateway.context;if("mini-cart"===i||"product"===i){new c(PayPalCommerceGateway,e,r).init()}if("product"===i){new p(PayPalCommerceGateway,e).init()}if("cart"===i){new f(PayPalCommerceGateway,e,r).init()}if("checkout"===i){new m(PayPalCommerceGateway,e,o).init()}})()}),document.body.append(e)})}]);
|
|
//# sourceMappingURL=button.js.map
|