Class: Croatia::PaymentBarcode

Inherits:
Object
  • Object
show all
Defined in:
lib/croatia/payment_barcode.rb

Constant Summary collapse

HEADER =
"HRVHUB30"
EXACT_LENGTH_FIELDS =
%i[ header currency total_cents model payment_purpose_code ].freeze
FIELD_MAX_LENGTHS =
{
  header: 8,
  currency: 3,
  total_cents: 15,
  buyer_name: 30,
  buyer_address: 27,
  buyer_postal_code_and_city: 27,
  seller_name: 25,
  seller_address: 25,
  seller_postal_code_and_city: 27,
  seller_iban: 21,
  model: 4,
  reference_number: 22,
  payment_purpose_code: 4,
  description: 35
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ PaymentBarcode

Returns a new instance of PaymentBarcode.



40
41
42
43
44
# File 'lib/croatia/payment_barcode.rb', line 40

def initialize(**options)
  options.each do |key, value|
    public_send("#{key}=", value)
  end
end

Instance Attribute Details

#buyer_addressObject

Returns the value of attribute buyer_address.



23
24
25
# File 'lib/croatia/payment_barcode.rb', line 23

def buyer_address
  @buyer_address
end

#buyer_cityObject

Returns the value of attribute buyer_city.



23
24
25
# File 'lib/croatia/payment_barcode.rb', line 23

def buyer_city
  @buyer_city
end

#buyer_nameObject

Returns the value of attribute buyer_name.



23
24
25
# File 'lib/croatia/payment_barcode.rb', line 23

def buyer_name
  @buyer_name
end

#buyer_postal_codeObject

Returns the value of attribute buyer_postal_code.



23
24
25
# File 'lib/croatia/payment_barcode.rb', line 23

def buyer_postal_code
  @buyer_postal_code
end

#currencyObject

Returns the value of attribute currency.



23
24
25
# File 'lib/croatia/payment_barcode.rb', line 23

def currency
  @currency
end

#descriptionObject

Returns the value of attribute description.



23
24
25
# File 'lib/croatia/payment_barcode.rb', line 23

def description
  @description
end

#modelObject

Returns the value of attribute model.



23
24
25
# File 'lib/croatia/payment_barcode.rb', line 23

def model
  @model
end

#payment_purpose_codeObject

Returns the value of attribute payment_purpose_code.



23
24
25
# File 'lib/croatia/payment_barcode.rb', line 23

def payment_purpose_code
  @payment_purpose_code
end

#reference_numberObject

Returns the value of attribute reference_number.



23
24
25
# File 'lib/croatia/payment_barcode.rb', line 23

def reference_number
  @reference_number
end

#seller_addressObject

Returns the value of attribute seller_address.



23
24
25
# File 'lib/croatia/payment_barcode.rb', line 23

def seller_address
  @seller_address
end

#seller_cityObject

Returns the value of attribute seller_city.



23
24
25
# File 'lib/croatia/payment_barcode.rb', line 23

def seller_city
  @seller_city
end

#seller_ibanObject

Returns the value of attribute seller_iban.



23
24
25
# File 'lib/croatia/payment_barcode.rb', line 23

def seller_iban
  @seller_iban
end

#seller_nameObject

Returns the value of attribute seller_name.



23
24
25
# File 'lib/croatia/payment_barcode.rb', line 23

def seller_name
  @seller_name
end

#seller_postal_codeObject

Returns the value of attribute seller_postal_code.



23
24
25
# File 'lib/croatia/payment_barcode.rb', line 23

def seller_postal_code
  @seller_postal_code
end

#total_centsObject

Returns the value of attribute total_cents.



23
24
25
# File 'lib/croatia/payment_barcode.rb', line 23

def total_cents
  @total_cents
end

Instance Method Details

#barcodeObject



83
84
85
86
# File 'lib/croatia/payment_barcode.rb', line 83

def barcode
  Croatia::PDF417.ensure_supported!
  Croatia::PDF417.new(data)
end

#dataObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/croatia/payment_barcode.rb', line 46

def data
  data = {
    header: HEADER,
    currency: currency,
    total_cents: total_cents.to_s.rjust(FIELD_MAX_LENGTHS[:total_cents], "0"),
    buyer_name: buyer_name,
    buyer_address: buyer_address,
    buyer_postal_code_and_city: "#{buyer_postal_code} #{buyer_city}".strip,
    seller_name: seller_name,
    seller_address: seller_address,
    seller_postal_code_and_city: "#{seller_postal_code} #{seller_city}".strip,
    seller_iban: seller_iban,
    model: model,
    reference_number: reference_number,
    payment_purpose_code: payment_purpose_code,
    description: description
  }

  data.each do |key, value|
    next if value.nil?

    max_length = FIELD_MAX_LENGTHS[key]

    if EXACT_LENGTH_FIELDS.include?(key) && value.length != max_length
      raise ArgumentError, "Value '#{value}' of field '#{key}' must be exactly #{max_length} characters long"
    elsif value.length > max_length
      raise ArgumentError, "Value '#{value}' of field '#{key}' exceeds maximum length of #{max_length} characters"
    end
  end

  if data[:seller_iban] && (!data[:seller_iban].match?(/\A[a-z]{2}\d{19}\Z/i) && !data[:seller_iban].match?(/\A\d{7}-\d{10}\Z/i))
    raise ArgumentError, "Invalid IBAN format '#{data[:seller_iban]}' expected IBAN or account number"
  end

  data.values.join("\n")
end

#to_pngObject



88
89
90
# File 'lib/croatia/payment_barcode.rb', line 88

def to_png(...)
  barcode.to_png(...)
end

#to_svgObject



92
93
94
# File 'lib/croatia/payment_barcode.rb', line 92

def to_svg(...)
  barcode.to_svg(...)
end