e.json())).then((e=>{t(e)})).catch((e=>{console.log(e),t(!1,e)}))}(n,t,this)}createSubscription(n,t){!async function(n,t,r){console.log(n);let o={priceId:n.json_data.transactions.stripe.price.id,customer:n.json_data.transactions.stripe.customer.id};e(r.service_request_url+"?"+new URLSearchParams({type:"create-subscription",data:JSON.stringify(o)}),{method:"get",headers:{"Content-Type":"application/json"}}).then((e=>e.json())).then((e=>{t(e)})).catch((e=>{console.error("Error:",e),t(e)}))}(n,t,this)}setupView(e,n,t,r,o){return function(e,n,t,r,o,s){let a=t.factory.view();return s.view=a,a.newSubView({id:"payment_details",init:!0,body:t.extra.config.html.stripe_subscription_payment_details,listener:function(){console.log(s);const a=s.processor.json.client_id,i=Stripe(a);let l=s.initialize_payment_details(i,e,n,t);setTimeout((()=>{$("#stripe_start_membership_id").css("display","block"),$("#stripe_start_membership_id").on("click",(function(a){a.preventDefault(),null!=r&&null!=r&&r(l),s.startMemebership(i,l,e,n,t,o)}))}),1e3)}}),a}(e,n,t,r,o,this)}startMemebership(e,n,t,r,o,s){!async function(e,n,t,r,o,s,a){const i=t.clientSecret;e.confirmCardPayment(i,{payment_method:{card:n.cardElement,billing_details:{name:r.firstname+" "+r.lastname}}}).then((e=>{e.error?s(e.error):s(e,t,r)}))}(e,n,t,r,0,s)}initialize_payment_details(e,n,t,r){return function(e,n,t,r,o){const s={theme:"flat",variables:{colorPrimaryText:"#262626"}},a=e.elements({clientSecret:n.clientSecret,appearance:s}),i=a.create("card"),l={mode:"billing"};return a.create("address",l).mount("#address-element"),i.mount("#payment-element"),{cardElement:i,stripe:e}}(e,n)}initializeSubscription(e){!async function(){}()}check(n){!async function(n,t){let r={checking:!0};console.log(t.service_request_url),e(t.service_request_url+"?"+new URLSearchParams({type:"check",data:JSON.stringify(r)}),{method:"get"}).then((e=>e.json())).then((e=>{console.log(e),n(e)})).catch((e=>{console.error(e),n(!1,e)}))}(n,this)}account(){document.addEventListener("DOMContentLoaded",(async()=>{const{subscriptions:e}=await fetch("/subscriptions").then((e=>e.json()));document.querySelector("#subscriptions").innerHTML=e.data.map((e=>{let n=e.default_payment_method?.card?.last4||"";return`\n
\n \n \n \n Status: ${e.status}\n
\n \n \n Card last4: ${n}\n
\n If the last4 is blank, ensure webhooks are being handled. The default payment method is set in the webhook handler.\n \n \n Current period end: ${new Date(1e3*e.current_period_end)}\n
\n \n \x3c!-- Update payment method
\n Change plan
--\x3e\n Cancel
\n `})).join("
")}))}cancel(){document.addEventListener("DOMContentLoaded",(async()=>{const e=new URLSearchParams(window.location.search).get("subscription");document.querySelector("#cancel-btn").addEventListener("click",(async t=>{t.preventDefault(),n("Cancelling subscription...");const{subscription:r}=await fetch("/cancel-subscription",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({subscriptionId:e})}).then((e=>e.json()));n(`Subscription status: ${r.status}`),n("Redirecting back to account in 7s."),setTimeout((()=>{window.location.href="account.html"}),7e3)}));const n=e=>{document.querySelector("#messages").innerHTML+="
"+e}}))}subscribe(){!function(n){const t=e=>{document.querySelector("#messages").innerHTML+="
"+e};let r,o,s={};e(n.service_request_url+"?"+new URLSearchParams({type:"config",data:JSON.stringify(s)}),{method:"get"}).then((e=>e.json())).then((e=>{r=Stripe(e.publishableKey);const n=r.elements();o=n.create("card"),o.mount("#card-element")})).catch((e=>{console.log(e)})),window.sessionStorage.getItem("subscriptionId");const a=window.sessionStorage.getItem("clientSecret");document.querySelector("#subscribe-form").addEventListener("submit",(async e=>{e.preventDefault();const n=document.getElementById("name");r.confirmCardPayment(a,{payment_method:{card:o,billing_details:{name:n.value}}}).then((e=>{e.error?t(`Payment failed: ${e.error.message}`):(t("Success! Redirecting to your account."),window.location.href="/account.html")}))}))}(this)}prices(){_stripe_prices(this)}standalone(n,t){!async function(n,t,r){let o={},s=null;e(r.service_request_url+"?"+new URLSearchParams({type:"keys",data:JSON.stringify(o)}),{method:"get"}).then((e=>e.json())).then((e=>{s=Stripe(e.keys.client_id),t(s)})).catch((e=>{console.log(e),t(null,null)}))}(0,t,this)}singleCharge(n,t,r,o,s){!async function(n,t,r,o,s,a){let i={};e(a.service_request_url+"?"+new URLSearchParams({type:"signle-create-payment-intent",data:JSON.stringify(i)}),{method:"get"}).then((e=>e.json())).then((async e=>{let a=e.clientSecret;if(e.error)return console.log(e),console.log(o),void(null!=o&&null!=o&&o(e));null==n.name&&(n.name=""),null==n.email&&(n.email=""),t(e.paymentIntent,(async function(){const{error:e,paymentIntent:t}=await n.stripe.confirmCardPayment(a,{payment_method:{card:n.card,billing_details:{name:n.name,phone:"713-777-5555",_member_reference_num:n._member_reference_num}},metadata:{}});if(e)return console.log(e),void(null!=s&&null!=s&&s(e));r(t)}))})).catch((e=>{console.error(e)}))}(n,t,r,o,s,this)}}),r=class{constructor(e,n,t,r){this.id=e,this.domain_settings=t,this.managed_domain=r,this.handlers=n,this.handler=this.getHandler(e)}getHandlers(){return this.handlers}getHandler(e){let n=this,t=null;for(const r in n.handlers)if(r==e){t=n.handlers[e];break}return t}setupVariables(e,n){const t=this;let r=t.managed_domain,o=t.domain_settings;for(const s in t.handler.handlers){let a=structuredClone(t.handler.handlers[s]),l=structuredClone(a.variables.schema),c=structuredClone(i(a,o,r));a.variables.schema=[l],a.variables.system=[c],a.variables.client=[n],0==a.alert&&("string"==typeof e&&(e=[e]),a.emails=e),t.handler.handlers[s]=a}}};class o{constructor(e){this.config=e}apicall(e,n,t){const r=this;n.headers.Authorization=`token ${r.config.key}`,async function(){const o=`${r.config.api.url}${e}`;try{const e=await fetch(o,n);if(!e.ok)throw new Error(`Response status: ${e.status}`);const r=await e.json();null!=t&&t(r)}catch(e){console.log(e)}}()}init(){!function(e,n){let t,r=new XMLHttpRequest,o=[];for(t in e)o.push(encodeURIComponent(t)+"="+encodeURIComponent(e[t]));r.open("POST","/portal/admin/core/api/php/request.php",!0),r.setRequestHeader("Content-type","application/x-www-form-urlencoded"),r.onreadystatechange=function(){4==r.readyState&&200==r.status&&n(JSON.parse(r.responseText))},r.send(o.join("&"))}({},(function(){}))}}class s{constructor(e){this.appManager=e,this.configurations=null,this.email_handler=null,this.processor=null,this.current_processor_name="",this.current_processor_object=null,this.stripeProcessor=null,this.paymentsManager=null}initialize(e,t){let r=this;null!=n?n.apicall("/v1/payments/direct_payments",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({key_id:e})},(function(e){r.configurations=e,null==e.message||"error"!=e.message?("stripe"==e.processor.json.processor&&(r.processor=r._init_stripe(),r.current_processor_name=e.processor.processor_name),e.processor.json.processor,e.processor.json.processor,null==r.processor&&console.log("No processor selected!"),r.paymentsManager=new a(e.processor),null!=t&&t()):console.log("Error no direct payment")})):console.log("Appfactory Studio: SDK has not been initialized!")}initializeSubscription(e,n){let t=this;console.log(t.configurations.direct_payment.json.configuration.recurring);let r=t.paymentsManager.getProcessorGroup(e,t.configurations.direct_payment.json.configuration.recurring),o=t.paymentsManager.getLookupKeys(r);console.log(o),t.processor.getSubscriptions(o,n,t)}initializeTier(e,n){this.processor.createMembershipTier(e,n,this)}setupView(e,n,t,r,o){return this.processor.setupView(e,n,t,r,o,this)}createCustomer(e,n){this.processor.createCustomer(e,n,this)}setupSubscription(e,n){this.processor.createSubscription(e,n,this)}setEmailHandler(e){this.email_handler=e}getDirectPayment(){return this.configurations.direct_payment}saveEmailHandlersForPayment(e,n){let t=this;console.log({direct_payment:t.getDirectPayment().json,mail_handlers:t.email_handler.handler.handlers,payment_intent:e}),t.appManager.apicall("/v1/payments/save_handler",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({direct_payment:JSON.stringify(t.getDirectPayment().json),mail_handlers:JSON.stringify(t.email_handler.handler.handlers),payment_intent:JSON.stringify(e)})},(function(e){null!=n&&n(e)}))}build(e){!function(e,n,t){n.stripeProcessor.standalone(e,(function(t){const r=t.elements().create("card");r.mount(e.cardElementId),null==e.submitId&&console.log("Please provide on submit button id for paymen component");let o=document.getElementById(e.submitId.split("#")[1]);o.replaceWith(o.cloneNode(!0)),document.querySelector(e.submitId).addEventListener("click",(async o=>{o.preventDefault();let s={success:!0};null!=e.validate&&null!=e.validate&&(s=e.validate()),s.stripe=t,s.card=r,s.mail_handlers=s.mail_handlers,0!=s.success&&n.stripeProcessor.singleCharge(s,(function(e,t){n.stripeProcessor.directPaymentInstance.saveEmailHandlersForPayment(e,(function(){setTimeout((()=>{n.stripeProcessor.createCustomer({reference_num:s.customer[0],email:s.customer[1],payment_intent:e,mail_handlers:s.mail_handlers},(function(e){null!=s.callback&&null!=s.callback&&"function"==typeof s.callback&&s.callback(e),t()}))}),1e3)}))}),(function(n){null!=e.success&&null!=e.success&&(s=e.success(n))}),(function(n){null!=e.error&&null!=e.error&&(s=e.error(n,null))}),(function(n){null!=e.error&&null!=e.error&&(s=e.error(null,n))}))}))}))}(e,this)}useEmailGroup(e,n,t,o){const s=this;let a=structuredClone(s.getDirectPayment().json.mail_handlers),i=s.app.config.managed_domain,l=s.app.config.configurations.domain_settings[0];s.email_handler=new r(e,a,l,i),null==s.email_handler.handler&&console.log(`Error No email group found for ${e}`),s.email_handler.setupVariables(n,t)}_init_stripe(){let e=this;if(null==document.getElementById("stripe_script_tag_id")){const e=document.createElement("script");e.src="https://js.stripe.com/v3/",e.id="stripe_script_tag_id",document.head.appendChild(e)}return e.stripeProcessor=new t(e.configurations.processor,e,e.configurations.managed_domain),e.stripeProcessor}}class a{constructor(e,n){this.processors=[e],this.access=null,this.app=n}setupNewMember(e,n,t,r){let o=this,s=[new Promise(((e,r)=>{$.post(t.extra.url,{get_configuration:!0,category:n},(function(t){t=JSON.parse(t),e(t.configuration[n])}))}))];Promise.all(s).then((n=>{let t=null;for(let r=0;r