Skip to content

Examples

Inspect a QRIS code

typescript
import { fromString } from '@heigoly/qris/parse'
import { formatDetailed } from '@heigoly/qris/format'

const qris = '00020101021126320012ID.GOPAY.WWW01120876543210985204541153033605802ID5916Toko Berkah Jaya6008Surabaya63043246'

const doc = fromString(qris)
console.log(formatDetailed(doc))

Output:

QRIS Static
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Merchant    Toko Berkah Jaya
City        Surabaya
Category    5411 · Grocery Stores and Supermarkets
Currency    IDR
Country     ID
Provider    DANA (tag 26)
CRC         C8C0

Charge a customer with fee

typescript
import { fromString } from '@heigoly/qris/parse'
import { charge } from '@heigoly/qris/upgrade'
import { encode } from '@heigoly/qris/encode'
import { format } from '@heigoly/qris/format'

const doc = fromString(inputQRIS)
const paid = charge(doc, 75000, { type: 'fixed', value: 2000 })
const output = encode(paid)

console.log(format(paid))
// "Toko Berkah Jaya | Rp75.000 +Rp2.000 | DANA"

Verify before processing

typescript
import { fromString } from '@heigoly/qris/parse'
import { isSecure } from '@heigoly/qris/verify'

function acceptPayment(qrisString: string): void {
  const doc = fromString(qrisString)
  if (!isSecure(doc)) {
    throw new Error('Invalid QRIS — possible data corruption')
  }
  // Proceed with payment...
}

Create a QRIS from scratch

typescript
import { create } from '@heigoly/qris/create'
import { encode } from '@heigoly/qris/encode'

const doc = create({
  merchantName: 'Toko Sejahtera',
  merchantCity: 'Bandung',
  merchantCategoryCode: '5411',
  postalCode: '40123',
  accounts: [
    { guid: 'ID.DANA.WWW', pan: '081234567890' },
    { guid: 'ID.GOPAY.WWW', merchantId: 'MERCH123' },
  ],
})

const qrisString = encode(doc)
// Ready to generate a QR code image

Pipeline with pipe

typescript
import { pipe } from '@heigoly/qris'
import { fromString } from '@heigoly/qris/parse'
import { charge } from '@heigoly/qris/upgrade'
import { encode } from '@heigoly/qris/encode'
import { isSecure } from '@heigoly/qris/verify'

const result = pipe(
  input,
  fromString as (arg: unknown) => unknown,
  (d: unknown) => charge(d as any, 50000),
  (d: unknown) => (isSecure(d as any) ? d : (() => { throw new Error('Invalid') })()),
  encode as (arg: unknown) => unknown,
) as string

Last updated: