@integralscope/command-transfer-checkout-js 0.1.2 → 0.1.4

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.
@@ -27,7 +27,7 @@ function m(r) {
27
27
  return r?.trim() || "";
28
28
  }
29
29
  const a = m(
30
- void 0
30
+ "https://ct-dev-checkout.web.app"
31
31
  ), g = /* @__PURE__ */ new Set([
32
32
  "checkout.ready",
33
33
  "checkout.success",
@@ -1 +1 @@
1
- (function(s,u){typeof exports=="object"&&typeof module<"u"?u(exports):typeof define=="function"&&define.amd?define(["exports"],u):(s=typeof globalThis<"u"?globalThis:s||self,u(s.CommandTransferCheckout={}))})(this,(function(s){"use strict";class u{constructor(){this.listeners=new Map}on(e,t){let n=this.listeners.get(e);return n||(n=new Set,this.listeners.set(e,n)),n.add(t),()=>{n?.delete(t)}}emit(e,t){const n=this.listeners.get(e);n&&[...n].forEach(i=>{try{i(t)}catch(o){console.error(`Checkout "${String(e)}" handler failed.`,o)}})}removeAllListeners(){this.listeners.clear()}}const c="commandtransfer.checkout",h="ctc_";function g(r){return r?.trim()||""}const l=g(void 0),w=new Set(["checkout.ready","checkout.success","checkout.cancel","checkout.error","checkout.resize"]);function k(r){return!!r&&typeof r=="object"&&!Array.isArray(r)}function a(r,e){if(typeof r!="string")return null;const t=r.trim();return!t||t.length>e||/[\u0000-\u001f\u007f]/.test(t)?null:t}function p(r,e){if(!k(r)||r.source!==c)return null;const t=a(r.type,64);if(!t||!w.has(t))return null;const n=a(r.checkoutId,128);if(n!==e)return null;switch(t){case"checkout.ready":return{source:c,type:t,checkoutId:n};case"checkout.success":{const i=a(r.reference,128);return i?{source:c,type:t,checkoutId:n,reference:i}:null}case"checkout.cancel":return{source:c,type:t,checkoutId:n};case"checkout.error":{const i=a(r.code,64),o=a(r.message,280);return!i||!o?null:{source:c,type:t,checkoutId:n,code:i,message:o}}case"checkout.resize":{const i=typeof r.height=="number"?r.height:Number(r.height);return!Number.isFinite(i)||i<200||i>4e3?null:{source:c,type:t,checkoutId:n,height:Math.round(i)}}default:return null}}class y{constructor(e){this.sourceWindow=null,this.handleMessage=t=>{if(t.origin!==this.checkoutOrigin||!this.sourceWindow||t.source!==this.sourceWindow)return;const n=p(t.data,this.checkoutId);n&&this.onMessage(n)},this.checkoutId=e.checkoutId,this.checkoutOrigin=e.checkoutOrigin,this.onMessage=e.onMessage,window.addEventListener("message",this.handleMessage)}setSourceWindow(e){this.sourceWindow=e}destroy(){window.removeEventListener("message",this.handleMessage),this.sourceWindow=null}}class b{constructor(e){this.iframe=null,this.overlay=null,this.previousBodyOverflow=null,this.escapeListener=null,this.checkoutUrl=e.checkoutUrl,this.presentation=e.presentation,this.onCloseRequest=e.onCloseRequest,this.bridge=new y({checkoutId:e.checkoutId,checkoutOrigin:e.checkoutOrigin,onMessage:e.onMessage})}mount(e){if(this.iframe)throw new Error("Checkout iframe is already mounted.");const t=this.createIframe();this.presentation==="modal"?this.mountModal(t):e.appendChild(t),this.iframe=t,this.bridge.setSourceWindow(t.contentWindow),t.addEventListener("load",()=>{this.bridge.setSourceWindow(t.contentWindow)})}setHeight(e){this.iframe&&(this.iframe.style.height=`${Math.round(e)}px`)}destroy(){this.bridge.destroy(),this.iframe?.remove(),this.iframe=null,this.teardownModal()}createIframe(){const e=document.createElement("iframe");return e.src=this.checkoutUrl,e.title="Command Transfer checkout",e.setAttribute("allow","payment; clipboard-write; publickey-credentials-get"),e.setAttribute("referrerpolicy","no-referrer"),e.setAttribute("sandbox",["allow-scripts","allow-same-origin","allow-forms","allow-popups","allow-popups-to-escape-sandbox","allow-top-navigation-by-user-activation"].join(" ")),e.style.width="100%",e.style.height="640px",e.style.border="0",e.style.display="block",e.style.background="#f3f0ff",e.style.borderRadius=this.presentation==="modal"?"16px":"0",e}mountModal(e){const t=document.createElement("div");t.setAttribute("data-ct-checkout","overlay"),t.setAttribute("role","dialog"),t.setAttribute("aria-modal","true"),t.setAttribute("aria-label","Payment checkout"),t.style.cssText=["position:fixed","inset:0","z-index:2147483647","display:flex","align-items:center","justify-content:center","padding:24px 16px","background:rgba(15,12,41,0.55)"].join(";");const n=document.createElement("div");n.style.cssText=["position:relative","width:min(420px,100%)","max-height:calc(100vh - 48px)","overflow:auto","border-radius:16px","box-shadow:0 24px 64px rgba(15,12,41,0.28)"].join(";");const i=document.createElement("button");i.type="button",i.setAttribute("aria-label","Close checkout"),i.textContent="×",i.style.cssText=["position:absolute","top:8px","right:8px","z-index:1","width:32px","height:32px","border:0","border-radius:999px","background:#ffffff","color:#1e1654","font-size:22px","line-height:1","cursor:pointer","box-shadow:0 2px 8px rgba(15,12,41,0.16)"].join(";"),i.addEventListener("click",this.onCloseRequest),t.addEventListener("click",o=>{o.target===t&&this.onCloseRequest()}),this.escapeListener=o=>{o.key==="Escape"&&this.onCloseRequest()},window.addEventListener("keydown",this.escapeListener),this.previousBodyOverflow=document.body.style.overflow,document.body.style.overflow="hidden",n.append(i,e),t.appendChild(n),document.body.appendChild(t),this.overlay=t,i.focus()}teardownModal(){this.escapeListener&&(window.removeEventListener("keydown",this.escapeListener),this.escapeListener=null),this.overlay?.remove(),this.overlay=null,this.previousBodyOverflow!==null&&(document.body.style.overflow=this.previousBodyOverflow,this.previousBodyOverflow=null)}}function v(r){if(typeof r!="string")return r;const e=r.trim();return e?e.startsWith("#")||e.startsWith(".")||e.startsWith("[")?document.querySelector(e):document.getElementById(e)??document.querySelector(e):null}const C=/^[A-Za-z0-9._:-]{1,128}$/;function d(r){return r.replace(/^\[|\]$/g,"").toLowerCase()}function f(r){const e=d(r);return e==="localhost"||e==="127.0.0.1"||e==="::1"}function E(r){const e=r.split(".");if(e.length!==4)return!1;const t=e.map(o=>Number(o));if(t.some(o=>!Number.isInteger(o)||o<0||o>255))return!1;const[n,i]=t;return n===10||n===127||n===192&&i===168||n===172&&i>=16&&i<=31}function I(r){const e=d(r);return f(e)||E(e)?!0:e.endsWith(".local")||e.endsWith(".test")||e.endsWith(".localhost")}function x(r){const e=r.trim();if(!C.test(e))throw new Error("intentId is invalid.");return e}function M(r){const e=r.trim(),t=e.toLowerCase().startsWith("bearer ")?e.slice(7).trim():e;if(!t.toLowerCase().startsWith(h))throw new Error(`clientToken is invalid. Expected a token starting with ${h}.`);if(t.length>512||/\s/.test(t))throw new Error("clientToken is invalid.");return t}function m(r,e,t){let n;try{n=new URL(r)}catch{throw new Error(`${e} is not a valid URL.`)}if(n.username||n.password)throw new Error(`${e} must not include credentials.`);if(n.protocol==="https:"||n.protocol==="http:"&&t(n.hostname))return n;throw new Error(`${e} must be an https URL.`)}function O(){if(!l)throw new Error("checkout origin is not configured.");return m(l,"checkout origin",f).origin}function U(r,e){return m(r,e,I).toString()}function T(r){const e=encodeURIComponent(r.intentId),t=new URL(`/pay/${e}`,r.checkoutOrigin);return r.mode==="redirect"?r.returnUrl&&t.searchParams.set("return_url",r.returnUrl):(t.searchParams.set("embed","1"),r.parentOrigin&&t.searchParams.set("origin",r.parentOrigin)),t.hash=`token=${encodeURIComponent(r.clientToken)}`,t.toString()}function L(r){if(!r||typeof r!="object")throw new Error("Checkout config is required.");if(!r.intentId?.trim())throw new Error("intentId is required.");const e=x(r.intentId),t=M(r.clientToken??""),n=O(),i=r.mode??"inline";if(i!=="inline"&&i!=="modal"&&i!=="redirect")throw new Error('mode must be "inline", "modal", or "redirect".');const o=r.returnUrl?U(r.returnUrl,"returnUrl"):void 0;return{intentId:e,clientToken:t,checkoutOrigin:n,mode:i,returnUrl:o}}class S{constructor(e){this.events=new u,this.iframeManager=null,this.mounted=!1,this.settled=!1,this.handleMessage=t=>{switch(t.type){case"checkout.ready":this.events.emit("ready",{intentId:t.checkoutId});break;case"checkout.success":this.settled=!0,this.events.emit("success",{intentId:t.checkoutId,reference:t.reference}),this.teardownFrame();break;case"checkout.cancel":this.settled=!0,this.events.emit("cancel",void 0),this.teardownFrame();break;case"checkout.error":this.settled=!0,this.events.emit("error",{code:t.code,message:t.message}),this.teardownFrame();break;case"checkout.resize":this.iframeManager?.setHeight(t.height);break}},this.config=L(e),this.containerOption=e.container}start(e){if(this.config.mode==="redirect"){this.redirect();return}if(this.config.mode==="modal"){this.open();return}this.mount(e??this.containerOption)}mount(e){this.mountFrame("inline",e??this.containerOption)}open(){this.mountFrame("modal",document.body)}redirect(){window.location.assign(this.getCheckoutUrl("redirect"))}on(e,t){return this.events.on(e,t)}close(){const e=this.mounted;this.teardownFrame(),e&&!this.settled&&(this.settled=!0,this.events.emit("cancel",void 0))}getCheckoutUrl(e){return T({intentId:this.config.intentId,clientToken:this.config.clientToken,checkoutOrigin:this.config.checkoutOrigin,mode:e,returnUrl:this.config.returnUrl,parentOrigin:e==="redirect"?void 0:window.location.origin})}mountFrame(e,t){if(this.mounted)throw new Error("Checkout is already mounted.");if(!t)throw new Error("Checkout container was not found.");const n=e==="modal"?document.body:v(t);if(!n)throw new Error("Checkout container was not found.");const i=this.getCheckoutUrl(e);this.iframeManager=new b({checkoutId:this.config.intentId,checkoutUrl:i,checkoutOrigin:this.config.checkoutOrigin,presentation:e,onMessage:this.handleMessage,onCloseRequest:()=>this.close()}),this.iframeManager.mount(n),this.mounted=!0}teardownFrame(){this.iframeManager?.destroy(),this.iframeManager=null,this.mounted=!1}}s.Checkout=S,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})}));
1
+ (function(s,u){typeof exports=="object"&&typeof module<"u"?u(exports):typeof define=="function"&&define.amd?define(["exports"],u):(s=typeof globalThis<"u"?globalThis:s||self,u(s.CommandTransferCheckout={}))})(this,(function(s){"use strict";class u{constructor(){this.listeners=new Map}on(e,t){let n=this.listeners.get(e);return n||(n=new Set,this.listeners.set(e,n)),n.add(t),()=>{n?.delete(t)}}emit(e,t){const n=this.listeners.get(e);n&&[...n].forEach(o=>{try{o(t)}catch(i){console.error(`Checkout "${String(e)}" handler failed.`,i)}})}removeAllListeners(){this.listeners.clear()}}const c="commandtransfer.checkout",h="ctc_";function g(r){return r?.trim()||""}const l=g("https://ct-dev-checkout.web.app"),w=new Set(["checkout.ready","checkout.success","checkout.cancel","checkout.error","checkout.resize"]);function k(r){return!!r&&typeof r=="object"&&!Array.isArray(r)}function a(r,e){if(typeof r!="string")return null;const t=r.trim();return!t||t.length>e||/[\u0000-\u001f\u007f]/.test(t)?null:t}function p(r,e){if(!k(r)||r.source!==c)return null;const t=a(r.type,64);if(!t||!w.has(t))return null;const n=a(r.checkoutId,128);if(n!==e)return null;switch(t){case"checkout.ready":return{source:c,type:t,checkoutId:n};case"checkout.success":{const o=a(r.reference,128);return o?{source:c,type:t,checkoutId:n,reference:o}:null}case"checkout.cancel":return{source:c,type:t,checkoutId:n};case"checkout.error":{const o=a(r.code,64),i=a(r.message,280);return!o||!i?null:{source:c,type:t,checkoutId:n,code:o,message:i}}case"checkout.resize":{const o=typeof r.height=="number"?r.height:Number(r.height);return!Number.isFinite(o)||o<200||o>4e3?null:{source:c,type:t,checkoutId:n,height:Math.round(o)}}default:return null}}class y{constructor(e){this.sourceWindow=null,this.handleMessage=t=>{if(t.origin!==this.checkoutOrigin||!this.sourceWindow||t.source!==this.sourceWindow)return;const n=p(t.data,this.checkoutId);n&&this.onMessage(n)},this.checkoutId=e.checkoutId,this.checkoutOrigin=e.checkoutOrigin,this.onMessage=e.onMessage,window.addEventListener("message",this.handleMessage)}setSourceWindow(e){this.sourceWindow=e}destroy(){window.removeEventListener("message",this.handleMessage),this.sourceWindow=null}}class b{constructor(e){this.iframe=null,this.overlay=null,this.previousBodyOverflow=null,this.escapeListener=null,this.checkoutUrl=e.checkoutUrl,this.presentation=e.presentation,this.onCloseRequest=e.onCloseRequest,this.bridge=new y({checkoutId:e.checkoutId,checkoutOrigin:e.checkoutOrigin,onMessage:e.onMessage})}mount(e){if(this.iframe)throw new Error("Checkout iframe is already mounted.");const t=this.createIframe();this.presentation==="modal"?this.mountModal(t):e.appendChild(t),this.iframe=t,this.bridge.setSourceWindow(t.contentWindow),t.addEventListener("load",()=>{this.bridge.setSourceWindow(t.contentWindow)})}setHeight(e){this.iframe&&(this.iframe.style.height=`${Math.round(e)}px`)}destroy(){this.bridge.destroy(),this.iframe?.remove(),this.iframe=null,this.teardownModal()}createIframe(){const e=document.createElement("iframe");return e.src=this.checkoutUrl,e.title="Command Transfer checkout",e.setAttribute("allow","payment; clipboard-write; publickey-credentials-get"),e.setAttribute("referrerpolicy","no-referrer"),e.setAttribute("sandbox",["allow-scripts","allow-same-origin","allow-forms","allow-popups","allow-popups-to-escape-sandbox","allow-top-navigation-by-user-activation"].join(" ")),e.style.width="100%",e.style.height="640px",e.style.border="0",e.style.display="block",e.style.background="#f3f0ff",e.style.borderRadius=this.presentation==="modal"?"16px":"0",e}mountModal(e){const t=document.createElement("div");t.setAttribute("data-ct-checkout","overlay"),t.setAttribute("role","dialog"),t.setAttribute("aria-modal","true"),t.setAttribute("aria-label","Payment checkout"),t.style.cssText=["position:fixed","inset:0","z-index:2147483647","display:flex","align-items:center","justify-content:center","padding:24px 16px","background:rgba(15,12,41,0.55)"].join(";");const n=document.createElement("div");n.style.cssText=["position:relative","width:min(420px,100%)","max-height:calc(100vh - 48px)","overflow:auto","border-radius:16px","box-shadow:0 24px 64px rgba(15,12,41,0.28)"].join(";");const o=document.createElement("button");o.type="button",o.setAttribute("aria-label","Close checkout"),o.textContent="×",o.style.cssText=["position:absolute","top:8px","right:8px","z-index:1","width:32px","height:32px","border:0","border-radius:999px","background:#ffffff","color:#1e1654","font-size:22px","line-height:1","cursor:pointer","box-shadow:0 2px 8px rgba(15,12,41,0.16)"].join(";"),o.addEventListener("click",this.onCloseRequest),t.addEventListener("click",i=>{i.target===t&&this.onCloseRequest()}),this.escapeListener=i=>{i.key==="Escape"&&this.onCloseRequest()},window.addEventListener("keydown",this.escapeListener),this.previousBodyOverflow=document.body.style.overflow,document.body.style.overflow="hidden",n.append(o,e),t.appendChild(n),document.body.appendChild(t),this.overlay=t,o.focus()}teardownModal(){this.escapeListener&&(window.removeEventListener("keydown",this.escapeListener),this.escapeListener=null),this.overlay?.remove(),this.overlay=null,this.previousBodyOverflow!==null&&(document.body.style.overflow=this.previousBodyOverflow,this.previousBodyOverflow=null)}}function v(r){if(typeof r!="string")return r;const e=r.trim();return e?e.startsWith("#")||e.startsWith(".")||e.startsWith("[")?document.querySelector(e):document.getElementById(e)??document.querySelector(e):null}const C=/^[A-Za-z0-9._:-]{1,128}$/;function d(r){return r.replace(/^\[|\]$/g,"").toLowerCase()}function f(r){const e=d(r);return e==="localhost"||e==="127.0.0.1"||e==="::1"}function E(r){const e=r.split(".");if(e.length!==4)return!1;const t=e.map(i=>Number(i));if(t.some(i=>!Number.isInteger(i)||i<0||i>255))return!1;const[n,o]=t;return n===10||n===127||n===192&&o===168||n===172&&o>=16&&o<=31}function I(r){const e=d(r);return f(e)||E(e)?!0:e.endsWith(".local")||e.endsWith(".test")||e.endsWith(".localhost")}function x(r){const e=r.trim();if(!C.test(e))throw new Error("intentId is invalid.");return e}function M(r){const e=r.trim(),t=e.toLowerCase().startsWith("bearer ")?e.slice(7).trim():e;if(!t.toLowerCase().startsWith(h))throw new Error(`clientToken is invalid. Expected a token starting with ${h}.`);if(t.length>512||/\s/.test(t))throw new Error("clientToken is invalid.");return t}function m(r,e,t){let n;try{n=new URL(r)}catch{throw new Error(`${e} is not a valid URL.`)}if(n.username||n.password)throw new Error(`${e} must not include credentials.`);if(n.protocol==="https:"||n.protocol==="http:"&&t(n.hostname))return n;throw new Error(`${e} must be an https URL.`)}function O(){if(!l)throw new Error("checkout origin is not configured.");return m(l,"checkout origin",f).origin}function U(r,e){return m(r,e,I).toString()}function T(r){const e=encodeURIComponent(r.intentId),t=new URL(`/pay/${e}`,r.checkoutOrigin);return r.mode==="redirect"?r.returnUrl&&t.searchParams.set("return_url",r.returnUrl):(t.searchParams.set("embed","1"),r.parentOrigin&&t.searchParams.set("origin",r.parentOrigin)),t.hash=`token=${encodeURIComponent(r.clientToken)}`,t.toString()}function L(r){if(!r||typeof r!="object")throw new Error("Checkout config is required.");if(!r.intentId?.trim())throw new Error("intentId is required.");const e=x(r.intentId),t=M(r.clientToken??""),n=O(),o=r.mode??"inline";if(o!=="inline"&&o!=="modal"&&o!=="redirect")throw new Error('mode must be "inline", "modal", or "redirect".');const i=r.returnUrl?U(r.returnUrl,"returnUrl"):void 0;return{intentId:e,clientToken:t,checkoutOrigin:n,mode:o,returnUrl:i}}class S{constructor(e){this.events=new u,this.iframeManager=null,this.mounted=!1,this.settled=!1,this.handleMessage=t=>{switch(t.type){case"checkout.ready":this.events.emit("ready",{intentId:t.checkoutId});break;case"checkout.success":this.settled=!0,this.events.emit("success",{intentId:t.checkoutId,reference:t.reference}),this.teardownFrame();break;case"checkout.cancel":this.settled=!0,this.events.emit("cancel",void 0),this.teardownFrame();break;case"checkout.error":this.settled=!0,this.events.emit("error",{code:t.code,message:t.message}),this.teardownFrame();break;case"checkout.resize":this.iframeManager?.setHeight(t.height);break}},this.config=L(e),this.containerOption=e.container}start(e){if(this.config.mode==="redirect"){this.redirect();return}if(this.config.mode==="modal"){this.open();return}this.mount(e??this.containerOption)}mount(e){this.mountFrame("inline",e??this.containerOption)}open(){this.mountFrame("modal",document.body)}redirect(){window.location.assign(this.getCheckoutUrl("redirect"))}on(e,t){return this.events.on(e,t)}close(){const e=this.mounted;this.teardownFrame(),e&&!this.settled&&(this.settled=!0,this.events.emit("cancel",void 0))}getCheckoutUrl(e){return T({intentId:this.config.intentId,clientToken:this.config.clientToken,checkoutOrigin:this.config.checkoutOrigin,mode:e,returnUrl:this.config.returnUrl,parentOrigin:e==="redirect"?void 0:window.location.origin})}mountFrame(e,t){if(this.mounted)throw new Error("Checkout is already mounted.");if(!t)throw new Error("Checkout container was not found.");const n=e==="modal"?document.body:v(t);if(!n)throw new Error("Checkout container was not found.");const o=this.getCheckoutUrl(e);this.iframeManager=new b({checkoutId:this.config.intentId,checkoutUrl:o,checkoutOrigin:this.config.checkoutOrigin,presentation:e,onMessage:this.handleMessage,onCloseRequest:()=>this.close()}),this.iframeManager.mount(n),this.mounted=!0}teardownFrame(){this.iframeManager?.destroy(),this.iframeManager=null,this.mounted=!1}}s.Checkout=S,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@integralscope/command-transfer-checkout-js",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "Command Transfer Checkout Web SDK",
5
5
  "repository": {
6
6
  "type": "git",