@sqala/threedsecure-js
Version:
A vanilla library for implementing 3D Secure authentication flows in payment applications
76 lines (75 loc) • 3.33 kB
JavaScript
var g = Object.defineProperty;
var u = (o, e, t) => e in o ? g(o, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : o[e] = t;
var s = (o, e, t) => u(o, typeof e != "symbol" ? e + "" : e, t);
import { AuthenticationState as i } from "../types/authentication.js";
import { ApiService as d } from "./api-service.js";
import { Base64Encoder as n } from "./base64-encoder.js";
import { DsMethodService as S } from "./dsmethod-service.js";
import { ChallengeService as v } from "./challenge-service.js";
import { v as p } from "../v4-CRLUkzQ6.js";
class a {
constructor(e, t = a.logger()) {
s(this, "container");
s(this, "apiService");
s(this, "dsMethodService");
s(this, "challengeService");
s(this, "actionMapping", /* @__PURE__ */ new Map([
[i.PendingDirectoryServer, this.handleDsMethod.bind(this)],
[i.PendingChallenge, this.handleChallenge.bind(this)],
[i.Failed, this.handleResult.bind(this)],
[i.Completed, this.handleResult.bind(this)],
[i.AuthorizedToAttempt, this.handleResult.bind(this)]
]));
this.logger = t, this.logger("ThreeDSecureService: constructor", e), this.container = e.container, this.apiService = new d(this.logger, e.publicKey, e.baseUrl), this.dsMethodService = new S(this.logger, new n()), this.challengeService = new v(this.logger, new n());
}
async execute(e, t = new AbortController()) {
this.logger("ThreeDSecureService: execute");
const h = 5 * 60 * 1e3;
this.logger("ThreeDSecureService: execute - configuring timeout");
const l = setTimeout(() => {
t.abort("timeout");
}, h);
try {
this.logger("ThreeDSecureService: setBrowserData", e), await this.apiService.setBrowserData(e);
let r;
for await (r of this.apiService.executeAuthentication(e, t.signal)) {
this.logger("ThreeDSecureService: flowStep", r);
const c = this.actionMapping.get(r.state);
await (c == null ? void 0 : c(r, t)), this.logger("ThreeDSecureService: flowStep - end");
}
return t.abort("completed"), {
id: r.id,
transStatus: r.transStatus,
transStatusReason: r.transStatusReason,
authenticationValue: r.authenticationValue,
eci: r.eci,
dsTransId: r.dsTransId,
protocolVersion: r.protocolVersion,
failReason: r.failReason,
isSuccess: () => r.state === i.Completed || r.state === i.AuthorizedToAttempt
};
} catch (r) {
throw this.logger("ThreeDSecureService: error", r), t.abort("error"), r;
} finally {
clearTimeout(l), this.challengeService.clean(), this.dsMethodService.clean(), this.logger("ThreeDSecureService: finally");
}
}
handleResult(e, t) {
return this.logger("ThreeDSecureService: handleResult", e), t.abort("completed"), Promise.resolve();
}
handleDsMethod(e, t) {
return this.logger("ThreeDSecureService: handleDsMethod", e), this.dsMethodService.executeDsMethod(e, this.container);
}
handleChallenge(e, t) {
return this.logger("ThreeDSecureService: handleChallenge", e), this.challengeService.executeChallenge(e, this.container);
}
static logger(e = p()) {
return (t, ...h) => {
console.log(`[${e}]: ${t}`, ...h);
};
}
}
export {
a as ThreeDSecureService
};
//# sourceMappingURL=threedsecure-service.js.map