Class: PaynowQR::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/paynow_qr/generator.rb

Overview

Builds an EMVCo-compliant PayNow (SGQR) payload string.

Examples:

Static UEN QR with fixed amount

PaynowQR::Generator.new(
  proxy_type: :uen,
  proxy_value: "201234567A",
  amount: 25.00,
  company: "ACME PTE LTD",
  reference: "INV-001"
).payload

Constant Summary collapse

PROXY_TYPES =
{ mobile: "0", uen: "2" }.freeze
DEFAULT_COMPANY =
"NA"
CURRENCY_SGD =
"702"
COUNTRY_SG =
"SG"
CITY_SINGAPORE =
"Singapore"
MCC_UNUSED =
"0000"

Instance Method Summary collapse

Constructor Details

#initialize(proxy_type:, proxy_value:, amount: nil, editable: false, company: DEFAULT_COMPANY, expiry: nil, reference: nil) ⇒ Generator

Returns a new instance of Generator.

Parameters:

  • proxy_type (Symbol, String)

    :mobile, :uen, or raw numeric code

  • proxy_value (String)

    mobile number (e.g. “+6591234567”) or UEN

  • amount (Numeric, String, nil) (defaults to: nil)

    transaction amount in SGD. nil = any amount.

  • editable (Boolean) (defaults to: false)

    if true, amount field is omitted and QR is dynamic

  • company (String) (defaults to: DEFAULT_COMPANY)

    merchant name (max 25 chars per EMVCo)

  • expiry (Date, Time, String, nil) (defaults to: nil)

    “YYYYMMDD” or Date-like. Default: today + 1 year.

  • reference (String, nil) (defaults to: nil)

    bill reference number



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/paynow_qr/generator.rb', line 32

def initialize(proxy_type:, proxy_value:, amount: nil, editable: false,
               company: DEFAULT_COMPANY, expiry: nil, reference: nil)
  @proxy_type  = resolve_proxy_type(proxy_type)
  @proxy_value = require_present(proxy_value, :proxy_value).strip
  @amount      = amount
  @editable    = editable ? true : false
  @company     = (company.to_s.strip.empty? ? DEFAULT_COMPANY : company.to_s.strip)
  @expiry      = normalize_expiry(expiry)
  @reference   = reference&.to_s&.strip
  @reference = nil if @reference && @reference.empty?
end

Instance Method Details

#payloadString Also known as: to_s

Returns complete EMVCo QR payload with CRC.

Returns:

  • (String)

    complete EMVCo QR payload with CRC



45
46
47
48
49
# File 'lib/paynow_qr/generator.rb', line 45

def payload
  content = build_content
  crc = format("%04X", CRC16.compute("#{content}6304"))
  "#{content}6304#{crc}"
end