diff --git a/src/utils/QrCodePix.ts b/src/utils/QrCodePix.ts index c1d46d6..e91962c 100644 --- a/src/utils/QrCodePix.ts +++ b/src/utils/QrCodePix.ts @@ -1,80 +1,87 @@ -import qrcode from 'qrcode'; -import type {QRCodeToDataURLOptions} from 'qrcode'; -import { crc16ccitt } from 'crc'; +import qrcode from "qrcode"; +import type { QRCodeToDataURLOptions } from "qrcode"; +import { crc16ccitt } from "crc"; interface PixParams { - pixKey: string; - merchantCity: string; - merchantName: string; - value?: number; - transactionId?: string; - message?: string; - cep?: string; - currency?: number; - countryCode?: string; + pixKey: string; + merchantCity: string; + merchantName: string; + value?: number; + transactionId?: string; + message?: string; + cep?: string; + currency?: number; + countryCode?: string; } const Pix = ({ - pixKey, - merchantCity, - merchantName, - value, - message, - cep, - transactionId = '', - currency = 986, - countryCode = 'BR', + pixKey, + merchantCity, + merchantName, + value, + message, + cep, + transactionId = "", + currency = 986, + countryCode = "BR", }: PixParams) => { - const payloadKeyString = generatePixKey(pixKey, message); + const payloadKeyString = generatePixKey(pixKey, message); - const payload: string[] = [ - formatEMV('00', '01'), //Payload Format Indicator - formatEMV('26', payloadKeyString), // Merchant Account Information - formatEMV('52', '0000'), //Merchant Category Code - formatEMV('53', String(currency)), // Transaction Currency - ]; + const payload: string[] = [ + formatEMV("00", "01"), //Payload Format Indicator + formatEMV("26", payloadKeyString), // Merchant Account Information + formatEMV("52", "0000"), //Merchant Category Code + formatEMV("53", String(currency)), // Transaction Currency + ]; - if (String(value) === '0') { - value = undefined; - } - if (value) { - payload.push(formatEMV('54', value.toFixed(2))); - } + if (String(value) === "0") { + value = undefined; + } + if (value) { + payload.push(formatEMV("54", value.toFixed(2))); + } - payload.push(formatEMV('58', countryCode.toUpperCase())); // Country Code - payload.push(formatEMV('59', merchantName)); // Merchant merchantName - payload.push(formatEMV('60', merchantCity)); // Merchant merchantCity + payload.push(formatEMV("58", countryCode.toUpperCase())); // Country Code + payload.push(formatEMV("59", merchantName)); // Merchant merchantName + payload.push(formatEMV("60", merchantCity)); // Merchant merchantCity - if (cep) { - payload.push(formatEMV('61', cep)); // Postal Code - } + if (cep) { + payload.push(formatEMV("61", cep)); // Postal Code + } - payload.push(formatEMV('62', formatEMV('05', transactionId))); // Additional Data Field Template + payload.push(formatEMV("62", formatEMV("05", transactionId))); // Additional Data Field Template - payload.push('6304'); + payload.push("6304"); - const stringPayload = payload.join(''); - const crcResult = crc16ccitt(stringPayload).toString(16).toUpperCase().padStart(4, '0'); + const stringPayload = payload.join(""); + const crcResult = crc16ccitt(stringPayload) + .toString(16) + .toUpperCase() + .padStart(4, "0"); - const payloadPIX = `${stringPayload}${crcResult}`; + const payloadPIX = `${stringPayload}${crcResult}`; - return { - payload: (): string => payloadPIX, - base64QrCode: (options?: QRCodeToDataURLOptions): Promise => qrcode.toDataURL(payloadPIX, options), - }; -} + return { + payload: (): string => payloadPIX, + base64QrCode: (options?: QRCodeToDataURLOptions): Promise => + qrcode.toDataURL(payloadPIX, options), + }; +}; const generatePixKey = (pixKey: string, message?: string): string => { - const payload: string[] = [formatEMV('00', 'BR.GOV.BCB.PIX'), formatEMV('01', pixKey)]; - if (message) { - payload.push(formatEMV('02', message)); - } - return payload.join(''); -} + const payload: string[] = [ + formatEMV("00", "BR.GOV.BCB.PIX"), + formatEMV("01", pixKey), + ]; + if (message) { + payload.push(formatEMV("02", message)); + } + return payload.join(""); +}; -const formatEMV = (id: string, param: string): string => { - const len = param.length.toString().padStart(2, '0'); - return `${id}${len}${param}`; -} +const formatEMV = (id: string, param: string): string => { + const len = param.length.toString().padStart(2, "0"); + return `${id}${len}${param}`; +}; -export {type PixParams, Pix }; \ No newline at end of file +export { type PixParams, Pix }; diff --git a/src/views/QrCodeForm.vue b/src/views/QrCodeForm.vue index a202727..7ff0571 100644 --- a/src/views/QrCodeForm.vue +++ b/src/views/QrCodeForm.vue @@ -12,31 +12,35 @@ const PixModel = ref({ qrCodePayload: "", }); -const qrCode = ref("") -const qrCodePayload = ref("") -const toggleModal= ref(false); +const qrCode = ref(""); +const qrCodePayload = ref(""); +const toggleModal = ref(false); const errors = ref({ pixRequiredError: false, nameRequiredError: false, - cityRequiredError: false + cityRequiredError: false, }); const submit = () => { - errors.value['pixRequiredError'] = PixModel.value['pixKey'] == '' ; - errors.value['nameRequiredError'] = PixModel.value['name'] == ''; - errors.value['cityRequiredError'] = PixModel.value['city'] == ''; + errors.value["pixRequiredError"] = PixModel.value["pixKey"] == ""; + errors.value["nameRequiredError"] = PixModel.value["name"] == ""; + errors.value["cityRequiredError"] = PixModel.value["city"] == ""; - if(errors.value['pixRequiredError'] || errors.value['nameRequiredError'] || errors.value['cityRequiredError']) + if ( + errors.value["pixRequiredError"] || + errors.value["nameRequiredError"] || + errors.value["cityRequiredError"] + ) return; const pix = Pix({ - pixKey: PixModel.value['pixKey'], - merchantName: PixModel.value['name'], - merchantCity: PixModel.value['city'], - transactionId: PixModel.value['transactionId'], - message: PixModel.value['message'], - value: PixModel.value['value'], + pixKey: PixModel.value["pixKey"], + merchantName: PixModel.value["name"], + merchantCity: PixModel.value["city"], + transactionId: PixModel.value["transactionId"], + message: PixModel.value["message"], + value: PixModel.value["value"], }); pix.base64QrCode().then((code) => { @@ -46,107 +50,168 @@ const submit = () => { qrCodePayload.value = pix.payload(); toggleModal.value = true; -} +}; \ No newline at end of file +