| 1 |
import{lpAddQueryArgs,lpFetchAPI,lpAjaxParseJsonOld}from"../utils.js";document.addEventListener("submit",(e=>{window.lpCheckout.submit(e)})),document.addEventListener("change",(e=>{window.lpCheckout.paymentSelect(e)})),document.addEventListener("keyup",(e=>{window.lpCheckout.checkEmailGuest(e)})),window.lpCheckout={idFormCheckout:"learn-press-checkout-form",idBtnPlaceOrder:"learn-press-checkout-place-order",classPaymentMethod:"lp-payment-method",classPaymentMethodForm:"payment-method-form",timeOutCheckEmail:null,fetchAPI:(e,t,s)=>{const o={headers:{}};0!==parseInt(lpData.user_id)&&(o.headers["X-WP-Nonce"]=lpData.nonce);const c=new URLSearchParams;Object.keys(t).forEach((e=>{c.append(e,t[e])})),o.method="POST",o.body=c,fetch(e,o).then((e=>e.text())).then((e=>{e=lpAjaxParseJsonOld(e),s.success(e)})).finally((()=>{s.completed()})).catch((e=>s.error(e)))},submit:e=>{const t=e.target;if(t.id!==window.lpCheckout.idFormCheckout)return;if(t.classList.contains("processing"))return;e.preventDefault(),t.classList.add("processing");const s=t.querySelector('button[type="submit"]');s.disabled=!0,window.lpCheckout.removeMessage();const o=document.getElementById(window.lpCheckout.idBtnPlaceOrder),c=new URL(lpCheckoutSettings.ajaxurl);c.searchParams.set("lp-ajax","checkout");const r=new FormData(t),n=Object.fromEntries(Array.from(r.keys(),(e=>{const t=r.getAll(e);return[e,t.length>1?t:t.pop()]})));o.classList.add("loading");const a={success:e=>{e=lpAjaxParseJsonOld(e);const{messages:s,result:o}=e;"success"!==o?window.lpCheckout.showErrors(t,"error",s):window.location.href=e.redirect},error:e=>{window.lpCheckout.showErrors(t,"error",e)},completed:()=>{o.classList.remove("loading"),t.classList.remove("processing"),s.disabled=!1}};window.lpCheckout.fetchAPI(c,n,a)},paymentSelect:e=>{const t=e.target.closest(`.${window.lpCheckout.classPaymentMethod}`);if(!t)return;const s=t.closest(".payment-methods");if(!s)return;s.querySelectorAll(`.${window.lpCheckout.classPaymentMethod}`).forEach((e=>{const s=e.querySelector(`.${window.lpCheckout.classPaymentMethodForm}`);s&&(s.style.display=t!==e?"none":"block")}))},checkEmailGuest:e=>{const t=e.target;"guest_email"===t.id&&window.lpCheckout.isEmail(t.value)&&(t.classList.add("loading"),null!==window.lpCheckout.timeOutCheckEmail&&clearTimeout(window.lpCheckout.timeOutCheckEmail),window.lpCheckout.timeOutCheckEmail=setTimeout((()=>{const e={success:e=>{const{message:s,data:o,status:c}=e;if("success"===c){const e=o.content||"",s=document.querySelector(".lp-guest-checkout-output");s&&s.remove(),t.insertAdjacentHTML("afterend",e)}else window.lpCheckout.showErrors(t.closest("form"),c,s)},error:e=>{window.lpCheckout.showErrors(t.closest("form"),"error",e)},completed:()=>{t.classList.remove("loading")}};window.lpCheckout.fetchAPI(window.location.href,{"lp-ajax":"checkout-user-email-exists",email:t.value},e)}),500))},removeMessage:()=>{const e=document.querySelector(".learn-press-message");e&&e.remove()},showErrors:(e,t,s)=>{const o=`<div class="learn-press-message ${t}">${s}</div>`;e.insertAdjacentHTML("afterbegin",o),e.scrollIntoView()},isEmail:e=>new RegExp("^[-!#$%&'*+\\./0-9=?A-Z^_`a-z{|}~]+@[-!#$%&'*+\\/0-9=?A-Z^_`a-z{|}~]+.[-!#$%&'*+\\./0-9=?A-Z^_`a-z{|}~]+$").test(e)}; |