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"
FIELD_LIMITS =

EMVCo per-field maximum lengths (counted in bytes to match TLV encoding).

{
  company: 25,   # EMVCo §4.11 Merchant Name
  reference: 25, # EMVCo §4.14.1 Bill Number
  amount: 13     # EMVCo §4.7 Transaction Amount
}.freeze
UEN_REGEX =

Singapore ACRA UEN formats: business (9 chars), local company (10 chars), other entity (10 chars).

/\A(?:\d{8}[A-Z]|\d{9}[A-Z]|[STR]\d{2}[A-Z]{2}\d{4}[A-Z])\z/
SG_MOBILE_REGEX =

Singapore mobile in E.164 — +65 followed by 8 digits starting with 8 or 9.

/\A\+65[89]\d{7}\z/

Instance Method Summary collapse

Constructor Details

#initialize(proxy_type:, proxy_value:, amount: nil, editable: false, company: DEFAULT_COMPANY, expiry: nil, reference: nil, strict: false) ⇒ 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 bytes 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 (max 25 bytes)

  • strict (Boolean) (defaults to: false)

    if true, validate UEN and mobile proxy_value formats



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/paynow_qr/generator.rb', line 47

def initialize(proxy_type:, proxy_value:, amount: nil, editable: false,
               company: DEFAULT_COMPANY, expiry: nil, reference: nil, strict: false)
  @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   = normalize_reference(reference)
  @amount_str  = amount.nil? ? nil : format_amount(amount)
  @strict      = strict ? true : false

  validate_lengths!
  validate_strict! if @strict
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



64
65
66
67
68
# File 'lib/paynow_qr/generator.rb', line 64

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