UNPKG

@braze/web-sdk

Version:

Braze SDK for web sites and other JS platforms.

65 lines (64 loc) 2.13 kB
import { EventTypes as p, logger as b } from "../../shared-lib/index.js"; import { isValidIso4217CurrencyCode as or } from "../util/iso-4217-currency-codes.js"; import { MAX_PURCHASE_QUANTITY as nr } from "../common/constants.js"; import v from "../common/event-logger.js"; import r from "../managers/braze-instance.js"; import ot from "../triggers/models/trigger-events.js"; import { TriggersProviderFactory as rt } from "../triggers/triggers-provider-factory.js"; import { validateCustomProperties as nt, validateCustomString as mt, } from "../util/validation-utils.js"; export function logPurchase(e, o, i, n, t) { if (!r.rr()) return !1; if ( (null == i && (i = "USD"), null == n && (n = 1), null == e || e.length <= 0) ) return ( b.error( `logPurchase requires a non-empty productId, got "${e}", ignoring.`, ), !1 ); if (!mt(e, "log purchase", "the purchase name")) return !1; if (null == o || isNaN(parseFloat(o.toString()))) return ( b.error(`logPurchase requires a numeric price, got ${o}, ignoring.`), !1 ); const s = parseFloat(o.toString()).toFixed(2); if (null == n || isNaN(parseInt(n.toString()))) return ( b.error(`logPurchase requires an integer quantity, got ${n}, ignoring.`), !1 ); const u = parseInt(n.toString()); if (u < 1 || u > nr) return ( b.error( `logPurchase requires a quantity >1 and <${nr}, got ${u}, ignoring.`, ), !1 ); if (((i = null != i ? i.toUpperCase() : i), !or(i))) return ( b.error(`logPurchase requires a valid currencyCode, got ${i}, ignoring.`), !1 ); const [a, g] = nt( t, "logPurchase", "purchaseProperties", `log purchase "${e}"`, "purchase", ); if (!a) return !1; const c = r.l(); if (c && c.$r(e)) return b.info(`Purchase "${e}" is blocklisted, ignoring.`), !1; const l = v.Dt(p.Pr, { pid: e, c: i, p: s, q: u, pr: g }); if (l.lt) { b.info(`Logged ${u} purchase${u > 1 ? "s" : ""} of "${e}" for ${i} ${s}.`); for (const r of l.Ce) rt.o().Ee(ot.qr, [e, t], r); } return l.lt; }