UNPKG

@paypayhub/sqala-3ds-js

Version:

A vanilla library for implementing 3D Secure authentication flows in payment applications

84 lines (83 loc) 3.24 kB
var w = Object.defineProperty; var A = (s, e, t) => e in s ? w(s, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : s[e] = t; var g = (s, e, t) => A(s, typeof e != "symbol" ? e + "" : e, t); import { AuthenticationState as p } from "../types/authentication.js"; import { Bucket as v } from "../models/bucket.js"; class x { constructor(e, t, r = "https://api.sqala.tech/core/v1/threedsecure") { g(this, "pollingIntervalMs", 3e3); this.logger = e, this.publicKey = t, this.baseUrl = r; } executeAuthentication(e, t) { const r = new v(), i = this.logger.bind(this); let a = null; const n = () => { a && clearTimeout(a), r.close(); }, h = async () => { if (t.aborted) return i("ApiService: executeAuthentication - aborted"), n(); try { const c = await fetch( `${this.baseUrl}/${e.id}/listen?publicKey=${this.publicKey}`, { method: "GET", signal: t, headers: { Accept: "text/event-stream" } } ); if (!c.ok) { i(`ApiService: failed with status ${c.status}`); return; } const u = await c.text(); i("ApiService: executeAuthentication - poll response text", u); const d = u.split(` `).filter((l) => l.startsWith("data: ")).map((l) => l.replace(/^data:\s*/, "")); for (const l of d) try { const o = JSON.parse(l); if (i("ApiService: executeAuthentication - parsed auth", o), r.push(o), o.state === p.Failed || o.state === p.AuthorizedToAttempt || o.state === p.Completed) return n(); } catch (o) { i("ApiService: executeAuthentication - JSON parse error", o); } a = setTimeout(h, this.pollingIntervalMs); } catch (c) { i("ApiService: executeAuthentication - fetch error", c), n(); } }; return h(), t.addEventListener("abort", () => { i("ApiServicePolling: executeAuthentication - abort listener triggered"), n(); }), r.iterator; } async setBrowserData(e) { this.logger("ApiServicePolling: setBrowserData", e); const r = [48, 32, 24, 16, 15, 8, 4, 1].find((n) => n <= screen.colorDepth) ?? 48, i = { javaEnabled: !0, javascriptEnabled: !0, language: navigator.language, userAgent: navigator.userAgent, screenWidth: window.screen.width, screenHeight: window.screen.height, timeZoneOffset: (/* @__PURE__ */ new Date()).getTimezoneOffset(), colorDepth: r, acceptHeader: "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9" }; this.logger("ApiServicePolling: setBrowserData - browser", i); const a = await fetch(`${this.baseUrl}/${e.id}/browser?publicKey=${this.publicKey}`, { method: "PATCH", headers: { "Content-Type": "application/json" }, body: JSON.stringify(i) }); if (this.logger("ApiServicePolling: setBrowserData - response", a), !a.ok) throw new Error("Failed to set browser data"); } } export { x as ApiServicePolling }; //# sourceMappingURL=api-service-polling.js.map