@mygateway/mygateway-three-ds-sdk-frontend
Version:
1 lines • 5.26 kB
JavaScript
function CartaoInvalidoException(){this.message="Número do cartão deve ter ao menos 6 digitos para o processamento do 3ds.",this.name="CartaoInvalidoException"}window.gateway_adiq_url="https://ecommerce.adiq.io/v1/",window.jwtString="",$.getScript("https://songbird.cardinalcommerce.com/cardinalcruise/v1/songbird.js",(function(e,t,n){console.log(e),console.log(t),console.log(n.status),console.log("Load was performed.")}));class Adiq3ds{constructor(e,t){this.ValidParams(e),this.cardNumberId=e,this.threeDsCode="",this.validateCallback=t,this.wasCancelled=!1}ValidParams(e){let t=document.getElementById(e).value;if(null==t||null==t||t.length<6)throw new CartaoInvalidoException;if((t=t.substring(0,6).replace(/[^\d]/,"")).length<6)throw new CartaoInvalidoException}async Authorization3ds(){configureSongbird(this.cardNumberId,this.validateCallback),await getTokenAuthorization3ds().then((e=>this.threeDsCode=e))}InitChallenge(e,t,n){Cardinal.continue("cca",{AcsUrl:e,Payload:t},{OrderDetails:{TransactionId:n}})}getThreeDsCode(){return this.threeDsCode}}function configureSongbird(e,t){console.log("configureSongbird"),Cardinal.configure({timeout:"30000",maxRequestRetries:"10",logging:{level:"verbose"},payment:{displayLoading:!0,displayExitButton:!0}}),Cardinal.on("payments.setupComplete",(function(t){console.log("payments.setupComplete event"),console.log(t),getBIN(document.getElementById(e))})),Cardinal.on("payments.validated",(function(e,n){console.log("payments.validated event"),console.log("Data:"),console.log(e),null==n?console.log("JWT is null."):(console.log("JWT:"),console.log(n),console.log(JSON.parse(parseJwt(n))),ChallengeResponseOk(n)&&t(n,this.wasCancelled?"Cancelled":"Approved"))}))}function ChallengeResponseOk(e){return window.jwtString==parseJwt(e)?(console.log("tratamento dos jwt duplicados."),!1):(window.jwtString=parseJwt(e),-1==window.jwtString.indexOf("ChallengeCancel")?(this.wasCancelled=!1,!0):(this.wasCancelled=!0,console.log("Challenge foi cancelado pelo usuario."),!0))}function parseJwt(e){var t=e.split(".")[1].replace(/-/g,"+").replace(/_/g,"/");return decodeURIComponent(atob(t).split("").map((function(e){return"%"+("00"+e.charCodeAt(0).toString(16)).slice(-2)})).join(""))}var previousBin="";function getBIN(e){e.value=e.value.replace(/[^\d]/,"");var t=e.value;if(t.length>=6){var n=t.slice(0,6);console.log("BIN: "+n),n!=previousBin&&(previousBin=n,Cardinal.trigger("bin.process",n))}}async function getTokenAuthorization3ds(){console.log("getTokenAuthorization3ds");var e={async:!0,crossDomain:!0,url:window.gateway_adiq_url+"threeds/token",method:"GET",headers:{Accept:"*/*"}};let t;try{t=await $.ajax(e)}catch(e){console.error(e)}return console.log("jwt: "+t),Cardinal.setup("init",{jwt:t.jwt}),t.orderNumber}function isUuid(e){return!!/^[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)}function isWithinOneHour(e,t){const n=function(e){const t=e.split(" "),n=parseInt(t[1],10),o=t[2],i=parseInt(t[3],10),[a,r,s]=t[4].split(":").map(Number),l={Jan:0,Feb:1,Mar:2,Apr:3,May:4,Jun:5,Jul:6,Aug:7,Sep:8,Oct:9,Nov:10,Dec:11}[o];return Date.UTC(i,l,n,a,r,s)}(e),o=function(e){const[t,n]=e.split(" "),[o,i,a]=t.split("-").map(Number),[r,s,l]=n.split(":").map(Number);return Date.UTC(o,i-1,a,r,s,l)}(t);return Math.abs(n-o)<=36e5}function injectAdiqScript(e,t,n){if(document.getElementById("challengeScript"))return;const o=document.createElement("script");o.id="challengeScript",o.type="text/javascript",o.src=`https://h.online-metrix.net/fp/tags.js?org_id=${t}&session_id=${e}${n}`,o.async=!0,document.head.appendChild(o)}function injectAdiqIframe(e,t,n){if(document.getElementById("challengeIframe"))return;const o=document.createElement("noscript"),i=document.createElement("iframe");i.id="challengeIframe",i.style.width="100px",i.style.height="100px",i.style.border="0",i.style.position="absolute",i.style.top="-5000px",i.src=`https://h.online-metrix.net/fp/tags.js?org_id=${t}&session_id=${e}${n}`,o.appendChild(i),document.body.appendChild(o)}function injectAdiqInput(e){if(document.getElementById("codeAntiFraud"))return;const t=document.querySelector("div[hidden]");if(t){const n=document.createElement("input");n.type="text",n.id="codeAntiFraud",n.name="codeAntiFraud",n.value=e,t.appendChild(n)}}class MyGateway3DS{callback;threeDsType;threeDs;constructor(e="ccnum",t,n=1,o,i){if(this.callback=t,this.threeDsType=n,!(e&&t&&n&&o&&i))throw new Error("Configuração inválida");const a=i.crypt.split("$$"),r=atob(a[0]),s=a[1],l=JSON.parse(atob(a[2])),c=a[3],d=atob(i.seed),u=(new Date).toUTCString();if(window.location.origin.toLowerCase()!==r.toLowerCase())throw new Error("Configuração inválida");if(!isUuid(s)||!isUuid(c))throw new Error("Configuração inválida");if(!isWithinOneHour(u,d))throw new Error("Configuração inválida");if(l.sessionId!==o)throw new Error("Sessão inválida");if(1==n){const n=l.providerExternalOne,i=atob(n.organizationId),a=atob(n.orgId);injectAdiqScript(i,a,o),injectAdiqIframe(i,a,o),injectAdiqInput(o),this.threeDs=new Adiq3ds(e,t)}}async Authorization3ds(){return await this.threeDs.Authorization3ds()}GetThreeDsCode(){return this.threeDs.getThreeDsCode()}InitChallenge(e,t,n){return this.threeDs.InitChallenge(e,t,n)}}