UNPKG

@juspay/blaze-sdk-web

Version:

SDK for integrating Breeze 1CCO into your Web Application

2 lines (1 loc) 6.03 kB
function e(e){return"string"==typeof e?e:null}function t(e){return e}function s(e){return"object"==typeof e&&null!==e&&!Array.isArray(e)}function n(n){if(s(n)){const r=e(n.requestId),a=e(n.service),i=function(e){switch(e){case"beta":case"release":case"smbBeta":case"smbRelease":return e}return null}(n.environment),o=function(e){if(s(e)){const s={};for(const n in e){const r=t(e[n]);s[n]=r}return s}return null}(n.payload);return null===r||null===a||null===o?null:{requestId:r,service:a,environment:i,payload:o}}return null}function r(e){if(s(e)){const n=function(e){if(s(e)){const s={};for(const n in e){const r=t(e[n]);s[n]=r}return s}return null}(e.cart),r=function(e){if(s(e)){const s={};for(const n in e){const r=t(e[n]);s[n]=r}return s}return null}(e.customer),a=function(e){if(s(e)){const s={};for(const n in e){const r=t(e[n]);s[n]=r}return s}return null}(e.address),i=function(e){if(s(e)){const s={};for(const n in e){const r=t(e[n]);s[n]=r}return s}return null}(e.additionalParams);return null===n?null:{cart:n,customer:r,address:a,additionalParams:i}}return null}function a(t){if(s(t)){const s=e(t.redirectionUrl);return null===s?null:{redirectionUrl:s}}return null}function i(t){if(s(t)){const s=e(t.status),n=e(t.message);return null===s||null===n?null:{status:s,message:n}}return null}async function o(e){return new Promise(t=>setTimeout(t,e))}class c{statusCode;status;response;time;constructor(e,t,s,n){this.statusCode=e,this.status=t,this.response=s,this.time=n}}class l{errorMessage;errorCode;response;errorResponse;errorDetails;time;constructor(e,t,s,n,r,a){this.errorMessage=e,this.errorCode=t,this.response=s,this.errorResponse=n,this.errorDetails=a,this.time=r}}class u{static startHooks=[];static endHooks=[];static retryHooks=[];static async call(e,t,s=null,n=fetch){let r;try{this.startHooks.forEach(t=>{t.func(e)});const a=Date.now(),i=await n(e.url.href,e),o=Date.now()-a,u=await async function(e){let t=null;const s=e.headers.get("content-type");return t=s?.includes("application/json")?await e.json():await e.text(),t}(i),d=t(u);if(null!==d){const e=new c(i.status,i.statusText,d,o);r=e}else{const e=null!==s?s(u):null,t=null!==e?null:{class:"DecodeFailure",name:"DecodeFailure",cause:null,message:"",stack:null},n=new l("Failed to decode API result to success response",i.status,e,u,o,t);r=n}}catch(e){r=new l("Exception in APICaller.call",-1,null,e,0,(a=e)instanceof DOMException?{class:"DOMException",name:a.name,message:a.message,cause:a.cause,stack:a.stack??null}:a instanceof TypeError?{class:"TypeError",name:a.name,message:a.message,cause:a.cause,stack:a.stack??null}:a instanceof Error?{class:"Error",name:a.name,message:a.message,cause:JSON.stringify(a),stack:a.stack??null}:{class:"UnhandledException",name:"UnhandledException",message:null,cause:JSON.stringify(a),stack:null})}var a;return this.endHooks.forEach(t=>{t.func(e,r)}),r}static async callWithRetries(e,t,s,n=null,r=fetch){let a=await this.call(e,t,n,r);if(a instanceof l)for(let i=0;i<s.maxRetries&&(this.retryHooks.forEach(t=>{t.func(e,a,i+1)}),await o(s.retryInterval),a=await this.call(e,t,n,r),!(a instanceof c||null!==a.errorDetails&&!s.retryOn.includes(a.errorDetails.class)));i++);return a}static registerStartHook(e){void 0===this.startHooks.find(t=>t.id===e.id)&&this.startHooks.push(e)}static registerEndHook(e){void 0===this.endHooks.find(t=>t.id===e.id)&&this.endHooks.push(e)}static registerRetryHook(e){void 0===this.retryHooks.find(t=>t.id===e.id)&&this.retryHooks.push(e)}}const d={beta:"https://api.beta.breeze.in",release:"https://api.breeze.in",smbBeta:"https://api.beta.breezesdk.store",smbRelease:"https://api.breezesdk.store"},f="/order/payment/independent/start";function h(e,t="in.breeze.onecco",s){return m(e,t,s,null,{error:!0,errorDetails:"Incorrect payload"})}function m(e,t="in.breeze.onecco",s,n=null,r={}){return{requestId:e??"unknown",service:t,payload:{eventName:s,action:n,...r}}}async function p(e,t,s,n,o){var l;let p;if("initiatePayments"===("initiatePayments"===(l=e.payload.action)?l:null))p=await async function(e,t,s,n){const o=r(e.payload);if(null===o)return m(e.requestId,e.service,"process","initiatePayments",{status:"error",message:"Invalid process payload"});const l=`${d[t]}${f}`,h=s.authToken.startsWith("Bearer ")?s.authToken:`Bearer ${s.authToken}`;try{const t={url:new URL(l),method:"POST",headers:{"Content-Type":"application/json",Authorization:h},body:JSON.stringify({cart:o.cart,customer:{...o.customer??{},address:o.address??{}},shopUrl:s.shopUrl,additionalParams:o.additionalParams}),agent:n?.proxyAgent},r=await u.call(t,a,i,n?.fetchOverride);return m(e.requestId,e.service,"process","initiatePayments",r instanceof c?{status:"success",message:"Payment initiated successfully",redirectionUrl:r.response.redirectionUrl}:{status:r.response?.status??"error",message:r.response?.message??r.errorMessage??"API request failed"})}catch(t){return m(e.requestId,e.service,"process","initiatePayments",{status:"error",message:t instanceof Error?`${t.name}: ${t.message}`:"UnknownError: Unknown error occurred"})}}(e,s,n,o);else p=h(e.requestId,e.service,"process");t(p)}class y{static environment;static initiateData;static callback;static networkOverrides;static initiate(t,r,a){this.callback=r;const i=n(t),o=function(t){if(s(t)){const s=e(t.merchantId),n=e(t.shopUrl),r=e(t.authToken);return null===s||null===n||null===r?null:{merchantId:s,shopUrl:n,authToken:r}}return null}(i?.payload);null!==i&&null!==o?(this.environment=i.environment??"release",this.initiateData=o,this.networkOverrides=a,r(m(i.requestId,i.service,"initiate",null,{status:"success",message:"SDK initialized successfully"}))):r(h(i?.requestId??null,"in.breeze.onecco","initiate"))}static process(e){if("function"!=typeof this.callback)return void console.error("BlazeBackendSDK not initialized. Call BlazeBackendSDK.initiate() first.");const t=n(e);null!==t?p(t,this.callback,this.environment,this.initiateData,this.networkOverrides):this.callback(h(null,"in.breeze.onecco","process"))}}export{y as default};