dodopayments-checkout
Version:
A TypeScript library for embedding Dodo Payments overlay checkout.
2 lines (1 loc) • 9.21 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).DodoPaymentsCheckout={})}(this,(function(e){"use strict";function t(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=Array(t);r<t;r++)n[r]=e[r];return n}function r(e,t,r){return(t=function(e){var t=function(e,t){if("object"!=typeof e||!e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,t||"default");if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:t+""}(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function n(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function o(e){for(var t=1;t<arguments.length;t++){var o=null!=arguments[t]?arguments[t]:{};t%2?n(Object(o),!0).forEach((function(t){r(e,t,o[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(o)):n(Object(o)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(o,t))}))}return e}function i(e,r){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var r=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=r){var n,o,i,c,a=[],l=!0,s=!1;try{if(i=(r=r.call(e)).next,0===t){if(Object(r)!==r)return;l=!1}else for(;!(l=(n=i.call(r)).done)&&(a.push(n.value),a.length!==t);l=!0);}catch(e){s=!0,o=e}finally{try{if(!l&&null!=r.return&&(c=r.return(),Object(c)!==c))return}finally{if(s)throw o}}return a}}(e,r)||function(e,r){if(e){if("string"==typeof e)return t(e,r);var n={}.toString.call(e).slice(8,-1);return"Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n?Array.from(e):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?t(e,r):void 0}}(e,r)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}var c={test:"https://test.checkout.dodopayments.com",live:"https://checkout.dodopayments.com"},a="\n width: 100dvw;\n height: 100dvh;\n position: fixed;\n top: 0;\n left: 0;\n right: 0;\n bottom: 0;\n border: none;\n background: transparent;\n z-index: 1000;\n",l=new Set(["checkout.opened","checkout.payment_page_opened","checkout.customer_details_submitted","checkout.closed","checkout.error","checkout.redirect"]);function s(e){if(!e.mode)throw new Error("Checkout mode is required");if(!Object.keys(c).includes(e.mode))throw new Error("Invalid checkout mode")}function u(e){try{var t=new URL(e);if("https:"!==t.protocol&&"http:"!==t.protocol)throw new Error("Invalid redirect URL protocol");if(e.length>2048)throw new Error("Redirect URL exceeds maximum length")}catch(e){throw new Error("Invalid redirect URL format")}}var d=null;function f(e){var t;if(d&&e.origin===d.allowedOrigin)try{var r=e.data;if(!r||!r.event_type)return void console.warn("Received message without event_type:",r);if(!l.has(r.event_type))return void console.warn("Received message with invalid event_type: ".concat(r.event_type));if(h(r.event_type,r.data||{}),"checkout.closed"===r.event_type&&setTimeout((function(){return p(!1)}),100),"checkout.redirect"===r.event_type){var n,o=null===(n=r.data)||void 0===n||null===(n=n.message)||void 0===n?void 0:n.redirect_to;if(o)try{u(o),window.location.href=o}catch(e){console.error("Invalid redirect URL:",e),h("checkout.error",{message:"Invalid redirect URL"})}}}catch(e){console.error("Error processing checkout message:",e),h("checkout.error",{message:"Failed to process message"})}else console.warn("Ignored message from unexpected origin: ".concat(e.origin,", expected: ").concat(null===(t=d)||void 0===t?void 0:t.allowedOrigin))}function h(e){var t,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(d)if(l.has(e)){var n=o(o({},r),{},{products:d.currentProducts,timestamp:Date.now()});if(d.config.onEvent)try{d.config.onEvent({event_type:e,data:n})}catch(t){console.error("Error in onEvent handler for ".concat(e,":"),t)}null!==(t=d.eventListeners[e])&&void 0!==t&&t.length&&d.eventListeners[e].forEach((function(t){try{t(n)}catch(t){console.error("Error in ".concat(e," event handler:"),t)}}))}else console.warn("Attempted to emit invalid event type: ".concat(e))}function p(){var e,t,r=!(arguments.length>0&&void 0!==arguments[0])||arguments[0];if(null!==(e=d)&&void 0!==e&&e.container||null!==(t=d)&&void 0!==t&&t.iframe)try{d.container?(d.container.remove(),d.container=null):d.iframe&&d.iframe.remove(),d.iframe=null,d.currentProducts=[],document.body.style.overflow="",document.body.style.contain="",r&&h("checkout.closed",{message:"Checkout closed manually"})}catch(e){throw console.error("Error closing checkout:",e),h("checkout.error",{message:"Failed to close checkout"}),e}}var m=null;function v(e){var t,r=e.products,n=e.paymentLink,o=e.queryParams,c=e.redirectUrl;if(n&&(!function(e){try{var t=new URL(e);if("https:"!==t.protocol&&"http:"!==t.protocol)throw new Error("Invalid payment URL protocol");if(e.length>2048)throw new Error("Payment URL exceeds maximum length")}catch(e){throw new Error("Invalid payment URL format")}}(n),t=n.slice(-8)),!m)throw new Error("Checkout not initialized");if(m.iframe||m.container)console.warn("Checkout is already open");else{if(c&&u(c),"static"===m.config.linkType){if(null==r||!r.length)throw new Error("Products are required for static checkout");if(r.length>1)throw new Error("Only one product is supported for checkout at the moment");!function(e){if(!e||"string"!=typeof e)throw new Error("Invalid product ID");if(e.length>50)throw new Error("Product ID exceeds maximum length");if(!/^[a-zA-Z0-9_-]+$/.test(e))throw new Error("Product ID contains invalid characters")}(r[0].productId)}else{if("dynamic"===m.config.linkType&&!t)throw new Error("Payment link ID is required for dynamic checkout");t&&function(e){if(!e||"string"!=typeof e)throw new Error("Invalid payment link ID");if(e.length>10)throw new Error("Payment link ID exceeds maximum length");if(!/^[a-zA-Z0-9_-]+$/.test(e))throw new Error("Payment link ID contains invalid characters")}(t)}m.currentProducts=r||[];try{!function(e,t,r,n){if(!m)return;var o="z-index: 6859456; position: fixed; top: 0px; display: block; left: 0px; height: 100%; width: 100%; max-height: 100dvh; backface-visibility: hidden; overflow-y: visible;",c="min-height: 100%; transition: 0.3s ease-out; position: fixed; top: 0px; left: 0px; width: 100%; height: 100%; background: transparent;",l=document.createElement("div");l.style.cssText=o,l.id="dodo-checkout-outer-div";var s=document.createElement("div");s.style.cssText=c,s.id="dodo-checkout-inner-div";var u,d=document.createElement("iframe");if(d.id=m.iframeId,"static"===m.config.linkType&&null!=e&&e[0])u=function(e,t,r){if(!m)throw new Error("Checkout not initialized");var n=new URL("".concat(m.baseUrl,"/overlay/buy/").concat(e.productId),window.location.origin);t&&Object.entries(t).forEach((function(e){var t=i(e,2),r=t[0],o=t[1];n.searchParams.append(r,o)}));r&&n.searchParams.append("redirect_url",r);return n.searchParams.append("quantity",e.quantity.toString()),n.searchParams.append("theme",m.config.theme||"light"),n}(e[0],r,n);else{if("dynamic"!==m.config.linkType||!t)throw new Error("Invalid checkout configuration");u=function(e){if(!m)throw new Error("Checkout not initialized");var t=new URL("".concat(m.baseUrl,"/overlay/").concat(e),window.location.origin);return t.searchParams.append("theme",m.config.theme||"light"),t}(t)}d.src=u.toString(),d.style.cssText=a,function(e){e.setAttribute("allowtransparency","true"),e.setAttribute("frameBorder","0"),e.allow="payment *",e.setAttribute("sandbox","allow-same-origin allow-scripts allow-forms allow-popups allow-popups-to-escape-sandbox"),e.setAttribute("referrerpolicy","strict-origin-when-cross-origin")}(d),s.appendChild(d),l.appendChild(s),document.body.style.overflow="hidden",document.body.style.contain="initial",document.body.appendChild(l),m.iframe=d,m.container=l}(r,t,o,c)}catch(e){throw console.error("Error creating checkout:",e),h("checkout.error",{message:"Failed to create checkout"}),e}}}var y={Initialize:function(e){if(s(e),m)!function(e){if(!m)throw new Error("Checkout not initialized");s(e);var t=e.onEvent||m.config.onEvent;m.config=o(o({},e),{},{onEvent:t}),m.baseUrl!==c[e.mode]&&(m.baseUrl=c[e.mode],m.allowedOrigin=new URL(m.baseUrl).origin)}(e);else{var t=c[e.mode],r=new URL(t).origin;d=m={iframe:null,container:null,config:e,currentProducts:[],baseUrl:t,allowedOrigin:r,iframeId:"dodo-checkout-iframe",eventListeners:{}},window.addEventListener("message",f)}},Checkout:{open:function(e){v(e)},close:p,isOpen:function(){var e,t;return!!(null!==(e=m)&&void 0!==e&&e.iframe||null!==(t=m)&&void 0!==t&&t.container)}},Status:{libraryVersion:"1.0.1"}};e.DodoPayments=y,Object.defineProperty(e,"__esModule",{value:!0})}));