UNPKG

@saazpayhq/react

Version:

React client for saazpay

2 lines 15.2 kB
"use strict";"use client";var de=Object.create;var I=Object.defineProperty;var ue=Object.getOwnPropertyDescriptor;var se=Object.getOwnPropertyNames;var fe=Object.getPrototypeOf,pe=Object.prototype.hasOwnProperty;var he=(e,t)=>{for(var i in t)I(e,i,{get:t[i],enumerable:!0})},q=(e,t,i,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let l of se(t))!pe.call(e,l)&&l!==i&&I(e,l,{get:()=>t[l],enumerable:!(o=ue(t,l))||o.enumerable});return e};var ye=(e,t,i)=>(i=e!=null?de(fe(e)):{},q(t||!e||!e.__esModule?I(i,"default",{value:e,enumerable:!0}):i,e)),ve=e=>q(I({},"__esModule",{value:!0}),e);var Ke={};he(Ke,{SaazpayEmbed:()=>Ve,SaazpayManagement:()=>ke,SaazpayProvider:()=>Re,useSaazpay:()=>F});module.exports=ve(Ke);var h=ye(require("react"),1);function J(e,t,i,o,l,u,y){try{var f=e[u](y),p=f.value}catch(g){return void i(g)}f.done?t(p):Promise.resolve(p).then(o,l)}function ge(e){return function(){var t=this,i=arguments;return new Promise(function(o,l){var u=e.apply(t,i);function y(p){J(u,o,l,y,f,"next",p)}function f(p){J(u,o,l,y,f,"throw",p)}y(void 0)})}}function me(e,t,i){return(t=be(t))in e?Object.defineProperty(e,t,{value:i,enumerable:!0,configurable:!0,writable:!0}):e[t]=i,e}function X(e,t){var i=Object.keys(e);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);t&&(o=o.filter(function(l){return Object.getOwnPropertyDescriptor(e,l).enumerable})),i.push.apply(i,o)}return i}function N(e){for(var t=1;t<arguments.length;t++){var i=arguments[t]!=null?arguments[t]:{};t%2?X(Object(i),!0).forEach(function(o){me(e,o,i[o])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(i)):X(Object(i)).forEach(function(o){Object.defineProperty(e,o,Object.getOwnPropertyDescriptor(i,o))})}return e}function ne(e,t){if(e==null)return{};var i,o,l=we(e,t);if(Object.getOwnPropertySymbols){var u=Object.getOwnPropertySymbols(e);for(o=0;o<u.length;o++)i=u[o],t.includes(i)||{}.propertyIsEnumerable.call(e,i)&&(l[i]=e[i])}return l}function we(e,t){if(e==null)return{};var i={};for(var o in e)if({}.hasOwnProperty.call(e,o)){if(t.includes(o))continue;i[o]=e[o]}return i}function H(){H=function(){return t};var e,t={},i=Object.prototype,o=i.hasOwnProperty,l=Object.defineProperty||function(n,r,a){n[r]=a.value},u=typeof Symbol=="function"?Symbol:{},y=u.iterator||"@@iterator",f=u.asyncIterator||"@@asyncIterator",p=u.toStringTag||"@@toStringTag";function g(n,r,a){return Object.defineProperty(n,r,{value:a,enumerable:!0,configurable:!0,writable:!0}),n[r]}try{g({},"")}catch{g=function(r,a,d){return r[a]=d}}function G(n,r,a,d){var c=r&&r.prototype instanceof U?r:U,s=Object.create(c.prototype),v=new V(d||[]);return l(s,"_invoke",{value:ce(n,a,v)}),s}function T(n,r,a){try{return{type:"normal",arg:n.call(r,a)}}catch(d){return{type:"throw",arg:d}}}t.wrap=G;var Y="suspendedStart",oe="suspendedYield",B="executing",x="completed",E={};function U(){}function A(){}function b(){}var j={};g(j,y,function(){return this});var z=Object.getPrototypeOf,D=z&&z(z(k([])));D&&D!==i&&o.call(D,y)&&(j=D);var O=b.prototype=U.prototype=Object.create(j);function $(n){["next","throw","return"].forEach(function(r){g(n,r,function(a){return this._invoke(r,a)})})}function C(n,r){function a(c,s,v,m){var w=T(n[c],n,s);if(w.type!=="throw"){var _=w.arg,L=_.value;return L&&typeof L=="object"&&o.call(L,"__await")?r.resolve(L.__await).then(function(P){a("next",P,v,m)},function(P){a("throw",P,v,m)}):r.resolve(L).then(function(P){_.value=P,v(_)},function(P){return a("throw",P,v,m)})}m(w.arg)}var d;l(this,"_invoke",{value:function(c,s){function v(){return new r(function(m,w){a(c,s,m,w)})}return d=d?d.then(v,v):v()}})}function ce(n,r,a){var d=Y;return function(c,s){if(d===B)throw Error("Generator is already running");if(d===x){if(c==="throw")throw s;return{value:e,done:!0}}for(a.method=c,a.arg=s;;){var v=a.delegate;if(v){var m=W(v,a);if(m){if(m===E)continue;return m}}if(a.method==="next")a.sent=a._sent=a.arg;else if(a.method==="throw"){if(d===Y)throw d=x,a.arg;a.dispatchException(a.arg)}else a.method==="return"&&a.abrupt("return",a.arg);d=B;var w=T(n,r,a);if(w.type==="normal"){if(d=a.done?x:oe,w.arg===E)continue;return{value:w.arg,done:a.done}}w.type==="throw"&&(d=x,a.method="throw",a.arg=w.arg)}}}function W(n,r){var a=r.method,d=n.iterator[a];if(d===e)return r.delegate=null,a==="throw"&&n.iterator.return&&(r.method="return",r.arg=e,W(n,r),r.method==="throw")||a!=="return"&&(r.method="throw",r.arg=new TypeError("The iterator does not provide a '"+a+"' method")),E;var c=T(d,n.iterator,r.arg);if(c.type==="throw")return r.method="throw",r.arg=c.arg,r.delegate=null,E;var s=c.arg;return s?s.done?(r[n.resultName]=s.value,r.next=n.nextLoc,r.method!=="return"&&(r.method="next",r.arg=e),r.delegate=null,E):s:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,E)}function le(n){var r={tryLoc:n[0]};1 in n&&(r.catchLoc=n[1]),2 in n&&(r.finallyLoc=n[2],r.afterLoc=n[3]),this.tryEntries.push(r)}function R(n){var r=n.completion||{};r.type="normal",delete r.arg,n.completion=r}function V(n){this.tryEntries=[{tryLoc:"root"}],n.forEach(le,this),this.reset(!0)}function k(n){if(n||n===""){var r=n[y];if(r)return r.call(n);if(typeof n.next=="function")return n;if(!isNaN(n.length)){var a=-1,d=function c(){for(;++a<n.length;)if(o.call(n,a))return c.value=n[a],c.done=!1,c;return c.value=e,c.done=!0,c};return d.next=d}}throw new TypeError(typeof n+" is not iterable")}return A.prototype=b,l(O,"constructor",{value:b,configurable:!0}),l(b,"constructor",{value:A,configurable:!0}),A.displayName=g(b,p,"GeneratorFunction"),t.isGeneratorFunction=function(n){var r=typeof n=="function"&&n.constructor;return!!r&&(r===A||(r.displayName||r.name)==="GeneratorFunction")},t.mark=function(n){return Object.setPrototypeOf?Object.setPrototypeOf(n,b):(n.__proto__=b,g(n,p,"GeneratorFunction")),n.prototype=Object.create(O),n},t.awrap=function(n){return{__await:n}},$(C.prototype),g(C.prototype,f,function(){return this}),t.AsyncIterator=C,t.async=function(n,r,a,d,c){c===void 0&&(c=Promise);var s=new C(G(n,r,a,d),c);return t.isGeneratorFunction(r)?s:s.next().then(function(v){return v.done?v.value:s.next()})},$(O),g(O,p,"Generator"),g(O,y,function(){return this}),g(O,"toString",function(){return"[object Generator]"}),t.keys=function(n){var r=Object(n),a=[];for(var d in r)a.push(d);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}},t.values=k,V.prototype={constructor:V,reset:function(n){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),!n)for(var r in this)r.charAt(0)==="t"&&o.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=e)},stop:function(){this.done=!0;var n=this.tryEntries[0].completion;if(n.type==="throw")throw n.arg;return this.rval},dispatchException:function(n){if(this.done)throw n;var r=this;function a(w,_){return s.type="throw",s.arg=n,r.next=w,_&&(r.method="next",r.arg=e),!!_}for(var d=this.tryEntries.length-1;d>=0;--d){var c=this.tryEntries[d],s=c.completion;if(c.tryLoc==="root")return a("end");if(c.tryLoc<=this.prev){var v=o.call(c,"catchLoc"),m=o.call(c,"finallyLoc");if(v&&m){if(this.prev<c.catchLoc)return a(c.catchLoc,!0);if(this.prev<c.finallyLoc)return a(c.finallyLoc)}else if(v){if(this.prev<c.catchLoc)return a(c.catchLoc,!0)}else{if(!m)throw Error("try statement without catch or finally");if(this.prev<c.finallyLoc)return a(c.finallyLoc)}}}},abrupt:function(n,r){for(var a=this.tryEntries.length-1;a>=0;--a){var d=this.tryEntries[a];if(d.tryLoc<=this.prev&&o.call(d,"finallyLoc")&&this.prev<d.finallyLoc){var c=d;break}}c&&(n==="break"||n==="continue")&&c.tryLoc<=r&&r<=c.finallyLoc&&(c=null);var s=c?c.completion:{};return s.type=n,s.arg=r,c?(this.method="next",this.next=c.finallyLoc,E):this.complete(s)},complete:function(n,r){if(n.type==="throw")throw n.arg;return n.type==="break"||n.type==="continue"?this.next=n.arg:n.type==="return"?(this.rval=this.arg=n.arg,this.method="return",this.next="end"):n.type==="normal"&&r&&(this.next=r),E},finish:function(n){for(var r=this.tryEntries.length-1;r>=0;--r){var a=this.tryEntries[r];if(a.finallyLoc===n)return this.complete(a.completion,a.afterLoc),R(a),E}},catch:function(n){for(var r=this.tryEntries.length-1;r>=0;--r){var a=this.tryEntries[r];if(a.tryLoc===n){var d=a.completion;if(d.type==="throw"){var c=d.arg;R(a)}return c}}throw Error("illegal catch attempt")},delegateYield:function(n,r,a){return this.delegate={iterator:k(n),resultName:r,nextLoc:a},this.method==="next"&&(this.arg=e),E}},t}function Ee(e,t){if(typeof e!="object"||!e)return e;var i=e[Symbol.toPrimitive];if(i!==void 0){var o=i.call(e,t||"default");if(typeof o!="object")return o;throw new TypeError("@@toPrimitive must return a primitive value.")}return(t==="string"?String:Number)(e)}function be(e){var t=Ee(e,"string");return typeof t=="symbol"?t:t+""}var S={CLASSIC:"classic",V1:"v1"},Pe=S.V1,Oe="https://cdn.paddle.com/paddle/paddle.js",_e="https://cdn.paddle.com/paddle/v2/paddle.js",Le={url:Oe},Se={url:_e};function xe(e){return document.querySelector('script[src="'.concat(e,'"]'))||void 0}function Ae(e){var t=document.createElement("script");t.src=e;var i=document.head||document.body;if(!i)throw new Error("Cannot inject Paddle.js. It needs a <head> or <body> element.");return i.appendChild(t),t}var K={classic:void 0,v1:void 0},De={classic:"PaddleClassic",v1:"PaddleBillingV1"};function Ce(e){var t,i=(t=Ie(e))===null||t===void 0?void 0:t.url;if(i){var o=K[e],l=De[e];return o!==void 0?o:(K[e]=new Promise(function(u,y){if(typeof window>"u"){u(void 0);return}if(window[l]){u(window[l]);return}try{var f=xe(i);f||(f=Ae(i)),f.addEventListener("load",function(){window[l]?u(window[l]):y(new Error("Paddle.js not available"))}),f.addEventListener("error",function(){y(new Error("Failed to load Paddle.js - ".concat(e)))})}catch(p){y(p);return}}),K[e])}}function Ie(e){if(e===S.CLASSIC)return Le;if(e===S.V1)return Se;console.error("[Paddle] Unknown Paddle Version")}var Te=["environment","version"],Ue=["environment","version"];function je(e,t){var i=e.environment;e.version;var o=ne(e,Te);try{i&&t.Environment.set(i),t.Initialized?t.Update(N({},o)):t.Initialize(N({},o))}catch(l){console.warn("[Paddle] Paddle Initialization failed. Please check the inputs",l)}}function ze(e,t){var i=e.environment;e.version;var o=ne(e,Ue);try{i&&t.Environment.set(i),t.Setup(N({},o))}catch(l){console.warn("[Paddle] Paddle Initialization failed. Please check the inputs",l)}}var Q;(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"})(Q||(Q={}));var Z;(function(e){e.DAY="day",e.WEEK="week",e.MONTH="month",e.YEAR="year"})(Z||(Z={}));var ee;(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"})(ee||(ee={}));var re;(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"})(re||(re={}));var te;(function(e){e.DRAFT="draft",e.READY="ready",e.COMPLETED="completed",e.BILLED="billed",e.canceled="canceled",e.PAST_DUE="past_due"})(te||(te={}));function ae(e){return M.apply(this,arguments)}function M(){return M=ge(H().mark(function e(t){var i,o;return H().wrap(function(u){for(;;)switch(u.prev=u.next){case 0:return i=t?.version||Pe,u.next=3,Ce(i);case 3:if(o=u.sent,!o){u.next=9;break}return t&&(i===S.V1?je(t,o):i===S.CLASSIC&&ze(t,o)),u.abrupt("return",o);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 ie=(0,h.createContext)(void 0),F=()=>{let e=(0,h.useContext)(ie);if(!e)throw new Error("useSaazpay must be used within a SaazpayProvider");return e},Re=({children:e,config:t})=>{let[i,o]=(0,h.useState)(void 0);return(0,h.useEffect)(()=>{ae({environment:t.environment,token:t.clientToken,eventCallback:function(l){l.name=="checkout.completed"&&(t.successUrl?window.location.href=t.successUrl:window.location.reload())}}).then(l=>{l&&o(l)})},[t]),h.default.createElement(ie.Provider,{value:{paddle:i,config:t}},e)},Ve=({access_token:e,darkMode:t,align:i})=>{let o=(0,h.useRef)(null),l=(0,h.useRef)(void 0),u=(0,h.useRef)("");try{let{paddle:p,config:g}=F();l.current=p,u.current=g.baseUrl}catch{throw new Error("SaazpayEmbed must be used within SaazpayProvider")}let y=p=>{p.data?.type==="iframe-resize"&&o.current&&(o.current.style.height=`${p.data.height}px`),p.data?.type==="is-dark-mode"&&(t=p.data.value)},f=p=>{p.data?.type==="checkout_data"&&p.data.payload&&l.current?.Checkout.open({...p.data.payload,settings:{theme:t?"dark":"light"}})};return(0,h.useEffect)(()=>(window.addEventListener("message",y),window.addEventListener("message",f),()=>{window.removeEventListener("message",y),window.removeEventListener("message",f)}),[]),h.default.createElement("div",{style:{position:"relative",width:"100%",height:"20vh",overflow:"hidden",transition:"height 0.25s ease"},ref:o},h.default.createElement("iframe",{loading:"lazy",style:{width:"100%",height:"100%",overflow:"hidden",border:"none"},src:`${u.current}/embed/client/${e}?dark=${t}&align=${i}`}))},ke=({access_token:e,darkMode:t})=>{let i=(0,h.useRef)(null),o=(0,h.useRef)(void 0),l=(0,h.useRef)("");try{let{paddle:f,config:p}=F();o.current=f,l.current=p.baseUrl}catch{throw new Error("SaazpayEmbed must be used within SaazpayProvider")}let u=f=>{f.data?.type==="iframe-resize"&&i.current&&(i.current.style.height=`${f.data.height}px`),f.data?.type==="is-dark-mode"&&(t=f.data.value)},y=f=>{f.data?.type==="checkout_data"&&f.data.payload&&o.current?.Checkout.open({...f.data.payload,settings:{theme:t?"dark":"light"}})};return(0,h.useEffect)(()=>(window.addEventListener("message",u),window.addEventListener("message",y),()=>{window.removeEventListener("message",u),window.removeEventListener("message",y)}),[]),h.default.createElement("div",{style:{position:"relative",width:"100%",height:"20vh",overflow:"hidden",transition:"height 0.25s ease"},ref:i},h.default.createElement("iframe",{loading:"lazy",style:{width:"100%",height:"100%",overflow:"hidden",border:"none"},src:`${l.current}/embed/manage/${e}?dark=${t}`}))};0&&(module.exports={SaazpayEmbed,SaazpayManagement,SaazpayProvider,useSaazpay}); //# sourceMappingURL=index.cjs.map