UNPKG

api-beep-onboarding

Version:

Oracle OCI FaaS for Api Beep Onboarding library

88 lines (67 loc) 2.23 kB
/** * * BeePay 2.023-2.024 - Oracle OCI FaaS for Api Beep Onboarding * Merchant ID. and Merchant AMEX generator with Luhn Algorithm * * Changes: * jlugo: 2023-dic-15. File creation * jlugo: 2024-apr-25. Moved to a library */ /* RGVzYXJyb2xsYWRvIHBvciBKb25hdGhhbiBMdWdv */ import luhn from "luhn-generator"; /** * Merchant ID. and Merchant AMEX generator with Luhn Algorithm */ export class Generator { static AmexRange = 219; static GenType = { Merchant: 8, Amex: 5 }; /** * * @param {String} bankCode - Financial institution bank code (2-digits). e.g.: 51 * @param {*} amexCode - AMEX bank ID. e.g.: 7 */ constructor(bankCode, amexCode) { this.bankCode = bankCode?.length > 2 ? bankCode.slice(-2) : bankCode; this.amexCode = amexCode; } /** * Generate a valid Merchant ID. with Luhn Algorithm * * @returns {String} */ genMerchantId() { let id = ""; do { let tmp = `${this.bankCode}${luhn.random(Generator.GenType.Merchant)}`; let checksum = luhn.checksum(tmp); id = tmp + checksum; } while (!luhn.validate(id)) this.merchantId = id; return this.merchantId; } /** * Generate a valid Merchant ID for AMEX with Luhn Algorithm * * @param {String} amexCode - AMEX code * @returns {String} */ genMerchantAmex() { let id = ""; const rangeStart = 21920000; // Inicio del rango const rangeEnd = 21926999; // Fin del rango do { // Genera los primeros 9 dígitos dentro del rango const randomBase = Math.floor(Math.random() * (rangeEnd - rangeStart + 1)) + rangeStart; // Calcula el checksum usando Luhn const checksum = luhn.checksum(randomBase.toString()); // Combina el número base con el checksum id = randomBase.toString() + checksum; } while (!luhn.validate(id)); this.merchantAmexId = `${this.amexCode}${id}`; return this.merchantAmexId; } } export default Generator;