oont-contents/plugins/woocommerce-square/build/assets/frontend/wc-square-gift-card.js
2025-02-08 15:10:23 +01:00

1 line
No EOL
4.4 KiB
JavaScript

(()=>{"use strict";jQuery(document).ready((t=>{window.WC_Square_Gift_Card_Handler=class{constructor(i){this.applicationId=i.applicationId,this.locationId=i.locationId,this.giftCard=null,this.applyGiftCardNonce=i.applyGiftCardNonce,this.removeGiftCardText=i.removeGiftCardText,this.applyDiffGiftCard=i.applyDiffGiftCard,this.splitPaymentQuestion=i.splitPaymentQuestion,this.splitPaymentMessage=i.splitPaymentMessage,this.isValidPage=i.isValidPage,this.reviewOrderTable=t(".woocommerce-checkout-review-order-table"),this.payments=!!window.Square&&window.Square.payments(this.applicationId,this.locationId),this.handleSingProductPage(),t(document.body).on("updated_checkout",this.init.bind(this))}async init(i,a){if(this.payments){this.renderGiftCardHtml(a.fragments),this.initVariables(),this.block_ui(this.giftCardAppWrapper),t(document.body).on("update_checkout",(()=>{this.hiddenFieldsEl&&this.hiddenFieldsEl.find('input[name="payment_method"]').remove(),this.block_ui(this.giftCardAppWrapper)}));try{this.giftCard=await this.initializeGiftCard(this.payments)}catch(i){console.error("Initializing Gift Card failed",i)}this.giftCardAppWrapper.unblock(),this.giftCardBtnEl.on("click",(async()=>{await this.applyGiftCardHandler(this.giftCard)})),this.splitPaymentEl.on("click",this.onSplitTotal.bind(this)),this.giftCard&&(this.giftCard.addEventListener("errorClassAdded",(()=>{this.toggleGiftCardApplyButton(!0)})),this.giftCard.addEventListener("errorClassRemoved",(()=>{this.toggleGiftCardApplyButton(!1)}))),this.removeGiftCardEl.on("click",this.onRemoveGiftCard.bind(this))}}toggleDisabledAttribute(i=null,a=!1){i&&i.find("input, textarea").each(((i,e)=>{t(e).attr("disabled",a)}))}handleSingProductPage(){t('input[name="square-gift-card-buying-option"]').on("click",(i=>{const a=t('[data-square-gift-card-activity="new"]'),e=t('[data-square-gift-card-activity="load"]');"new"===t(i.target).val()?(a.show(),e.hide(),this.toggleDisabledAttribute(a),this.toggleDisabledAttribute(e,!0)):(e.show(),a.hide(),this.toggleDisabledAttribute(e),this.toggleDisabledAttribute(a,!0))}))}initVariables(){this.giftCardWrapperEl=t("#square-gift-card-fields-input"),this.giftCardAppWrapper=t("#square-gift-card-wrapper"),this.giftCardBtnEl=t("#square-gift-card-apply-btn"),this.removeGiftCardEl=t("#square-gift-card-remove"),this.splitPaymentEl=t("#square-split-payment")}toggleGiftCardApplyButton(t=!1){this.giftCardBtnEl.prop("disabled",t)}async initializeGiftCard(t=null){if(!t)return;const i=await t.giftCard();return await i.attach(this.giftCardWrapperEl[0]),i}renderGiftCardHtml(i){t("#square-gift-card-wrapper").remove(),t("#square-gift-card-split-details").remove(),t(".woocommerce-checkout-review-order-table").after(i[".woocommerce-square-gift-card-html"]),this.hiddenFieldsEl=t("#square-gift-card-hidden-fields"),this.hiddenFieldsEl&&i["has-balance"]?this.hiddenFieldsEl.append('<input name="payment_method" type="hidden" value="square_credit_card" />'):this.hiddenFieldsEl.find('input[name="payment_method"]').remove()}async applyGiftCardHandler(i){let a;this.block_ui(this.giftCardAppWrapper);try{a=await this.tokenize(i)}catch{i.setError("giftCardNumber"),this.giftCardAppWrapper.unblock()}a&&(await this.checkGiftCardBalance(a),t(document.body).trigger("update_checkout"))}async tokenize(t){const i=await t.tokenize();return"OK"===i.status&&i.token}async checkGiftCardBalance(t=null){const i=new FormData;i.append("token",t),i.append("action","wc_square_check_gift_card_balance"),i.append("security",this.applyGiftCardNonce);const a=await fetch(woocommerce_params.ajax_url,{method:"POST",body:i});if(200===a.status)return a.json()}async onRemoveGiftCard(i){i.preventDefault(),this.giftCard&&await this.giftCard.destroy()&&(this.giftCard=await this.initializeGiftCard(this.payments));const a=new FormData;a.append("action","wc_square_gift_card_remove"),a.append("security",this.applyGiftCardNonce);let e=await fetch(woocommerce_params.ajax_url,{method:"POST",body:a});200===e.status&&(e=await e.json(),e.success&&t(document.body).trigger("update_checkout"))}async onSplitTotal(i){i.preventDefault();const a=new FormData;a.append("action","wc_square_split_payments"),a.append("security",this.applyGiftCardNonce);let e=await fetch(woocommerce_params.ajax_url,{method:"POST",body:a});200===e.status&&(e=await e.json(),e.success&&t(document.body).trigger("update_checkout"))}block_ui(t=null){t&&t.block({message:null,overlayCSS:{background:"#fff",opacity:.6}})}}}))})();