Class: QrcodePixRuby::Payload

Inherits:
Object
  • Object
show all
Defined in:
lib/qrcode_pix_ruby/payload.rb

Constant Summary collapse

ID_PAYLOAD_FORMAT_INDICATOR =
'00'
ID_POINT_OF_INITIATION_METHOD =
'01'
ID_MERCHANT_ACCOUNT_INFORMATION =
'26'
ID_MERCHANT_ACCOUNT_INFORMATION_GUI =
'00'
ID_MERCHANT_ACCOUNT_INFORMATION_KEY =
'01'
ID_MERCHANT_ACCOUNT_INFORMATION_DESCRIPTION =
'02'
ID_MERCHANT_ACCOUNT_INFORMATION_URL =
'25'
ID_MERCHANT_CATEGORY_CODE =
'52'
ID_TRANSACTION_CURRENCY =
'53'
ID_TRANSACTION_AMOUNT =
'54'
ID_COUNTRY_CODE =
'58'
ID_MERCHANT_NAME =
'59'
ID_MERCHANT_CITY =
'60'
ID_POSTAL_CODE =
'61'
ID_ADDITIONAL_DATA_FIELD_TEMPLATE =
'62'
ID_ADDITIONAL_DATA_FIELD_TEMPLATE_TXID =
'05'
ID_CRC16 =
'63'
ATTRIBUTES =
%i[pix_key url repeatable currency country_code description postal_code
merchant_name merchant_city transaction_id amount].freeze

Instance Method Summary collapse

Constructor Details

#initialize(**kwargs) ⇒ Payload

Returns a new instance of Payload.



30
31
32
33
34
35
36
37
# File 'lib/qrcode_pix_ruby/payload.rb', line 30

def initialize(**kwargs)
  verify_kwargs(kwargs.keys)

  ATTRIBUTES.each do |attribute|
    singleton_class.class_eval { attr_accessor attribute }
    instance_variable_set("@#{attribute}", kwargs[attribute])
  end
end

Instance Method Details

#base64Object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/qrcode_pix_ruby/payload.rb', line 57

def base64
  ::RQRCode::QRCode.new(payload).as_png(
    bit_depth: 1,
    border_modules: 0,
    color_mode: 0,
    color: 'black',
    file: nil,
    fill: 'white',
    module_px_size: 6,
    resize_exactly_to: false,
    resize_gte_to: false
  ).to_data_url
end

#payloadObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/qrcode_pix_ruby/payload.rb', line 39

def payload
  p = ''

  p += emv(ID_PAYLOAD_FORMAT_INDICATOR, '01')
  p += emv_repeatable
  p += emv_merchant
  p += emv(ID_MERCHANT_CATEGORY_CODE, '0000')
  p += emv(ID_TRANSACTION_CURRENCY, currency || '986')
  p += emv(ID_TRANSACTION_AMOUNT, amount) if amount
  p += emv(ID_COUNTRY_CODE, country_code || 'BR')
  p += emv(ID_MERCHANT_NAME, merchant_name)
  p += emv(ID_MERCHANT_CITY, merchant_city)
  p += emv(ID_POSTAL_CODE, postal_code) if postal_code
  p += emv_additional_data

  p + crc16(p)
end