PromptParse

repository·main·Indexed 41 days ago

https://github.com/maythiwat/promptparse

A cross-platform, dependency-free JavaScript library for parsing, generating, and manipulating PromptPay and EMVCo QR Code data. Version 1.7.0 supports Node.js, Deno, Bun, and browsers. It provides utilities for generating PromptPay AnyID (Tag 29) and Bill Payment (Tag 30) QR codes, TrueMoney Wallet payloads, BOT Barcodes, and Slip Verify (Mini QR) validation and generation.

Tokens
6.1K
Snippets
32
Records
41
Agent score
53%

What's inside PromptParse

  1. Use PromptParse via CDN in the browser

    main

    You can use PromptParse directly in a web browser by including the library via a CDN script tag. The library is exposed under the global promptparse object. For example, you can access promptparse.generate to create payloads for various services like TrueMoney.

    <script src="https://cdn.jsdelivr.net/npm/promptparse"></script>
    
    <script>
      ;(function () {
        // Generate QR code payload (use function from "promptparse" global)
        const payload = promptparse.generate.trueMoney({
          mobileNo: '08xxxxxxxx',
          amount: 10.0,
          message: 'Hello World!',
        })
    
        // Quick & dirty way to show QR Code image
        document.write(
          `<img src="https://api.qrserver.com/v1/create-qr-code/?size=300x300&data=${payload}">`,
        )
      })()
    </script>
  2. Generate PromptPay Bill Payment QR

    main

    Use the billPayment function from the promptparse/generate sub-module to create a payload for PromptPay Bill Payment QR codes. This is useful for generating data that can then be converted into a visual QR code image.

    import { billPayment } from 'promptparse/generate'
    
    const payload = billPayment({
      billerId: '1xxxxxxxxxxxx',
      amount: 300.0,
      ref1: 'INV12345',
    })
    
    // TODO: Create QR Code from payload
  3. Parse PromptPay & EMVCo QR Code data

    main

    Use the parse function to convert a raw QR Code data string into a parseable object. Once parsed, you can retrieve specific values using the getTagValue(tagId) method, where tagId is the string identifier for the tag (e.g., '00').

    import { parse } from 'promptparse'
    
    // Example data
    const ppqr = parse('000201010211...')
    
    // Get Value of Tag ID '00'
    ppqr.getTagValue('00') // Returns '01'
  4. Convert BOT Barcode to PromptPay QR Tag 30

    main

    Use parseBarcode to parse a Bank of Thailand (BOT) standard barcode string. If the barcode is valid, you can call the .toQrTag30() method on the resulting object to convert the barcode data into a PromptPay QR Tag 30 (Bill Payment) payload.

    import { parseBarcode } from 'promptparse'
    
    const botBarcode = parseBarcode('|310109999999901\r...')
    
    if (!botBarcode) {
      console.error('Invalid Payload')
    }
    
    const payload = botBarcode.toQrTag30()
    
    // TODO: Create QR Code from payload
  5. Validate and extract data from Slip Verify QR

    main

    Use the slipVerify function from promptparse/validate to validate the checksum and structure of a Slip Verify (Mini QR) payload. If the payload is valid, it returns an object containing transaction details like sendingBank and transRef. If invalid, it returns null or undefined.

    import { slipVerify } from 'promptparse/validate'
    
    const data = slipVerify('00550006000001...')
    
    if (!data) {
      console.error('Invalid Payload')
    }
    
    const { sendingBank, transRef } = data
    
    // TODO: Inquiry transaction from Bank Open API
  6. Build QR data and append CRC tag

    main

    To construct a raw QR data string from scratch, use tag(id, value) to create individual tag segments and encode(data) to combine them. To ensure the string is valid for scanners, use withCrcTag(encodedData, crcTagId) to append the checksum (typically using tag ID '63').

    import { encode, tag, withCrcTag } from 'promptparse'
    
    // Example data
    const data = [
      tag('00', '01'),
      tag('01', '11'),
      // ...
    ]
    
    // Set CRC Tag ID '63'
    withCrcTag(encode(data), '63') // Returns '000201010211...'
  7. Encode TLVTag objects into a TLV string

    main

    Use the encode function to convert an array of TLVTag objects back into a single TLV-encoded string. This function supports nested tags via the subTags property.

    import { encode, tag } from '@/lib/tlv';
    
    const tags = [
      tag('01', 'VAL1'),
      tag('02', 'VAL2')
    ];
    const payload = encode(tags);
    // Result: '0104VAL10204VAL2'
  8. Parse BOT Barcode data with parseBarcode()

    main

    Use the parseBarcode function to parse barcode data strings that follow the BOT (Bank of Thailand) Barcode Standard. It returns a BOTBarcode instance.

    Parameters:

    • payload (string): The raw barcode data string from the scanner.
    import { parseBarcode } from 'promptparse';
    
    const barcode = parseBarcode('01021234567890123456');
  9. Append a CRC tag to a TLV payload

    main

    The withCrcTag function takes an existing TLV payload and appends a CRC tag. It calculates the checksum of the payload (including the new CRC tag ID and length) and appends the resulting checksum value.

    Parameters:

    • payload: The original TLV string.
    • crcTagId: The 2-character ID to use for the CRC tag.
    • upperCase: Whether to return the checksum in uppercase (default: true).
    import { withCrcTag } from '@/lib/tlv';
    
    const payload = '0105HELLO';
    const securePayload = withCrcTag(payload, '99');
    // Result: '0105HELLO9904XXXX' where XXXX is the checksum
  10. Parse EMVCo-compatible QR Code data with parse()

    main

    Use the parse function to decode EMVCo-compatible QR code payload strings. It returns an EMVCoQR instance containing the parsed TLV (Tag-Length-Value) tags.

    Parameters:

    • payload (string): The raw QR code data string obtained from a scanner.
    • strict (boolean, default: false): If set to true, the function validates the 4-character CRC checksum at the end of the payload before proceeding with parsing. If the checksum is invalid, it returns null.
    • subTags (boolean, default: true): If set to true, the parser attempts to recursively decode TLV sub-tags found within tag values that follow the EMVCo format (starting with 4 digits).

    Returns:

    • An EMVCoQR instance if parsing is successful.
    • null if the payload does not match the expected format, if strict validation fails, or if no tags are decoded.
    import { parse } from 'promptparse';
    
    // Basic parsing
    const qr = parse('01021234567890123456');
    
    // Strict parsing with CRC validation
    const strictQr = parse('01021234567890123456', true);
    
    // Parsing without sub-tag recursion
    const simpleQr = parse('01021234567890123456', false, false);
  11. Generate a CRC16 checksum

    main

    The checksum function generates a CRC16-XMODEM checksum for a given string. The result is a 4-character hex string. You can control whether the output is uppercase or lowercase using the upperCase option (defaults to true).

    import { checksum } from '@/lib/tlv';
    
    const crc = checksum('some-payload');
    // Returns 4-character hex string, e.g., 'A1B2'