| 1 |
CHARITABLE=window.CHARITABLE||{},function(e){var t=e("body");const a=CHARITABLE_SQUARE_VARS.client_id,n=CHARITABLE_SQUARE_VARS.location_id;e("#square-card-container").html('\x3c!-- By Sam Herbert (@sherb), for everyone. More @ http://goo.gl/7AJzbL --\x3e\n <svg width="40" height="10" viewBox="0 0 120 30" xmlns="http://www.w3.org/2000/svg" fill="#f89d35">\n <circle cx="15" cy="15" r="15">\n <animate attributeName="r" from="15" to="15"\n begin="0s" dur="0.8s"\n values="15;9;15" calcMode="linear"\n repeatCount="indefinite" />\n <animate attributeName="fill-opacity" from="1" to="1"\n begin="0s" dur="0.8s"\n values="1;.5;1" calcMode="linear"\n repeatCount="indefinite" />\n </circle>\n <circle cx="60" cy="15" r="9" fill-opacity="0.3">\n <animate attributeName="r" from="9" to="9"\n begin="0s" dur="0.8s"\n values="9;15;9" calcMode="linear"\n repeatCount="indefinite" />\n <animate attributeName="fill-opacity" from="0.5" to="0.5"\n begin="0s" dur="0.8s"\n values=".5;1;.5" calcMode="linear"\n repeatCount="indefinite" />\n </circle>\n <circle cx="105" cy="15" r="15">\n <animate attributeName="r" from="15" to="15"\n begin="0s" dur="0.8s"\n values="15;9;15" calcMode="linear"\n repeatCount="indefinite" />\n <animate attributeName="fill-opacity" from="1" to="1"\n begin="0s" dur="0.8s"\n values="1;.5;1" calcMode="linear"\n repeatCount="indefinite" />\n </circle>\n </svg>\n ');e("#google-pay-button"),e("#apple-pay-button");var r=!1;const o={};let i=1;function s(t){t.cancel_processing(),e("#charitable-gateway-fields").removeClass("square-wallet-processing")}function c(t){var a=e('[id^="charitable-gateway-fields-square"]');t.is_recurring_donation()?a.addClass("needs-recurring-support"):a.removeClass("needs-recurring-support")}async function l(a){a.preventDefault();const n=a.currentTarget.helper,r=a.currentTarget.type;if(function(t){e("#charitable-gateway-fields").addClass("square-wallet-processing"),t.show_processing()}(n),n.clear_errors(),n.validate_amount(),n.validate_required_fields(),n.is_recurring_donation()&&n.add_error(CHARITABLE_SQUARE_VARS.wallet_not_supported),n.errors.length>0)return s(n),!1;try{const e=await o[r].tokenize();"OK"===e.status&&(n.get_input("square_token").val(e.token),n.remove_pending_process_by_name("square"),t.trigger("charitable:form:process",n))}catch(e){n.add_error(e.message),n.remove_pending_process_by_name("square"),s(n)}}async function u(t,a){if(!a.is_recurring_donation())try{const n=a.get_amount()?a.get_amount().toString():"1.00";i=n;const r=t.paymentRequest({countryCode:CHARITABLE_VARS.country,currencyCode:CHARITABLE_VARS.currency,total:{amount:n,label:"Total"}}),o=[];if(0===o.length)return;const s=[];for(let e of o)s.push(d(e,r,t,a));await Promise.all(s).catch(e=>{"PaymentMethodUnsupportedError"===e.name?console.log(e.message):console.error(e.message)}),e("#charitable-square-wallet-buttons").show()}catch(e){console.error("Error initializing wallets:",e)}}async function d(a,n,r,i){try{(!o[a]||"applePay"===a&&o[a]._paymentRequest?.total?.amount!==n.total.amount)&&(o[a]&&await o[a].destroy(),o[a]=await r[a](n));const s=a.split("P")[0]+"-pay-button";if("applePay"===a){const e=o[a];if(!e)throw new Error("Apple Pay is not available");let c=document.getElementById(s);c||(c=document.createElement("button"),c.id=s,c.className="apple-pay-button",document.getElementById("charitable-square-wallet-buttons").appendChild(c)),c.style.cssText="\n -apple-pay-button-style: black;\n -apple-pay-button-type: plain;\n appearance: -apple-pay-button;\n display: inline-block;\n width: 200px;\n height: 40px;\n border-radius: 5px;\n padding: 0;\n margin: 10px auto;\n background: #000;\n cursor: pointer;\n ",c.replaceWith(c.cloneNode(!0)),c=document.getElementById(s),c.addEventListener("click",async o=>{o.preventDefault();try{if(!e||e._destroyed)throw new Error("Apple Pay session has expired. Please try again.");const a=await e.tokenize();"OK"===a.status&&(i.get_input("square_token").val(a.token),i.remove_pending_process_by_name("square"),t.trigger("charitable:form:process",i))}catch(e){console.error("Apple Pay tokenization error:",e),i.add_error(e.message),await d(a,n,r,i)}}),c.helper=i,c.type=a,c.style.display="block"}else{const e=document.getElementById(s);if(e&&!e.hasAttribute("data-wallet-attached"))try{await o[a].attach("#"+s),e.setAttribute("data-wallet-attached","true"),e.replaceWith(e.cloneNode(!0));const t=document.getElementById(s);t.addEventListener("click",l),t.helper=i,t.type=a,t.style.display="block"}catch(t){console.error(`Error attaching ${a}:`,t),await o[a].destroy(),o[a]=await r[a](n),await o[a].attach("#"+s),e.setAttribute("data-wallet-attached","true")}}e("#charitable-square-wallet-buttons").addClass("wallet-enabled").show(),e("#charitable-square-card-payment-fields").addClass("wallet-enabled")}catch(e){console.error(`Error initializing ${a}:`,e),console.error("Wallet object:",o[a]),console.error("PaymentRequest:",n),console.error("Payments object:",r);const t=a.split("P")[0]+"-pay-button",i=document.getElementById(t);i&&(i.style.display="none")}}function p(){const t=e('input[name="gateway"][value="square"], input[name="gateway"][value="square_core"]'),a=e("#charitable-gateway-fields-square, #charitable-gateway-fields-square_core");(t.is(":checked")||a.is(":visible"))&&setTimeout(function(){window.dispatchEvent(new Event("resize"))},150)}t.on("charitable:form:initialize",async function(o,s){if(!window.Square)throw new Error("Square.js failed to load properly");let l,p;try{l=window.Square.payments(a,n)}catch(e){const t=document.getElementById("square-payment-status-container");return document.getElementById("square-card-container").innerHTML="",t.className="charitable-notice",t.style.visibility="visible",void(t.innerHTML="Square: "+e.message)}try{p=await async function(t){const a=await t.card();return e("#square-card-container").html(""),await a.attach("#square-card-container"),a}(l),await u(l,s)}catch(e){return void console.error("Initializing Card failed",e)}async function y(e,t,a){if(e.preventDefault(),"make_donation"===a.get_input("charitable_action").val()&&a.get_payment_method().startsWith("square"))if(a.errors.length>0)a.cancel_processing();else{a.add_pending_process("square");try{const e=await async function(e){const t=await e.tokenize();if("OK"===t.status)return t.token;{let e="Unable to validate card.Please check your details.";throw t.errors&&(errors=t.errors.map(function(e){return e.message}),e=errors.join(". ")),new Error(e)}}(t);a.get_input("square_token").val(e);const n=await async function(e,t,a){const n=a.is_recurring_donation()?"STORE":"CHARGE",r={amount:a.get_amount().toString(),billingContact:m(),currencyCode:CHARITABLE_VARS.currency,intent:n},o=await e.verifyBuyer(t,r);return o.token}(l,e,a);a.get_input("square_verification_token").val(n),a.remove_pending_process_by_name("square")}catch(e){a.add_error(e.message),a.remove_pending_process_by_name("square")}}}function m(){const e={},t=[];s.get_input("address").val()&&t.push(s.get_input("address").val()),s.get_input("address_2").val()&&t.push(s.get_input("address_2").val());const a={givenName:s.get_input("first_name").val(),familyName:s.get_input("last_name").val(),emailAddress:s.get_input("email").val(),addressLines:t,phone:s.get_input("phone").val(),country:s.get_input("country").val(),city:s.get_input("city").val(),postalCode:s.get_input("postcode").val(),region:s.get_input("state").val()};for(let t in a)a[t]&&a[t].length&&(e[t]=a[t]);return e}t.on("charitable:form:validate",async function(e,t){await y(e,p,t)}),t.on("charitable:form:amount:changed",async function(e,t){const a=t.get_amount().toString();if(a!==i){i=a;try{const e=l.paymentRequest({countryCode:CHARITABLE_VARS.country,currencyCode:CHARITABLE_VARS.currency,total:{amount:a,label:"Total"}}),n=[];r;const o=[];for(let a of n)o.push(d(a,e,l,t));await Promise.all(o)}catch(e){console.error("Error updating wallet amount:",e)}}}),c(s),e("#charitable-donation-form").on("change","input[name=recurring_donation]",async()=>{c(s),await u(l,s)})}),t.on("charitable:modal:open",function(){p()}),t.on("charitable:form:loaded",function(e,t){p()}),t.on("charitable:modal:resize",function(){p()})}(jQuery); |