@sqala/threedsecure-js
Version:
A vanilla library for implementing 3D Secure authentication flows in payment applications
59 lines (58 loc) • 2.42 kB
JavaScript
import { AuthenticationState as c } from "../types/authentication.js";
import { Bucket as l } from "../models/bucket.js";
class d {
constructor(i, a, t = "https://api.sqala.tech/core/v1/threedsecure") {
this.logger = i, this.publicKey = a, this.baseUrl = t;
}
executeAuthentication(i, a) {
const t = new EventSource(`${this.baseUrl}/${i.id}/listen?publicKey=${this.publicKey}`), o = new l(), e = this.logger.bind(this), s = () => {
try {
o.close(), t.close();
} catch (n) {
e("ApiService: executeAuthentication - close - error", n);
}
};
return t.addEventListener("message", (n) => {
try {
const r = JSON.parse(n.data);
e("ApiService: executeAuthentication - onmessage", r), o.push(r), (r.state === c.Failed || r.state === c.AuthorizedToAttempt || r.state === c.Completed || a.aborted) && s();
} catch (r) {
e("ApiService: executeAuthentication - onmessage - error", r);
}
}), t.addEventListener("close", () => {
e("ApiService: executeAuthentication - onclose"), s();
}), t.addEventListener("error", (n) => {
e("ApiService: executeAuthentication - onerror", n), s();
}), a.addEventListener("abort", () => {
e("ApiService: executeAuthentication - abort"), s();
}), o.iterator;
}
async setBrowserData(i) {
this.logger("ApiService: setBrowserData", i);
const t = [48, 32, 24, 16, 15, 8, 4, 1].find((s) => s <= screen.colorDepth) ?? 48, o = {
javaEnabled: !0,
javascriptEnabled: !0,
language: navigator.language,
userAgent: navigator.userAgent,
screenWidth: window.screen.width,
screenHeight: window.screen.height,
timeZoneOffset: (/* @__PURE__ */ new Date()).getTimezoneOffset(),
colorDepth: t,
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("ApiService: setBrowserData - browser", o);
const e = await fetch(`${this.baseUrl}/${i.id}/browser?publicKey=${this.publicKey}`, {
method: "PATCH",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(o)
});
if (this.logger("ApiService: setBrowserData - response", e), !e.ok)
throw new Error("Failed to set browser data");
}
}
export {
d as ApiService
};
//# sourceMappingURL=api-service.js.map