28 lines
830 B
JavaScript
28 lines
830 B
JavaScript
import { randomBytes, createHmac, randomUUID } from 'crypto';
|
|
|
|
export function newId() {
|
|
return randomUUID().replace(/-/g, '');
|
|
}
|
|
|
|
export function genRefCode() {
|
|
// 6 uppercase alphanumeric chars — customer puts this in transfer description
|
|
return randomBytes(4).toString('base64url').slice(0, 6).toUpperCase();
|
|
}
|
|
|
|
export function normalizeRut(rut) {
|
|
if (!rut) return null;
|
|
// Strip dots, lowercase → '12345678-9' or '123456789'
|
|
return rut.replace(/\./g, '').toLowerCase();
|
|
}
|
|
|
|
export function hmacSign(secret, payload) {
|
|
return createHmac('sha256', secret).update(payload).digest('hex');
|
|
}
|
|
|
|
// Parse CLP amount from BCI (arrives as string like "1500" or "1500.00")
|
|
export function parsePesos(raw) {
|
|
if (raw == null) return null;
|
|
const n = Math.round(parseFloat(String(raw)));
|
|
return isNaN(n) ? null : n;
|
|
}
|