UNPKG

modify-qris

Version:

Modify QRIS Generator is a javascript based libraries that enabling flexible payments with customizable amounts for improved efficiency and user experience

125 lines (124 loc) 5.05 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.QRISReader = void 0; const canvas_1 = require("canvas"); const jsqr_1 = require("jsqr"); const fs = require("fs"); class QRISReader { /** * Read QR Code from image file * @param imagePath Path to the image file * @returns Promise<string> QR Code content */ static readFromFile(imagePath) { return __awaiter(this, void 0, void 0, function* () { try { // Check if file exists if (!fs.existsSync(imagePath)) { throw new Error(`File not found: ${imagePath}`); } // Check if file is readable try { fs.accessSync(imagePath, fs.constants.R_OK); } catch (error) { throw new Error(`File is not readable: ${imagePath}`); } // Load image const image = yield (0, canvas_1.loadImage)(imagePath); // Create canvas const canvas = (0, canvas_1.createCanvas)(image.width, image.height); const ctx = canvas.getContext('2d'); // Draw image to canvas ctx.drawImage(image, 0, 0); // Get image data const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height); // Decode QR Code const code = (0, jsqr_1.default)(imageData.data, imageData.width, imageData.height); if (!code) { throw new Error('No QR Code found in image'); } return code.data; } catch (error) { throw new Error(`Failed to read QR Code: ${error.message}`); } }); } /** * Read QR Code from base64 image * @param base64Image Base64 encoded image string * @returns Promise<string> QR Code content */ static readFromBase64(base64Image) { return __awaiter(this, void 0, void 0, function* () { try { // Remove data URL prefix if present const base64Data = base64Image.replace(/^data:image\/\w+;base64,/, ''); // Validate base64 string if (!/^[A-Za-z0-9+/]*={0,2}$/.test(base64Data)) { throw new Error('Invalid base64 string'); } // Create buffer from base64 const buffer = Buffer.from(base64Data, 'base64'); // Load image from buffer const image = yield (0, canvas_1.loadImage)(buffer); // Create canvas const canvas = (0, canvas_1.createCanvas)(image.width, image.height); const ctx = canvas.getContext('2d'); // Draw image to canvas ctx.drawImage(image, 0, 0); // Get image data const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height); // Decode QR Code const code = (0, jsqr_1.default)(imageData.data, imageData.width, imageData.height); if (!code) { throw new Error('No QR Code found in image'); } return code.data; } catch (error) { throw new Error(`Failed to read QR Code: ${error.message}`); } }); } /** * Validate if the content is a valid QRIS * @param content QR Code content * @returns boolean */ static isValidQRIS(content) { // Basic QRIS validation // Check if it starts with 000201 if (!content.startsWith('000201')) { return false; } // Check if it contains required fields const requiredFields = ['26', '52', '53', '54', '58', '59', '60', '61']; for (const field of requiredFields) { if (!content.includes(field)) { return false; } } return true; } /** * Ensure directory exists * @param dirPath Directory path */ static ensureDirectoryExists(dirPath) { if (!fs.existsSync(dirPath)) { fs.mkdirSync(dirPath, { recursive: true }); } } } exports.QRISReader = QRISReader;