@whop/checkout
Version:
Embed Whop checkout on any website
1 lines • 16 kB
JavaScript
;function e(e,t){if(t==null||t>e.length)t=e.length;for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}function t(e){if(Array.isArray(e))return e}function r(t){if(Array.isArray(t))return e(t)}function n(e){if(e===void 0){throw new ReferenceError("this hasn't been initialised - super() hasn't been called")}return e}function o(e,t,r,n,o,a,i){try{var u=e[a](i);var l=u.value}catch(e){r(e);return}if(u.done){t(l)}else{Promise.resolve(l).then(n,o)}}function a(e){return function(){var t=this,r=arguments;return new Promise(function(n,a){var i=e.apply(t,r);function u(e){o(i,n,a,u,l,"next",e)}function l(e){o(i,n,a,u,l,"throw",e)}u(undefined)})}}function i(e,t,r){t=f(t);return k(e,x()?Reflect.construct(t,r||[],f(e).constructor):t.apply(e,r))}function u(e,t){if(!(e instanceof t)){throw new TypeError("Cannot call a class as a function")}}function l(e,t,r){if(x()){l=Reflect.construct}else{l=function e(e,t,r){var n=[null];n.push.apply(n,t);var o=Function.bind.apply(e,n);var a=new o;if(r)O(a,r.prototype);return a}}return l.apply(null,arguments)}function c(e,t,r){if(t in e){Object.defineProperty(e,t,{value:r,enumerable:true,configurable:true,writable:true})}else{e[t]=r}return e}function f(e){f=Object.setPrototypeOf?Object.getPrototypeOf:function e(e){return e.__proto__||Object.getPrototypeOf(e)};return f(e)}function s(e,t){if(typeof t!=="function"&&t!==null){throw new TypeError("Super expression must either be null or a function")}e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:true,configurable:true}});if(t)O(e,t)}function d(e,t){if(t!=null&&typeof Symbol!=="undefined"&&t[Symbol.hasInstance]){return!!t[Symbol.hasInstance](e)}else{return e instanceof t}}function h(e){return Function.toString.call(e).indexOf("[native code]")!==-1}function v(e){if(typeof Symbol!=="undefined"&&e[Symbol.iterator]!=null||e["@@iterator"]!=null)return Array.from(e)}function p(e,t){var r=e==null?null:typeof Symbol!=="undefined"&&e[Symbol.iterator]||e["@@iterator"];if(r==null)return;var n=[];var o=true;var a=false;var i,u;try{for(r=r.call(e);!(o=(i=r.next()).done);o=true){n.push(i.value);if(t&&n.length===t)break}}catch(e){a=true;u=e}finally{try{if(!o&&r["return"]!=null)r["return"]()}finally{if(a)throw u}}return n}function w(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function y(){throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function m(e){for(var t=1;t<arguments.length;t++){var r=arguments[t]!=null?arguments[t]:{};var n=Object.keys(r);if(typeof Object.getOwnPropertySymbols==="function"){n=n.concat(Object.getOwnPropertySymbols(r).filter(function(e){return Object.getOwnPropertyDescriptor(r,e).enumerable}))}n.forEach(function(t){c(e,t,r[t])})}return e}function b(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);if(t){n=n.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})}r.push.apply(r,n)}return r}function g(e,t){t=t!=null?t:{};if(Object.getOwnPropertyDescriptors){Object.defineProperties(e,Object.getOwnPropertyDescriptors(t))}else{b(Object(t)).forEach(function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))})}return e}function k(e,t){if(t&&(j(t)==="object"||typeof t==="function")){return t}return n(e)}function O(e,t){O=Object.setPrototypeOf||function e(e,t){e.__proto__=t;return e};return O(e,t)}function P(e,r){return t(e)||p(e,r)||_(e,r)||w()}function S(e){return t(e)||v(e)||_(e)||w()}function C(e){return r(e)||v(e)||_(e)||y()}function j(e){"@swc/helpers - typeof";return e&&typeof Symbol!=="undefined"&&e.constructor===Symbol?"symbol":typeof e}function _(t,r){if(!t)return;if(typeof t==="string")return e(t,r);var n=Object.prototype.toString.call(t).slice(8,-1);if(n==="Object"&&t.constructor)n=t.constructor.name;if(n==="Map"||n==="Set")return Array.from(n);if(n==="Arguments"||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))return e(t,r)}function E(e){var t=typeof Map==="function"?new Map:undefined;E=function e(e){if(e===null||!h(e))return e;if(typeof e!=="function"){throw new TypeError("Super expression must either be null or a function")}if(typeof t!=="undefined"){if(t.has(e))return t.get(e);t.set(e,r)}function r(){return l(e,arguments,f(this).constructor)}r.prototype=Object.create(e.prototype,{constructor:{value:r,enumerable:false,writable:true,configurable:true}});return O(r,e)};return E(e)}function x(){try{var e=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(e){}return(x=function(){return!!e})()}function A(e,t){var r,n,o,a={label:0,sent:function(){if(o[0]&1)throw o[1];return o[1]},trys:[],ops:[]},i=Object.create((typeof Iterator==="function"?Iterator:Object).prototype);return i.next=u(0),i["throw"]=u(1),i["return"]=u(2),typeof Symbol==="function"&&(i[Symbol.iterator]=function(){return this}),i;function u(e){return function(t){return l([e,t])}}function l(u){if(r)throw new TypeError("Generator is already executing.");while(i&&(i=0,u[0]&&(a=0)),a)try{if(r=1,n&&(o=u[0]&2?n["return"]:u[0]?n["throw"]||((o=n["return"])&&o.call(n),0):n.next)&&!(o=o.call(n,u[1])).done)return o;if(n=0,o)u=[u[0]&2,o.value];switch(u[0]){case 0:case 1:o=u;break;case 4:a.label++;return{value:u[1],done:false};case 5:a.label++;n=u[1];u=[0];continue;case 7:u=a.ops.pop();a.trys.pop();continue;default:if(!(o=a.trys,o=o.length>0&&o[o.length-1])&&(u[0]===6||u[0]===2)){a=0;continue}if(u[0]===3&&(!o||u[1]>o[0]&&u[1]<o[3])){a.label=u[1];break}if(u[0]===6&&a.label<o[1]){a.label=o[1];o=u;break}if(o&&a.label<o[2]){a.label=o[2];a.ops.push(u);break}if(o[2])a.ops.pop();a.trys.pop();continue}u=t.call(e,a)}catch(e){u=[6,e];n=0}finally{r=o=0}if(u[0]&5)throw u[1];return{value:u[0]?u[1]:void 0,done:true}}}(function(){var e=function e(e){return j(e.data)=="object"&&e.data!==null&&"event"in e.data&&M.includes(e.data.event)};var t=function t(t,r){return e(t)&&t.data.event===r};var r=function e(e){return U[e]||(U[e]=(e+256).toString(16).slice(1))};var n=function e(){var e=new Uint8Array(16);if((typeof crypto==="undefined"?"undefined":j(crypto))<"u"&&typeof crypto.getRandomValues=="function")crypto.getRandomValues(e);else for(var t=0;t<16;t++)e[t]=Math.random()*256|0;return e[6]=e[6]&15|64,e[8]=e[8]&63|128,r(e[0])+r(e[1])+r(e[2])+r(e[3])+"-"+r(e[4])+r(e[5])+"-"+r(e[6])+r(e[7])+"-"+r(e[8])+r(e[9])+"-"+r(e[10])+r(e[11])+r(e[12])+r(e[13])+r(e[14])+r(e[15])};var o=function e(e,r,o,a){var i=arguments.length>4&&arguments[4]!==void 0?arguments[4]:2e3;var u;var l=new URL(e.src).origin,c=n();return(u=e.contentWindow)===null||u===void 0?void 0:u.postMessage(g(m({},r),{__scope:"whop-embedded-checkout",event_id:c}),l),new Promise(function(r,n){var u=setTimeout(function(){n(new L),window.removeEventListener("message",l)},i),l=function(i){if(i.source===e.contentWindow&&t(i,o)&&i.data.event===o&&i.data.event_id===c){clearTimeout(u),window.removeEventListener("message",l);try{r(a(i.data))}catch(e){n(e)}}};window.addEventListener("message",l)})};var l=function t(t,r){function n(n){n.source===t.contentWindow&&e(n)&&r(n.data)}return window.addEventListener("message",n),function(){window.removeEventListener("message",n)}};var c=function e(e,t){var r=arguments.length>2&&arguments[2]!==void 0?arguments[2]:2e3;return a(function(){return A(this,function(n){return[2,o(e,{event:"set-email",email:t},"set-email-result",function(e){if(!e.ok)throw new W(e.error)},r)]})})()};var f=function e(e){var t=arguments.length>1&&arguments[1]!==void 0?arguments[1]:2e3;return a(function(){return A(this,function(r){return[2,o(e,{event:"get-email"},"get-email-result",function(e){return e.email},t)]})})()};var h=function e(e,t){var r;var n=new URL(e.src).origin;(r=e.contentWindow)===null||r===void 0?void 0:r.postMessage({__scope:"whop-embedded-checkout",event:"submit"},n)};var v=function e(e,t,r,n,o,a,i,u,l,c,f,s,d,h){var v=new URL("/embedded/checkout/".concat(e,"/"),n!==null&&n!==void 0?n:"https://whop.com/");if(v.searchParams.set("h",window.location.origin),t&&v.searchParams.set("theme",t),r&&v.searchParams.set("session",r),o&&v.searchParams.set("hide_price","true"),a&&v.searchParams.set("skip_redirect","true"),f&&v.searchParams.set("hide_submit_button","true"),s&&v.searchParams.set("hide_tos","true"),d&&v.searchParams.set("email.hidden","1"),h&&v.searchParams.set("email.disabled","1"),i){var p=true,w=false,y=undefined,m=true,b=false,g=undefined;try{for(var k=Object.entries(i).sort(function(e,t){return e[0].localeCompare(t[0])})[Symbol.iterator](),O;!(m=(O=k.next()).done);m=true){var S=P(O.value,2),C=S[0],j=S[1];if(C.startsWith("utm_"))if(Array.isArray(j))try{for(var _=j[Symbol.iterator](),E;!(p=(E=_.next()).done);p=true){var x=E.value;v.searchParams.append(C,x)}}catch(e){w=true;y=e}finally{try{if(!p&&_.return!=null){_.return()}}finally{if(w){throw y}}}else v.searchParams.set(C,j)}}catch(e){b=true;g=e}finally{try{if(!m&&k.return!=null){k.return()}}finally{if(b){throw g}}}}if(u){var A=true,R=false,T=undefined,I=true,L=false,W=undefined;try{for(var M=Object.entries(u)[Symbol.iterator](),U;!(I=(U=M.next()).done);I=true){var F=P(U.value,2),D=F[0],H=F[1];if(H)try{for(var N=Object.entries(H)[Symbol.iterator](),B;!(A=(B=N.next()).done);A=true){var V=P(B.value,2),q=V[0],z=V[1];v.searchParams.set("style.".concat(D,".").concat(q),z.toString())}}catch(e){R=true;T=e}finally{try{if(!A&&N.return!=null){N.return()}}finally{if(R){throw T}}}}}catch(e){L=true;W=e}finally{try{if(!I&&M.return!=null){M.return()}}finally{if(L){throw W}}}}var G=true,$=false,J=undefined;if((l===null||l===void 0?void 0:l.email)&&v.searchParams.set("email",l.email),c)try{for(var K=Object.entries(c)[Symbol.iterator](),Q;!(G=(Q=K.next()).done);G=true){var X=P(Q.value,2),Y=X[0],Z=X[1];Z&&v.searchParams.set("theme.".concat(Y),Z)}}catch(e){$=true;J=e}finally{try{if(!G&&K.return!=null){K.return()}}finally{if($){throw J}}}return v.toString()};var p=function e(e){for(var t=arguments.length,r=new Array(t>1?t-1:0),n=1;n<t;n++){r[n-1]=arguments[n]}if(!e)return;var o=window[e];o===null||o===void 0?void 0:o.apply(void 0,C(r))};var w=function e(e,t){var r;e.addEventListener("checkout:submit",function(t){h(e,t.detail)}),(r=window.wco)===null||r===void 0?void 0:r.frames.set(e,l(e,function(r){switch(r.event){case"resize":{e.style.height="".concat(r.height,"px");break}case"center":{e.scrollIntoView({block:"center",inline:"center"});break}case"complete":{p(t.dataset.whopCheckoutOnComplete,r.plan_id,r.receipt_id);break}case"state":{p(t.dataset.whopCheckoutOnStateChange,r.state);break}}}))};var y=function e(){var e=new URLSearchParams(window.location.search);return Array.from(e.keys()).reduce(function(t,r){if(!r.startsWith("utm_"))return t;var n=e.getAll(r);switch(n.length){case 0:return t;case 1:{t[r]=n[0];break}default:t[r]=n}return t},{})};var b=function e(e){var t={};var r=true,n=false,o=undefined;try{for(var a=e.attributes[Symbol.iterator](),i;!(r=(i=a.next()).done);r=true){var u=i.value;if(u.name.startsWith(H)){var l=u.name.slice(H.length),c=u.value,f=S(l.split("-")),s=f[0],d=f.slice(1);if(s&&d.length>0){var h=d.reduce(function(e,t,r){if(r===0)return t;var n=S(t),o=n[0],a=n.slice(1);return"".concat(e).concat(o.toUpperCase()).concat(a.join(""))},"");var v;(v=t[s])!==null&&v!==void 0?v:t[s]={},t[s][h]=c}}}}catch(e){n=true;o=e}finally{try{if(!r&&a.return!=null){a.return()}}finally{if(n){throw o}}}return t};var k=function e(e){var t={};return e.dataset.whopCheckoutThemeAccentColor&&(t.accentColor=e.dataset.whopCheckoutThemeAccentColor),t};var O=function e(e){var t={};return e.dataset.whopCheckoutPrefillEmail&&(t.email=e.dataset.whopCheckoutPrefillEmail),t};var _=function e(e){var t;var r;if(e.dataset.whopCheckoutMounted)return;var n=e.dataset.whopCheckoutPlanId;if(!n)return;var o=v(n,e.dataset.whopCheckoutTheme,e.dataset.whopCheckoutSession,e.dataset.whopCheckoutOrigin,e.dataset.whopCheckoutHidePrice==="true",e.dataset.whopCheckoutSkipRedirect==="true"||!!e.dataset.whopCheckoutOnComplete,e.dataset.whopCheckoutSkipUtm==="true"?void 0:y(),b(e),O(e),k(e),e.dataset.whopCheckoutHideSubmitButton==="true",e.dataset.whopCheckoutHideTos==="true",e.dataset.whopCheckoutHideEmail==="true",e.dataset.whopCheckoutDisableEmail==="true"),a=document.createElement("iframe");a.src=o,a.style.width="100%",a.style.height="480px",a.style.border="none",a.style.overflow="hidden",(t=a.sandbox).add.apply(t,C(F)),a.allow=D,e.dataset.whopCheckoutMounted="true",e.appendChild(a);var i=e.id;i&&((r=window.wco)===null||r===void 0?void 0:r.identifiedFrames.set(i,a),a.dataset.whopCheckoutIdentifier=i),w(a,e)};var x=Object.defineProperty;var R=function(e,t,r){return t in e?x(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r};var T=function(e,t,r){return R(e,(typeof t==="undefined"?"undefined":j(t))!="symbol"?t+"":t,r)};var I=/*#__PURE__*/function(e){s(t,e);function t(){u(this,t);var e;e=i(this,t,arguments);T(e,"type","WhopCheckoutError");return e}return t}(E(Error));var L=/*#__PURE__*/function(e){s(t,e);function t(e){u(this,t);var r;r=i(this,t,[e!==null&&e!==void 0?e:"Timeout waiting for embed response"]);T(r,"name","WhopCheckoutRpcTimeoutError");return r}return t}(I);var W=/*#__PURE__*/function(e){s(t,e);function t(){u(this,t);var e;e=i(this,t,arguments);T(e,"name","WhopCheckoutSetEmailError");return e}return t}(I);var M=["resize","center","complete","state","get-email-result","set-email-result"];var U=[];var F=["allow-forms","allow-modals","allow-orientation-lock","allow-pointer-lock","allow-popups","allow-presentation","allow-same-origin","allow-scripts","allow-top-navigation","allow-top-navigation-by-user-activation","allow-downloads"],D="document-domain; execution-while-not-rendered; execution-while-out-of-viewport; payment; paymentRequest; sync-script;";var H="data-whop-checkout-style-";if((typeof window==="undefined"?"undefined":j(window))<"u"&&window.wco&&!window.wco.listening){window.wco.getEmail=function(e,t){var r;var n=(r=window.wco)===null||r===void 0?void 0:r.identifiedFrames.get(e);if(!n)throw new Error("Failed to get email for Whop embedded checkout. No embed with identifier ".concat(e," found."));return f(n,t)},window.wco.setEmail=function(e,t,r){var n;var o=(n=window.wco)===null||n===void 0?void 0:n.identifiedFrames.get(e);if(!o)throw new Error("Failed to set email for Whop embedded checkout. No embed with identifier ".concat(e," found."));return c(o,t,r)};var N=new MutationObserver(function(e){var t=true,r=false,n=undefined;try{for(var o=e[Symbol.iterator](),a;!(t=(a=o.next()).done);t=true){var i=a.value;var u,l,c;var f=true,s=false,h=undefined;try{for(var v=i.addedNodes[Symbol.iterator](),p;!(f=(p=v.next()).done);f=true){var w=p.value;d(w,HTMLElement)&&w.dataset.whopCheckoutPlanId&&_(w)}}catch(e){s=true;h=e}finally{try{if(!f&&v.return!=null){v.return()}}finally{if(s){throw h}}}var y=Array.from(i.removedNodes);var m;var b=true,g=false,k=undefined;try{for(var O=((m=(u=window.wco)===null||u===void 0?void 0:u.frames)!==null&&m!==void 0?m:[])[Symbol.iterator](),S;!(b=(S=O.next()).done);b=true){var C=P(S.value,2),j=C[0],E=C[1];y.includes(j)&&(j.dataset.whopCheckoutIdentifier&&((l=window.wco)===null||l===void 0?void 0:l.identifiedFrames.delete(j.dataset.whopCheckoutIdentifier)),E(),(c=window.wco)===null||c===void 0?void 0:c.frames.delete(j))}}catch(e){g=true;k=e}finally{try{if(!b&&O.return!=null){O.return()}}finally{if(g){throw k}}}}}catch(e){r=true;n=e}finally{try{if(!t&&o.return!=null){o.return()}}finally{if(r){throw n}}}});var B=true,V=false,q=undefined;try{for(var z=document.querySelectorAll("[data-whop-checkout-plan-id]")[Symbol.iterator](),G;!(B=(G=z.next()).done);B=true){var $=G.value;d($,HTMLElement)&&_($)}}catch(e){V=true;q=e}finally{try{if(!B&&z.return!=null){z.return()}}finally{if(V){throw q}}}N.observe(document.body,{childList:!0,subtree:!0}),window.wco.listening=!0}})();