UNPKG

@onepaynpm/onepay-sdk

Version:

Official OnePay payment gateway SDK for JavaScript applications

13 lines (12 loc) 13 kB
/*! * @onepaynpm/onepay-sdk v1.0.2 * Official OnePay payment gateway SDK for JavaScript applications * https://github.com/onepay-lk/onepay-sdk#readme * * Copyright (c) 2025 OnePay Team * Released under the MIT License */ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).OnePaySDK={})}(this,(function(e){"use strict";function t(e,t,n,r,o,i,a){try{var s=e[i](a),c=s.value}catch(e){return void n(e)}s.done?t(c):Promise.resolve(c).then(r,o)}function n(e){return function(){var n=this,r=arguments;return new Promise((function(o,i){var a=e.apply(n,r);function s(e){t(a,o,i,s,c,"next",e)}function c(e){t(a,o,i,s,c,"throw",e)}s(void 0)}))}}function r(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,a(r.key),r)}}function o(){ /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */ var e,t,n="function"==typeof Symbol?Symbol:{},r=n.iterator||"@@iterator",a=n.toStringTag||"@@toStringTag";function s(n,r,o,a){var s=r&&r.prototype instanceof u?r:u,l=Object.create(s.prototype);return i(l,"_invoke",function(n,r,o){var i,a,s,u=0,l=o||[],d=!1,f={p:0,n:0,v:e,a:p,f:p.bind(e,4),d:function(t,n){return i=t,a=0,s=e,f.n=n,c}};function p(n,r){for(a=n,s=r,t=0;!d&&u&&!o&&t<l.length;t++){var o,i=l[t],p=f.p,y=i[2];n>3?(o=y===r)&&(s=i[(a=i[4])?5:(a=3,3)],i[4]=i[5]=e):i[0]<=p&&((o=n<2&&p<i[1])?(a=0,f.v=r,f.n=i[1]):p<y&&(o=n<3||i[0]>r||r>y)&&(i[4]=n,i[5]=r,f.n=y,a=0))}if(o||n>1)return c;throw d=!0,r}return function(o,l,y){if(u>1)throw TypeError("Generator is already running");for(d&&1===l&&p(l,y),a=l,s=y;(t=a<2?e:s)||!d;){i||(a?a<3?(a>1&&(f.n=-1),p(a,s)):f.n=s:f.v=s);try{if(u=2,i){if(a||(o="next"),t=i[o]){if(!(t=t.call(i,s)))throw TypeError("iterator result is not an object");if(!t.done)return t;s=t.value,a<2&&(a=0)}else 1===a&&(t=i.return)&&t.call(i),a<2&&(s=TypeError("The iterator does not provide a '"+o+"' method"),a=1);i=e}else if((t=(d=f.n<0)?s:n.call(r,f))!==c)break}catch(t){i=e,a=1,s=t}finally{u=1}}return{value:t,done:d}}}(n,o,a),!0),l}var c={};function u(){}function l(){}function d(){}t=Object.getPrototypeOf;var f=[][r]?t(t([][r]())):(i(t={},r,(function(){return this})),t),p=d.prototype=u.prototype=Object.create(f);function y(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,d):(e.__proto__=d,i(e,a,"GeneratorFunction")),e.prototype=Object.create(p),e}return l.prototype=d,i(p,"constructor",d),i(d,"constructor",l),l.displayName="GeneratorFunction",i(d,a,"GeneratorFunction"),i(p),i(p,a,"Generator"),i(p,r,(function(){return this})),i(p,"toString",(function(){return"[object Generator]"})),(o=function(){return{w:s,m:y}})()}function i(e,t,n,r){var o=Object.defineProperty;try{o({},"",{})}catch(e){o=0}i=function(e,t,n,r){if(t)o?o(e,t,{value:n,enumerable:!r,configurable:!r,writable:!r}):e[t]=n;else{function a(t,n){i(e,t,(function(e){return this._invoke(t,n,e)}))}a("next",0),a("throw",1),a("return",2)}},i(e,t,n,r)}function a(e){var t=function(e,t){if("object"!=typeof e||!e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var r=n.call(e,t||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:t+""}function s(e){return new Promise((function(t,n){var r=document.createElement("script");r.src=e,r.async=!0,r.onload=function(){return t()},r.onerror=function(){return n(new Error("Script load error: ".concat(e)))},document.head.appendChild(r)}))}function c(){return u.apply(this,arguments)}function u(){return(u=n(o().m((function e(){return o().w((function(e){for(;;)switch(e.n){case 0:return e.n=1,s("https://www.gstatic.com/firebasejs/9.17.2/firebase-app-compat.js");case 1:return e.n=2,s("https://www.gstatic.com/firebasejs/9.17.2/firebase-firestore-compat.js");case 2:return e.a(2)}}),e)})))).apply(this,arguments)}function l(e){if("undefined"==typeof window)throw new Error("Firebase can only be initialized in browser environment");if(!window.firebase)throw new Error("Firebase scripts not loaded. Call loadFirebaseScripts() first.");return window.firebase.apps.length||window.firebase.initializeApp(e),window.firebase.firestore()}function d(e,t){return f.apply(this,arguments)}function f(){return(f=n(o().m((function e(t,n){var r,i,a,s;return o().w((function(e){for(;;)switch(e.n){case 0:return r="".concat(t.app_id).concat(t.currency).concat(t.amount.toFixed(2)).concat(n),i=new TextEncoder,a=i.encode(r),e.n=1,crypto.subtle.digest("SHA-256",a);case 1:return s=e.v,e.a(2,Array.from(new Uint8Array(s)).map((function(e){return e.toString(16).padStart(2,"0")})).join(""))}}),e)})))).apply(this,arguments)}var p=null;function y(e,t){(p=document.createElement("div")).style.position="fixed",p.style.top="0",p.style.left="0",p.style.width="100%",p.style.height="100%",p.style.backgroundColor="rgba(0, 0, 0, 0.5)",p.style.zIndex="9998",p.style.display="flex",p.style.alignItems="center",p.style.justifyContent="center";var n=document.createElement("div");n.style.position="relative",n.style.zIndex="9999",n.style.backgroundColor="#fff",n.style.borderRadius="8px",n.style.overflow="hidden",n.style.boxShadow="0 4px 6px rgba(0, 0, 0, 0.1)";var r=document.createElement("button");r.innerHTML="×",r.style.position="absolute",r.style.right="10px",r.style.top="30px",r.style.zIndex="10000",r.style.backgroundColor="#fff",r.style.border="none",r.style.borderRadius="50%",r.style.width="30px",r.style.height="30px",r.style.fontSize="20px",r.style.cursor="pointer",r.style.display="flex",r.style.alignItems="center",r.style.justifyContent="center",r.style.boxShadow="0 2px 4px rgba(0,0,0,0.2)",r.onmouseover=function(){r.style.backgroundColor="#f0f0f0"},r.onmouseout=function(){r.style.backgroundColor="#fff"},r.addEventListener("click",(function(){console.log("Close button clicked"),p&&p.parentNode&&p.parentNode.removeChild(p);var e=new CustomEvent("onePayClose",{detail:{transaction_id:t,status:"CLOSED"},bubbles:!0});document.dispatchEvent(e),console.log("Close event dispatched")}));var o=document.createElement("iframe");o.src=e,o.style.width=(window.innerWidth,"430px"),o.style.height="600px",o.style.border="none",o.style.display="block",n.appendChild(r),n.appendChild(o),p.appendChild(n),document.body.appendChild(p)}function h(){try{return p&&p.parentNode?(p.parentNode.removeChild(p),p=null,console.log("Payment gateway closed successfully"),!0):(console.log("No payment gateway found to close"),!1)}catch(e){return console.error("Error closing payment gateway:",e),!1}}function v(e,t){if(!e)return console.error("Firestore (db) is not initialized!"),function(){};var n=e.collection("onepay").doc("transaction").onSnapshot((function(e){if(console.log("Transaction document updated:",e),e.exists){console.log("Document data:",e.data());var r=e.data()[t];if(console.log("Transaction data:",r),r)if(r.is_pay){h();var o={code:"201",transaction_id:t,status:"SUCCESS"};document.dispatchEvent(new CustomEvent("onePaySuccess",{detail:o})),n()}else{h();var i={code:"400",transaction_id:t,status:"FAIL"};document.dispatchEvent(new CustomEvent("onePayFail",{detail:i})),n()}}else console.error("Firestore document not found!")}));return n}var m="https://api.onepay.lk";function b(e,t){var n=arguments.length>3?arguments[3]:void 0;return fetch("".concat(arguments.length>2&&void 0!==arguments[2]?arguments[2]:m,"/v3/checkout/link/"),{method:"POST",headers:{"Content-Type":"application/json",Authorization:t},body:JSON.stringify(e)}).then((function(e){if(!e.ok)throw new Error("HTTP error! status: ".concat(e.status));return e.json()})).then((function(e){var t,r,o,i=null===(r=null===(t=null==e?void 0:e.data)||void 0===t?void 0:t.gateway)||void 0===r?void 0:r.redirect_url,a=null===(o=null==e?void 0:e.data)||void 0===o?void 0:o.ipg_transaction_id;if(!i||!a)throw new Error("Failed to retrieve redirect URL/transaction ID from API response.");y(i,a),n&&v(n,a)})).catch((function(e){throw console.error("Error in triggerPaymentAPI:",e),e}))}function w(e,t){var n=arguments.length>3?arguments[3]:void 0;return fetch("".concat(arguments.length>2&&void 0!==arguments[2]?arguments[2]:m,"/v3/subscription/"),{method:"POST",headers:{"Content-Type":"application/json",Authorization:t},body:JSON.stringify(e)}).then((function(e){if(!e.ok)throw new Error("HTTP ".concat(e.status));return e.json()})).then((function(e){var t,r,o=null===(t=null==e?void 0:e.data)||void 0===t?void 0:t.url,i=null===(r=null==e?void 0:e.data)||void 0===r?void 0:r.subscription_id;if(!o||!i)throw new Error("Subscription API did not return URL / ID.");y(o,i),n&&v(n,i)})).catch((function(e){throw console.error("Subscription API error:",e),e}))}var g=function(){return e=function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.db=null,this.initialized=!1,this.firebaseConfig=t.firebaseConfig||{apiKey:"AIzaSyDGxd3PoP01JN25cPXfgRsVoc-C6XE2FTk",authDomain:"spemai-global.firebaseapp.com",projectId:"spemai-global",storageBucket:"spemai-global.appspot.com",messagingSenderId:"864345120875",appId:"1:864345120875:web:d33dbc1b2b02f8098dd12e"},this.apiBaseUrl=t.apiBaseUrl||"https://api.onepay.lk",this.debug=t.debug||!1},t=[{key:"initialize",value:(p=n(o().m((function e(){var t;return o().w((function(e){for(;;)switch(e.n){case 0:if(!this.initialized){e.n=1;break}return this.debug&&console.log("OnePay SDK already initialized"),e.a(2);case 1:return e.p=1,e.n=2,c();case 2:this.db=l(this.firebaseConfig),this.initialized=!0,this.debug&&console.log("OnePay SDK initialized successfully"),e.n=4;break;case 3:throw e.p=3,t=e.v,console.error("Failed to initialize OnePay SDK:",t),t;case 4:return e.a(2)}}),e,this,[[1,3]])}))),function(){return p.apply(this,arguments)})},{key:"processDirectPayment",value:(f=n(o().m((function e(t){return o().w((function(e){for(;;)switch(e.n){case 0:return e.n=1,this.ensureInitialized();case 1:y(t.directGatewayURL,t.directTransactionId),this.startListening(t.directTransactionId);case 2:return e.a(2)}}),e,this)}))),function(e){return f.apply(this,arguments)})},{key:"processPayment",value:(u=n(o().m((function e(t){var n,r,i;return o().w((function(e){for(;;)switch(e.n){case 0:return e.n=1,this.ensureInitialized();case 1:return e.p=1,n={currency:t.currency,amount:t.amount,app_id:t.appid,reference:t.orderReference,customer_first_name:t.customerFirstName,customer_last_name:t.customerLastName,customer_phone_number:t.customerPhoneNumber,customer_email:t.customerEmail,transaction_redirect_url:t.transactionRedirectUrl,additional_data:t.additionalData},e.n=2,d(n,t.hashToken);case 2:return r=e.v,n.hash=r,e.n=3,b(n,t.apptoken,this.apiBaseUrl,this.db);case 3:e.n=5;break;case 4:throw e.p=4,i=e.v,console.error("Error processing payment:",i),i;case 5:return e.a(2)}}),e,this,[[1,4]])}))),function(e){return u.apply(this,arguments)})},{key:"processSubscription",value:(s=n(o().m((function e(t){var n,r,i;return o().w((function(e){for(;;)switch(e.n){case 0:return e.n=1,this.ensureInitialized();case 1:return e.p=1,n={app_id:t.appid,name:t.name,amount:t.amount,currency:t.currency,interval:t.interval,interval_count:t.interval_count,days_until_due:t.days_until_due,trial_period_days:t.trial_period_days,customer_details:t.customer_details},e.n=2,d(n,t.apptoken);case 2:return r=e.v,n.hash=r,e.n=3,w(n,t.apptoken,this.apiBaseUrl,this.db);case 3:e.n=5;break;case 4:throw e.p=4,i=e.v,console.error("Error processing subscription:",i),i;case 5:return e.a(2)}}),e,this,[[1,4]])}))),function(e){return s.apply(this,arguments)})},{key:"addEventListener",value:function(e){e.onSuccess&&document.addEventListener("onePaySuccess",(function(t){e.onSuccess(t.detail)})),e.onFail&&document.addEventListener("onePayFail",(function(t){e.onFail(t.detail)})),e.onClose&&document.addEventListener("onePayClose",(function(t){e.onClose(t.detail)}))}},{key:"removeEventListener",value:function(e,t){document.removeEventListener(e,t)}},{key:"closePaymentGateway",value:function(){return h()}},{key:"isInitialized",value:function(){return this.initialized}},{key:"ensureInitialized",value:(a=n(o().m((function e(){return o().w((function(e){for(;;)switch(e.n){case 0:if(this.initialized){e.n=1;break}return e.n=1,this.initialize();case 1:return e.a(2)}}),e,this)}))),function(){return a.apply(this,arguments)})},{key:"startListening",value:function(e){return v(this.db,e)}}],t&&r(e.prototype,t),i&&r(e,i),Object.defineProperty(e,"prototype",{writable:!1}),e;var e,t,i,a,s,u,f,p}();e.OnePaySDK=g,e.closePaymentGateway=h,e.default=g,e.generateSHA256Hash=d,e.initializeFirebase=l,e.loadFirebaseScripts=c,e.openPaymentIframe=y,Object.defineProperty(e,"__esModule",{value:!0})})); //# sourceMappingURL=onepay-sdk.umd.min.js.map