UNPKG

@whop/checkout

Version:

Embed Whop checkout on any website

1 lines 15.2 kB
function e(e,r){if(r==null||r>e.length)r=e.length;for(var n=0,t=new Array(r);n<r;n++)t[n]=e[n];return t}function r(e){if(Array.isArray(e))return e}function n(r){if(Array.isArray(r))return e(r)}function t(e,r,n,t,o,i,a){try{var l=e[i](a);var u=l.value}catch(e){n(e);return}if(l.done){r(u)}else{Promise.resolve(u).then(t,o)}}function o(e){return function(){var r=this,n=arguments;return new Promise(function(o,i){var a=e.apply(r,n);function l(e){t(a,o,i,l,u,"next",e)}function u(e){t(a,o,i,l,u,"throw",e)}l(undefined)})}}function i(e,r,n){if(r in e){Object.defineProperty(e,r,{value:n,enumerable:true,configurable:true,writable:true})}else{e[r]=n}return e}function a(e){if(typeof Symbol!=="undefined"&&e[Symbol.iterator]!=null||e["@@iterator"]!=null)return Array.from(e)}function l(e,r){var n=e==null?null:typeof Symbol!=="undefined"&&e[Symbol.iterator]||e["@@iterator"];if(n==null)return;var t=[];var o=true;var i=false;var a,l;try{for(n=n.call(e);!(o=(a=n.next()).done);o=true){t.push(a.value);if(r&&t.length===r)break}}catch(e){i=true;l=e}finally{try{if(!o&&n["return"]!=null)n["return"]()}finally{if(i)throw l}}return t}function u(){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 d(){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 c(e){for(var r=1;r<arguments.length;r++){var n=arguments[r]!=null?arguments[r]:{};var t=Object.keys(n);if(typeof Object.getOwnPropertySymbols==="function"){t=t.concat(Object.getOwnPropertySymbols(n).filter(function(e){return Object.getOwnPropertyDescriptor(n,e).enumerable}))}t.forEach(function(r){i(e,r,n[r])})}return e}function s(e,r){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var t=Object.getOwnPropertySymbols(e);if(r){t=t.filter(function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable})}n.push.apply(n,t)}return n}function v(e,r){r=r!=null?r:{};if(Object.getOwnPropertyDescriptors){Object.defineProperties(e,Object.getOwnPropertyDescriptors(r))}else{s(Object(r)).forEach(function(n){Object.defineProperty(e,n,Object.getOwnPropertyDescriptor(r,n))})}return e}function f(e,r){if(e==null)return{};var n=p(e,r);var t,o;if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(e);for(o=0;o<i.length;o++){t=i[o];if(r.indexOf(t)>=0)continue;if(!Object.prototype.propertyIsEnumerable.call(e,t))continue;n[t]=e[t]}}return n}function p(e,r){if(e==null)return{};var n={};var t=Object.keys(e);var o,i;for(i=0;i<t.length;i++){o=t[i];if(r.indexOf(o)>=0)continue;n[o]=e[o]}return n}function m(e,n){return r(e)||l(e,n)||b(e,n)||u()}function h(e){return n(e)||a(e)||b(e)||d()}function b(r,n){if(!r)return;if(typeof r==="string")return e(r,n);var t=Object.prototype.toString.call(r).slice(8,-1);if(t==="Object"&&r.constructor)t=r.constructor.name;if(t==="Map"||t==="Set")return Array.from(t);if(t==="Arguments"||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t))return e(r,n)}function y(e,r){var n,t,o,i={label:0,sent:function(){if(o[0]&1)throw o[1];return o[1]},trys:[],ops:[]},a=Object.create((typeof Iterator==="function"?Iterator:Object).prototype);return a.next=l(0),a["throw"]=l(1),a["return"]=l(2),typeof Symbol==="function"&&(a[Symbol.iterator]=function(){return this}),a;function l(e){return function(r){return u([e,r])}}function u(l){if(n)throw new TypeError("Generator is already executing.");while(a&&(a=0,l[0]&&(i=0)),i)try{if(n=1,t&&(o=l[0]&2?t["return"]:l[0]?t["throw"]||((o=t["return"])&&o.call(t),0):t.next)&&!(o=o.call(t,l[1])).done)return o;if(t=0,o)l=[l[0]&2,o.value];switch(l[0]){case 0:case 1:o=l;break;case 4:i.label++;return{value:l[1],done:false};case 5:i.label++;t=l[1];l=[0];continue;case 7:l=i.ops.pop();i.trys.pop();continue;default:if(!(o=i.trys,o=o.length>0&&o[o.length-1])&&(l[0]===6||l[0]===2)){i=0;continue}if(l[0]===3&&(!o||l[1]>o[0]&&l[1]<o[3])){i.label=l[1];break}if(l[0]===6&&i.label<o[1]){i.label=o[1];o=l;break}if(o&&i.label<o[2]){i.label=o[2];i.ops.push(l);break}if(o[2])i.ops.pop();i.trys.pop();continue}l=r.call(e,i)}catch(e){l=[6,e];t=0}finally{n=o=0}if(l[0]&5)throw l[1];return{value:l[0]?l[1]:void 0,done:true}}}import{a as g,c as C}from"../chunk-V6AIUOX7.mjs";import{c as w,d as k,e as O,f as E,g as A,h as j,i as I,j as P,k as S,n as _,r as x,s as R,t as U}from"../chunk-VM3BGBF7.mjs";import W,{forwardRef as F,useEffect as M,useImperativeHandle as D,useMemo as L,useRef as V,useState as B}from"react";import{useSyncExternalStore as N}from"react";function T(){return function(){}}function z(){return N(T,function(){return!0},function(){return!1})}import{useRef as G}from"react";var H=Symbol("none");function J(e){var r=G(H);return r.current===H&&(r.current=e()),r}var X=["tomato","red","ruby","crimson","pink","plum","purple","violet","iris","cyan","teal","jade","green","grass","brown","blue","orange","indigo","sky","mint","yellow","amber","lime","lemon","magenta","gold","bronze","gray"];function $(e){return e?X.includes(e):!1}import{useEffect as q,useMemo as K}from"react";function Q(){for(var e=arguments.length,r=new Array(e),n=0;n<e;n++){r[n]=arguments[n]}var t=J(function(){return _.apply(void 0,h(r))}),o=t.current;return Y.apply(void 0,[o].concat(h(r))),o}function Y(e){for(var r=arguments.length,n=new Array(r>1?r-1:0),t=1;t<r;t++){n[t-1]=arguments[t]}var o=m(n,25),i=o[0],a=o[1],l=o[2],u=o[3],d=o[4],c=o[5],s=o[6],v=o[7],f=o[8],p=o[9],h=o[10],b=o[11],y=o[12],g=o[13],C=o[14],w=o[15],k=o[16],O=o[17],E=o[18],A=o[19],j=o[20],I=o[21],P=o[22],S=o[23],x=o[24];var R=K(function(){return _(i,a,l,void 0,d,c,s,v,f,p,h,b,y,g,C,w,k,O,E,A,j,I,P,S,x)},[i,a,l,d,c,s,v,f,p,h,b,y,g,C,w,k,O,E,A,j,I,P,S,x]);q(function(){e!==R&&process.env.NODE_ENV==="development"&&console.warn("[WhopCheckoutEmbed] iframeUrl changed from ".concat(e," to ").concat(R,". Updating props on the checkout embed is not supported. Please rerender the component."))},[e,R])}var Z=x.join(" ");var ee=F(function(e,r){var n=e.theme,t=e.hidePrice,i=t===void 0?!1:t,a=e.skipRedirect,l=a===void 0?!1:a,u=e.onComplete,d=e.onStateChange,s=e.onAddressValidationError,p=e.onPaymentError,h=e.onPromoCodeChanged,b=e.onIdentityCaptured,C=e.onCurrenciesAvailable,_=e.onCurrencyChanged,x=e.utm,F=e.styles,N=e.prefill,T=e.themeOptions,z=e.hideSubmitButton,G=z===void 0?!1:z,H=e.hideTermsAndConditions,X=H===void 0?!1:H,$=e.hideEmail,q=$===void 0?!1:$,K=e.disableEmail,Y=K===void 0?!1:K,ee=e.hideAddressForm,er=ee===void 0?!1:ee,en=e.affiliateCode,et=e.setupFutureUsage,eo=e.redirectUrl,ei=e.returnUrl,ea=e.stateId,el=e.promoCode,eu=e.environment,ed=e.wuid,ec=e.adaptivePricing,es=ec===void 0?!1:ec,ev=e.collectPhoneNumbers,ef=e.locale,ep=f(e,["theme","hidePrice","skipRedirect","onComplete","onStateChange","onAddressValidationError","onPaymentError","onPromoCodeChanged","onIdentityCaptured","onCurrenciesAvailable","onCurrencyChanged","utm","styles","prefill","themeOptions","hideSubmitButton","hideTermsAndConditions","hideEmail","disableEmail","hideAddressForm","affiliateCode","setupFutureUsage","redirectUrl","returnUrl","stateId","promoCode","environment","wuid","adaptivePricing","collectPhoneNumbers","locale"]);var em="planId"in ep?ep.planId:void 0,eh="sessionId"in ep?ep.sessionId:void 0,eb=L(function(){var e;return{accentColor:T===null||T===void 0?void 0:T.accentColor,highContrast:(e=T===null||T===void 0?void 0:T.highContrast)!==null&&e!==void 0?e:!1,backgroundColor:T===null||T===void 0?void 0:T.backgroundColor,borderRadius:T===null||T===void 0?void 0:T.borderRadius}},[T===null||T===void 0?void 0:T.accentColor,T===null||T===void 0?void 0:T.highContrast,T===null||T===void 0?void 0:T.backgroundColor,T===null||T===void 0?void 0:T.borderRadius]),ey=Q(em,n,eh,void 0,i,l||!!u,x,F,N,eb,G,X,q,Y,er,en,S(et),ei!==null&&ei!==void 0?ei:eo,ea,el,eu,ed,es,ev,ef),eg=V(null),eC=m(B(480),2),ew=eC[0],ek=eC[1],eO=V(null),eE=V({theme:n,environment:eu,locale:ef});eE.current={theme:n,environment:eu,locale:ef};var eA=J(function(){return g({getOverlayContext:function(){var e;return{theme:eE.current.theme,origin:void 0,environment:(e=eE.current.environment)!==null&&e!==void 0?e:"production",locale:eE.current.locale}},onOverlayClose:function(){var e=eg.current;if(e)try{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:"overlay-closed"},n)}catch(e){}}})});return M(function(){var e=eg.current;if(e)return k(e,function(r){switch(r.event){case"resize":{ek(r.height);break}case"center":{e.scrollIntoView({block:"center",inline:"center"});break}case"complete":{if(u){var n=r.plan_id,t=r.setup_intent_id||r.receipt_id;u(n,t)}eA.current.scheduleAutoCloseAfterComplete();break}case"state":{d&&d(r.state);break}case"address-validation-error":{s&&s({error_message:r.error_message,error_code:r.error_code});break}case"payment-error":{p&&p({message:r.message,code:r.code});break}case"promo-code-changed":{h&&h(r.promoCode);break}case"identity-captured":{U(r),b&&b({email:r.email,user_id:r.user_id});break}case"currencies-available":{var o={base_currency:r.base_currency,optional_currency:r.optional_currency,current_currency:r.current_currency,exchange_rate:r.exchange_rate};eO.current=o,C&&C(o);break}case"currency-changed":{eO.current&&(eO.current=v(c({},eO.current),{current_currency:r.currency,exchange_rate:r.exchange_rate})),_&&_({currency:r.currency,exchange_rate:r.exchange_rate});break}}},eA.current)},[u,d,s,p,h,b,C,_]),M(function(){return function(){eg.current&&w.remove(eg.current),eA.current.closeOwnOverlay()}},[]),D(r,function(){return{submit:function(e){return o(function(){return y(this,function(r){switch(r.label){case 0:if(!eg.current)return[3,2];return[4,P(eg.current,e)];case 1:return[2,r.sent()];case 2:return[2]}})})()},getEmail:function(e){if(!eg.current)throw new Error("Whop embedded checkout frame not found");return E(eg.current,e)},setEmail:function(e,r){if(!eg.current)throw new Error("Whop embedded checkout frame not found");return O(eg.current,e,r)},setAddress:function(e,r){if(!eg.current)throw new Error("Whop embedded checkout frame not found");return A(eg.current,e,r)},getAddress:function(e){if(!eg.current)throw new Error("Whop embedded checkout frame not found");return I(eg.current,e)},setDisplayCurrency:function(e,r){if(!eg.current)throw new Error("Whop embedded checkout frame not found");return j(eg.current,e,r)},getAvailableCurrencies:function(){return eO.current}}},[]),W.createElement("iframe",{ref:eg,allow:R,sandbox:Z,title:"Whop Embedded Checkout",src:ey!==null&&ey!==void 0?ey:void 0,style:{border:"none",height:"".concat(ew,"px"),width:"100%",overflow:"hidden",background:"transparent",colorScheme:"normal"}})});ee.displayName="WhopCheckoutEmbedInner";var er=F(function(e,r){var n=e.fallback,t=f(e,["fallback"]);return z()?W.createElement(ee,v(c({},t),{ref:r})):n!==null&&n!==void 0?n:null});er.displayName="WhopCheckoutEmbed";import en,{useEffect as et,useRef as eo,useState as ei}from"react";C();function ea(e){var r=e.methods,n=e.theme,t=e.themeOptions,o=e.prefill,i=e.affiliateCode,a=e.returnUrl,l=e.stateId,u=e.promoCode,d=e.setupFutureUsage,c=e.skipRedirect,s=c===void 0?!1:c,v=e.environment,p=e.wuid,h=e.adaptivePricing,b=h===void 0?!1:h,y=e.locale,g=e.onComplete,C=e.onExpressMethodResolved,w=e.fallback,k=f(e,["methods","theme","themeOptions","prefill","affiliateCode","returnUrl","stateId","promoCode","setupFutureUsage","skipRedirect","environment","wuid","adaptivePricing","locale","onComplete","onExpressMethodResolved","fallback"]);var O,E,A,j,I,P,S,_,x,R,U,W,F,M;var D="planId"in k?k.planId:void 0,L="checkoutConfigurationId"in k?k.checkoutConfigurationId:void 0,V=eo(null),B=m(ei(!1),2),N=B[0],T=B[1],z=eo(g),G=eo(C);z.current=g,G.current=C,et(function(){var e;var r=V.current;if(!r)return;var n=function(e,r){return e==="ready"||e==="error"||r==="none"};n(r.dataset.state,r.dataset.rendered)&&T(!0);var t=r.dataset.rendered;t&&((e=G.current)===null||e===void 0?void 0:e.call(G,{rendered:t}));var o=new MutationObserver(function(){n(r.dataset.state,r.dataset.rendered)&&T(!0)});o.observe(r,{attributes:!0,attributeFilter:["data-state","data-rendered"]});var i=function(){return T(!0)},a=function(e){var r;var n=e.detail;n.rendered==="none"&&T(!0),(r=G.current)===null||r===void 0?void 0:r.call(G,{rendered:n.rendered})},l=function(e){var r;var n=e.detail;(r=z.current)===null||r===void 0?void 0:r.call(z,n.planId,n.receiptOrSetupIntentId)};return r.addEventListener("ready",i),r.addEventListener("express-method-resolved",a),r.addEventListener("complete",l),function(){o.disconnect(),r.removeEventListener("ready",i),r.removeEventListener("express-method-resolved",a),r.removeEventListener("complete",l)}},[]);var H=$(t===null||t===void 0?void 0:t.accentColor)?t.accentColor:void 0,J=s||!!g;return en.createElement(en.Fragment,null,en.createElement("whop-express-checkout-button",{ref:V,"plan-id":D,"checkout-configuration-id":L,"return-url":a,methods:r===null||r===void 0?void 0:r.join(","),theme:n,"theme-accent-color":H,"theme-high-contrast":(t===null||t===void 0?void 0:t.highContrast)?"true":void 0,"prefill-email":o===null||o===void 0?void 0:o.email,"prefill-name":o===null||o===void 0?void 0:(O=o.address)===null||O===void 0?void 0:O.name,"prefill-address-line1":o===null||o===void 0?void 0:(E=o.address)===null||E===void 0?void 0:E.line1,"prefill-address-line2":o===null||o===void 0?void 0:(A=o.address)===null||A===void 0?void 0:A.line2,"prefill-address-city":o===null||o===void 0?void 0:(j=o.address)===null||j===void 0?void 0:j.city,"prefill-address-country":o===null||o===void 0?void 0:(I=o.address)===null||I===void 0?void 0:I.country,"prefill-address-state":o===null||o===void 0?void 0:(P=o.address)===null||P===void 0?void 0:P.state,"prefill-address-postal-code":o===null||o===void 0?void 0:(S=o.address)===null||S===void 0?void 0:S.postalCode,"prefill-shipping-name":o===null||o===void 0?void 0:(_=o.shippingAddress)===null||_===void 0?void 0:_.name,"prefill-shipping-address-line1":o===null||o===void 0?void 0:(x=o.shippingAddress)===null||x===void 0?void 0:x.line1,"prefill-shipping-address-line2":o===null||o===void 0?void 0:(R=o.shippingAddress)===null||R===void 0?void 0:R.line2,"prefill-shipping-address-city":o===null||o===void 0?void 0:(U=o.shippingAddress)===null||U===void 0?void 0:U.city,"prefill-shipping-address-country":o===null||o===void 0?void 0:(W=o.shippingAddress)===null||W===void 0?void 0:W.country,"prefill-shipping-address-state":o===null||o===void 0?void 0:(F=o.shippingAddress)===null||F===void 0?void 0:F.state,"prefill-shipping-address-postal-code":o===null||o===void 0?void 0:(M=o.shippingAddress)===null||M===void 0?void 0:M.postalCode,"affiliate-code":i,"state-id":l,"promo-code":u,"setup-future-usage":d,"skip-redirect":J?"true":void 0,environment:v,wuid:p,"adaptive-pricing":b?"true":void 0,locale:y}),N?null:w)}function el(e){var r=e.fallback,n=f(e,["fallback"]);return z()?en.createElement(ea,v(c({},n),{fallback:r})):r!==null&&r!==void 0?r:null}import{useRef as eu}from"react";function ed(){return eu(null)}export{er as WhopCheckoutEmbed,el as WhopExpressCheckoutButton,X as accentColorValues,$ as isAccentColor,ed as useCheckoutEmbedControls};