UNPKG

@sqala/threedsecure-js

Version:

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

58 lines (57 loc) 2.63 kB
var c = Object.defineProperty; var n = (t, e, r) => e in t ? c(t, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : t[e] = r; var a = (t, e, r) => n(t, typeof e != "symbol" ? e + "" : e, r); import d from "./assert.js"; import { Base64Encoder as f } from "./base64-encoder.js"; import { v as m } from "../v4-CRLUkzQ6.js"; var g = /* @__PURE__ */ ((t) => (t.H400xW250 = "01", t.H400xW390 = "02", t.H600xW500 = "03", t.H400xW600 = "04", t.Fullscreen = "05", t))(g || {}); class b { constructor(e, r = new f()) { a(this, "iFrame"); a(this, "form"); this.logger = e, this.base64Encoder = r; } getChallengeWindowSize(e) { return e.clientWidth <= 250 && "01" || e.clientWidth <= 390 && "02" || e.clientWidth <= 500 && "03" || e.clientWidth <= 600 && "04" || "05"; } async executeChallenge(e, r) { var i; try { if (d(e.acsUrl, "acsUrl is required"), (i = this.form) != null && i.hasAttribute("data-submitted")) return; r.style.position = "relative", this.iFrame = document.createElement("iframe"), this.iFrame.name = m(), this.iFrame.style.width = "100%", this.iFrame.style.height = "100%", this.iFrame.style.position = "absolute", this.iFrame.style.top = "0", this.iFrame.style.left = "0", this.form = document.createElement("form"), this.form.style.visibility = "hidden", this.form.name = m(), this.form.target = this.iFrame.name, this.form.action = e.acsUrl, this.form.method = "POST"; const s = document.createElement("input"); s.type = "hidden", s.name = "creq", this.form.appendChild(s); const o = { threeDSServerTransID: e.transactionId, acsTransID: e.acsTransId, messageVersion: e.acsProtocolVersion, messageType: "CReq", challengeWindowSize: this.getChallengeWindowSize(r) }; s.value = this.base64Encoder.encode(o), r.appendChild(this.form), r.appendChild(this.iFrame), await new Promise((h, l) => { this.iFrame.onload = () => { h(); }, this.iFrame.onerror = () => { l(new Error("Failed to execute challenge")); }, this.form.submit(), this.form.setAttribute("data-submitted", "true"); }); } catch (s) { throw this.logger("ChallengeService: error", s), s; } } clean() { var e, r; this.logger("ChallengeService: clean"); try { (e = this.iFrame) == null || e.remove(), (r = this.form) == null || r.remove(); } catch (i) { this.logger("ChallengeService: clean - error", i); } } } export { b as ChallengeService, g as ChallengeWindowSize }; //# sourceMappingURL=challenge-service.js.map