| Current File : /home/digitaw/www/wp-content/plugins/event-tickets/build/js/commerce/gateway/square/checkout.js |
window.tec=window.tec||{},window.tec.tickets=window.tec.tickets||{},window.tec.tickets.commerce=window.tec.tickets.commerce||{},window.tec.tickets.commerce.square=window.tec.tickets.commerce.square||{},window.tec.tickets.commerce.square.checkout=window.tec.tickets.commerce.square.checkout||{},((e,t,r)=>{"use strict";t.selectors={cardElement:"#tec-tc-gateway-square-card-element",cardErrors:"#tec-tc-gateway-square-errors",paymentMessage:"#tec-tc-gateway-square-payment-message",infoForm:".tribe-tickets__commerce-checkout-purchaser-info-wrapper",submitButton:"#tec-tc-gateway-square-checkout-button",hiddenElement:".tribe-common-a11y-hidden",form:".tribe-tickets__commerce-checkout-square-form",squareCardForm:"#tec-tc-gateway-square-form",editLink:".tec-tickets__commerce-checkout-purchaser-info-edit-link"},t.square=null,t.card=null,t.checkoutContainer=null,t.initialized=!1,t.handleErrorDisplay=(e,r=()=>{})=>{e.map((e=>t.showNotice({},"",e[1]))),r()},t.getRequestArgs=(e,r)=>{void 0===r&&(r={"X-WP-Nonce":t.data.nonce});const a={headers:r,hooks:{beforeRetry:[t.onBeforeRetry],beforeError:[t.onBeforeError]},timeout:3e4,throwHttpErrors:!1};return e&&(a.json=e),a},t.onBeforeRetry=async e=>(console.log(e),r.stop),t.onBeforeError=async e=>(console.log(e),r.stop),t.onPaymentError=r=>{tribe.tickets.debug.log("square","paymentError",r);const a=e(t.selectors.cardErrors);r?a.text(r.message).show():a.text("").hide()},t.getVerificationDetails=()=>{const e={amount:t.data.amount,intent:"CHARGE",currencyCode:t.data.currencyCode,customerInitiated:!0,sellerKeyedIn:!1};if(t.data.userLoggedIn)return e.billingContact=t.data.userData,e;const{name:r,...a}=t.getPurchaserData(),o=r.indexOf(" "),c=o>0?r.substring(0,o):r,i=o>0?r.substring(o+1):"";return e.billingContact={givenName:c,familyName:i,...a},e},t.createPayment=async()=>{try{const e=await t.card.tokenize(t.getVerificationDetails());if("OK"===e.status)await t.processPayment(e.token);else{let r="Card tokenization failed.";e.errors&&(r=e.errors.map((e=>e.message)).join(", ")),t.onPaymentError({message:r}),t.loader.hide()}}catch(e){t.onPaymentError({message:e.message||"An error occurred while processing the payment."}),t.loader.hide()}},t.processPayment=async e=>{const a={payment_source_id:e,purchaser:t.getPurchaserData()};try{const e=await r.post(t.data.orderEndpoint,t.getRequestArgs(a)).json();if(!e.success)throw new Error("Failed to create order.",{cause:e});e.redirect_url&&(window.location.href=e.redirect_url)}catch(e){t.onPaymentError({message:e.message||"An error occurred while processing the payment."}),t.loader.hide()}},t.initializeSquare=async(r=!1)=>{try{if(!window.Square)return void console.error("Square SDK not loaded");if(r=r||t.data.userLoggedIn,t.initialized)return void(r&&e(t.selectors.squareCardForm).show());if(!r)return;t.initialized=!0;const a=window.Square.payments(t.data.applicationId,t.data.locationId);t.card=await a.card(t.data.squareCardOptions),await t.card.attach(t.selectors.cardElement),e(t.selectors.squareCardForm).show(),e(t.selectors.submitButton).show(),e(t.selectors.form).on("submit",(e=>{e.preventDefault(),t.loader.show(),t.createPayment({})}))}catch(e){console.error("Failed to initialize Square",e),t.onPaymentError({message:"Failed to initialize payment form."})}},t.showNotice=(t,r,a,o=6e4)=>{if("function"!=typeof tribe.tickets.commerce.notice.show)return;let c={type:r||"error",message:a||"",delay:o};c=e.extend(c,t),tribe.tickets.commerce.notice.show(c)},t.loader={show:()=>{tribe.tickets.loader.show(e(t.selectors.form)),e(t.selectors.submitButton).prop("disabled",!0)},hide:()=>{tribe.tickets.loader.hide(e(t.selectors.form)),e(t.selectors.submitButton).prop("disabled",!1)}},t.initializeSquareOnDataProvided=()=>{if(t.data.userLoggedIn)return;const r=e(t.selectors.editLink),a=e(t.selectors.infoForm);r.on("click",(r=>{r.preventDefault(),a.find("input").prop("readonly",!1),a.find("select").prop("disabled",!1),a.find("#tec-tc-gateway-stripe-render-payment").show(),a.find(t.selectors.editLink).hide(),e(t.selectors.squareCardForm).hide(),a.find('[name="purchaser-name"]').focus()})),a.on("submit",(r=>{r.preventDefault(),a.find("input").prop("readonly",!0),a.find("select").prop("disabled",!0),a.find("#tec-tc-gateway-stripe-render-payment").hide(),a.find(t.selectors.editLink).css("display","inline-block"),t.initializeSquare(!0),e(t.selectors.squareCardForm).show()}))},t.ready=()=>{t.initializeSquareOnDataProvided(),t.initializeSquare()},t.getPurchaserData=()=>{const r=e(t.selectors.infoForm);return{name:r.find('[name="purchaser-name"]').val(),email:r.find('[name="purchaser-email"]').val(),countryCode:r.find('[name="purchaser-country"]').val(),addressLines:[r.find('[name="purchaser-address1"]').val(),r.find('[name="purchaser-address2"]').val()],state:r.find('[name="purchaser-state"]').val(),city:r.find('[name="purchaser-city"]').val(),postalCode:r.find('[name="purchaser-zip"]').val()}},e(t.ready)})(jQuery,window.tec.tickets.commerce.square.checkout,tribe.ky),window.tec=window.tec||{},window.tec.tickets=window.tec.tickets||{},window.tec.tickets.commerce=window.tec.tickets.commerce||{},window.tec.tickets.commerce.gateway=window.tec.tickets.commerce.gateway||{},window.tec.tickets.commerce.gateway.square=window.tec.tickets.commerce.gateway.square||{},window.tec.tickets.commerce.gateway.square.checkout={};