@dropp.cc/payment-sdk 1.0.29 โ†’ 1.0.31

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.
package/README.md CHANGED
@@ -907,6 +907,73 @@ if (!status?.data?.linked) {
907
907
  }
908
908
  ```
909
909
 
910
+ ## 19.1 QR Scanner and UUID Lookup APIs
911
+
912
+ The SDK provides two additive APIs for QR-driven payment detail retrieval:
913
+
914
+ ```js
915
+ const scanResult = await Dropp.scanPaymentDetails();
916
+ const lookupResult = await Dropp.fetchPaymentDetails('6b0f8bb2-6d17-4d5d-bb5f-478e37dcae11');
917
+ ```
918
+
919
+ Both methods return the same response shape:
920
+
921
+ | Field | Type | Description |
922
+ |---|---|---|
923
+ | `success` | boolean | `true` only when lookup succeeded and data was returned. |
924
+ | `uuid` | string | UUID used for lookup or resolved from scanner flow. |
925
+ | `message` | string | Human-readable status message. |
926
+ | `data` | object \| null | Parsed payment details payload when available. |
927
+ | `rawResponse` | object \| null | Unmodified backend/bridge payload for debugging. |
928
+ | `error` | string \| null | Error description when `success` is `false`. |
929
+ | `responseCode` | number \| null | Service response code when available. |
930
+
931
+ ### `Dropp.scanPaymentDetails()`
932
+
933
+ Behavior summary:
934
+
935
+ 1. Opens the hosted scanner page (`openScanner`) in SDK modal.
936
+ 2. Waits for webview/native bridge lookup result event (`UUID_LOOKUP_RESULT`).
937
+ 3. Resolves with structured success/failure payload.
938
+ 4. Uses built-in timeout and deterministic failure result on cancel/close/bridge timeout.
939
+
940
+ Example:
941
+
942
+ ```js
943
+ const result = await Dropp.scanPaymentDetails();
944
+
945
+ if (result.success) {
946
+ console.log('UUID', result.uuid);
947
+ console.log('details', result.data);
948
+ } else {
949
+ console.error(result.message, result.error);
950
+ }
951
+ ```
952
+
953
+ ### `Dropp.fetchPaymentDetails(uuid)`
954
+
955
+ Behavior summary:
956
+
957
+ 1. Validates UUID format before opening hosted lookup flow.
958
+ 2. Opens hosted UUID page route in webview modal:
959
+ `#/uuid/<uuid>`
960
+ 3. Passes SDK context (`sessionId`, `merchantDomain`, `merchantAccount`, `auth`) to hosted page.
961
+ 4. Waits for bridge/webview lookup result event (`UUID_LOOKUP_RESULT`).
962
+ 5. Parses `data` when returned as JSON string.
963
+ 6. Returns:
964
+ - success payload when `responseCode === 0` and data exists
965
+ - failure payload with message `No data found or invalid UUID` for invalid UUID/no data/non-zero response/timeout.
966
+
967
+ Example:
968
+
969
+ ```js
970
+ const result = await Dropp.fetchPaymentDetails('6b0f8bb2-6d17-4d5d-bb5f-478e37dcae11');
971
+
972
+ if (!result.success) {
973
+ console.error(result.message, result.error);
974
+ }
975
+ ```
976
+
910
977
  ---
911
978
 
912
979
  # 20. Custom Pages and Modules
@@ -1,2 +1,2 @@
1
- const e={READY:"DROPP_SDK_READY",PAYMENT_SUCCESS:"PAYMENT_SUCCESS",PAYMENT_FAILED:"PAYMENT_FAILED",PAYMENT_CANCELLED:"PAYMENT_CANCELLED",ACCOUNT_EVENT:"ACCOUNT_EVENT",PAGE_EVENT:"PAGE_EVENT",ACCOUNT_STATUS:"ACCOUNT_STATUS",CLOSE_WEBVIEW:"CLOSE_WEBVIEW",AUTO_CLOSE_WEBVIEW:"AUTO_CLOSE_WEBVIEW",PAYMENT_PAGE_LOADED:"PAYMENT_PAGE_LOADED",PAGE_LOADED:"PAGE_LOADED"},t="DROPP_SDK_INIT",n="DROPP_SDK_CLOSE",s={PRODUCTION:"production",QA:"qa",SANDBOX:"sandbox"},i={[s.PRODUCTION]:"https://wv.dropp.cc",[s.QA]:"https://wv.qa.dropp.cc",[s.SANDBOX]:"https://wv.sandbox.dropp.cc"},o={[s.PRODUCTION]:["https://wv.dropp.cc"],[s.QA]:["https://wv.qa.dropp.cc"],[s.SANDBOX]:["https://wv.sandbox.dropp.cc"]},r=3e4,a=6e5,l={STANDARD:"standard",PREAUTH:"preauth",RECURRING:"recurring"},c="INIT_TIMEOUT",u="PAYMENT_TIMEOUT",d="INVALID_CONFIG",p="ALREADY_OPEN",h="UNKNOWN_ERROR",m="1.0.2";const g=new class{constructor(e,t={}){this.environment=e,this.forceEnabled=!!t.enabled,this.enabled=this._shouldEnableLogging()}_shouldEnableLogging(){return this.forceEnabled}setEnvironment(e,t={}){this.environment=e,"boolean"==typeof t.enabled&&(this.forceEnabled=t.enabled),this.enabled=this._shouldEnableLogging()}setEnabled(e){this.forceEnabled=!!e,this.enabled=this._shouldEnableLogging()}log(...e){this.enabled&&console.log(...e)}warn(...e){this.enabled&&console.warn(...e)}error(...e){this.enabled&&console.error(...e)}debug(...e){this.enabled&&console.debug(...e)}info(...e){this.enabled&&console.info(...e)}}("qa",{enabled:!1}),y=["USD","HBAR","USDC"],f=["NONE","HALF_HOURLY","HOURLY","DAILY","WEEKLY","MONTHLY","YEARLY"],D=[l.STANDARD,l.PREAUTH,l.RECURRING],S=["item","subTotal","otherCharges","discount"],b=["label","value","type"];function w(e,t={}){const{isProduction:n=!1,allowLocalCallbacksInNonProd:s=!1}=t;if("string"!=typeof e||!e.trim())return!1;try{const t=new URL(e.trim()),i="https:"===t.protocol;if(!("http:"===t.protocol)&&!i)return!1;return function(e){if(!e)return!1;const t=e.trim().toLowerCase();if("localhost"===t||"127.0.0.1"===t||"::1"===t||t.endsWith(".local"))return!0;if(!/^(\d{1,3}\.){3}\d{1,3}$/.test(t))return!1;const n=t.split(".").map(e=>Number(e));if(n.some(e=>Number.isNaN(e)||e<0||e>255))return!1;const[s,i]=n;return 10===s||127===s||172===s&&i>=16&&i<=31||192===s&&168===i||169===s&&254===i}(t.hostname)?!n&&s:!n||i}catch{return!1}}function T(e){return function(e){const t="string"==typeof e?e:String(e??"");if("undefined"!=typeof TextEncoder){const e=(new TextEncoder).encode(t);let n="";return e.forEach(e=>{n+=String.fromCharCode(e)}),btoa(n)}return btoa(unescape(encodeURIComponent(t)))}(e).replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/,"")}function v(e={},t={}){const n=[],s=e.paymentType||l.STANDARD,i="production"===(t.environment||"").toLowerCase(),o=!!t.allowLocalCallbacksInNonProd,r="string"==typeof e.currency?e.currency.trim().toUpperCase():"";if(D.includes(s)||n.push(`paymentType must be one of: ${D.join(", ")}`),void 0===e.amount||null===e.amount||""===e.amount?n.push("amount is required"):function(e){const t="number"==typeof e?e:parseFloat(e);return"number"==typeof t&&!Number.isNaN(t)&&t>0}(e.amount)||n.push("amount must be a positive number"),e.currency&&"string"==typeof e.currency&&e.currency.trim()?y.includes(r)||n.push(`currency must be one of: ${y.join(", ")}`):n.push("currency is required"),e.itemName&&"string"==typeof e.itemName&&e.itemName.trim()||n.push("itemName is required"),s===l.STANDARD){if(void 0!==e.displayItems){const t=function(e){if(!Array.isArray(e))return"displayItems must be an array of objects with keys: label, value, type";for(let t=0;t<e.length;t+=1){const n=e[t];if(!n||"object"!=typeof n||Array.isArray(n))return`displayItems[${t}] must be a valid object`;if(Object.keys(n).length!==b.length||!b.every(e=>Object.prototype.hasOwnProperty.call(n,e)))return`displayItems[${t}] must contain exactly these keys: ${b.join(", ")}`;if("string"!=typeof n.label||!n.label.trim())return`displayItems[${t}].label must be a non-empty string`;const s="number"==typeof n.value?n.value:Number(n.value);if(!Number.isFinite(s)||s<0)return`displayItems[${t}].value must be a number greater than or equal to 0`;if(!S.includes(n.type))return`displayItems[${t}].type must be one of: ${S.join(", ")}`}return null}(e.displayItems);t&&n.push(t)}void 0===e.metaData||function(e){if(!e||"object"!=typeof e||Array.isArray(e))return!1;try{return JSON.stringify(e),!0}catch{return!1}}(e.metaData)||n.push("metaData must be a valid JSON object for standard payments")}if(s===l.PREAUTH&&(r&&"USD"!==r&&n.push("currency must be USD for preauth payments"),e.callbackUrl&&String(e.callbackUrl).trim()?w(e.callbackUrl,{isProduction:i,allowLocalCallbacksInNonProd:o})||n.push(i?"callbackUrl must be HTTPS in production and cannot target loopback/private hosts":"callbackUrl must be a valid HTTP/HTTPS URL and cannot target loopback/private hosts unless explicitly enabled"):n.push("callbackUrl (signing URL) is required for preauth payments"),void 0===e.authHoldTimeInSeconds||null===e.authHoldTimeInSeconds||""===e.authHoldTimeInSeconds?n.push("authHoldTimeInSeconds is required for preauth payments"):function(e){const t="number"==typeof e?e:parseInt(e,10);return Number.isInteger(t)&&t>0}(e.authHoldTimeInSeconds)||n.push("authHoldTimeInSeconds must be a positive integer (seconds)")),s===l.RECURRING){e.callbackUrl&&String(e.callbackUrl).trim()?w(e.callbackUrl,{isProduction:i,allowLocalCallbacksInNonProd:o})||n.push(i?"callbackUrl must be HTTPS in production and cannot target loopback/private hosts":"callbackUrl must be a valid HTTP/HTTPS URL and cannot target loopback/private hosts unless explicitly enabled"):n.push("callbackUrl (signing URL) is required for recurring payments");const t=(e.frequency||e.recurringInterval||"").toString().trim().toUpperCase();t?f.includes(t)||n.push(`frequency must be one of: ${f.join(", ")}`):n.push("frequency is required for recurring payments"),e.recurringEndDate&&String(e.recurringEndDate).trim()?!function(e){if("string"!=typeof e||!e.trim())return!1;const t=new Date(e.trim());return!Number.isNaN(t.getTime())}(e.recurringEndDate)?n.push("recurringEndDate must be a valid ISO date-time string"):new Date(e.recurringEndDate.trim())<=new Date&&n.push("recurringEndDate must be in the future"):n.push("recurringEndDate (expiry) is required for recurring payments")}return{valid:0===n.length,errors:n}}function A(e,t,n={}){return{code:e,message:t,details:n,timestamp:(new Date).toISOString(),toString(){return this.message||"Dropp SDK error"}}}class _{constructor(e,t,n=null){this.environment=e,this.onMessage=t,this.sessionId=`dropp-sdk-${Date.now()}-${Math.random().toString(36).substring(2,15)}`,g.log("[MessageHandler] Session initialized"),this.iframe=null,this.allowedOrigins=n?.length>0?n:o[e]||[],this.messageListener=null,this.pendingMessages=new Map}init(e){this.iframe=e,this.messageListener=this._handleMessage.bind(this),window.addEventListener("message",this.messageListener)}destroy(){this.messageListener&&(window.removeEventListener("message",this.messageListener),this.messageListener=null),this.iframe=null,this.pendingMessages.clear()}_handleMessage(e){if(g.log("[MessageHandler] Received postMessage event"),e.source===window)return void g.log("[MessageHandler] Ignoring message from self");if(!this._validateOrigin(e.origin))return void g.warn("[Dropp SDK] Rejected message from untrusted origin:",e.origin);if(!this.iframe||e.source!==this.iframe.contentWindow)return void g.warn("[Dropp SDK] Rejected message from unexpected source",{eventOrigin:e.origin});const t=e.data;this._validateMessageStructure(t)?t.sessionId&&t.sessionId!==this.sessionId?g.warn("[Dropp SDK] Rejected message with invalid session ID"):(g.log("[MessageHandler] Message passed validation"),this._processMessage(t)):g.warn("[Dropp SDK] Rejected message with invalid structure")}_validateOrigin(e){return!(!e.startsWith("http://localhost:")&&!e.startsWith("http://127.0.0.1:"))||this.allowedOrigins.includes(e)}_validateMessageStructure(t){if(!t||"object"!=typeof t)return!1;if(!t.type||"string"!=typeof t.type)return!1;return!!Object.values(e).includes(t.type)&&!(t.type!==e.ACCOUNT_STATUS&&!t.timestamp)}_processMessage(e){g.log("[Dropp SDK] Received message:",e.type),this.onMessage&&this.onMessage(e)}sendToPaymentApp(e,t={}){if(!this.iframe||!this.iframe.contentWindow)return g.error("[Dropp SDK] Cannot send message: iframe not ready"),!1;const n={type:e,data:t,sessionId:this.sessionId,timestamp:(new Date).toISOString(),sdkVersion:"1.0.0"},s=this.allowedOrigins[0]||"*";try{return this.iframe.contentWindow.postMessage(n,s),g.log("[Dropp SDK] Sent message:",e),!0}catch(e){return g.error("[Dropp SDK] Error sending message:",e),!1}}sendInit(e){return this.sendToPaymentApp(t,{mode:"sdk",config:e})}sendClose(){return this.sendToPaymentApp(n,{})}getSessionId(){return this.sessionId}}class E{constructor(e){this.onClose=e,this.overlay=null,this.container=null,this.iframe=null,this.closeButton=null,this.isOpen=!1,this.escapeListener=null}open(){return this.isOpen?(g.warn("[Dropp SDK] Modal is already open"),null):(this._createModal(),this._attachEventListeners(),this._show(),this.isOpen=!0,this.iframe)}close(){g.log("[ModalManager] ๐Ÿšฆ close() called, isOpen:",this.isOpen),this.isOpen?(g.log("[ModalManager] ๐Ÿšฆ Hiding modal..."),this._hide(),g.log("[ModalManager] ๐Ÿšฆ Removing event listeners..."),this._removeEventListeners(),g.log("[ModalManager] ๐Ÿšฆ Destroying modal..."),this._destroyModal(),this.isOpen=!1,g.log("[ModalManager] โœ… Modal close sequence initiated")):g.log("[ModalManager] โš ๏ธ Modal is not open, skipping close")}_createModal(){this.overlay=document.createElement("div"),this.overlay.id="dropp-payment-overlay",this._applyOverlayStyles(this.overlay),this.container=document.createElement("div"),this.container.id="dropp-payment-container",this._applyContainerStyles(this.container),this.iframe=document.createElement("iframe"),this.iframe.id="dropp-payment-iframe",this.iframe.setAttribute("allow","payment"),this.iframe.setAttribute("sandbox","allow-scripts allow-same-origin allow-forms allow-popups"),this.iframe.setAttribute("referrerpolicy","no-referrer"),this.iframe.setAttribute("title","Dropp Payment"),this._applyIframeStyles(this.iframe),this.container.appendChild(this.iframe),this.overlay.appendChild(this.container),document.body.appendChild(this.overlay)}_applyOverlayStyles(e){Object.assign(e.style,{position:"fixed",inset:"0",width:"100vw",height:"100vh",margin:"0",padding:"20px",boxSizing:"border-box",backgroundColor:"rgba(0, 0, 0, 0.6)",zIndex:999999..toString(),display:"flex",alignItems:"center",justifyContent:"center",opacity:"0",transition:"opacity 0.3s ease",backdropFilter:"blur(4px)",overflow:"auto"})}_applyContainerStyles(e){const t=window.innerWidth<768;this._isMobile=t,Object.assign(e.style,{position:"relative",flex:"0 0 auto",alignSelf:"center",margin:"auto",width:"100%",maxWidth:t?"100%":"400px",height:t?"100%":"auto",minHeight:t?"100%":"800px",maxHeight:t?"100%":"min(1000px, calc(100vh - 40px))",backgroundColor:"#ffffff",borderRadius:t?"0":"16px",boxShadow:"0 20px 60px rgba(0, 0, 0, 0.3)",overflow:"hidden",transform:"scale(0.95)",transition:"transform 0.3s ease, opacity 0.3s ease",display:"flex",flexDirection:"column"})}_getContainerTransform(e){return`scale(${e})`}_applyCloseButtonStyles(e){Object.assign(e.style,{position:"absolute",top:"16px",right:"16px",zIndex:"10",width:"40px",height:"40px",border:"none",borderRadius:"50%",backgroundColor:"rgba(255, 255, 255, 0.9)",color:"#333",fontSize:"28px",lineHeight:"1",cursor:"pointer",display:"flex",alignItems:"center",justifyContent:"center",boxShadow:"0 2px 8px rgba(0, 0, 0, 0.15)",transition:"all 0.2s ease",fontFamily:"Arial, sans-serif",padding:"0"}),e.addEventListener("mouseenter",()=>{Object.assign(e.style,{backgroundColor:"#f5f5f5",transform:"scale(1.1)"})}),e.addEventListener("mouseleave",()=>{Object.assign(e.style,{backgroundColor:"rgba(255, 255, 255, 0.9)",transform:"scale(1)"})})}_applyIframeStyles(e){const t=this._isMobile;Object.assign(e.style,{width:"100%",flex:"1 1 auto",minHeight:t?"100%":"min(520px, calc(90vh - 40px))",height:t?"100%":"min(520px, calc(90vh - 40px))",border:"none",display:"block"})}_show(){document.body.style.overflow="hidden",requestAnimationFrame(()=>{this.overlay&&(this.overlay.style.opacity="1"),this.container&&(this.container.style.transform=this._getContainerTransform(1))})}_hide(){g.log("[ModalManager] ๐ŸŽญ _hide() called"),this.overlay&&(this.overlay.style.opacity="0",g.log("[ModalManager] ๐ŸŽญ Overlay opacity set to 0")),this.container&&(this.container.style.transform=this._getContainerTransform(.95),g.log("[ModalManager] ๐ŸŽญ Container transform set to scale(0.95)")),document.body.style.overflow="",g.log("[ModalManager] ๐ŸŽญ Body scroll restored")}_destroyModal(){g.log("[ModalManager] ๐Ÿ—‘๏ธ _destroyModal() called, waiting 300ms for animation"),setTimeout(()=>{g.log("[ModalManager] ๐Ÿ—‘๏ธ Animation complete, removing from DOM"),this.overlay&&this.overlay.parentNode&&(this.overlay.parentNode.removeChild(this.overlay),g.log("[ModalManager] ๐Ÿ—‘๏ธ Overlay removed from DOM")),this.overlay=null,this.container=null,this.iframe=null,this.closeButton=null,g.log("[ModalManager] โœ… Modal destroyed")},300)}_attachEventListeners(){}_removeEventListeners(){this.escapeListener&&(document.removeEventListener("keydown",this.escapeListener),this.escapeListener=null)}_handleCloseClick(e){e.stopPropagation(),this.onClose&&this.onClose("user_closed")}_handleOverlayClick(e){e.target===this.overlay&&this.onClose&&this.onClose("user_closed")}_handleEscapeKey(e){"Escape"===e.key&&this.isOpen&&this.onClose&&this.onClose("user_closed")}getIframe(){return this.iframe}getIsOpen(){return this.isOpen}}const I=["fundnow","linkbank","linkcard","redeemnow","fundcrypto","transferusdc","usdchistory","redeemcrypto","manageaccounts","transactions","merchantlist","favorites","offers","profile","pinchange","unlinkaccount","accountsettings","aboutus"];class C{constructor(e={}){if("undefined"==typeof window||"undefined"==typeof document)throw new Error("Dropp Payment SDK can only be used in a browser environment");if(!e.merchantId||!e.apiKey)throw new Error("merchantId and apiKey are required to initialize SDK");if(this.merchantId=e.merchantId,this.apiKey=e.apiKey,this.environment=e.environment||s.QA,this.baseUrl=e.paymentAppUrl||e.baseUrl||i[this.environment],this.isInitialized=!1,this.initializationPromise=null,this.getServerAuthToken="function"==typeof e.getServerAuthToken?e.getServerAuthToken:null,this.requireServerAuthToken="boolean"==typeof e.requireServerAuthToken?e.requireServerAuthToken:this.environment===s.PRODUCTION,this.allowLocalCallbacksInNonProd=!!e.allowLocalCallbacksInNonProd,e.allowedOrigins?.length)this.allowedOrigins=e.allowedOrigins;else try{this.allowedOrigins=[new URL(this.baseUrl).origin]}catch{this.allowedOrigins=o[this.environment]||[]}this.isOpen=!1,this.currentSession=null,this.currentFlowType=null,this.statusSession=null,this.hiddenIframe=null,this.statusTimeout=null,this.initTimeout=null,this.paymentTimeout=null,this.hostAppDomain=null,this.onAccountChanged=null,this.onPageFailed=null,this.messageHandler=null,this.modalManager=null,g.setEnvironment(this.environment,{enabled:!!e.debugLogging}),g.log(`[Dropp SDK] Initialized v${m} - Environment: ${this.environment}`)}pay(e={}){return new Promise((t,n)=>{if(this.isOpen||this.currentFlowType){const e=A(p,"A payment session is already in progress",{currentSession:this.currentSession,currentFlowType:this.currentFlowType});return void n(e)}(async()=>{try{await this.initialize();const s=v(e,{environment:this.environment,allowLocalCallbacksInNonProd:this.allowLocalCallbacksInNonProd});if(!s.valid){const e=A(d,"Invalid payment configuration",{errors:s.errors});return void n(e)}this.onSuccess=e.onSuccess||null,this.onFailure=e.onFailure||null,this.onCancel=e.onCancel||null,this.onClose=e.onClose||null,this.currentSession={resolve:t,reject:n,options:e,startTime:Date.now()},await this._initializePayment(e)}catch(e){const t=e&&e.code===d?e:A(h,"Failed to initialize payment",{originalError:e?.message||String(e)});n(t),this._cleanup()}})()})}async initialize(){return this.initializationPromise||(this.initializationPromise=(async()=>{if(this.hostAppDomain=this._detectHostAppDomain(),!this.hostAppDomain)throw A(d,"Unable to detect host app domain for SDK validation");return await this._validateInitializationCredentials(),this.isInitialized=!0,{status:"success",code:"INITIALIZED",message:"Dropp SDK initialized successfully"}})().catch(e=>{throw this.isInitialized=!1,this.initializationPromise=null,e})),this.initializationPromise}_getValidationUrl(){const e={[s.QA]:"https://bc0qfqi8c9.execute-api.us-east-1.amazonaws.com/QA-test-app-sdk-gateway-stage/sdk/payer/webview/validateSdk",[s.SANDBOX]:"https://76xbxsi4kf.execute-api.us-east-1.amazonaws.com/sandbox/sdk/payer/webview/validateSdk",[s.PRODUCTION]:"https://api.dropp.cc/payer/webview/validateSdk"};return e[this.environment]||e[s.QA]}_detectHostAppDomain(){if("undefined"!=typeof window&&window.location)return window.location.hostname||void 0}_getHostedPageAuthToken(){const e="string"==typeof this.apiKey?this.apiKey.trim():"",t="string"==typeof this.merchantId?this.merchantId.trim():"";if(!e)return"";try{return T(JSON.stringify({apikey:e,merchantAccount:t}))}catch{return""}}_formatValidationFailureMessage(e,t,n){const s=[e];Number.isFinite(t)&&s.push(`httpStatus=${t}`);const i=n?.responseCode,o=Number(i);Number.isNaN(o)||s.push(`responseCode=${o}`);let r="";if(Array.isArray(n?.errors)&&n.errors.length>0){const e=n.errors[0];r="string"==typeof e?e:e&&"string"==typeof e.message?e.message:JSON.stringify(e)}else"string"==typeof n?.message&&n.message.trim()&&(r=n.message.trim());return r&&s.push(`reason=${r}`),s.join(" | ")}async _validateInitializationCredentials(){const e=this._getValidationUrl(),t=this.hostAppDomain;if(!t)throw A(d,"Unable to resolve host app domain for SDK validation");const n={accept:"application/json, text/plain, */*","content-type":"application/json","x-api-key":this.apiKey,"x-app-package":t,"dropp-user-agent":"WEB"};let s;n["dropp-merchant-domain"]=t;let i=null;try{s=await fetch(e,{method:"POST",headers:n,body:JSON.stringify({id:this.merchantId})})}catch(t){throw A(d,"Unable to validate merchant credentials",{endpoint:e,originalError:t?.message||String(t)})}try{i=await s.json()}catch{i=null}if(!s.ok){const t=this._formatValidationFailureMessage("Merchant validation failed for merchantId/apiKey/domain",s.status,i);throw A(d,t,{endpoint:e,status:s.status,response:i})}const o=Number(i?.responseCode),r=i?.errors,a=!Array.isArray(r)||0===r.length;if(!(i&&0===o&&a)){const t=this._formatValidationFailureMessage("Merchant validation returned an unsuccessful response",s.status,i);throw A(d,t,{endpoint:e,status:s.status,response:i})}g.log("[Dropp SDK] Merchant credentials validated successfully")}closePayment(){if(this.isOpen){if("page"===this.currentFlowType)return void this._cleanup();this._handleCancel("sdk_close")}}dashboard(e={}){return this._openHostedPage("/firsttimeflow",e)}open(e,t={}){const n="string"==typeof e?e.trim():"";if(!n)throw A(d,"moduleId is required for Dropp.open(moduleId)",{moduleId:e});if(!I.includes(n))throw A(d,"Unsupported moduleId passed to Dropp.open(moduleId)",{moduleId:n,supportedModuleIds:[...I]});const s="about"===n?"/aboutus":`/${n}`;return this._openHostedPage(s,t)}unlinkAccount(e={}){return this._openHostedPage("/unlink",e)}transactions(e={}){return this._openHostedPage("/transactions",e)}offers(e={}){return this._openHostedPage("/offers",e)}status(){return new Promise((e,t)=>{this.isOpen||this.currentFlowType?t(A(p,"A Dropp session is already in progress",{currentSession:this.currentSession,currentFlowType:this.currentFlowType})):(async()=>{try{this.currentFlowType="status",await this.initialize(),this.currentSession=null,this.onSuccess=null,this.onFailure=null,this.onCancel=null,this.onClose=null,this.messageHandler=new _(this.environment,this._handleMessage.bind(this),this.allowedOrigins);const n=document.createElement("iframe");n.id="dropp-status-iframe",n.setAttribute("allow","payment"),n.setAttribute("sandbox","allow-scripts allow-same-origin allow-forms allow-popups"),n.setAttribute("referrerpolicy","no-referrer"),n.setAttribute("title","Dropp Account Status"),Object.assign(n.style,{position:"absolute",width:"1px",height:"1px",border:"0",opacity:"0",pointerEvents:"none",visibility:"hidden"}),document.body.appendChild(n),this.hiddenIframe=n,this.messageHandler.init(n);const s=new URLSearchParams({sdkMode:"true",sessionId:this.messageHandler.getSessionId(),sdkVersion:m,platform:"web",merchantDomain:this.hostAppDomain}),i=this._getHostedPageAuthToken();i&&s.set("auth",i);const o=this.baseUrl.replace(/\/+$/,"");n.src=`${o}/#/status?${s.toString()}`,this.statusSession={resolve:e,reject:t,startTime:Date.now()},this.statusTimeout=setTimeout(()=>{const e=A(c,"Account status request timed out",{type:"status",sessionDuration:this.statusSession?Date.now()-this.statusSession.startTime:0});this.statusSession&&this.statusSession.reject(e),this._cleanup()},r)}catch(e){t(A(h,"Failed to get account status",{originalError:e?.message||String(e)})),this._cleanup()}})()})}async _initializePayment(e){g.log("[Dropp SDK] Initializing payment session"),this.currentFlowType="payment",this.messageHandler=new _(this.environment,this._handleMessage.bind(this),this.allowedOrigins),this.modalManager=new E(this._handleModalClose.bind(this));const t=this.modalManager.open();if(!t)throw new Error("Failed to create modal iframe");this.messageHandler.init(t);const n=e.paymentType||l.STANDARD;let s="/payViaUrl";n===l.PREAUTH?s="/preAuthPayment":n===l.RECURRING&&(s="/recurringPayment");const i=n===l.RECURRING?(e.frequency||e.recurringInterval||"").toString().trim().toUpperCase():"",o=n===l.RECURRING&&"NONE"===i,c=e.appUrl||("undefined"!=typeof window&&window.location?window.location.href:void 0),u={merchantAccount:this.merchantId,amount:n===l.PREAUTH?void 0:e.amount,maxAmount:n===l.PREAUTH||o?e.amount:void 0,fixAmount:n!==l.RECURRING||o?void 0:e.amount,acceptPaymentDelay:n===l.STANDARD&&!0===e.acceptPaymentDelay||void 0,currency:e.currency,itemName:e.itemName,description:e.description,invoiceId:e.invoiceId||`INV-${Date.now()}`,invoiceType:n===l.PREAUTH?"PREAUTH":void 0,apiKey:this.apiKey,packageName:this.hostAppDomain,merchantDomain:this.hostAppDomain,sdkVersion:m,platform:"web",appUrl:c,sessionId:this.messageHandler.getSessionId()};if(n===l.STANDARD&&(void 0!==e.displayItems&&(u.displayItems=e.displayItems),void 0!==e.metaData&&(u.metaData=e.metaData)),g.log("[Dropp SDK] Including SDK session context in payment params"),e.successURL&&(u.successURL=e.successURL),e.failureURL&&(u.failureURL=e.failureURL),n===l.RECURRING){const t=e.frequency||e.recurringInterval;t&&(u.frequency=t),e.recurringEndDate&&(u.expiry=e.recurringEndDate)}n===l.PREAUTH&&e.authHoldTimeInSeconds&&(u.authHoldTimeInSeconds=e.authHoldTimeInSeconds),e.callbackUrl&&(u.url=e.callbackUrl,u.submitToCallBack="post");const p={sessionId:u.sessionId,merchantAccount:this.merchantId,amount:e.amount,paymentType:n,invoiceId:u.invoiceId,expiry:e.recurringEndDate||null,issuedAt:Date.now()};let h="string"==typeof e.serverAuthToken?e.serverAuthToken.trim():"";if(!h&&this.getServerAuthToken)try{const e=await this.getServerAuthToken(p);h="string"==typeof e?e.trim():""}catch(e){throw A(d,"Failed to obtain server auth token",{originalError:e?.message||String(e)})}if(this.requireServerAuthToken&&!h)throw A(d,"serverAuthToken is required. Provide options.serverAuthToken or config.getServerAuthToken.",{environment:this.environment,paymentType:n,invoiceId:u.invoiceId});const y=await async function(e,t,n,s,i={}){try{const{serverAuthToken:o=""}=i,r={};Object.keys(n).forEach(e=>{const t=n[e];null!=t&&(r[e]=t)}),g.log("[buildPaymentUrl] Building payment URL payload");const a=T(JSON.stringify(r)),l=Date.now().toString(),c=new URLSearchParams({pay:a,sdkTs:l,sdkVersion:s,platform:"web"});return o&&c.set("authToken",o),`${e}/#${t}?${c.toString()}`}catch(e){throw g.error("Error building payment URL:",e),e}}(this.baseUrl,s,u,m,{serverAuthToken:h});g.log("[Dropp SDK] Loading payment flow iframe"),t.src=y,this.isOpen=!0,this.initTimeout=setTimeout(()=>{this._handleTimeout("init")},r),this.paymentTimeout=setTimeout(()=>{this._handleTimeout("payment")},a)}async _openHostedPage(e,t={}){if("status"===this.currentFlowType)throw A(p,"A Dropp session is already in progress",{currentSession:this.currentSession,currentFlowType:this.currentFlowType,route:e});if(this.isOpen){if("page"!==this.currentFlowType)throw A(p,"A Dropp session is already in progress",{currentSession:this.currentSession,route:e});this._cleanup()}await this.initialize(),this.currentFlowType="page",this.currentSession=null,this.onSuccess=null,this.onFailure="function"==typeof t.onFailed?t.onFailed:null,this.onCancel="function"==typeof t.onCancel?t.onCancel:null,this.onClose="function"==typeof t.onClose?t.onClose:null,this.onAccountChanged="function"==typeof t.onAccountChanged?t.onAccountChanged:null,this.onPageFailed="function"==typeof t.onPageFailed?t.onPageFailed:this.onFailure,this.messageHandler=new _(this.environment,this._handleMessage.bind(this),this.allowedOrigins),this.modalManager=new E(this._handleModalClose.bind(this));const n=this.modalManager.open();if(!n)throw new Error("Failed to create modal iframe");this.messageHandler.init(n);const s=new URLSearchParams({sdkMode:"true",sessionId:this.messageHandler.getSessionId(),sdkVersion:m,platform:"web",merchantDomain:this.hostAppDomain}),i=this._getHostedPageAuthToken();if(i&&s.set("auth",i),"/firsttimeflow"===e&&Array.isArray(t.modules)){const e=t.modules.map(e=>"string"==typeof e?e.trim():"").filter(Boolean);e.length>0&&s.set("modules",e.join(","))}const o=this.baseUrl.replace(/\/+$/,"");return n.src=`${o}/#${e}?${s.toString()}`,this.isOpen=!0,g.log("[Dropp SDK] Hosted page opened",{route:e}),{status:"opened",route:e}}_handleMessage(t){const{type:n,data:s}=t;switch(n){case e.READY:this._handleReady(s);break;case e.PAGE_LOADED:case e.PAYMENT_PAGE_LOADED:this._handlePageLoaded(s);break;case e.PAYMENT_SUCCESS:this._handleSuccess(s);break;case e.PAYMENT_FAILED:this._handleFailure(s);break;case e.PAYMENT_CANCELLED:this._handleCancel("user_cancelled");break;case e.ACCOUNT_STATUS:this._handleAccountStatus(s);break;case e.ACCOUNT_EVENT:this._handleAccountEvent(s);break;case e.PAGE_EVENT:this._handlePageEvent(s);break;case e.AUTO_CLOSE_WEBVIEW:g.log("[Dropp SDK] AUTO_CLOSE_WEBVIEW received - closing modal"),this._cleanup(),g.log("[Dropp SDK] Cleanup completed");break;case e.CLOSE_WEBVIEW:if(g.log("[Dropp SDK] CLOSE_WEBVIEW received from app - closing modal"),"status"===this.currentFlowType&&this.statusSession){const e=A(h,"Account status request was closed before response was received",{type:"status",sessionDuration:Date.now()-this.statusSession.startTime});this.statusSession.reject(e),this._cleanup();break}this._handleCancel("app_close");break;default:g.warn("[Dropp SDK] Unknown message type:",n)}}_handleReady(e){if(g.log("[Dropp SDK] Payment app ready"),this.initTimeout&&(clearTimeout(this.initTimeout),this.initTimeout=null),this.messageHandler&&this.currentSession){const e={};Object.keys(this.currentSession.options).forEach(t=>{const n=this.currentSession.options[t];"function"!=typeof n&&(e[t]=n)}),g.log("[Dropp SDK] Sending init config to payment app"),this.messageHandler.sendInit(e)}}_handlePageLoaded(e){g.log("[Dropp SDK] Payment page loaded")}_getBridgeAction(e={}){return e&&"object"==typeof e?"string"==typeof e.action?e.action:e.result&&"object"==typeof e.result&&"string"==typeof e.result.action?e.result.action:"":""}_getSafeFailureMessage(e={},t="Request failed"){return e&&"string"==typeof e.message&&e.message.trim()?e.message.trim():e&&e.result&&"object"==typeof e.result&&"string"==typeof e.result.message&&e.result.message.trim()?e.result.message.trim():t}_getSafeResult(e={}){return e&&e.result&&"object"==typeof e.result?e.result:{}}_handleAccountEvent(e){const t=this._getBridgeAction(e),n=this._getSafeResult(e);if("onAccountChanged"!==t)if("onCancel"!==t){if("onFailed"===t){const t=this._getSafeFailureMessage(e,"Account request failed");return void(this.onFailure&&this.onFailure(t))}g.warn("[Dropp SDK] Unknown ACCOUNT_EVENT action:",t)}else this.onCancel&&this.onCancel();else this.onAccountChanged&&this.onAccountChanged({linked:!0===n.linked,rawData:n.rawData})}_handlePageEvent(e){const t=this._getBridgeAction(e);if("onFailed"===t){const t=this._getSafeFailureMessage(e,"Page request failed");return void(this.onPageFailed&&this.onPageFailed(t))}g.warn("[Dropp SDK] Unknown PAGE_EVENT action:",t)}_handleAccountStatus(t){g.log("[Dropp SDK] Account status received"),this.statusSession?(this.statusSession.resolve({type:e.ACCOUNT_STATUS,data:t}),this._cleanup()):g.warn("[Dropp SDK] ACCOUNT_STATUS received without active status session")}_handleSuccess(e){g.log("[Dropp SDK] Payment successful");const t={status:"success",...e,sessionDuration:Date.now()-this.currentSession.startTime};this.currentSession&&this.currentSession.resolve(t),this.onSuccess&&this.onSuccess(t)}_handleFailure(e){g.log("[Dropp SDK] Payment failed");const t={status:"failed",...e,sessionDuration:Date.now()-this.currentSession.startTime};this.currentSession&&this.currentSession.resolve(t),this.onFailure&&this.onFailure(t)}_handleCancel(e){if(g.log("[Dropp SDK] Payment cancelled"),"page"===this.currentFlowType)return this.onCancel&&this.onCancel(),void this._cleanup();const t={status:"cancelled",reason:e,sessionDuration:this.currentSession?Date.now()-this.currentSession.startTime:0};this.currentSession&&this.currentSession.resolve(t),this.onCancel&&this.onCancel(t),this._cleanup()}_handleModalClose(e){g.log("[Dropp SDK] Modal closed by user"),"page"!==this.currentFlowType?this._handleCancel(e):this._cleanup()}_handleTimeout(e){g.error("[Dropp SDK] Timeout:",e);const t=A("init"===e?c:u,"init"===e?"Payment app failed to load within timeout period":"Payment session timed out",{type:e,sessionDuration:this.currentSession?Date.now()-this.currentSession.startTime:0});this.currentSession&&this.currentSession.reject(t),this.onFailure&&this.onFailure({status:"failed",error:t}),this._cleanup()}_cleanup(){g.log("[Dropp SDK] ๐Ÿงน Cleaning up session - starting cleanup"),this.initTimeout&&(clearTimeout(this.initTimeout),this.initTimeout=null),this.paymentTimeout&&(clearTimeout(this.paymentTimeout),this.paymentTimeout=null),this.statusTimeout&&(clearTimeout(this.statusTimeout),this.statusTimeout=null),this.messageHandler&&(g.log("[Dropp SDK] ๐Ÿงน Destroying message handler"),this.messageHandler.destroy(),this.messageHandler=null),this.modalManager&&(g.log("[Dropp SDK] ๐Ÿงน Closing modal manager"),this.modalManager.close(),g.log("[Dropp SDK] ๐Ÿงน Modal manager closed"),this.modalManager=null),this.hiddenIframe&&this.hiddenIframe.parentNode&&this.hiddenIframe.parentNode.removeChild(this.hiddenIframe),this.hiddenIframe=null,this.currentSession=null,this.statusSession=null,this.currentFlowType=null,this.isOpen=!1,this.onClose&&this.onClose()}static getVersion(){return m}static getEnvironments(){return{...s}}static getPaymentTypes(){return{...l}}}const N="__droppDisableContextMenuListener__";!function(){if("undefined"==typeof window||"undefined"==typeof document)return;if(window[N])return;document.addEventListener("contextmenu",e=>{e.preventDefault(),e.stopPropagation()},!0),window[N]=!0}();let O=null;async function P(e){O=new C(e);return{...await O.initialize(),sdk:O}}function k(){return O}const M={init:e=>P(e),pay(e){if(!O){const e=new Error("Dropp SDK is not initialized. Call Dropp.init(config) before Dropp.pay(options).");throw e.code="SDK_NOT_INITIALIZED",e}return O.pay(e)},dashboard(e){if(!O){const e=new Error("Dropp SDK is not initialized. Call Dropp.init(config) before Dropp.dashboard(options).");throw e.code="SDK_NOT_INITIALIZED",e}return O.dashboard(e)},open(e,t){if(!O){const e=new Error("Dropp SDK is not initialized. Call Dropp.init(config) before Dropp.open(moduleId, options).");throw e.code="SDK_NOT_INITIALIZED",e}return O.open(e,t)},unlinkAccount(e){if(!O){const e=new Error("Dropp SDK is not initialized. Call Dropp.init(config) before Dropp.unlinkAccount(options).");throw e.code="SDK_NOT_INITIALIZED",e}return O.unlinkAccount(e)},transactions(e){if(!O){const e=new Error("Dropp SDK is not initialized. Call Dropp.init(config) before Dropp.transactions(options).");throw e.code="SDK_NOT_INITIALIZED",e}return O.transactions(e)},offers(e){if(!O){const e=new Error("Dropp SDK is not initialized. Call Dropp.init(config) before Dropp.offers(options).");throw e.code="SDK_NOT_INITIALIZED",e}return O.offers(e)},status(){if(!O){const e=new Error("Dropp SDK is not initialized. Call Dropp.init(config) before Dropp.status().");throw e.code="SDK_NOT_INITIALIZED",e}return O.status()},getInstance:()=>O};var U={DroppPaymentSDK:C,createPaymentSDK:P,Dropp:M,ENVIRONMENTS:s,PAYMENT_TYPES:l};export{M as Dropp,C as DroppPaymentSDK,s as ENVIRONMENTS,l as PAYMENT_TYPES,P as createPaymentSDK,U as default,k as getDroppInstance};
1
+ const e={READY:"DROPP_SDK_READY",PAYMENT_SUCCESS:"PAYMENT_SUCCESS",PAYMENT_FAILED:"PAYMENT_FAILED",PAYMENT_CANCELLED:"PAYMENT_CANCELLED",ACCOUNT_EVENT:"ACCOUNT_EVENT",PAGE_EVENT:"PAGE_EVENT",ACCOUNT_STATUS:"ACCOUNT_STATUS",CLOSE_WEBVIEW:"CLOSE_WEBVIEW",AUTO_CLOSE_WEBVIEW:"AUTO_CLOSE_WEBVIEW",PAYMENT_PAGE_LOADED:"PAYMENT_PAGE_LOADED",PAGE_LOADED:"PAGE_LOADED",UUID_LOOKUP_RESULT:"UUID_LOOKUP_RESULT"},t="DROPP_SDK_INIT",s="DROPP_SDK_CLOSE",n={PRODUCTION:"production",QA:"qa",SANDBOX:"sandbox"},i={[n.PRODUCTION]:"https://wv.dropp.cc",[n.QA]:"https://wv.qa.dropp.cc",[n.SANDBOX]:"https://wv.sandbox.dropp.cc"},o={[n.PRODUCTION]:["https://wv.dropp.cc"],[n.QA]:["https://wv.qa.dropp.cc"],[n.SANDBOX]:["https://wv.sandbox.dropp.cc"]},r=3e4,a=6e5,l=18e4,u=3e4,c={STANDARD:"standard",PREAUTH:"preauth",RECURRING:"recurring"},d="INIT_TIMEOUT",p="PAYMENT_TIMEOUT",h="INVALID_CONFIG",m="ALREADY_OPEN",g="UNKNOWN_ERROR",y="1.0.2";const f=new class{constructor(e,t={}){this.environment=e,this.forceEnabled=!!t.enabled,this.enabled=this._shouldEnableLogging()}_shouldEnableLogging(){return this.forceEnabled}setEnvironment(e,t={}){this.environment=e,"boolean"==typeof t.enabled&&(this.forceEnabled=t.enabled),this.enabled=this._shouldEnableLogging()}setEnabled(e){this.forceEnabled=!!e,this.enabled=this._shouldEnableLogging()}log(...e){this.enabled&&console.log(...e)}warn(...e){this.enabled&&console.warn(...e)}error(...e){this.enabled&&console.error(...e)}debug(...e){this.enabled&&console.debug(...e)}info(...e){this.enabled&&console.info(...e)}}("qa",{enabled:!1}),D=["USD","HBAR","USDC"],S=["NONE","HALF_HOURLY","HOURLY","DAILY","WEEKLY","MONTHLY","YEARLY"],w=[c.STANDARD,c.PREAUTH,c.RECURRING],b=["item","subTotal","otherCharges","discount"],_=["label","value","type"];function T(e,t={}){const{isProduction:s=!1,allowLocalCallbacksInNonProd:n=!1}=t;if("string"!=typeof e||!e.trim())return!1;try{const t=new URL(e.trim()),i="https:"===t.protocol;if(!("http:"===t.protocol)&&!i)return!1;return function(e){if(!e)return!1;const t=e.trim().toLowerCase();if("localhost"===t||"127.0.0.1"===t||"::1"===t||t.endsWith(".local"))return!0;if(!/^(\d{1,3}\.){3}\d{1,3}$/.test(t))return!1;const s=t.split(".").map(e=>Number(e));if(s.some(e=>Number.isNaN(e)||e<0||e>255))return!1;const[n,i]=s;return 10===n||127===n||172===n&&i>=16&&i<=31||192===n&&168===i||169===n&&254===i}(t.hostname)?!s&&n:!s||i}catch{return!1}}function v(e){return function(e){const t="string"==typeof e?e:String(e??"");if("undefined"!=typeof TextEncoder){const e=(new TextEncoder).encode(t);let s="";return e.forEach(e=>{s+=String.fromCharCode(e)}),btoa(s)}return btoa(unescape(encodeURIComponent(t)))}(e).replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/,"")}function I(e={},t={}){const s=[],n=e.paymentType||c.STANDARD,i="production"===(t.environment||"").toLowerCase(),o=!!t.allowLocalCallbacksInNonProd,r="string"==typeof e.currency?e.currency.trim().toUpperCase():"";if(w.includes(n)||s.push(`paymentType must be one of: ${w.join(", ")}`),void 0===e.amount||null===e.amount||""===e.amount?s.push("amount is required"):function(e){const t="number"==typeof e?e:parseFloat(e);return"number"==typeof t&&!Number.isNaN(t)&&t>0}(e.amount)||s.push("amount must be a positive number"),e.currency&&"string"==typeof e.currency&&e.currency.trim()?D.includes(r)||s.push(`currency must be one of: ${D.join(", ")}`):s.push("currency is required"),e.itemName&&"string"==typeof e.itemName&&e.itemName.trim()||s.push("itemName is required"),n===c.STANDARD){if(void 0!==e.displayItems){const t=function(e){if(!Array.isArray(e))return"displayItems must be an array of objects with keys: label, value, type";for(let t=0;t<e.length;t+=1){const s=e[t];if(!s||"object"!=typeof s||Array.isArray(s))return`displayItems[${t}] must be a valid object`;if(Object.keys(s).length!==_.length||!_.every(e=>Object.prototype.hasOwnProperty.call(s,e)))return`displayItems[${t}] must contain exactly these keys: ${_.join(", ")}`;if("string"!=typeof s.label||!s.label.trim())return`displayItems[${t}].label must be a non-empty string`;const n="string"==typeof s.value?s.value.replace(/[$,\s]/g,""):s.value,i="number"==typeof n?n:Number(n);if(!Number.isFinite(i)||i<0)return`displayItems[${t}].value must be a number greater than or equal to 0`;if(!b.includes(s.type))return`displayItems[${t}].type must be one of: ${b.join(", ")}`}return null}(e.displayItems);t&&s.push(t)}void 0===e.metaData||function(e){if(!e||"object"!=typeof e||Array.isArray(e))return!1;try{return JSON.stringify(e),!0}catch{return!1}}(e.metaData)||s.push("metaData must be a valid JSON object for standard payments")}if(n===c.PREAUTH&&(r&&"USD"!==r&&s.push("currency must be USD for preauth payments"),e.callbackUrl&&String(e.callbackUrl).trim()?T(e.callbackUrl,{isProduction:i,allowLocalCallbacksInNonProd:o})||s.push(i?"callbackUrl must be HTTPS in production and cannot target loopback/private hosts":"callbackUrl must be a valid HTTP/HTTPS URL and cannot target loopback/private hosts unless explicitly enabled"):s.push("callbackUrl (signing URL) is required for preauth payments"),void 0===e.authHoldTimeInSeconds||null===e.authHoldTimeInSeconds||""===e.authHoldTimeInSeconds?s.push("authHoldTimeInSeconds is required for preauth payments"):function(e){const t="number"==typeof e?e:parseInt(e,10);return Number.isInteger(t)&&t>0}(e.authHoldTimeInSeconds)||s.push("authHoldTimeInSeconds must be a positive integer (seconds)")),n===c.RECURRING){e.callbackUrl&&String(e.callbackUrl).trim()?T(e.callbackUrl,{isProduction:i,allowLocalCallbacksInNonProd:o})||s.push(i?"callbackUrl must be HTTPS in production and cannot target loopback/private hosts":"callbackUrl must be a valid HTTP/HTTPS URL and cannot target loopback/private hosts unless explicitly enabled"):s.push("callbackUrl (signing URL) is required for recurring payments");const t=(e.frequency||e.recurringInterval||"").toString().trim().toUpperCase();t?S.includes(t)||s.push(`frequency must be one of: ${S.join(", ")}`):s.push("frequency is required for recurring payments"),e.recurringEndDate&&String(e.recurringEndDate).trim()?!function(e){if("string"!=typeof e||!e.trim())return!1;const t=new Date(e.trim());return!Number.isNaN(t.getTime())}(e.recurringEndDate)?s.push("recurringEndDate must be a valid ISO date-time string"):new Date(e.recurringEndDate.trim())<=new Date&&s.push("recurringEndDate must be in the future"):s.push("recurringEndDate (expiry) is required for recurring payments")}return{valid:0===s.length,errors:s}}function A(e,t,s={}){return{code:e,message:t,details:s,timestamp:(new Date).toISOString(),toString(){return this.message||"Dropp SDK error"}}}class E{constructor(e,t,s=null){this.environment=e,this.onMessage=t,this.sessionId=`dropp-sdk-${Date.now()}-${Math.random().toString(36).substring(2,15)}`,f.log("[MessageHandler] Session initialized"),this.iframe=null,this.allowedOrigins=s?.length>0?s:o[e]||[],this.messageListener=null,this.pendingMessages=new Map}init(e){this.iframe=e,this.messageListener=this._handleMessage.bind(this),window.addEventListener("message",this.messageListener)}destroy(){this.messageListener&&(window.removeEventListener("message",this.messageListener),this.messageListener=null),this.iframe=null,this.pendingMessages.clear()}_handleMessage(e){if(f.log("[MessageHandler] Received postMessage event"),e.source===window)return void f.log("[MessageHandler] Ignoring message from self");if(!this._validateOrigin(e.origin))return void f.warn("[Dropp SDK] Rejected message from untrusted origin:",e.origin);if(!this.iframe||e.source!==this.iframe.contentWindow)return void f.warn("[Dropp SDK] Rejected message from unexpected source",{eventOrigin:e.origin});const t=e.data;this._validateMessageStructure(t)?t.sessionId&&t.sessionId!==this.sessionId?f.warn("[Dropp SDK] Rejected message with invalid session ID"):(f.log("[MessageHandler] Message passed validation"),this._processMessage(t)):f.warn("[Dropp SDK] Rejected message with invalid structure")}_validateOrigin(e){return!(!e.startsWith("http://localhost:")&&!e.startsWith("http://127.0.0.1:"))||this.allowedOrigins.includes(e)}_validateMessageStructure(t){if(!t||"object"!=typeof t)return!1;if(!t.type||"string"!=typeof t.type)return!1;return!!Object.values(e).includes(t.type)&&!(t.type!==e.ACCOUNT_STATUS&&!t.timestamp)}_processMessage(e){f.log("[Dropp SDK] Received message:",e.type),this.onMessage&&this.onMessage(e)}sendToPaymentApp(e,t={}){if(!this.iframe||!this.iframe.contentWindow)return f.error("[Dropp SDK] Cannot send message: iframe not ready"),!1;const s={type:e,data:t,sessionId:this.sessionId,timestamp:(new Date).toISOString(),sdkVersion:"1.0.0"},n=this.allowedOrigins[0]||"*";try{return this.iframe.contentWindow.postMessage(s,n),f.log("[Dropp SDK] Sent message:",e),!0}catch(e){return f.error("[Dropp SDK] Error sending message:",e),!1}}sendInit(e){return this.sendToPaymentApp(t,{mode:"sdk",config:e})}sendClose(){return this.sendToPaymentApp(s,{})}getSessionId(){return this.sessionId}}class C{constructor(e){this.onClose=e,this.overlay=null,this.container=null,this.iframe=null,this.closeButton=null,this.isOpen=!1,this.escapeListener=null}open(){return this.isOpen?(f.warn("[Dropp SDK] Modal is already open"),null):(this._createModal(),this._attachEventListeners(),this._show(),this.isOpen=!0,this.iframe)}close(){f.log("[ModalManager] ๐Ÿšฆ close() called, isOpen:",this.isOpen),this.isOpen?(f.log("[ModalManager] ๐Ÿšฆ Hiding modal..."),this._hide(),f.log("[ModalManager] ๐Ÿšฆ Removing event listeners..."),this._removeEventListeners(),f.log("[ModalManager] ๐Ÿšฆ Destroying modal..."),this._destroyModal(),this.isOpen=!1,f.log("[ModalManager] โœ… Modal close sequence initiated")):f.log("[ModalManager] โš ๏ธ Modal is not open, skipping close")}_createModal(){this.overlay=document.createElement("div"),this.overlay.id="dropp-payment-overlay",this._applyOverlayStyles(this.overlay),this.container=document.createElement("div"),this.container.id="dropp-payment-container",this._applyContainerStyles(this.container),this.iframe=document.createElement("iframe"),this.iframe.id="dropp-payment-iframe",this.iframe.setAttribute("allow","camera *; payment *"),this.iframe.setAttribute("sandbox","allow-scripts allow-same-origin allow-forms allow-popups"),this.iframe.setAttribute("referrerpolicy","no-referrer"),this.iframe.setAttribute("title","Dropp Payment"),this._applyIframeStyles(this.iframe),this.container.appendChild(this.iframe),this.overlay.appendChild(this.container),document.body.appendChild(this.overlay)}_applyOverlayStyles(e){Object.assign(e.style,{position:"fixed",inset:"0",width:"100vw",height:"100vh",margin:"0",padding:"20px",boxSizing:"border-box",backgroundColor:"rgba(0, 0, 0, 0.6)",zIndex:999999..toString(),display:"flex",alignItems:"center",justifyContent:"center",opacity:"0",transition:"opacity 0.3s ease",backdropFilter:"blur(4px)",overflow:"auto"})}_applyContainerStyles(e){const t=window.innerWidth<768;this._isMobile=t,Object.assign(e.style,{position:"relative",flex:"0 0 auto",alignSelf:"center",margin:"auto",width:"100%",maxWidth:t?"100%":"400px",height:t?"100%":"auto",minHeight:t?"100%":"800px",maxHeight:t?"100%":"min(1000px, calc(100vh - 40px))",backgroundColor:"#ffffff",borderRadius:t?"0":"16px",boxShadow:"0 20px 60px rgba(0, 0, 0, 0.3)",overflow:"hidden",transform:"scale(0.95)",transition:"transform 0.3s ease, opacity 0.3s ease",display:"flex",flexDirection:"column"})}_getContainerTransform(e){return`scale(${e})`}_applyCloseButtonStyles(e){Object.assign(e.style,{position:"absolute",top:"16px",right:"16px",zIndex:"10",width:"40px",height:"40px",border:"none",borderRadius:"50%",backgroundColor:"rgba(255, 255, 255, 0.9)",color:"#333",fontSize:"28px",lineHeight:"1",cursor:"pointer",display:"flex",alignItems:"center",justifyContent:"center",boxShadow:"0 2px 8px rgba(0, 0, 0, 0.15)",transition:"all 0.2s ease",fontFamily:"Arial, sans-serif",padding:"0"}),e.addEventListener("mouseenter",()=>{Object.assign(e.style,{backgroundColor:"#f5f5f5",transform:"scale(1.1)"})}),e.addEventListener("mouseleave",()=>{Object.assign(e.style,{backgroundColor:"rgba(255, 255, 255, 0.9)",transform:"scale(1)"})})}_applyIframeStyles(e){const t=this._isMobile;Object.assign(e.style,{width:"100%",flex:"1 1 auto",minHeight:t?"100%":"min(520px, calc(90vh - 40px))",height:t?"100%":"min(520px, calc(90vh - 40px))",border:"none",display:"block"})}_show(){document.body.style.overflow="hidden",requestAnimationFrame(()=>{this.overlay&&(this.overlay.style.opacity="1"),this.container&&(this.container.style.transform=this._getContainerTransform(1))})}_hide(){f.log("[ModalManager] ๐ŸŽญ _hide() called"),this.overlay&&(this.overlay.style.opacity="0",f.log("[ModalManager] ๐ŸŽญ Overlay opacity set to 0")),this.container&&(this.container.style.transform=this._getContainerTransform(.95),f.log("[ModalManager] ๐ŸŽญ Container transform set to scale(0.95)")),document.body.style.overflow="",f.log("[ModalManager] ๐ŸŽญ Body scroll restored")}_destroyModal(){f.log("[ModalManager] ๐Ÿ—‘๏ธ _destroyModal() called, waiting 300ms for animation"),setTimeout(()=>{f.log("[ModalManager] ๐Ÿ—‘๏ธ Animation complete, removing from DOM"),this.overlay&&this.overlay.parentNode&&(this.overlay.parentNode.removeChild(this.overlay),f.log("[ModalManager] ๐Ÿ—‘๏ธ Overlay removed from DOM")),this.overlay=null,this.container=null,this.iframe=null,this.closeButton=null,f.log("[ModalManager] โœ… Modal destroyed")},300)}_attachEventListeners(){}_removeEventListeners(){this.escapeListener&&(document.removeEventListener("keydown",this.escapeListener),this.escapeListener=null)}_handleCloseClick(e){e.stopPropagation(),this.onClose&&this.onClose("user_closed")}_handleOverlayClick(e){e.target===this.overlay&&this.onClose&&this.onClose("user_closed")}_handleEscapeKey(e){"Escape"===e.key&&this.isOpen&&this.onClose&&this.onClose("user_closed")}getIframe(){return this.iframe}getIsOpen(){return this.isOpen}}const U=["fundnow","linkbank","linkcard","redeemnow","fundcrypto","transferusdc","usdchistory","redeemcrypto","manageaccounts","transactions","merchantlist","favorites","offers","profile","pinchange","unlinkaccount","accountsettings","aboutus"];class N{constructor(e={}){if("undefined"==typeof window||"undefined"==typeof document)throw new Error("Dropp Payment SDK can only be used in a browser environment");if(!e.merchantId||!e.apiKey)throw new Error("merchantId and apiKey are required to initialize SDK");if(this.merchantId=e.merchantId,this.apiKey=e.apiKey,this.environment=e.environment||n.QA,this.baseUrl=e.paymentAppUrl||e.baseUrl||i[this.environment],this.isInitialized=!1,this.initializationPromise=null,this.getServerAuthToken="function"==typeof e.getServerAuthToken?e.getServerAuthToken:null,this.requireServerAuthToken="boolean"==typeof e.requireServerAuthToken?e.requireServerAuthToken:this.environment===n.PRODUCTION,this.allowLocalCallbacksInNonProd=!!e.allowLocalCallbacksInNonProd,e.allowedOrigins?.length)this.allowedOrigins=e.allowedOrigins;else try{this.allowedOrigins=[new URL(this.baseUrl).origin]}catch{this.allowedOrigins=o[this.environment]||[]}this.isOpen=!1,this.currentSession=null,this.currentFlowType=null,this.statusSession=null,this.hiddenIframe=null,this.statusTimeout=null,this.initTimeout=null,this.paymentTimeout=null,this.scanTimeout=null,this.hostAppDomain=null,this.onAccountChanged=null,this.onPageFailed=null,this.scanSession=null,this.messageHandler=null,this.modalManager=null,f.setEnvironment(this.environment,{enabled:!!e.debugLogging}),f.log(`[Dropp SDK] Initialized v${y} - Environment: ${this.environment}`)}pay(e={}){return new Promise((t,s)=>{if(this.isOpen||this.currentFlowType){const e=A(m,"A payment session is already in progress",{currentSession:this.currentSession,currentFlowType:this.currentFlowType});return void s(e)}(async()=>{try{await this.initialize();const n=I(e,{environment:this.environment,allowLocalCallbacksInNonProd:this.allowLocalCallbacksInNonProd});if(!n.valid){const e=n.errors.length>0?`Invalid payment configuration: ${n.errors.join(" | ")}`:"Invalid payment configuration",t=A(h,e,{errors:n.errors});return void s(t)}this.onSuccess=e.onSuccess||null,this.onFailure=e.onFailure||null,this.onCancel=e.onCancel||null,this.onClose=e.onClose||null,this.currentSession={resolve:t,reject:s,options:e,startTime:Date.now()},await this._initializePayment(e)}catch(e){const t=e&&e.code===h?e:A(g,"Failed to initialize payment",{originalError:e?.message||String(e)});s(t),this._cleanup()}})()})}async initialize(){return this.initializationPromise||(this.initializationPromise=(async()=>{if(this.hostAppDomain=this._detectHostAppDomain(),!this.hostAppDomain)throw A(h,"Unable to detect host app domain for SDK validation");return await this._validateInitializationCredentials(),this.isInitialized=!0,{status:"success",code:"INITIALIZED",message:"Dropp SDK initialized successfully"}})().catch(e=>{throw this.isInitialized=!1,this.initializationPromise=null,e})),this.initializationPromise}_getValidationUrl(){const e={[n.QA]:"https://bc0qfqi8c9.execute-api.us-east-1.amazonaws.com/QA-test-app-sdk-gateway-stage/sdk/payer/webview/validateSdk",[n.SANDBOX]:"https://76xbxsi4kf.execute-api.us-east-1.amazonaws.com/sandbox/sdk/payer/webview/validateSdk",[n.PRODUCTION]:"https://api.dropp.cc/payer/webview/validateSdk"};return e[this.environment]||e[n.QA]}_detectHostAppDomain(){if("undefined"!=typeof window&&window.location)return window.location.hostname||void 0}_getHostedPageAuthToken(){const e="string"==typeof this.apiKey?this.apiKey.trim():"",t="string"==typeof this.merchantId?this.merchantId.trim():"";if(!e)return"";try{return v(JSON.stringify({apikey:e,merchantAccount:t}))}catch{return""}}_formatValidationFailureMessage(e,t,s){const n=[e];Number.isFinite(t)&&n.push(`httpStatus=${t}`);const i=s?.responseCode,o=Number(i);Number.isNaN(o)||n.push(`responseCode=${o}`);let r="";if(Array.isArray(s?.errors)&&s.errors.length>0){const e=s.errors[0];r="string"==typeof e?e:e&&"string"==typeof e.message?e.message:JSON.stringify(e)}else"string"==typeof s?.message&&s.message.trim()&&(r=s.message.trim());return r&&n.push(`reason=${r}`),n.join(" | ")}async _validateInitializationCredentials(){const e=this._getValidationUrl(),t=this.hostAppDomain;if(!t)throw A(h,"Unable to resolve host app domain for SDK validation");const s={accept:"application/json, text/plain, */*","content-type":"application/json","x-api-key":this.apiKey,"x-app-package":t,"dropp-user-agent":"WEB"};let n;s["dropp-merchant-domain"]=t;let i=null;try{n=await fetch(e,{method:"POST",headers:s,body:JSON.stringify({id:this.merchantId})})}catch(t){throw A(h,"Unable to validate merchant credentials",{endpoint:e,originalError:t?.message||String(t)})}try{i=await n.json()}catch{i=null}if(!n.ok){const t=this._formatValidationFailureMessage("Merchant validation failed for merchantId/apiKey/domain",n.status,i);throw A(h,t,{endpoint:e,status:n.status,response:i})}const o=Number(i?.responseCode),r=i?.errors,a=!Array.isArray(r)||0===r.length;if(!(i&&0===o&&a)){const t=this._formatValidationFailureMessage("Merchant validation returned an unsuccessful response",n.status,i);throw A(h,t,{endpoint:e,status:n.status,response:i})}f.log("[Dropp SDK] Merchant credentials validated successfully")}closePayment(){if(this.isOpen){if("page"===this.currentFlowType)return void this._cleanup();this._handleCancel("sdk_close")}}dashboard(e={}){return this._openHostedPage("/firsttimeflow",e)}open(e,t={}){const s="string"==typeof e?e.trim():"";if(!s)throw A(h,"moduleId is required for Dropp.open(moduleId)",{moduleId:e});if(!U.includes(s))throw A(h,"Unsupported moduleId passed to Dropp.open(moduleId)",{moduleId:s,supportedModuleIds:[...U]});const n="about"===s?"/aboutus":`/${s}`;return this._openHostedPage(n,t)}unlinkAccount(e={}){return this._openHostedPage("/unlink",e)}transactions(e={}){return this._openHostedPage("/transactions",e)}offers(e={}){return this._openHostedPage("/offers",e)}status(){return new Promise((e,t)=>{this.isOpen||this.currentFlowType?t(A(m,"A Dropp session is already in progress",{currentSession:this.currentSession,currentFlowType:this.currentFlowType})):(async()=>{try{this.currentFlowType="status",await this.initialize(),this.currentSession=null,this.onSuccess=null,this.onFailure=null,this.onCancel=null,this.onClose=null,this.messageHandler=new E(this.environment,this._handleMessage.bind(this),this.allowedOrigins);const s=document.createElement("iframe");s.id="dropp-status-iframe",s.setAttribute("allow","payment"),s.setAttribute("sandbox","allow-scripts allow-same-origin allow-forms allow-popups"),s.setAttribute("referrerpolicy","no-referrer"),s.setAttribute("title","Dropp Account Status"),Object.assign(s.style,{position:"absolute",width:"1px",height:"1px",border:"0",opacity:"0",pointerEvents:"none",visibility:"hidden"}),document.body.appendChild(s),this.hiddenIframe=s,this.messageHandler.init(s);const n=new URLSearchParams({sdkMode:"true",sessionId:this.messageHandler.getSessionId(),sdkVersion:y,platform:"web",merchantDomain:this.hostAppDomain}),i=this._getHostedPageAuthToken();i&&n.set("auth",i);const o=this.baseUrl.replace(/\/+$/,"");s.src=`${o}/#/status?${n.toString()}`,this.statusSession={resolve:e,reject:t,startTime:Date.now()},this.statusTimeout=setTimeout(()=>{const e=A(d,"Account status request timed out",{type:"status",sessionDuration:this.statusSession?Date.now()-this.statusSession.startTime:0});this.statusSession&&this.statusSession.reject(e),this._cleanup()},r)}catch(e){t(A(g,"Failed to get account status",{originalError:e?.message||String(e)})),this._cleanup()}})()})}scanPaymentDetails(){return new Promise((e,t)=>{this.isOpen||this.currentFlowType?t(A(m,"A Dropp session is already in progress",{currentSession:this.currentSession,currentFlowType:this.currentFlowType})):(async()=>{try{await this.initialize(),this.currentFlowType="scanner",this.currentSession=null,this.onSuccess=null,this.onFailure=null,this.onCancel=null,this.onClose=null,this.messageHandler=new E(this.environment,this._handleMessage.bind(this),this.allowedOrigins),this.modalManager=new C(this._handleModalClose.bind(this));const t=this.modalManager.open();if(!t)throw new Error("Failed to create scanner iframe");this.messageHandler.init(t);const s=`scan-${Date.now()}-${Math.random().toString(36).slice(2,10)}`,n=new URLSearchParams({sdkMode:"true",sessionId:this.messageHandler.getSessionId(),sdkVersion:y,platform:"web",merchantDomain:this.hostAppDomain,requestId:s}),i=this._getHostedPageAuthToken();i&&n.set("auth",i);const o=this.baseUrl.replace(/\/+$/,"");t.src=`${o}/#/openScanner?${n.toString()}`,this.isOpen=!0,this.scanSession={requestId:s,resolve:e,startTime:Date.now()},this.scanTimeout=setTimeout(()=>{this._resolveScanSession(this._createPaymentDetailsResult({success:!1,message:"Scanner flow timed out before payment details were fetched",error:"Scanner timeout error.",responseCode:null,rawResponse:null,data:null}))},l)}catch(t){e(this._createPaymentDetailsResult({success:!1,message:"Failed to open scanner flow",error:t?.message||String(t)})),this._cleanup()}})()})}fetchPaymentDetails(e){return new Promise(t=>{if(this.isOpen||this.currentFlowType)return void t(this._createPaymentDetailsResult({success:!1,uuid:"string"==typeof e?e.trim():"",message:"No data found or invalid UUID",error:"A Dropp session is already in progress",responseCode:null,rawResponse:null,data:null}));const s="string"==typeof e?e.trim():"";this._isValidUuid(s)?(async()=>{try{await this.initialize(),this.currentFlowType="uuid_lookup",this.currentSession=null,this.onSuccess=null,this.onFailure=null,this.onCancel=null,this.onClose=null,this.messageHandler=new E(this.environment,this._handleMessage.bind(this),this.allowedOrigins);const e=document.createElement("iframe");e.id="dropp-uuid-lookup-iframe",e.setAttribute("allow","payment *"),e.setAttribute("sandbox","allow-scripts allow-same-origin allow-forms allow-popups"),e.setAttribute("referrerpolicy","no-referrer"),e.setAttribute("title","Dropp UUID Lookup"),Object.assign(e.style,{position:"absolute",width:"1px",height:"1px",border:"0",opacity:"0",pointerEvents:"none",visibility:"hidden"}),document.body.appendChild(e),this.hiddenIframe=e,this.messageHandler.init(e);const n=`uuid-${Date.now()}-${Math.random().toString(36).slice(2,10)}`,i=new URLSearchParams({sdkMode:"true",sessionId:this.messageHandler.getSessionId(),sdkVersion:y,platform:"web",merchantDomain:this.hostAppDomain,merchantAccount:this.merchantId,requestId:n}),o=this._getHostedPageAuthToken();o&&i.set("auth",o);const r=this.baseUrl.replace(/\/+$/,"");e.src=`${r}/#/uuid/${encodeURIComponent(s)}?${i.toString()}`,this.scanSession={requestId:n,resolve:t,startTime:Date.now()},this.scanTimeout=setTimeout(()=>{this._resolveScanSession(this._createPaymentDetailsResult({success:!1,uuid:s,message:"No data found or invalid UUID",error:"Fetch payment details timeout error.",responseCode:null,rawResponse:null,data:null}))},u)}catch(e){t(this._createPaymentDetailsResult({success:!1,uuid:s,message:"No data found or invalid UUID",error:e?.message||String(e),responseCode:null,rawResponse:null,data:null})),this._cleanup()}})():t(this._createPaymentDetailsResult({success:!1,uuid:s,message:"No data found or invalid UUID",error:"Invalid UUID error.",responseCode:null,rawResponse:null,data:null}))})}async _initializePayment(e){f.log("[Dropp SDK] Initializing payment session"),this.currentFlowType="payment",this.messageHandler=new E(this.environment,this._handleMessage.bind(this),this.allowedOrigins),this.modalManager=new C(this._handleModalClose.bind(this));const t=this.modalManager.open();if(!t)throw new Error("Failed to create modal iframe");this.messageHandler.init(t);const s=e.paymentType||c.STANDARD;let n="/payViaUrl";s===c.PREAUTH?n="/preAuthPayment":s===c.RECURRING&&(n="/recurringPayment");const i=s===c.RECURRING?(e.frequency||e.recurringInterval||"").toString().trim().toUpperCase():"",o=s===c.RECURRING&&"NONE"===i,l=e.appUrl||("undefined"!=typeof window&&window.location?window.location.href:void 0),u={merchantAccount:this.merchantId,amount:s===c.PREAUTH?void 0:e.amount,maxAmount:s===c.PREAUTH||o?e.amount:void 0,fixAmount:s!==c.RECURRING||o?void 0:e.amount,acceptPaymentDelay:s===c.STANDARD&&!0===e.acceptPaymentDelay||void 0,currency:e.currency,itemName:e.itemName,description:e.description,invoiceId:e.invoiceId||`INV-${Date.now()}`,invoiceType:s===c.PREAUTH?"PREAUTH":void 0,apiKey:this.apiKey,packageName:this.hostAppDomain,merchantDomain:this.hostAppDomain,sdkVersion:y,platform:"web",appUrl:l,sessionId:this.messageHandler.getSessionId()};if(s===c.STANDARD&&(void 0!==e.displayItems&&(u.displayItems=e.displayItems),void 0!==e.metaData&&(u.metaData=e.metaData)),f.log("[Dropp SDK] Including SDK session context in payment params"),e.successURL&&(u.successURL=e.successURL),e.failureURL&&(u.failureURL=e.failureURL),s===c.RECURRING){const t=e.frequency||e.recurringInterval;t&&(u.frequency=t),e.recurringEndDate&&(u.expiry=e.recurringEndDate)}s===c.PREAUTH&&e.authHoldTimeInSeconds&&(u.authHoldTimeInSeconds=e.authHoldTimeInSeconds),e.callbackUrl&&(u.url=e.callbackUrl,u.submitToCallBack="post");const d={sessionId:u.sessionId,merchantAccount:this.merchantId,amount:e.amount,paymentType:s,invoiceId:u.invoiceId,expiry:e.recurringEndDate||null,issuedAt:Date.now()};let p="string"==typeof e.serverAuthToken?e.serverAuthToken.trim():"";if(!p&&this.getServerAuthToken)try{const e=await this.getServerAuthToken(d);p="string"==typeof e?e.trim():""}catch(e){throw A(h,"Failed to obtain server auth token",{originalError:e?.message||String(e)})}if(this.requireServerAuthToken&&!p)throw A(h,"serverAuthToken is required. Provide options.serverAuthToken or config.getServerAuthToken.",{environment:this.environment,paymentType:s,invoiceId:u.invoiceId});const m=await async function(e,t,s,n,i={}){try{const{serverAuthToken:o=""}=i,r={};Object.keys(s).forEach(e=>{const t=s[e];null!=t&&(r[e]=t)}),f.log("[buildPaymentUrl] Building payment URL payload");const a=v(JSON.stringify(r)),l=Date.now().toString(),u=new URLSearchParams({pay:a,sdkTs:l,sdkVersion:n,platform:"web"});return o&&u.set("authToken",o),`${e}/#${t}?${u.toString()}`}catch(e){throw f.error("Error building payment URL:",e),e}}(this.baseUrl,n,u,y,{serverAuthToken:p});f.log("[Dropp SDK] Loading payment flow iframe"),t.src=m,this.isOpen=!0,this.initTimeout=setTimeout(()=>{this._handleTimeout("init")},r),this.paymentTimeout=setTimeout(()=>{this._handleTimeout("payment")},a)}async _openHostedPage(e,t={}){if("status"===this.currentFlowType)throw A(m,"A Dropp session is already in progress",{currentSession:this.currentSession,currentFlowType:this.currentFlowType,route:e});if(this.isOpen){if("page"!==this.currentFlowType)throw A(m,"A Dropp session is already in progress",{currentSession:this.currentSession,route:e});this._cleanup()}await this.initialize(),this.currentFlowType="page",this.currentSession=null,this.onSuccess=null,this.onFailure="function"==typeof t.onFailed?t.onFailed:null,this.onCancel="function"==typeof t.onCancel?t.onCancel:null,this.onClose="function"==typeof t.onClose?t.onClose:null,this.onAccountChanged="function"==typeof t.onAccountChanged?t.onAccountChanged:null,this.onPageFailed="function"==typeof t.onPageFailed?t.onPageFailed:this.onFailure,this.messageHandler=new E(this.environment,this._handleMessage.bind(this),this.allowedOrigins),this.modalManager=new C(this._handleModalClose.bind(this));const s=this.modalManager.open();if(!s)throw new Error("Failed to create modal iframe");this.messageHandler.init(s);const n=new URLSearchParams({sdkMode:"true",sessionId:this.messageHandler.getSessionId(),sdkVersion:y,platform:"web",merchantDomain:this.hostAppDomain}),i=this._getHostedPageAuthToken();if(i&&n.set("auth",i),"/firsttimeflow"===e&&Array.isArray(t.modules)){const e=t.modules.map(e=>"string"==typeof e?e.trim():"").filter(Boolean);e.length>0&&n.set("modules",e.join(","))}const o=this.baseUrl.replace(/\/+$/,"");return s.src=`${o}/#${e}?${n.toString()}`,this.isOpen=!0,f.log("[Dropp SDK] Hosted page opened",{route:e}),{status:"opened",route:e}}_handleMessage(t){const{type:s,data:n}=t;if("scanner"===this.currentFlowType||"uuid_lookup"===this.currentFlowType){const e=this._extractUuidLookupPayload(s,n);if(e)return void this._handleUuidLookupResult(e)}switch(s){case e.READY:this._handleReady(n);break;case e.PAGE_LOADED:this._handlePageLoaded(n);break;case e.UUID_LOOKUP_RESULT:this._handleUuidLookupResult(n);break;case e.PAYMENT_PAGE_LOADED:this._handlePageLoaded(n);break;case e.PAYMENT_SUCCESS:this._handleSuccess(n);break;case e.PAYMENT_FAILED:this._handleFailure(n);break;case e.PAYMENT_CANCELLED:this._handleCancel("user_cancelled");break;case e.ACCOUNT_STATUS:this._handleAccountStatus(n);break;case e.ACCOUNT_EVENT:this._handleAccountEvent(n);break;case e.PAGE_EVENT:this._handlePageEvent(n);break;case e.AUTO_CLOSE_WEBVIEW:f.log("[Dropp SDK] AUTO_CLOSE_WEBVIEW received - closing modal"),this._cleanup(),f.log("[Dropp SDK] Cleanup completed");break;case e.CLOSE_WEBVIEW:if(f.log("[Dropp SDK] CLOSE_WEBVIEW received from app - closing modal"),"status"===this.currentFlowType&&this.statusSession){const e=A(g,"Account status request was closed before response was received",{type:"status",sessionDuration:Date.now()-this.statusSession.startTime});this.statusSession.reject(e),this._cleanup();break}this._handleCancel("app_close");break;default:f.warn("[Dropp SDK] Unknown message type:",s)}}_handleReady(e){if(f.log("[Dropp SDK] Payment app ready"),this.initTimeout&&(clearTimeout(this.initTimeout),this.initTimeout=null),this.messageHandler&&this.currentSession){const e={};Object.keys(this.currentSession.options).forEach(t=>{const s=this.currentSession.options[t];"function"!=typeof s&&(e[t]=s)}),f.log("[Dropp SDK] Sending init config to payment app"),this.messageHandler.sendInit(e)}}_handlePageLoaded(e){f.log("[Dropp SDK] Payment page loaded")}_getBridgeAction(e={}){return e&&"object"==typeof e?"string"==typeof e.action?e.action:e.result&&"object"==typeof e.result&&"string"==typeof e.result.action?e.result.action:"":""}_getSafeFailureMessage(e={},t="Request failed"){return e&&"string"==typeof e.message&&e.message.trim()?e.message.trim():e&&e.result&&"object"==typeof e.result&&"string"==typeof e.result.message&&e.result.message.trim()?e.result.message.trim():t}_getSafeResult(e={}){return e&&e.result&&"object"==typeof e.result?e.result:{}}_handleAccountEvent(t){const s=this._extractUuidLookupPayload(e.ACCOUNT_EVENT,t);if(s)return void this._handleUuidLookupResult(s);const n=this._getBridgeAction(t),i=this._getSafeResult(t);if("onAccountChanged"!==n)if("onCancel"!==n){if("onFailed"===n){const e=this._getSafeFailureMessage(t,"Account request failed");return void(this.onFailure&&this.onFailure(e))}f.warn("[Dropp SDK] Unknown ACCOUNT_EVENT action:",n)}else this.onCancel&&this.onCancel();else this.onAccountChanged&&this.onAccountChanged({linked:!0===i.linked,rawData:i.rawData})}_handlePageEvent(t){const s=this._extractUuidLookupPayload(e.PAGE_EVENT,t);if(s)return void this._handleUuidLookupResult(s);const n=this._getBridgeAction(t);if("onFailed"===n){const e=this._getSafeFailureMessage(t,"Page request failed");return void(this.onPageFailed&&this.onPageFailed(e))}f.warn("[Dropp SDK] Unknown PAGE_EVENT action:",n)}_handleAccountStatus(t){f.log("[Dropp SDK] Account status received"),this.statusSession?(this.statusSession.resolve({type:e.ACCOUNT_STATUS,data:t}),this._cleanup()):f.warn("[Dropp SDK] ACCOUNT_STATUS received without active status session")}_handleSuccess(e){f.log("[Dropp SDK] Payment successful");const t={status:"success",...e,sessionDuration:Date.now()-this.currentSession.startTime};this.currentSession&&this.currentSession.resolve(t),this.onSuccess&&this.onSuccess(t)}_handleFailure(e){f.log("[Dropp SDK] Payment failed");const t={status:"failed",...e,sessionDuration:Date.now()-this.currentSession.startTime};this.currentSession&&this.currentSession.resolve(t),this.onFailure&&this.onFailure(t)}_handleCancel(e){if(f.log("[Dropp SDK] Payment cancelled"),"scanner"===this.currentFlowType||"uuid_lookup"===this.currentFlowType)return void this._resolveScanSession(this._createPaymentDetailsResult({success:!1,message:"No data found or invalid UUID",error:`Scanner cancelled: ${e}`,responseCode:null,rawResponse:null,data:null}));if("page"===this.currentFlowType)return this.onCancel&&this.onCancel(),void this._cleanup();const t={status:"cancelled",reason:e,sessionDuration:this.currentSession?Date.now()-this.currentSession.startTime:0};this.currentSession&&this.currentSession.resolve(t),this.onCancel&&this.onCancel(t),this._cleanup()}_handleModalClose(e){f.log("[Dropp SDK] Modal closed by user"),"scanner"!==this.currentFlowType&&"uuid_lookup"!==this.currentFlowType?"page"!==this.currentFlowType?this._handleCancel(e):this._cleanup():this._resolveScanSession(this._createPaymentDetailsResult({success:!1,message:"No data found or invalid UUID",error:`Scanner closed: ${e}`,responseCode:null,rawResponse:null,data:null}))}_handleTimeout(e){f.error("[Dropp SDK] Timeout:",e);const t=A("init"===e?d:p,"init"===e?"Payment app failed to load within timeout period":"Payment session timed out",{type:e,sessionDuration:this.currentSession?Date.now()-this.currentSession.startTime:0});this.currentSession&&this.currentSession.reject(t),this.onFailure&&this.onFailure({status:"failed",error:t}),this._cleanup()}_cleanup(){f.log("[Dropp SDK] ๐Ÿงน Cleaning up session - starting cleanup"),this.initTimeout&&(clearTimeout(this.initTimeout),this.initTimeout=null),this.paymentTimeout&&(clearTimeout(this.paymentTimeout),this.paymentTimeout=null),this.statusTimeout&&(clearTimeout(this.statusTimeout),this.statusTimeout=null),this.scanTimeout&&(clearTimeout(this.scanTimeout),this.scanTimeout=null),this.messageHandler&&(f.log("[Dropp SDK] ๐Ÿงน Destroying message handler"),this.messageHandler.destroy(),this.messageHandler=null),this.modalManager&&(f.log("[Dropp SDK] ๐Ÿงน Closing modal manager"),this.modalManager.close(),f.log("[Dropp SDK] ๐Ÿงน Modal manager closed"),this.modalManager=null),this.hiddenIframe&&this.hiddenIframe.parentNode&&this.hiddenIframe.parentNode.removeChild(this.hiddenIframe),this.hiddenIframe=null,this.currentSession=null,this.statusSession=null,this.scanSession=null,this.currentFlowType=null,this.isOpen=!1,this.onClose&&this.onClose()}static getVersion(){return y}static getEnvironments(){return{...n}}static getPaymentTypes(){return{...c}}_isValidUuid(e){return"string"==typeof e&&/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(e.trim())}_createPaymentDetailsResult({success:e=!1,uuid:t="",message:s="",data:n=null,rawResponse:i=null,error:o=null,responseCode:r=null}={}){return{success:!0===e,uuid:"string"==typeof t?t:"",message:"string"==typeof s?s:"",data:n&&"object"==typeof n?n:null,rawResponse:i&&"object"==typeof i?i:null,error:"string"==typeof o&&o.trim()?o:null,responseCode:Number.isFinite(r)?Number(r):null}}_extractUuidLookupPayload(t,s){if(t===e.UUID_LOOKUP_RESULT)return s;const n=this._getBridgeAction(s),i="string"==typeof n?n.trim().toUpperCase():"";if("UUID_LOOKUP_RESULT"===i||"ONUUIDLOOKUPRESULT"===i)return s?.result||s;return"UUID_LOOKUP_RESULT"===("string"==typeof s?.type?s.type.trim().toUpperCase():"string"==typeof s?.eventName?s.eventName.trim().toUpperCase():"")?s?.payload||s?.result||s:null}_handleUuidLookupResult(e){if(!this.scanSession||"scanner"!==this.currentFlowType&&"uuid_lookup"!==this.currentFlowType)return;const t="string"==typeof e?.requestId?e.requestId:"string"==typeof e?.correlationId?e.correlationId:null;if(t&&this.scanSession.requestId&&t!==this.scanSession.requestId)return void f.warn("[Dropp SDK] UUID lookup response ignored due to request mismatch");const s=this._normalizeUuidLookupResult(e);this._resolveScanSession(s)}_normalizeUuidLookupResult(e={}){const t=e?.result&&"object"==typeof e.result?e.result:e,s="string"==typeof t?.uuid?t.uuid:"string"==typeof t?.storedUuid?t.storedUuid:"";let n=t?.data??null;if("string"==typeof n)try{n=JSON.parse(n)}catch{n=n.trim()?n:null}const i=Number(t?.responseCode??t?.rawResponse?.responseCode),o=Number.isNaN(i)?null:i,r="string"==typeof t?.error&&t.error.trim()?t.error.trim():"string"==typeof t?.message&&t.message.trim()&&!1===t.success?t.message.trim():null;return!0===t?.success||!1!==t?.success&&0===o&&null!==n?this._createPaymentDetailsResult({success:!0,uuid:s,message:"string"==typeof t?.message&&t.message.trim()?t.message.trim():"Payment details fetched successfully",data:n,rawResponse:t?.rawResponse&&"object"==typeof t.rawResponse?t.rawResponse:t,error:null,responseCode:o}):this._createPaymentDetailsResult({success:!1,uuid:s,message:"No data found or invalid UUID",data:null,rawResponse:t?.rawResponse&&"object"==typeof t.rawResponse?t.rawResponse:t,error:r||"No data found or invalid UUID",responseCode:o})}_resolveScanSession(e){if(!this.scanSession)return;const{resolve:t}=this.scanSession;t(e),this._cleanup()}}const P="__droppDisableContextMenuListener__";!function(){if("undefined"==typeof window||"undefined"==typeof document)return;if(window[P])return;document.addEventListener("contextmenu",e=>{e.preventDefault(),e.stopPropagation()},!0),window[P]=!0}();let O=null;async function k(e){O=new N(e);return{...await O.initialize(),sdk:O}}function R(){return O}const L={init:e=>k(e),pay(e){if(!O){const e=new Error("Dropp SDK is not initialized. Call Dropp.init(config) before Dropp.pay(options).");throw e.code="SDK_NOT_INITIALIZED",e}return O.pay(e)},scanPaymentDetails(){if(!O){const e=new Error("Dropp SDK is not initialized. Call Dropp.init(config) before Dropp.scanPaymentDetails().");throw e.code="SDK_NOT_INITIALIZED",e}return O.scanPaymentDetails()},fetchPaymentDetails(e){if(!O){const e=new Error("Dropp SDK is not initialized. Call Dropp.init(config) before Dropp.fetchPaymentDetails(uuid).");throw e.code="SDK_NOT_INITIALIZED",e}return O.fetchPaymentDetails(e)},dashboard(e){if(!O){const e=new Error("Dropp SDK is not initialized. Call Dropp.init(config) before Dropp.dashboard(options).");throw e.code="SDK_NOT_INITIALIZED",e}return O.dashboard(e)},open(e,t){if(!O){const e=new Error("Dropp SDK is not initialized. Call Dropp.init(config) before Dropp.open(moduleId, options).");throw e.code="SDK_NOT_INITIALIZED",e}return O.open(e,t)},unlinkAccount(e){if(!O){const e=new Error("Dropp SDK is not initialized. Call Dropp.init(config) before Dropp.unlinkAccount(options).");throw e.code="SDK_NOT_INITIALIZED",e}return O.unlinkAccount(e)},transactions(e){if(!O){const e=new Error("Dropp SDK is not initialized. Call Dropp.init(config) before Dropp.transactions(options).");throw e.code="SDK_NOT_INITIALIZED",e}return O.transactions(e)},offers(e){if(!O){const e=new Error("Dropp SDK is not initialized. Call Dropp.init(config) before Dropp.offers(options).");throw e.code="SDK_NOT_INITIALIZED",e}return O.offers(e)},status(){if(!O){const e=new Error("Dropp SDK is not initialized. Call Dropp.init(config) before Dropp.status().");throw e.code="SDK_NOT_INITIALIZED",e}return O.status()},getInstance:()=>O};var M={DroppPaymentSDK:N,createPaymentSDK:k,Dropp:L,ENVIRONMENTS:n,PAYMENT_TYPES:c};export{L as Dropp,N as DroppPaymentSDK,n as ENVIRONMENTS,c as PAYMENT_TYPES,k as createPaymentSDK,M as default,R as getDroppInstance};
2
2
  //# sourceMappingURL=dropp-payment-sdk.esm.js.map