@saazpayhq/react
Version:
React client for saazpay
2 lines • 14.4 kB
JavaScript
"use client";import x,{useEffect as F,useRef as _,createContext as Ae,useContext as De,useState as Ce}from"react";function q(e,n,o,i,d,u,h){try{var f=e[u](h),p=f.value}catch(v){return void o(v)}f.done?n(p):Promise.resolve(p).then(i,d)}function de(e){return function(){var n=this,o=arguments;return new Promise(function(i,d){var u=e.apply(n,o);function h(p){q(u,i,d,h,f,"next",p)}function f(p){q(u,i,d,h,f,"throw",p)}h(void 0)})}}function ue(e,n,o){return(n=pe(n))in e?Object.defineProperty(e,n,{value:o,enumerable:!0,configurable:!0,writable:!0}):e[n]=o,e}function J(e,n){var o=Object.keys(e);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(e);n&&(i=i.filter(function(d){return Object.getOwnPropertyDescriptor(e,d).enumerable})),o.push.apply(o,i)}return o}function N(e){for(var n=1;n<arguments.length;n++){var o=arguments[n]!=null?arguments[n]:{};n%2?J(Object(o),!0).forEach(function(i){ue(e,i,o[i])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(o)):J(Object(o)).forEach(function(i){Object.defineProperty(e,i,Object.getOwnPropertyDescriptor(o,i))})}return e}function te(e,n){if(e==null)return{};var o,i,d=se(e,n);if(Object.getOwnPropertySymbols){var u=Object.getOwnPropertySymbols(e);for(i=0;i<u.length;i++)o=u[i],n.includes(o)||{}.propertyIsEnumerable.call(e,o)&&(d[o]=e[o])}return d}function se(e,n){if(e==null)return{};var o={};for(var i in e)if({}.hasOwnProperty.call(e,i)){if(n.includes(i))continue;o[i]=e[i]}return o}function H(){H=function(){return n};var e,n={},o=Object.prototype,i=o.hasOwnProperty,d=Object.defineProperty||function(t,r,a){t[r]=a.value},u=typeof Symbol=="function"?Symbol:{},h=u.iterator||"@@iterator",f=u.asyncIterator||"@@asyncIterator",p=u.toStringTag||"@@toStringTag";function v(t,r,a){return Object.defineProperty(t,r,{value:a,enumerable:!0,configurable:!0,writable:!0}),t[r]}try{v({},"")}catch{v=function(r,a,l){return r[a]=l}}function G(t,r,a,l){var c=r&&r.prototype instanceof U?r:U,s=Object.create(c.prototype),y=new V(l||[]);return d(s,"_invoke",{value:ce(t,a,y)}),s}function T(t,r,a){try{return{type:"normal",arg:t.call(r,a)}}catch(l){return{type:"throw",arg:l}}}n.wrap=G;var Y="suspendedStart",oe="suspendedYield",B="executing",A="completed",w={};function U(){}function D(){}function E(){}var j={};v(j,h,function(){return this});var z=Object.getPrototypeOf,C=z&&z(z(k([])));C&&C!==o&&i.call(C,h)&&(j=C);var P=E.prototype=U.prototype=Object.create(j);function $(t){["next","throw","return"].forEach(function(r){v(t,r,function(a){return this._invoke(r,a)})})}function I(t,r){function a(c,s,y,g){var m=T(t[c],t,s);if(m.type!=="throw"){var O=m.arg,L=O.value;return L&&typeof L=="object"&&i.call(L,"__await")?r.resolve(L.__await).then(function(b){a("next",b,y,g)},function(b){a("throw",b,y,g)}):r.resolve(L).then(function(b){O.value=b,y(O)},function(b){return a("throw",b,y,g)})}g(m.arg)}var l;d(this,"_invoke",{value:function(c,s){function y(){return new r(function(g,m){a(c,s,g,m)})}return l=l?l.then(y,y):y()}})}function ce(t,r,a){var l=Y;return function(c,s){if(l===B)throw Error("Generator is already running");if(l===A){if(c==="throw")throw s;return{value:e,done:!0}}for(a.method=c,a.arg=s;;){var y=a.delegate;if(y){var g=W(y,a);if(g){if(g===w)continue;return g}}if(a.method==="next")a.sent=a._sent=a.arg;else if(a.method==="throw"){if(l===Y)throw l=A,a.arg;a.dispatchException(a.arg)}else a.method==="return"&&a.abrupt("return",a.arg);l=B;var m=T(t,r,a);if(m.type==="normal"){if(l=a.done?A:oe,m.arg===w)continue;return{value:m.arg,done:a.done}}m.type==="throw"&&(l=A,a.method="throw",a.arg=m.arg)}}}function W(t,r){var a=r.method,l=t.iterator[a];if(l===e)return r.delegate=null,a==="throw"&&t.iterator.return&&(r.method="return",r.arg=e,W(t,r),r.method==="throw")||a!=="return"&&(r.method="throw",r.arg=new TypeError("The iterator does not provide a '"+a+"' method")),w;var c=T(l,t.iterator,r.arg);if(c.type==="throw")return r.method="throw",r.arg=c.arg,r.delegate=null,w;var s=c.arg;return s?s.done?(r[t.resultName]=s.value,r.next=t.nextLoc,r.method!=="return"&&(r.method="next",r.arg=e),r.delegate=null,w):s:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,w)}function le(t){var r={tryLoc:t[0]};1 in t&&(r.catchLoc=t[1]),2 in t&&(r.finallyLoc=t[2],r.afterLoc=t[3]),this.tryEntries.push(r)}function R(t){var r=t.completion||{};r.type="normal",delete r.arg,t.completion=r}function V(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(le,this),this.reset(!0)}function k(t){if(t||t===""){var r=t[h];if(r)return r.call(t);if(typeof t.next=="function")return t;if(!isNaN(t.length)){var a=-1,l=function c(){for(;++a<t.length;)if(i.call(t,a))return c.value=t[a],c.done=!1,c;return c.value=e,c.done=!0,c};return l.next=l}}throw new TypeError(typeof t+" is not iterable")}return D.prototype=E,d(P,"constructor",{value:E,configurable:!0}),d(E,"constructor",{value:D,configurable:!0}),D.displayName=v(E,p,"GeneratorFunction"),n.isGeneratorFunction=function(t){var r=typeof t=="function"&&t.constructor;return!!r&&(r===D||(r.displayName||r.name)==="GeneratorFunction")},n.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,E):(t.__proto__=E,v(t,p,"GeneratorFunction")),t.prototype=Object.create(P),t},n.awrap=function(t){return{__await:t}},$(I.prototype),v(I.prototype,f,function(){return this}),n.AsyncIterator=I,n.async=function(t,r,a,l,c){c===void 0&&(c=Promise);var s=new I(G(t,r,a,l),c);return n.isGeneratorFunction(r)?s:s.next().then(function(y){return y.done?y.value:s.next()})},$(P),v(P,p,"Generator"),v(P,h,function(){return this}),v(P,"toString",function(){return"[object Generator]"}),n.keys=function(t){var r=Object(t),a=[];for(var l in r)a.push(l);return a.reverse(),function c(){for(;a.length;){var s=a.pop();if(s in r)return c.value=s,c.done=!1,c}return c.done=!0,c}},n.values=k,V.prototype={constructor:V,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=e,this.done=!1,this.delegate=null,this.method="next",this.arg=e,this.tryEntries.forEach(R),!t)for(var r in this)r.charAt(0)==="t"&&i.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=e)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if(t.type==="throw")throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var r=this;function a(m,O){return s.type="throw",s.arg=t,r.next=m,O&&(r.method="next",r.arg=e),!!O}for(var l=this.tryEntries.length-1;l>=0;--l){var c=this.tryEntries[l],s=c.completion;if(c.tryLoc==="root")return a("end");if(c.tryLoc<=this.prev){var y=i.call(c,"catchLoc"),g=i.call(c,"finallyLoc");if(y&&g){if(this.prev<c.catchLoc)return a(c.catchLoc,!0);if(this.prev<c.finallyLoc)return a(c.finallyLoc)}else if(y){if(this.prev<c.catchLoc)return a(c.catchLoc,!0)}else{if(!g)throw Error("try statement without catch or finally");if(this.prev<c.finallyLoc)return a(c.finallyLoc)}}}},abrupt:function(t,r){for(var a=this.tryEntries.length-1;a>=0;--a){var l=this.tryEntries[a];if(l.tryLoc<=this.prev&&i.call(l,"finallyLoc")&&this.prev<l.finallyLoc){var c=l;break}}c&&(t==="break"||t==="continue")&&c.tryLoc<=r&&r<=c.finallyLoc&&(c=null);var s=c?c.completion:{};return s.type=t,s.arg=r,c?(this.method="next",this.next=c.finallyLoc,w):this.complete(s)},complete:function(t,r){if(t.type==="throw")throw t.arg;return t.type==="break"||t.type==="continue"?this.next=t.arg:t.type==="return"?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):t.type==="normal"&&r&&(this.next=r),w},finish:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var a=this.tryEntries[r];if(a.finallyLoc===t)return this.complete(a.completion,a.afterLoc),R(a),w}},catch:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var a=this.tryEntries[r];if(a.tryLoc===t){var l=a.completion;if(l.type==="throw"){var c=l.arg;R(a)}return c}}throw Error("illegal catch attempt")},delegateYield:function(t,r,a){return this.delegate={iterator:k(t),resultName:r,nextLoc:a},this.method==="next"&&(this.arg=e),w}},n}function fe(e,n){if(typeof e!="object"||!e)return e;var o=e[Symbol.toPrimitive];if(o!==void 0){var i=o.call(e,n||"default");if(typeof i!="object")return i;throw new TypeError("@@toPrimitive must return a primitive value.")}return(n==="string"?String:Number)(e)}function pe(e){var n=fe(e,"string");return typeof n=="symbol"?n:n+""}var S={CLASSIC:"classic",V1:"v1"},he=S.V1,ye="https://cdn.paddle.com/paddle/paddle.js",ve="https://cdn.paddle.com/paddle/v2/paddle.js",ge={url:ye},me={url:ve};function we(e){return document.querySelector('script[src="'.concat(e,'"]'))||void 0}function Ee(e){var n=document.createElement("script");n.src=e;var o=document.head||document.body;if(!o)throw new Error("Cannot inject Paddle.js. It needs a <head> or <body> element.");return o.appendChild(n),n}var K={classic:void 0,v1:void 0},be={classic:"PaddleClassic",v1:"PaddleBillingV1"};function Pe(e){var n,o=(n=Oe(e))===null||n===void 0?void 0:n.url;if(o){var i=K[e],d=be[e];return i!==void 0?i:(K[e]=new Promise(function(u,h){if(typeof window>"u"){u(void 0);return}if(window[d]){u(window[d]);return}try{var f=we(o);f||(f=Ee(o)),f.addEventListener("load",function(){window[d]?u(window[d]):h(new Error("Paddle.js not available"))}),f.addEventListener("error",function(){h(new Error("Failed to load Paddle.js - ".concat(e)))})}catch(p){h(p);return}}),K[e])}}function Oe(e){if(e===S.CLASSIC)return ge;if(e===S.V1)return me;console.error("[Paddle] Unknown Paddle Version")}var _e=["environment","version"],Le=["environment","version"];function Se(e,n){var o=e.environment;e.version;var i=te(e,_e);try{o&&n.Environment.set(o),n.Initialized?n.Update(N({},i)):n.Initialize(N({},i))}catch(d){console.warn("[Paddle] Paddle Initialization failed. Please check the inputs",d)}}function xe(e,n){var o=e.environment;e.version;var i=te(e,Le);try{o&&n.Environment.set(o),n.Setup(N({},i))}catch(d){console.warn("[Paddle] Paddle Initialization failed. Please check the inputs",d)}}var X;(function(e){e.CHECKOUT_LOADED="checkout.loaded",e.CHECKOUT_CLOSED="checkout.closed",e.CHECKOUT_UPDATED="checkout.updated",e.CHECKOUT_COMPLETED="checkout.completed",e.CHECKOUT_ERROR="checkout.error",e.CHECKOUT_FAILED="checkout.failed",e.CHECKOUT_ITEMS_UPDATED="checkout.items.updated",e.CHECKOUT_ITEMS_REMOVED="checkout.items.removed",e.CHECKOUT_CUSTOMER_CREATED="checkout.customer.created",e.CHECKOUT_CUSTOMER_UPDATED="checkout.customer.updated",e.CHECKOUT_CUSTOMER_REMOVED="checkout.customer.removed",e.CHECKOUT_PAYMENT_SELECTED="checkout.payment.selected",e.CHECKOUT_PAYMENT_INITIATED="checkout.payment.initiated",e.CHECKOUT_PAYMENT_FAILED="checkout.payment.failed",e.CHECKOUT_DISCOUNT_APPLIED="checkout.discount.applied",e.CHECKOUT_DISCOUNT_REMOVED="checkout.discount.removed"})(X||(X={}));var Q;(function(e){e.DAY="day",e.WEEK="week",e.MONTH="month",e.YEAR="year"})(Q||(Q={}));var Z;(function(e){e.ALIPAY="alipay",e.APPLE_PAY="apple-pay",e.CARD="card",e.GOOGLE_PAY="google-pay",e.IDEAL="ideal",e.PAYPAL="paypal",e.WIRE_TRANSFER="wire-transfer",e.NONE="none"})(Z||(Z={}));var ee;(function(e){e.AMERICAN_EXPRESS="american_express",e.DINERS_CLUB="diners_club",e.DISCOVER="discover",e.JCB="jcb",e.MADA="mada",e.MAESTRO="maestro",e.MASTER_CARD="mastercard",e.UNION_PAY="union_pay",e.VISA="visa",e.UNKNOWN="unknown"})(ee||(ee={}));var re;(function(e){e.DRAFT="draft",e.READY="ready",e.COMPLETED="completed",e.BILLED="billed",e.canceled="canceled",e.PAST_DUE="past_due"})(re||(re={}));function ne(e){return M.apply(this,arguments)}function M(){return M=de(H().mark(function e(n){var o,i;return H().wrap(function(u){for(;;)switch(u.prev=u.next){case 0:return o=n?.version||he,u.next=3,Pe(o);case 3:if(i=u.sent,!i){u.next=9;break}return n&&(o===S.V1?Se(n,i):o===S.CLASSIC&&xe(n,i)),u.abrupt("return",i);case 9:return console.error("[Paddle] Error Loading Paddle"),u.abrupt("return");case 11:case"end":return u.stop()}},e)})),M.apply(this,arguments)}var ae=Ae(void 0),ie=()=>{let e=De(ae);if(!e)throw new Error("useSaazpay must be used within a SaazpayProvider");return e},Re=({children:e,config:n})=>{let[o,i]=Ce(void 0);return F(()=>{ne({environment:n.environment,token:n.clientToken,eventCallback:function(d){d.name=="checkout.completed"&&(n.successUrl?window.location.href=n.successUrl:window.location.reload())}}).then(d=>{d&&i(d)})},[n]),x.createElement(ae.Provider,{value:{paddle:o,config:n}},e)},Ve=({access_token:e,darkMode:n,align:o})=>{let i=_(null),d=_(void 0),u=_("");try{let{paddle:p,config:v}=ie();d.current=p,u.current=v.baseUrl}catch{throw new Error("SaazpayEmbed must be used within SaazpayProvider")}let h=p=>{p.data?.type==="iframe-resize"&&i.current&&(i.current.style.height=`${p.data.height}px`),p.data?.type==="is-dark-mode"&&(n=p.data.value)},f=p=>{p.data?.type==="checkout_data"&&p.data.payload&&d.current?.Checkout.open({...p.data.payload,settings:{theme:n?"dark":"light"}})};return F(()=>(window.addEventListener("message",h),window.addEventListener("message",f),()=>{window.removeEventListener("message",h),window.removeEventListener("message",f)}),[]),x.createElement("div",{style:{position:"relative",width:"100%",height:"20vh",overflow:"hidden",transition:"height 0.25s ease"},ref:i},x.createElement("iframe",{loading:"lazy",style:{width:"100%",height:"100%",overflow:"hidden",border:"none"},src:`${u.current}/embed/client/${e}?dark=${n}&align=${o}`}))},ke=({access_token:e,darkMode:n})=>{let o=_(null),i=_(void 0),d=_("");try{let{paddle:f,config:p}=ie();i.current=f,d.current=p.baseUrl}catch{throw new Error("SaazpayEmbed must be used within SaazpayProvider")}let u=f=>{f.data?.type==="iframe-resize"&&o.current&&(o.current.style.height=`${f.data.height}px`),f.data?.type==="is-dark-mode"&&(n=f.data.value)},h=f=>{f.data?.type==="checkout_data"&&f.data.payload&&i.current?.Checkout.open({...f.data.payload,settings:{theme:n?"dark":"light"}})};return F(()=>(window.addEventListener("message",u),window.addEventListener("message",h),()=>{window.removeEventListener("message",u),window.removeEventListener("message",h)}),[]),x.createElement("div",{style:{position:"relative",width:"100%",height:"20vh",overflow:"hidden",transition:"height 0.25s ease"},ref:o},x.createElement("iframe",{loading:"lazy",style:{width:"100%",height:"100%",overflow:"hidden",border:"none"},src:`${d.current}/embed/manage/${e}?dark=${n}`}))};export{Ve as SaazpayEmbed,ke as SaazpayManagement,Re as SaazpayProvider,ie as useSaazpay};
//# sourceMappingURL=index.js.map