126 lines
3.7 KiB
JavaScript
126 lines
3.7 KiB
JavaScript
"use strict";
|
|
|
|
/**
|
|
* Column auto-detection + status classification for Stripe, Dodo Payments
|
|
* and Paddle CSV exports. Each provider names its columns differently; this
|
|
* module maps any of them onto one shared shape without needing the caller
|
|
* to say which provider the file came from.
|
|
*/
|
|
|
|
const COLUMN_PATTERNS = {
|
|
status: /^(status|payment.?status|invoice.?status|state)$/i,
|
|
amount: /^(amount|gross.?amount|total|invoice.?amount|converted.?amount)(\s*\(.*\))?$/i,
|
|
currency: /^(currency|converted.?currency)$/i,
|
|
declineReason: /^(decline.?reason|failure.?reason|failure.?message|seller.?message|error.?message)$/i,
|
|
customer: /^(customer|customer.?id|customer.?email|email|customer.?description)$/i,
|
|
created: /^(created|created.?\(utc\)|date|billed.?at|invoice.?date)$/i,
|
|
};
|
|
|
|
/** Status values that count as a *failed* payment event. */
|
|
const FAILED_STATUSES = new Set([
|
|
"failed",
|
|
"declined",
|
|
"decline",
|
|
"past_due",
|
|
"past due",
|
|
"incomplete",
|
|
"incomplete_expired",
|
|
"uncollectible",
|
|
"unpaid",
|
|
"canceled",
|
|
"cancelled",
|
|
"void",
|
|
"error",
|
|
]);
|
|
|
|
/** Status values that count as a *recovered / succeeded* payment event. */
|
|
const RECOVERED_STATUSES = new Set([
|
|
"succeeded",
|
|
"success",
|
|
"paid",
|
|
"recovered",
|
|
"captured",
|
|
"complete",
|
|
"completed",
|
|
]);
|
|
|
|
/**
|
|
* @param {string[]} headers
|
|
* @returns {Record<string, string|undefined>} maps shared field name -> actual header text, if found
|
|
*/
|
|
function detectColumns(headers) {
|
|
/** @type {Record<string, string|undefined>} */
|
|
const map = {};
|
|
for (const [field, pattern] of Object.entries(COLUMN_PATTERNS)) {
|
|
map[field] = headers.find((h) => pattern.test(h.trim()));
|
|
}
|
|
return map;
|
|
}
|
|
|
|
/**
|
|
* @param {string} rawStatus
|
|
* @returns {"failed"|"recovered"|"unknown"}
|
|
*/
|
|
function classifyStatus(rawStatus) {
|
|
const s = (rawStatus || "").trim().toLowerCase();
|
|
if (FAILED_STATUSES.has(s)) return "failed";
|
|
if (RECOVERED_STATUSES.has(s)) return "recovered";
|
|
return "unknown";
|
|
}
|
|
|
|
/**
|
|
* Parses a money string ("19.99", "$19.99", "1999" cents-style Stripe export,
|
|
* "19,99" EU decimal comma) into a plain float. Returns 0 for anything
|
|
* unparseable rather than throwing, since a malformed single row should
|
|
* never crash the whole report.
|
|
* @param {string} raw
|
|
* @returns {number}
|
|
*/
|
|
function parseAmount(raw) {
|
|
if (!raw) return 0;
|
|
let cleaned = String(raw).replace(/[^0-9.,-]/g, "");
|
|
if (cleaned === "") return 0;
|
|
|
|
// Normalize "1.234,56" (EU) -> "1234.56"; leave "1,234.56" (US) alone.
|
|
if (/,\d{1,2}$/.test(cleaned) && !/\.\d{1,2}$/.test(cleaned)) {
|
|
cleaned = cleaned.replace(/\./g, "").replace(",", ".");
|
|
} else {
|
|
cleaned = cleaned.replace(/,/g, "");
|
|
}
|
|
|
|
const value = parseFloat(cleaned);
|
|
return Number.isFinite(value) ? value : 0;
|
|
}
|
|
|
|
/**
|
|
* Normalizes raw CSV records into a shared event shape:
|
|
* { customer, status, amount, currency, declineReason, created }
|
|
* @param {Record<string,string>[]} records
|
|
* @param {string[]} headers
|
|
*/
|
|
function normalizeRecords(records, headers) {
|
|
const columns = detectColumns(headers);
|
|
return records.map((record) => {
|
|
const statusRaw = columns.status ? record[columns.status] : "";
|
|
return {
|
|
customer: columns.customer ? (record[columns.customer] || "").trim() : "",
|
|
statusRaw: statusRaw,
|
|
status: classifyStatus(statusRaw),
|
|
amount: columns.amount ? parseAmount(record[columns.amount]) : 0,
|
|
currency: columns.currency ? (record[columns.currency] || "").trim().toUpperCase() : "",
|
|
declineReason: columns.declineReason ? (record[columns.declineReason] || "").trim() : "",
|
|
created: columns.created ? (record[columns.created] || "").trim() : "",
|
|
};
|
|
});
|
|
}
|
|
|
|
module.exports = {
|
|
detectColumns,
|
|
classifyStatus,
|
|
parseAmount,
|
|
normalizeRecords,
|
|
COLUMN_PATTERNS,
|
|
FAILED_STATUSES,
|
|
RECOVERED_STATUSES,
|
|
};
|