@getruba/checkout 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/embed.cjs ADDED
@@ -0,0 +1 @@
1
+ "use strict";var e,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,a=Object.getOwnPropertyNames,s=Object.prototype.hasOwnProperty,o={};((e,n)=>{for(var a in n)t(e,a,{get:n[a],enumerable:!0})})(o,{RubaEmbedCheckout:()=>i}),module.exports=(e=o,((e,o,r,i)=>{if(o&&"object"==typeof o||"function"==typeof o)for(let d of a(o))s.call(e,d)||d===r||t(e,d,{get:()=>o[d],enumerable:!(i=n(o,d))||i.enumerable});return e})(t({},"__esModule",{value:!0}),e));var r="RUBA_CHECKOUT",i=class e{iframe;loader;loaded;closable;eventTarget;windowMessageListener;constructor(e,t){this.iframe=e,this.loader=t,this.loaded=!1,this.closable=!0,this.eventTarget=new EventTarget,this.windowMessageListener=this.handleWindowMessage.bind(this),window.addEventListener("message",this.windowMessageListener)}static postMessage(e,t){window.parent.postMessage({...e,type:r},t)}static async create(t,n){"string"==typeof n&&(console.warn(`Passing theme as string is deprecated. Use { theme: "${n}" } instead.`),n={theme:n});const a=document.createElement("style");a.innerText=`\n .ruba-loader-spinner {\n width: 20px;\n aspect-ratio: 1;\n border-radius: 50%;\n background: ${"dark"===n?.theme?"#000":"#fff"};\n box-shadow: 0 0 0 0 ${"dark"===n?.theme?"#fff":"#000"};\n animation: ruba-loader-spinner-animation 1s infinite;\n }\n @keyframes ruba-loader-spinner-animation {\n 100% {box-shadow: 0 0 0 30px #0000}\n }\n body.ruba-no-scroll {\n overflow: hidden;\n }\n `,document.head.appendChild(a);const s=document.createElement("div");s.style.position="absolute",s.style.top="50%",s.style.left="50%",s.style.transform="translate(-50%, -50%)",s.style.zIndex="2147483647",s.style.colorScheme="auto";const o=document.createElement("div");o.className="ruba-loader-spinner",s.appendChild(o),document.body.classList.add("ruba-no-scroll"),document.body.appendChild(s);const r=new URL(t);r.searchParams.set("embed","true"),r.searchParams.set("embed_origin",window.location.origin),n?.theme&&r.searchParams.set("theme",n.theme);const i=r.toString(),d=document.createElement("iframe");d.src=i,d.style.position="fixed",d.style.top="0",d.style.left="0",d.style.width="100%",d.style.height="100%",d.style.border="none",d.style.zIndex="2147483647",d.style.backgroundColor="rgba(0, 0, 0, 0.5)",d.style.colorScheme="auto";const c="http://127.0.0.1:3000".split(",").join(" ");d.allow=`payment 'self' ${c}; publickey-credentials-get 'self' ${c};`,document.body.appendChild(d);const l=new e(d,s);return n?.onLoaded&&l.addEventListener("loaded",n.onLoaded,{once:!0}),new Promise(e=>{l.addEventListener("loaded",()=>e(l),{once:!0})})}static init(){document.querySelectorAll("[data-ruba-checkout]").forEach(t=>{t.removeEventListener("click",e.checkoutElementClickHandler),t.addEventListener("click",e.checkoutElementClickHandler)})}close(){window.removeEventListener("message",this.windowMessageListener),document.body.contains(this.iframe)&&document.body.removeChild(this.iframe),document.body.classList.remove("ruba-no-scroll")}addEventListener(e,t,n){this.eventTarget.addEventListener(e,t,n)}removeEventListener(e,t){this.eventTarget.removeEventListener(e,t)}static async checkoutElementClickHandler(t){t.preventDefault();let n=t.target;for(;!n.hasAttribute("data-ruba-checkout");){if(!n.parentElement)return;n=n.parentElement}const a=n.getAttribute("href")||n.getAttribute("data-ruba-checkout"),s=n.getAttribute("data-ruba-checkout-theme");e.create(a,s?{theme:s}:void 0)}handleLoaded(){this.loaded||(document.body.removeChild(this.loader),this.loaded=!0)}handleClose(){this.closable&&this.close()}handleConfirmed(){this.closable=!1}handleSuccess(e){this.closable=!0,e.redirect&&(window.location.href=e.successURL)}handleWindowMessage({data:e,origin:t}){if(!"http://127.0.0.1:3000".split(",").includes(t))return;if(e.type!==r)return;const n=new CustomEvent(e.event,{detail:e,cancelable:!0});if(this.eventTarget.dispatchEvent(n),!n.defaultPrevented)switch(e.event){case"loaded":this.handleLoaded();break;case"close":this.handleClose();break;case"confirmed":this.handleConfirmed();break;case"success":this.handleSuccess(e)}}};if("undefined"!=typeof window&&(window.Ruba={...window.Ruba??{},EmbedCheckout:i}),"undefined"!=typeof document){const e=document.currentScript;e&&e.hasAttribute("data-auto-init")&&document.addEventListener("DOMContentLoaded",async()=>{i.init()})}
@@ -0,0 +1,55 @@
1
+ interface EmbedCheckoutMessageLoaded {
2
+ event: 'loaded';
3
+ }
4
+ interface EmbedCheckoutMessageClose {
5
+ event: 'close';
6
+ }
7
+ interface EmbedCheckoutMessageConfirmed {
8
+ event: 'confirmed';
9
+ }
10
+ interface EmbedCheckoutMessageSuccess {
11
+ event: 'success';
12
+ successURL: string;
13
+ redirect: boolean;
14
+ }
15
+ type EmbedCheckoutMessage = EmbedCheckoutMessageLoaded | EmbedCheckoutMessageClose | EmbedCheckoutMessageConfirmed | EmbedCheckoutMessageSuccess;
16
+ declare class EmbedCheckout {
17
+ private iframe;
18
+ private loader;
19
+ private loaded;
20
+ private closable;
21
+ private eventTarget;
22
+ private windowMessageListener;
23
+ constructor(iframe: HTMLIFrameElement, loader: HTMLDivElement);
24
+ static postMessage(message: EmbedCheckoutMessage, targetOrigin: string): void;
25
+ static create(url: string, options?: {
26
+ theme?: 'light' | 'dark';
27
+ onLoaded?: (event: CustomEvent<EmbedCheckoutMessageLoaded>) => void;
28
+ }): Promise<EmbedCheckout>;
29
+ static init(): void;
30
+ close(): void;
31
+ addEventListener(type: 'loaded', listener: (event: CustomEvent<EmbedCheckoutMessageLoaded>) => void, options?: AddEventListenerOptions | boolean): void;
32
+ addEventListener(type: 'close', listener: (event: CustomEvent<EmbedCheckoutMessageClose>) => void, options?: AddEventListenerOptions | boolean): void;
33
+ addEventListener(type: 'confirmed', listener: (event: CustomEvent<EmbedCheckoutMessageConfirmed>) => void, options?: AddEventListenerOptions | boolean): void;
34
+ addEventListener(type: 'success', listener: (event: CustomEvent<EmbedCheckoutMessageSuccess>) => void, options?: AddEventListenerOptions | boolean): void;
35
+ removeEventListener(type: 'loaded', listener: (event: CustomEvent<EmbedCheckoutMessageLoaded>) => void): void;
36
+ removeEventListener(type: 'close', listener: (event: CustomEvent<EmbedCheckoutMessageClose>) => void): void;
37
+ removeEventListener(type: 'confirmed', listener: (event: CustomEvent<EmbedCheckoutMessageConfirmed>) => void): void;
38
+ removeEventListener(type: 'success', listener: (event: CustomEvent<EmbedCheckoutMessageSuccess>) => void): void;
39
+ private static checkoutElementClickHandler;
40
+ private handleLoaded;
41
+ private handleClose;
42
+ private handleConfirmed;
43
+ private handleSuccess;
44
+ private handleWindowMessage;
45
+ }
46
+ declare global {
47
+ interface RubaWindow {
48
+ EmbedCheckout: typeof EmbedCheckout;
49
+ }
50
+ interface Window {
51
+ Ruba: Partial<RubaWindow>;
52
+ }
53
+ }
54
+
55
+ export { EmbedCheckout as RubaEmbedCheckout };
@@ -0,0 +1,55 @@
1
+ interface EmbedCheckoutMessageLoaded {
2
+ event: 'loaded';
3
+ }
4
+ interface EmbedCheckoutMessageClose {
5
+ event: 'close';
6
+ }
7
+ interface EmbedCheckoutMessageConfirmed {
8
+ event: 'confirmed';
9
+ }
10
+ interface EmbedCheckoutMessageSuccess {
11
+ event: 'success';
12
+ successURL: string;
13
+ redirect: boolean;
14
+ }
15
+ type EmbedCheckoutMessage = EmbedCheckoutMessageLoaded | EmbedCheckoutMessageClose | EmbedCheckoutMessageConfirmed | EmbedCheckoutMessageSuccess;
16
+ declare class EmbedCheckout {
17
+ private iframe;
18
+ private loader;
19
+ private loaded;
20
+ private closable;
21
+ private eventTarget;
22
+ private windowMessageListener;
23
+ constructor(iframe: HTMLIFrameElement, loader: HTMLDivElement);
24
+ static postMessage(message: EmbedCheckoutMessage, targetOrigin: string): void;
25
+ static create(url: string, options?: {
26
+ theme?: 'light' | 'dark';
27
+ onLoaded?: (event: CustomEvent<EmbedCheckoutMessageLoaded>) => void;
28
+ }): Promise<EmbedCheckout>;
29
+ static init(): void;
30
+ close(): void;
31
+ addEventListener(type: 'loaded', listener: (event: CustomEvent<EmbedCheckoutMessageLoaded>) => void, options?: AddEventListenerOptions | boolean): void;
32
+ addEventListener(type: 'close', listener: (event: CustomEvent<EmbedCheckoutMessageClose>) => void, options?: AddEventListenerOptions | boolean): void;
33
+ addEventListener(type: 'confirmed', listener: (event: CustomEvent<EmbedCheckoutMessageConfirmed>) => void, options?: AddEventListenerOptions | boolean): void;
34
+ addEventListener(type: 'success', listener: (event: CustomEvent<EmbedCheckoutMessageSuccess>) => void, options?: AddEventListenerOptions | boolean): void;
35
+ removeEventListener(type: 'loaded', listener: (event: CustomEvent<EmbedCheckoutMessageLoaded>) => void): void;
36
+ removeEventListener(type: 'close', listener: (event: CustomEvent<EmbedCheckoutMessageClose>) => void): void;
37
+ removeEventListener(type: 'confirmed', listener: (event: CustomEvent<EmbedCheckoutMessageConfirmed>) => void): void;
38
+ removeEventListener(type: 'success', listener: (event: CustomEvent<EmbedCheckoutMessageSuccess>) => void): void;
39
+ private static checkoutElementClickHandler;
40
+ private handleLoaded;
41
+ private handleClose;
42
+ private handleConfirmed;
43
+ private handleSuccess;
44
+ private handleWindowMessage;
45
+ }
46
+ declare global {
47
+ interface RubaWindow {
48
+ EmbedCheckout: typeof EmbedCheckout;
49
+ }
50
+ interface Window {
51
+ Ruba: Partial<RubaWindow>;
52
+ }
53
+ }
54
+
55
+ export { EmbedCheckout as RubaEmbedCheckout };
@@ -0,0 +1 @@
1
+ "use strict";(()=>{var e="RUBA_CHECKOUT",t=class t{iframe;loader;loaded;closable;eventTarget;windowMessageListener;constructor(e,t){this.iframe=e,this.loader=t,this.loaded=!1,this.closable=!0,this.eventTarget=new EventTarget,this.windowMessageListener=this.handleWindowMessage.bind(this),window.addEventListener("message",this.windowMessageListener)}static postMessage(t,n){window.parent.postMessage({...t,type:e},n)}static async create(e,n){"string"==typeof n&&(console.warn(`Passing theme as string is deprecated. Use { theme: "${n}" } instead.`),n={theme:n});const a=document.createElement("style");a.innerText=`\n .ruba-loader-spinner {\n width: 20px;\n aspect-ratio: 1;\n border-radius: 50%;\n background: ${"dark"===n?.theme?"#000":"#fff"};\n box-shadow: 0 0 0 0 ${"dark"===n?.theme?"#fff":"#000"};\n animation: ruba-loader-spinner-animation 1s infinite;\n }\n @keyframes ruba-loader-spinner-animation {\n 100% {box-shadow: 0 0 0 30px #0000}\n }\n body.ruba-no-scroll {\n overflow: hidden;\n }\n `,document.head.appendChild(a);const s=document.createElement("div");s.style.position="absolute",s.style.top="50%",s.style.left="50%",s.style.transform="translate(-50%, -50%)",s.style.zIndex="2147483647",s.style.colorScheme="auto";const o=document.createElement("div");o.className="ruba-loader-spinner",s.appendChild(o),document.body.classList.add("ruba-no-scroll"),document.body.appendChild(s);const i=new URL(e);i.searchParams.set("embed","true"),i.searchParams.set("embed_origin",window.location.origin),n?.theme&&i.searchParams.set("theme",n.theme);const r=i.toString(),d=document.createElement("iframe");d.src=r,d.style.position="fixed",d.style.top="0",d.style.left="0",d.style.width="100%",d.style.height="100%",d.style.border="none",d.style.zIndex="2147483647",d.style.backgroundColor="rgba(0, 0, 0, 0.5)",d.style.colorScheme="auto";const l="http://127.0.0.1:3000".split(",").join(" ");d.allow=`payment 'self' ${l}; publickey-credentials-get 'self' ${l};`,document.body.appendChild(d);const c=new t(d,s);return n?.onLoaded&&c.addEventListener("loaded",n.onLoaded,{once:!0}),new Promise(e=>{c.addEventListener("loaded",()=>e(c),{once:!0})})}static init(){document.querySelectorAll("[data-ruba-checkout]").forEach(e=>{e.removeEventListener("click",t.checkoutElementClickHandler),e.addEventListener("click",t.checkoutElementClickHandler)})}close(){window.removeEventListener("message",this.windowMessageListener),document.body.contains(this.iframe)&&document.body.removeChild(this.iframe),document.body.classList.remove("ruba-no-scroll")}addEventListener(e,t,n){this.eventTarget.addEventListener(e,t,n)}removeEventListener(e,t){this.eventTarget.removeEventListener(e,t)}static async checkoutElementClickHandler(e){e.preventDefault();let n=e.target;for(;!n.hasAttribute("data-ruba-checkout");){if(!n.parentElement)return;n=n.parentElement}const a=n.getAttribute("href")||n.getAttribute("data-ruba-checkout"),s=n.getAttribute("data-ruba-checkout-theme");t.create(a,s?{theme:s}:void 0)}handleLoaded(){this.loaded||(document.body.removeChild(this.loader),this.loaded=!0)}handleClose(){this.closable&&this.close()}handleConfirmed(){this.closable=!1}handleSuccess(e){this.closable=!0,e.redirect&&(window.location.href=e.successURL)}handleWindowMessage({data:t,origin:n}){if(!"http://127.0.0.1:3000".split(",").includes(n))return;if(t.type!==e)return;const a=new CustomEvent(t.event,{detail:t,cancelable:!0});if(this.eventTarget.dispatchEvent(a),!a.defaultPrevented)switch(t.event){case"loaded":this.handleLoaded();break;case"close":this.handleClose();break;case"confirmed":this.handleConfirmed();break;case"success":this.handleSuccess(t)}}};if("undefined"!=typeof window&&(window.Ruba={...window.Ruba??{},EmbedCheckout:t}),"undefined"!=typeof document){const e=document.currentScript;e&&e.hasAttribute("data-auto-init")&&document.addEventListener("DOMContentLoaded",async()=>{t.init()})}var n="RUBA_PAYMENT_METHOD",a="/embed/payment-method",s="ruba_payment_method_status",o=()=>{const e="http://127.0.0.1:3000".split(",");return"undefined"!=typeof window&&e.includes(window.location.origin)?window.location.origin:e[0]},i=()=>{const e="http://127.0.0.1:3000".split(",").join(" ");return`payment 'self' ${e}; publickey-credentials-get 'self' ${e};`},r=class e{iframe;mode;loader;loaded;closable;eventTarget;windowMessageListener;constructor(e,t,n){this.iframe=e,this.mode=t,this.loader=n,this.loaded=!1,this.closable=!0,this.eventTarget=new EventTarget,this.windowMessageListener=this.handleWindowMessage.bind(this),window.addEventListener("message",this.windowMessageListener)}static postMessage(e,t){window.parent.postMessage({...e,type:n},t)}static create(t){const{sessionToken:n,theme:s,setAsDefault:r,returnUrl:d,locale:l,onLoaded:c}=t,h=document.createElement("style");h.innerText=`\n .ruba-loader-spinner {\n width: 20px;\n aspect-ratio: 1;\n border-radius: 50%;\n background: ${"dark"===s?"#000":"#fff"};\n box-shadow: 0 0 0 0 ${"dark"===s?"#fff":"#000"};\n animation: ruba-loader-spinner-animation 1s infinite;\n }\n @keyframes ruba-loader-spinner-animation {\n 100% {box-shadow: 0 0 0 30px #0000}\n }\n body.ruba-no-scroll {\n overflow: hidden;\n }\n `,document.head.appendChild(h);const m=document.createElement("div");m.style.position="absolute",m.style.top="50%",m.style.left="50%",m.style.transform="translate(-50%, -50%)",m.style.zIndex="2147483647",m.style.colorScheme="auto";const u=document.createElement("div");u.className="ruba-loader-spinner",m.appendChild(u),document.body.classList.add("ruba-no-scroll"),document.body.appendChild(m);const b=new URL(a,o());b.searchParams.set("session_token",n),b.searchParams.set("embed","true"),b.searchParams.set("embed_origin",window.location.origin),b.searchParams.set("mode","modal"),b.searchParams.set("embed_return_url",d??window.location.href),s&&b.searchParams.set("theme",s),!1===r&&b.searchParams.set("set_default","false"),l&&b.searchParams.set("locale",l);const w=document.createElement("iframe");w.src=b.toString(),w.style.position="fixed",w.style.top="0",w.style.left="0",w.style.width="100%",w.style.height="100%",w.style.border="none",w.style.zIndex="2147483647",w.style.backgroundColor="rgba(0, 0, 0, 0.5)",w.style.colorScheme="auto",w.allow=i(),document.body.appendChild(w);const f=new e(w,"modal",m);return c&&f.addEventListener("loaded",c,{once:!0}),new Promise(e=>{f.addEventListener("loaded",()=>e(f),{once:!0})})}static createInline(t){const{sessionToken:n,element:s,theme:r,setAsDefault:d,locale:l,onLoaded:c}=t,h=new URL(a,o());h.searchParams.set("session_token",n),h.searchParams.set("embed","true"),h.searchParams.set("embed_origin",window.location.origin),h.searchParams.set("mode","inline"),r&&h.searchParams.set("theme",r),!1===d&&h.searchParams.set("set_default","false"),l&&h.searchParams.set("locale",l);const m=document.createElement("iframe");m.src=h.toString(),m.style.display="block",m.style.width="100%",m.style.height="0",m.style.border="none",m.style.colorScheme="auto",m.allow=i(),s.replaceChildren(m);const u=new e(m,"inline",null);return c&&u.addEventListener("loaded",c,{once:!0}),u}static init(){document.querySelectorAll("[data-ruba-payment-method]").forEach(t=>{t.removeEventListener("click",e.elementClickHandler),t.addEventListener("click",e.elementClickHandler)})}static getRedirectResult(){if("undefined"==typeof window)return null;const e=new URLSearchParams(window.location.search),t=e.get(s);if("succeeded"!==t&&"failed"!==t)return null;e.delete(s);const n=e.toString();return window.history.replaceState({},"",`${window.location.pathname}${n?`?${n}`:""}${window.location.hash}`),{status:t}}close(){window.removeEventListener("message",this.windowMessageListener),this.iframe.remove(),"modal"===this.mode&&document.body.classList.remove("ruba-no-scroll")}addEventListener(e,t,n){this.eventTarget.addEventListener(e,t,n)}removeEventListener(e,t){this.eventTarget.removeEventListener(e,t)}static async elementClickHandler(t){t.preventDefault();let n=t.target;for(;!n.hasAttribute("data-ruba-payment-method");){if(!n.parentElement)return;n=n.parentElement}const a=n.getAttribute("data-ruba-payment-method");if(!a)return;const s=n.getAttribute("data-ruba-payment-method-theme"),o=n.getAttribute("data-ruba-payment-method-set-as-default"),i=n.getAttribute("data-ruba-payment-method-return-url"),r=n.getAttribute("data-ruba-payment-method-locale");e.create({sessionToken:a,theme:s??void 0,setAsDefault:null===o?void 0:"false"!==o,returnUrl:i??void 0,locale:r??void 0})}handleLoaded(){this.loaded||(this.loader&&document.body.contains(this.loader)&&document.body.removeChild(this.loader),this.loaded=!0)}handleClose(){this.closable&&this.close()}handleConfirmed(){this.closable=!1}handleSuccess(){"inline"!==this.mode&&(this.closable=!0,this.close())}handleError(){this.closable=!0}handleWindowMessage({data:e,origin:t}){if(!"http://127.0.0.1:3000".split(",").includes(t))return;if(e.type!==n)return;if("resize"===e.event)return void("inline"===this.mode&&(this.iframe.style.height=`${Math.max(0,Math.ceil(e.height))}px`));const a=new CustomEvent(e.event,{detail:e,cancelable:!0});if(this.eventTarget.dispatchEvent(a),!a.defaultPrevented)switch(e.event){case"loaded":this.handleLoaded();break;case"close":this.handleClose();break;case"confirmed":this.handleConfirmed();break;case"success":this.handleSuccess();break;case"error":this.handleError()}}};if("undefined"!=typeof window&&(window.Ruba={...window.Ruba??{},EmbedPaymentMethod:r}),"undefined"!=typeof document){const e=document.currentScript;e&&e.hasAttribute("data-auto-init")&&document.addEventListener("DOMContentLoaded",()=>{r.init()})}})();
package/dist/embed.js ADDED
@@ -0,0 +1 @@
1
+ var e="RUBA_CHECKOUT",t=class t{iframe;loader;loaded;closable;eventTarget;windowMessageListener;constructor(e,t){this.iframe=e,this.loader=t,this.loaded=!1,this.closable=!0,this.eventTarget=new EventTarget,this.windowMessageListener=this.handleWindowMessage.bind(this),window.addEventListener("message",this.windowMessageListener)}static postMessage(t,n){window.parent.postMessage({...t,type:e},n)}static async create(e,n){"string"==typeof n&&(console.warn(`Passing theme as string is deprecated. Use { theme: "${n}" } instead.`),n={theme:n});const s=document.createElement("style");s.innerText=`\n .ruba-loader-spinner {\n width: 20px;\n aspect-ratio: 1;\n border-radius: 50%;\n background: ${"dark"===n?.theme?"#000":"#fff"};\n box-shadow: 0 0 0 0 ${"dark"===n?.theme?"#fff":"#000"};\n animation: ruba-loader-spinner-animation 1s infinite;\n }\n @keyframes ruba-loader-spinner-animation {\n 100% {box-shadow: 0 0 0 30px #0000}\n }\n body.ruba-no-scroll {\n overflow: hidden;\n }\n `,document.head.appendChild(s);const a=document.createElement("div");a.style.position="absolute",a.style.top="50%",a.style.left="50%",a.style.transform="translate(-50%, -50%)",a.style.zIndex="2147483647",a.style.colorScheme="auto";const o=document.createElement("div");o.className="ruba-loader-spinner",a.appendChild(o),document.body.classList.add("ruba-no-scroll"),document.body.appendChild(a);const d=new URL(e);d.searchParams.set("embed","true"),d.searchParams.set("embed_origin",window.location.origin),n?.theme&&d.searchParams.set("theme",n.theme);const i=d.toString(),r=document.createElement("iframe");r.src=i,r.style.position="fixed",r.style.top="0",r.style.left="0",r.style.width="100%",r.style.height="100%",r.style.border="none",r.style.zIndex="2147483647",r.style.backgroundColor="rgba(0, 0, 0, 0.5)",r.style.colorScheme="auto";const c="http://127.0.0.1:3000".split(",").join(" ");r.allow=`payment 'self' ${c}; publickey-credentials-get 'self' ${c};`,document.body.appendChild(r);const l=new t(r,a);return n?.onLoaded&&l.addEventListener("loaded",n.onLoaded,{once:!0}),new Promise(e=>{l.addEventListener("loaded",()=>e(l),{once:!0})})}static init(){document.querySelectorAll("[data-ruba-checkout]").forEach(e=>{e.removeEventListener("click",t.checkoutElementClickHandler),e.addEventListener("click",t.checkoutElementClickHandler)})}close(){window.removeEventListener("message",this.windowMessageListener),document.body.contains(this.iframe)&&document.body.removeChild(this.iframe),document.body.classList.remove("ruba-no-scroll")}addEventListener(e,t,n){this.eventTarget.addEventListener(e,t,n)}removeEventListener(e,t){this.eventTarget.removeEventListener(e,t)}static async checkoutElementClickHandler(e){e.preventDefault();let n=e.target;for(;!n.hasAttribute("data-ruba-checkout");){if(!n.parentElement)return;n=n.parentElement}const s=n.getAttribute("href")||n.getAttribute("data-ruba-checkout"),a=n.getAttribute("data-ruba-checkout-theme");t.create(s,a?{theme:a}:void 0)}handleLoaded(){this.loaded||(document.body.removeChild(this.loader),this.loaded=!0)}handleClose(){this.closable&&this.close()}handleConfirmed(){this.closable=!1}handleSuccess(e){this.closable=!0,e.redirect&&(window.location.href=e.successURL)}handleWindowMessage({data:t,origin:n}){if(!"http://127.0.0.1:3000".split(",").includes(n))return;if(t.type!==e)return;const s=new CustomEvent(t.event,{detail:t,cancelable:!0});if(this.eventTarget.dispatchEvent(s),!s.defaultPrevented)switch(t.event){case"loaded":this.handleLoaded();break;case"close":this.handleClose();break;case"confirmed":this.handleConfirmed();break;case"success":this.handleSuccess(t)}}};if("undefined"!=typeof window&&(window.Ruba={...window.Ruba??{},EmbedCheckout:t}),"undefined"!=typeof document){const e=document.currentScript;e&&e.hasAttribute("data-auto-init")&&document.addEventListener("DOMContentLoaded",async()=>{t.init()})}export{t as RubaEmbedCheckout};
@@ -0,0 +1 @@
1
+ "use strict";var t=Object.defineProperty;var s=Object.getOwnPropertyDescriptor;var n=Object.getOwnPropertyNames;var o=Object.prototype.hasOwnProperty;var d=(r,c)=>{for(var e in c)t(r,e,{get:c[e],enumerable:!0})},p=(r,c,e,u)=>{if(c&&typeof c=="object"||typeof c=="function")for(let i of n(c))!o.call(r,i)&&i!==e&&t(r,i,{get:()=>c[i],enumerable:!(u=s(c,i))||u.enumerable});return r};var P=r=>p(t({},"__esModule",{value:!0}),r);var g={};d(g,{getFixedPrice:()=>m,getSeatPrice:()=>h,hasProductCheckout:()=>a,isLegacyRecurringProductPrice:()=>l});module.exports=P(g);var a=r=>r.product!==null&&r.prices!==null,l=r=>r.type==="recurring",h=r=>{if(!r.product||!r.prices)return null;let c=r.prices[r.product.id];return c?c.filter(e=>e.price_currency===r.currency).find(e=>e.amount_type==="seat_based")??null:null},m=r=>{if(!r.product||!r.prices)return null;let c=r.prices[r.product.id];return c?c.filter(e=>e.price_currency===r.currency).find(e=>e.amount_type==="fixed")??null:null};0&&(module.exports={getFixedPrice,getSeatPrice,hasProductCheckout,isLegacyRecurringProductPrice});
@@ -0,0 +1,19 @@
1
+ import { s as schemas } from './index.d-DvLuMU7T.cjs';
2
+ import 'openapi-fetch';
3
+
4
+ interface ProductCheckoutMixin {
5
+ product_id: string;
6
+ product: schemas['CheckoutProduct'];
7
+ product_price_id: string;
8
+ product_price: schemas['ProductPrice'];
9
+ prices: {
10
+ [k: string]: schemas['ProductPrice'][];
11
+ };
12
+ }
13
+ type ProductCheckoutPublic = schemas['CheckoutPublic'] & ProductCheckoutMixin;
14
+ declare const hasProductCheckout: (checkout: schemas["CheckoutPublic"]) => checkout is ProductCheckoutPublic;
15
+ declare const isLegacyRecurringProductPrice: (price: schemas["ProductPrice"] | schemas["LegacyRecurringProductPrice"]) => price is schemas["LegacyRecurringProductPrice"];
16
+ declare const getSeatPrice: (checkout: schemas["CheckoutPublic"]) => schemas["ProductPriceSeatBased"] | null;
17
+ declare const getFixedPrice: (checkout: schemas["CheckoutPublic"]) => schemas["ProductPriceFixed"] | null;
18
+
19
+ export { type ProductCheckoutPublic, getFixedPrice, getSeatPrice, hasProductCheckout, isLegacyRecurringProductPrice };
@@ -0,0 +1,19 @@
1
+ import { s as schemas } from './index.d-DvLuMU7T.js';
2
+ import 'openapi-fetch';
3
+
4
+ interface ProductCheckoutMixin {
5
+ product_id: string;
6
+ product: schemas['CheckoutProduct'];
7
+ product_price_id: string;
8
+ product_price: schemas['ProductPrice'];
9
+ prices: {
10
+ [k: string]: schemas['ProductPrice'][];
11
+ };
12
+ }
13
+ type ProductCheckoutPublic = schemas['CheckoutPublic'] & ProductCheckoutMixin;
14
+ declare const hasProductCheckout: (checkout: schemas["CheckoutPublic"]) => checkout is ProductCheckoutPublic;
15
+ declare const isLegacyRecurringProductPrice: (price: schemas["ProductPrice"] | schemas["LegacyRecurringProductPrice"]) => price is schemas["LegacyRecurringProductPrice"];
16
+ declare const getSeatPrice: (checkout: schemas["CheckoutPublic"]) => schemas["ProductPriceSeatBased"] | null;
17
+ declare const getFixedPrice: (checkout: schemas["CheckoutPublic"]) => schemas["ProductPriceFixed"] | null;
18
+
19
+ export { type ProductCheckoutPublic, getFixedPrice, getSeatPrice, hasProductCheckout, isLegacyRecurringProductPrice };
package/dist/guards.js ADDED
@@ -0,0 +1 @@
1
+ import{a,b,c,d}from"./chunk-GBQE5XCD.js";import"./chunk-ZWRDP37E.js";export{d as getFixedPrice,c as getSeatPrice,a as hasProductCheckout,b as isLegacyRecurringProductPrice};
@@ -0,0 +1 @@
1
+ "use strict";"use client";var T=Object.create;var _=Object.defineProperty;var U=Object.getOwnPropertyDescriptor;var j=Object.getOwnPropertyNames;var z=Object.getPrototypeOf,J=Object.prototype.hasOwnProperty;var q=(t,e)=>()=>(e||t((e={exports:{}}).exports,e),e.exports),G=(t,e)=>{for(var r in e)_(t,r,{get:e[r],enumerable:!0})},O=(t,e,r,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let c of j(e))!J.call(t,c)&&c!==r&&_(t,c,{get:()=>e[c],enumerable:!(n=U(e,c))||n.enumerable});return t};var I=(t,e,r)=>(r=t!=null?T(z(t)):{},O(e||!t||!t.__esModule?_(r,"default",{value:t,enumerable:!0}):r,t)),K=t=>O(_({},"__esModule",{value:!0}),t);var N=q((Z,k)=>{"use strict";var Q=Object.prototype.hasOwnProperty,f="~";function v(){}Object.create&&(v.prototype=Object.create(null),new v().__proto__||(f=!1));function V(t,e,r){this.fn=t,this.context=e,this.once=r||!1}function A(t,e,r,n,c){if(typeof r!="function")throw new TypeError("The listener must be a function");var u=new V(r,n||t,c),o=f?f+e:e;return t._events[o]?t._events[o].fn?t._events[o]=[t._events[o],u]:t._events[o].push(u):(t._events[o]=u,t._eventsCount++),t}function b(t,e){--t._eventsCount===0?t._events=new v:delete t._events[e]}function a(){this._events=new v,this._eventsCount=0}a.prototype.eventNames=function(){var e=[],r,n;if(this._eventsCount===0)return e;for(n in r=this._events)Q.call(r,n)&&e.push(f?n.slice(1):n);return Object.getOwnPropertySymbols?e.concat(Object.getOwnPropertySymbols(r)):e};a.prototype.listeners=function(e){var r=f?f+e:e,n=this._events[r];if(!n)return[];if(n.fn)return[n.fn];for(var c=0,u=n.length,o=new Array(u);c<u;c++)o[c]=n[c].fn;return o};a.prototype.listenerCount=function(e){var r=f?f+e:e,n=this._events[r];return n?n.fn?1:n.length:0};a.prototype.emit=function(e,r,n,c,u,o){var p=f?f+e:e;if(!this._events[p])return!1;var s=this._events[p],l=arguments.length,d,i;if(s.fn){switch(s.once&&this.removeListener(e,s.fn,void 0,!0),l){case 1:return s.fn.call(s.context),!0;case 2:return s.fn.call(s.context,r),!0;case 3:return s.fn.call(s.context,r,n),!0;case 4:return s.fn.call(s.context,r,n,c),!0;case 5:return s.fn.call(s.context,r,n,c,u),!0;case 6:return s.fn.call(s.context,r,n,c,u,o),!0}for(i=1,d=new Array(l-1);i<l;i++)d[i-1]=arguments[i];s.fn.apply(s.context,d)}else{var m=s.length,h;for(i=0;i<m;i++)switch(s[i].once&&this.removeListener(e,s[i].fn,void 0,!0),l){case 1:s[i].fn.call(s[i].context);break;case 2:s[i].fn.call(s[i].context,r);break;case 3:s[i].fn.call(s[i].context,r,n);break;case 4:s[i].fn.call(s[i].context,r,n,c);break;default:if(!d)for(h=1,d=new Array(l-1);h<l;h++)d[h-1]=arguments[h];s[i].fn.apply(s[i].context,d)}}return!0};a.prototype.on=function(e,r,n){return A(this,e,r,n,!1)};a.prototype.once=function(e,r,n){return A(this,e,r,n,!0)};a.prototype.removeListener=function(e,r,n,c){var u=f?f+e:e;if(!this._events[u])return this;if(!r)return b(this,u),this;var o=this._events[u];if(o.fn)o.fn===r&&(!c||o.once)&&(!n||o.context===n)&&b(this,u);else{for(var p=0,s=[],l=o.length;p<l;p++)(o[p].fn!==r||c&&!o[p].once||n&&o[p].context!==n)&&s.push(o[p]);s.length?this._events[u]=s.length===1?s[0]:s:b(this,u)}return this};a.prototype.removeAllListeners=function(e){var r;return e?(r=f?f+e:e,this._events[r]&&b(this,r)):(this._events=new v,this._eventsCount=0),this};a.prototype.off=a.prototype.removeListener;a.prototype.addListener=a.prototype.on;a.prefixed=f;a.EventEmitter=a;typeof k<"u"&&(k.exports=a)});var W={};G(W,{useCheckoutFulfillmentListener:()=>M});module.exports=K(W);var g=require("react");var F=t=>t.product!==null&&t.prices!==null,R=t=>t.type==="recurring";var D=require("event-source-plus");var $=I(N(),1);var B=$.default;var H=t=>{let e=new B,r=new D.EventSourcePlus(t,{credentials:"include"}),n={onMessage:async u=>{let o=JSON.parse(u.data);o.type!=="reconnect"&&e.emit(o.key,o.payload)}};return[e,()=>r.listen(n)]};var M=(t,e,r=15e3)=>{let[n,c]=(0,g.useState)(null);return[(0,g.useCallback)(async()=>await new Promise((o,p)=>{let s=`${t.baseUrl}/v1/checkouts/client/${e.client_secret}/stream`,[l,d]=H(s),i=d(),m=!1,h=!1,y=!F(e)||!R(e.product_price),x=!1,P=()=>{m&&h&&y&&c(`Waiting confirmation from ${e.organization.name} `),m&&h&&y&&x&&(i.abort(),o())},L=C=>{C.status==="succeeded"&&(m=!0,c("Payment successful! Processing order..."),l.off("checkout.updated",L),P())};l.on("checkout.updated",L);let E=()=>{h=!0,l.off("checkout.order_created",E),P()};l.on("checkout.order_created",E);let w=()=>{y=!0,l.off("checkout.subscription_created",w),P()};y||l.on("checkout.subscription_created",w);let S=C=>{C.status==="succeeded"&&(x=!0,l.off("checkout.webhook_event_delivered",S),P())};l.on("checkout.webhook_event_delivered",S),setTimeout(()=>{i.abort(),p()},r)}),[t,e,r]),n]};0&&(module.exports={useCheckoutFulfillmentListener});
@@ -0,0 +1,6 @@
1
+ import { C as Client, s as schemas } from '../index.d-DvLuMU7T.cjs';
2
+ import 'openapi-fetch';
3
+
4
+ declare const useCheckoutFulfillmentListener: (client: Client, checkout: schemas["CheckoutPublic"], maxWaitingTimeMs?: number) => [() => Promise<void>, string | null];
5
+
6
+ export { useCheckoutFulfillmentListener };
@@ -0,0 +1,6 @@
1
+ import { C as Client, s as schemas } from '../index.d-DvLuMU7T.js';
2
+ import 'openapi-fetch';
3
+
4
+ declare const useCheckoutFulfillmentListener: (client: Client, checkout: schemas["CheckoutPublic"], maxWaitingTimeMs?: number) => [() => Promise<void>, string | null];
5
+
6
+ export { useCheckoutFulfillmentListener };
@@ -0,0 +1 @@
1
+ "use client";import{a as S,b as P}from"../chunk-GBQE5XCD.js";import{b as D,c as H}from"../chunk-ZWRDP37E.js";var A=D((q,E)=>{"use strict";var R=Object.prototype.hasOwnProperty,a="~";function m(){}Object.create&&(m.prototype=Object.create(null),new m().__proto__||(a=!1));function T(s,e,t){this.fn=s,this.context=e,this.once=t||!1}function O(s,e,t,r,u){if(typeof t!="function")throw new TypeError("The listener must be a function");var c=new T(t,r||s,u),o=a?a+e:e;return s._events[o]?s._events[o].fn?s._events[o]=[s._events[o],c]:s._events[o].push(c):(s._events[o]=c,s._eventsCount++),s}function b(s,e){--s._eventsCount===0?s._events=new m:delete s._events[e]}function f(){this._events=new m,this._eventsCount=0}f.prototype.eventNames=function(){var e=[],t,r;if(this._eventsCount===0)return e;for(r in t=this._events)R.call(t,r)&&e.push(a?r.slice(1):r);return Object.getOwnPropertySymbols?e.concat(Object.getOwnPropertySymbols(t)):e};f.prototype.listeners=function(e){var t=a?a+e:e,r=this._events[t];if(!r)return[];if(r.fn)return[r.fn];for(var u=0,c=r.length,o=new Array(c);u<c;u++)o[u]=r[u].fn;return o};f.prototype.listenerCount=function(e){var t=a?a+e:e,r=this._events[t];return r?r.fn?1:r.length:0};f.prototype.emit=function(e,t,r,u,c,o){var p=a?a+e:e;if(!this._events[p])return!1;var n=this._events[p],l=arguments.length,h,i;if(n.fn){switch(n.once&&this.removeListener(e,n.fn,void 0,!0),l){case 1:return n.fn.call(n.context),!0;case 2:return n.fn.call(n.context,t),!0;case 3:return n.fn.call(n.context,t,r),!0;case 4:return n.fn.call(n.context,t,r,u),!0;case 5:return n.fn.call(n.context,t,r,u,c),!0;case 6:return n.fn.call(n.context,t,r,u,c,o),!0}for(i=1,h=new Array(l-1);i<l;i++)h[i-1]=arguments[i];n.fn.apply(n.context,h)}else{var d=n.length,v;for(i=0;i<d;i++)switch(n[i].once&&this.removeListener(e,n[i].fn,void 0,!0),l){case 1:n[i].fn.call(n[i].context);break;case 2:n[i].fn.call(n[i].context,t);break;case 3:n[i].fn.call(n[i].context,t,r);break;case 4:n[i].fn.call(n[i].context,t,r,u);break;default:if(!h)for(v=1,h=new Array(l-1);v<l;v++)h[v-1]=arguments[v];n[i].fn.apply(n[i].context,h)}}return!0};f.prototype.on=function(e,t,r){return O(this,e,t,r,!1)};f.prototype.once=function(e,t,r){return O(this,e,t,r,!0)};f.prototype.removeListener=function(e,t,r,u){var c=a?a+e:e;if(!this._events[c])return this;if(!t)return b(this,c),this;var o=this._events[c];if(o.fn)o.fn===t&&(!u||o.once)&&(!r||o.context===r)&&b(this,c);else{for(var p=0,n=[],l=o.length;p<l;p++)(o[p].fn!==t||u&&!o[p].once||r&&o[p].context!==r)&&n.push(o[p]);n.length?this._events[c]=n.length===1?n[0]:n:b(this,c)}return this};f.prototype.removeAllListeners=function(e){var t;return e?(t=a?a+e:e,this._events[t]&&b(this,t)):(this._events=new m,this._eventsCount=0),this};f.prototype.off=f.prototype.removeListener;f.prototype.addListener=f.prototype.on;f.prefixed=a;f.EventEmitter=f;typeof E<"u"&&(E.exports=f)});import{useCallback as j,useState as z}from"react";import{EventSourcePlus as U}from"event-source-plus";var F=H(A(),1);var N=F.default;var $=s=>{let e=new N,t=new U(s,{credentials:"include"}),r={onMessage:async c=>{let o=JSON.parse(c.data);o.type!=="reconnect"&&e.emit(o.key,o.payload)}};return[e,()=>t.listen(r)]};var J=(s,e,t=15e3)=>{let[r,u]=z(null);return[j(async()=>await new Promise((o,p)=>{let n=`${s.baseUrl}/v1/checkouts/client/${e.client_secret}/stream`,[l,h]=$(n),i=h(),d=!1,v=!1,_=!S(e)||!P(e.product_price),w=!1,y=()=>{d&&v&&_&&u(`Waiting confirmation from ${e.organization.name} `),d&&v&&_&&w&&(i.abort(),o())},C=k=>{k.status==="succeeded"&&(d=!0,u("Payment successful! Processing order..."),l.off("checkout.updated",C),y())};l.on("checkout.updated",C);let L=()=>{v=!0,l.off("checkout.order_created",L),y()};l.on("checkout.order_created",L);let g=()=>{_=!0,l.off("checkout.subscription_created",g),y()};_||l.on("checkout.subscription_created",g);let x=k=>{k.status==="succeeded"&&(w=!0,l.off("checkout.webhook_event_delivered",x),y())};l.on("checkout.webhook_event_delivered",x),setTimeout(()=>{i.abort(),p()},t)}),[s,e,t]),r]};export{J as useCheckoutFulfillmentListener};