/* room service · tier changes on /join/ for signed-in members (comped → checkout, paid → switch plan) */ (function(){ if(!/^\/join\/?$/.test(location.pathname))return; var KEY='b3d8edcbe4375e7fcc6c2853a3',YM=9; function eur(c){return '€'+(c%100?(c/100).toFixed(2):c/100)} function el(t,css,txt){var e=document.createElement(t);if(css)e.style.cssText=css;if(txt!=null)e.textContent=txt;return e} var BTN='display:block;width:100%;box-sizing:border-box;margin-top:8px;border:0;border-radius:999px;background:#34414C;color:#F2E2CA;padding:10px 8px;font:inherit;font-size:12px;cursor:pointer'; var GHOST='display:block;width:100%;box-sizing:border-box;margin-top:8px;border:1px solid #34414C;border-radius:999px;background:transparent;color:#34414C;padding:9px 8px;font:inherit;font-size:12px;cursor:pointer'; function price(c){var p=c.querySelector('.tier__price'),m=p&&p.textContent.replace(/\s/g,'').match(/(\d+(?:[.,]\d+)?)/);return m?Math.round(parseFloat(m[1].replace(',','.'))*100):null} async function identity(){return (await fetch('/members/api/session',{credentials:'include'})).text()} async function checkout(t,cad,b){b.disabled=true;b.textContent='one moment…';try{var id=await identity();var r=await fetch('/members/api/create-stripe-checkout-session/',{method:'POST',credentials:'include',headers:{'Content-Type':'application/json'},body:JSON.stringify({identity:id,tierId:t.id,cadence:cad,successUrl:location.origin+'/?action=checkout&success=true',cancelUrl:location.href,metadata:{requestSrc:'join'}})});var j=await r.json();if(j.url){location.href=j.url;return}throw 0}catch(e){b.disabled=false;b.textContent='something went wrong, try again'}} document.addEventListener('DOMContentLoaded',async function(){try{ var r=await fetch('/members/api/member/',{credentials:'include'});if(r.status!==200)return;var m=await r.json(); if(m.status==='free')return;/* free members are handled by the join buttons in the head file */ var T=(await (await fetch('/ghost/api/content/tiers/?key='+KEY+'&limit=all')).json()).tiers||[]; var sub=(m.subscriptions||[]).find(function(s){return s.id&&(s.status==='active'||s.status==='trialing'||s.status==='past_due')&&s.price&&s.price.amount>0}); var comp=(m.subscriptions||[]).find(function(s){return !s.id&&s.tier}),compN=comp?comp.tier.name.toLowerCase():null;var cur=sub&&sub.tier?sub.tier.name.toLowerCase():null,curCad=sub&&sub.price?sub.price.interval:'month'; document.querySelectorAll('.tiers--join .tier').forEach(function(c){ if(c.querySelector('.tier__cta,.rs-mj,.rs-tc'))return; var n=((c.querySelector('.tier__name')||{}).textContent||'').trim().toLowerCase(); var t=T.find(function(x){return x.type==='paid'&&x.name.toLowerCase()===n});if(!t)return; var w=el('div','margin-top:14px');w.className='rs-tc';var mp=price(c),yp=mp!=null?mp*YM:null; if(sub&&cur===n){w.appendChild(el('div','text-align:center;font-size:11px;letter-spacing:.14em;text-transform:uppercase;border:1px solid currentColor;border-radius:999px;padding:7px','your place'));c.appendChild(w);return} if(!sub){/* complimentary or gift access: make it a paid membership */if(compN===n)w.appendChild(el('div','text-align:center;font-size:11px;letter-spacing:.14em;text-transform:uppercase;opacity:.75;margin-bottom:4px','you\u2019re here as a guest')); [['month',(mp!=null?eur(mp)+' a month':'monthly')],['year',(yp!=null?eur(yp)+' a year · three months on me':'yearly')]].forEach(function(p){var b=el('button',BTN,p[1]);b.type='button';b.onclick=function(){checkout(t,p[0],b)};w.appendChild(b)});c.appendChild(w);return} var go=el('button',BTN,'switch to '+n);go.type='button';w.appendChild(go);c.appendChild(w); go.onclick=function(){ go.style.display='none';var cad=curCad;var box=el('div','margin-top:8px;font-size:12px;line-height:1.5'); var opts=el('div','display:grid;grid-template-columns:1fr 1fr;gap:6px'); function pick(v){cad=v;[].forEach.call(opts.children,function(o){var on=o.dataset.v===v;o.style.background=on?'#34414C':'transparent';o.style.color=on?'#F2E2CA':'#34414C'})} [['month',mp!=null?eur(mp)+'/mo':'monthly'],['year',yp!=null?eur(yp)+'/yr':'yearly']].forEach(function(p){var o=el('button',GHOST.replace('margin-top:8px;',''),p[1]);o.type='button';o.dataset.v=p[0];o.onclick=function(){pick(p[0])};opts.appendChild(o)}); box.appendChild(opts);box.appendChild(el('p','margin:10px 0 0;opacity:.75','your plan changes right away. stripe charges or credits the difference on your card.')); var ok=el('button',BTN,'confirm switch');ok.type='button';var no=el('button',GHOST,'keep my place');no.type='button';box.appendChild(ok);box.appendChild(no); var msg=el('p','margin:8px 0 0;color:#B58181');box.appendChild(msg);w.appendChild(box);pick(cad); no.onclick=function(){box.remove();go.style.display=''}; ok.onclick=async function(){ok.disabled=true;ok.textContent='one moment…';try{var id=await identity();var res=await fetch('/members/api/subscriptions/'+sub.id+'/',{method:'PUT',credentials:'include',headers:{'Content-Type':'application/json'},body:JSON.stringify({identity:id,tierId:t.id,cadence:cad})});if(!res.ok)throw 0;box.innerHTML='';box.appendChild(el('p','margin:8px 0 0','done. welcome to '+n+'.'));setTimeout(function(){location.reload()},1600)}catch(e){ok.disabled=false;ok.textContent='confirm switch';msg.textContent='that did not go through. try again, or write to music@unders.nl.'}}; }; }); }catch(e){}}); })();