UNPKG

@pix.js/qrcode

Version:

A set of utilities for working with Pix QRCode

1 lines 11.9 kB
{"version":3,"sources":["../src/index.ts","../src/dynamic.ts","../src/static.ts","../src/types.ts"],"sourcesContent":["export * from './dynamic'\nexport * from './static'\n\nexport * from '@pix.js/core'\nexport * from './types'\n","import { PixQrCodeType, PointOfInitiationMethod, dynamicPixSchema, toDynamicPix } from '@pix.js/core'\nimport { toDataURL } from 'qrcode'\nimport { DynamicQrCode, DynamicQrCodeParams, GetPayloadParams, InstantPayload, ScheduledPayload } from './types'\n\n/**\n * Creates a dynamic PIX QR code\n * @param params The parameters for the dynamic PIX QR code\n * @returns The dynamic PIX QR code object\n */\nexport const createDynamic = (params: DynamicQrCodeParams): DynamicQrCode => {\n const payloadWithDefaultValues = {\n merchantAccountInfo: {\n gui: params.merchantAccountInfo.gui ?? 'br.gov.bcb.pix',\n url: params.merchantAccountInfo.url,\n merchantAdditionalInfo: params.merchantAccountInfo.merchantAdditionalInfo,\n fss: params.merchantAccountInfo.fss,\n },\n pointOfInitiationMethod: params.pointOfInitiationMethod ?? PointOfInitiationMethod.OnTimeOnly,\n merchantCategoryCode: params.merchantCategoryCode ?? '0000',\n transactionCurrency: params.transactionCurrency ?? '986',\n countryCode: params.countryCode ?? 'BR',\n merchantName: params.merchantName ?? '',\n merchantCity: params.merchantCity ?? '',\n additionalData: {\n txId: params.additionalData?.txId ?? '***',\n },\n value: params.value,\n postalCode: params.postalCode,\n unreservedTemplate: {\n gui: params.unreservedTemplate?.gui,\n url: params.unreservedTemplate?.url,\n },\n }\n\n const parsed = dynamicPixSchema.parse(payloadWithDefaultValues)\n const brcode = toDynamicPix(parsed)\n\n return {\n brcode,\n toBase64: () => toDataURL(brcode),\n toDataUrl: () => toDataURL(brcode),\n type: PixQrCodeType.Dynamic,\n getPayload: async (getPayloadParams?: GetPayloadParams) => {\n const url = new URL(`https://${params.merchantAccountInfo.url}`)\n if (getPayloadParams?.dpp) url.searchParams.append('dpp', getPayloadParams.dpp)\n if (getPayloadParams?.codMun) url.searchParams.append('codMun', getPayloadParams.codMun)\n\n const res = await fetch(url.toString())\n const jwt = await res.text()\n const parts = jwt.split('.').map((b64) => Buffer.from(b64, 'base64').toString('utf-8'))\n const header = JSON.parse(parts[0])\n const payload = JSON.parse(parts[1])\n\n return { payload, header } as {\n payload: InstantPayload | ScheduledPayload\n header: Record<string, unknown>\n }\n },\n }\n}\n","import { PixQrCodeType, getPixKeyType, staticPixSchema, toStaticPix } from '@pix.js/core'\nimport { toDataURL } from 'qrcode'\n\nimport { StaticQrCodeParams } from './types'\n\n/**\n * Creates a static PIX QR code\n * @param params The parameters for the static PIX QR code\n * @returns The static PIX QR code object\n */\nexport const createStatic = (params: StaticQrCodeParams) => {\n const payloadWithDefaultValues = {\n merchantAccountInfo: {\n gui: params.merchantAccountInfo.gui ?? 'br.gov.bcb.pix',\n key: params.merchantAccountInfo.key,\n merchantAdditionalInfo: params.merchantAccountInfo.merchantAdditionalInfo,\n fss: params.merchantAccountInfo.fss,\n },\n merchantCategoryCode: params.merchantCategoryCode ?? '0000',\n transactionCurrency: params.transactionCurrency ?? '986',\n countryCode: params.countryCode ?? 'BR',\n merchantName: params.merchantName ?? '',\n merchantCity: params.merchantCity ?? '',\n additionalData: {\n txId: params.additionalData?.txId ?? '***',\n },\n value: params.value,\n postalCode: params.postalCode ?? '',\n unreservedTemplate: {\n gui: params.unreservedTemplate?.gui,\n url: params.unreservedTemplate?.url,\n },\n }\n\n const parsed = staticPixSchema.parse(payloadWithDefaultValues)\n const brcode = toStaticPix(parsed)\n\n return {\n brcode,\n toBase64: toDataURL(brcode),\n type: PixQrCodeType.Static,\n keyType: getPixKeyType(params.merchantAccountInfo.key),\n }\n}\n","import { PixKeyType, PixQrCodeType, PointOfInitiationMethod } from '@pix.js/core'\n\nexport type StaticQrCodeParams = {\n merchantAccountInfo: {\n /** Global Unique Identifier defaults to br.gov.bcb.pix */\n gui?: string\n /** Pix key used to receive the payment */\n key: string\n /** A message that will be displayed on the payment screen */\n merchantAdditionalInfo?: string\n /** ISPB of the financial institution */\n fss?: string\n }\n /**\n * Value of the payment, defaults to 0\n * When not provided, use can edit the value on the payment screen\n */\n value?: number\n /** Merchant Category Code defaults to 0000 */\n merchantCategoryCode?: string\n /** Transaction Currency defaults to 986 */\n transactionCurrency?: string\n /** Country Code defaults to BR */\n countryCode?: string\n /** Merchant Name */\n merchantName: string\n /** Merchant City */\n merchantCity: string\n additionalData?: {\n /** Transaction ID used to identify the payment, defaults to *** */\n txId: string\n }\n /** Postal Code */\n postalCode?: string\n unreservedTemplate?: {\n gui?: string\n url?: string\n }\n}\n\nexport type DynamicQrCodeParams = {\n merchantAccountInfo: {\n /** Global Unique Identifier defaults to br.gov.bcb.pix */\n gui?: string\n /** URL used to get data about the payment */\n url: string\n /** A message that will be displayed on the payment screen */\n merchantAdditionalInfo?: string\n /** ISPB of the financial institution */\n fss?: string\n }\n /**\n * Point of Initiation Method, when not provided this qrcode can be used multiple times\n * Defaults to OnTimeOnly\n */\n pointOfInitiationMethod?: PointOfInitiationMethod\n /**\n * Value of the payment\n */\n value?: number\n /**\n * Merchant Category Code\n * @default 0000\n */\n merchantCategoryCode?: string\n /** Transaction Currency\n * @default 986\n */\n transactionCurrency?: string\n /** Country Code defaults to BR\n * @default BR\n */\n countryCode?: string\n /** Merchant Name */\n merchantName: string\n /** Merchant City */\n merchantCity: string\n additionalData: {\n /** Transaction ID used to identify the payment\n * @default ***\n */\n txId: string\n }\n /** Postal Code */\n postalCode?: string\n unreservedTemplate?: {\n gui?: string\n url?: string\n }\n}\n\nexport type GetPayloadParams = {\n /**\n * Expected date of payment\n */\n dpp?: string\n /**\n * City code\n */\n codMun?: string\n}\n\nexport type ParseOptions = {\n /**\n * If true, the qrcode will be validated and error will be thrown if it's invalid\n * @default true\n */\n validate?: boolean\n}\n\nexport enum DynamicStatus {\n Ativa = 'ATIVA',\n Concluida = 'CONCLUIDA',\n RemovidaPeloUsuarioRecebedor = 'REMOVIDA_PELO_USUARIO_RECEBEDOR',\n RemovidaPeloPsp = 'REMOVIDA_PELO_PSP',\n}\n\ntype InfoAdicional = {\n readonly nome: string\n readonly valor: string\n}\n\nexport type ScheduledPayload = {\n readonly revisao: number\n readonly calendario: {\n readonly criacao: string\n readonly apresentacao: string\n readonly dataDeVencimento?: string\n readonly validadeAposVencimento?: number\n }\n readonly devedor?: {\n readonly cpf?: string\n readonly cnpj?: string\n readonly nome?: string\n }\n readonly recebedor?: {\n readonly cpf?: string\n readonly cnpj?: string\n readonly nome: string\n readonly logradouro: string\n readonly cidade: string\n readonly utf: string\n readonly cep: string\n }\n readonly valor: {\n readonly original?: string\n readonly multa?: string\n readonly juros?: string\n readonly abatimento?: string\n readonly desconto?: string\n readonly final: string\n }\n readonly chave: string\n readonly txid: string\n readonly solicitacaoPagador?: string\n readonly infoAdicionais: readonly InfoAdicional[]\n readonly status: DynamicStatus\n}\n\nexport type InstantPayload = {\n readonly revisao: number\n readonly calendario: {\n readonly criacao: string\n readonly apresentacao: string\n readonly expiracao: number\n }\n readonly devedor?: {\n readonly cpf?: string\n readonly cnpj?: string\n readonly nome: string\n }\n readonly valor: {\n readonly original: string\n readonly modalidadeAlteracao?: 0 | 1\n }\n readonly chave: string\n readonly txid: string\n readonly solicitacaoPagador?: string\n readonly infoAdicionais?: readonly InfoAdicional[]\n readonly status: DynamicStatus\n}\n\nexport type DynamicQrCode = {\n /**\n * The QRCode as a base64 string\n */\n toBase64: () => Promise<string>\n /**\n * The QRCode as a data URL\n */\n toDataUrl: () => Promise<string>\n /**\n * The type of the QRCode\n */\n type: PixQrCodeType\n\n /**\n * The brcode of the QRCode\n */\n brcode: string\n /**\n * The payload of the QRCode\n */\n getPayload: () => Promise<{\n payload: InstantPayload | ScheduledPayload\n header: Record<string, unknown>\n }>\n}\n\nexport type StaticQrCode = {\n /**\n * The QRCode as a base64 string\n */\n toBase64: () => Promise<string>\n /**\n * The QRCode as a data URL\n */\n toDataUrl: () => Promise<string>\n /**\n * The type of the QR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