Class: Croatia::PaymentBarcode
- Inherits:
-
Object
- Object
- Croatia::PaymentBarcode
- 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
-
#buyer_address ⇒ Object
Returns the value of attribute buyer_address.
-
#buyer_city ⇒ Object
Returns the value of attribute buyer_city.
-
#buyer_name ⇒ Object
Returns the value of attribute buyer_name.
-
#buyer_postal_code ⇒ Object
Returns the value of attribute buyer_postal_code.
-
#currency ⇒ Object
Returns the value of attribute currency.
-
#description ⇒ Object
Returns the value of attribute description.
-
#model ⇒ Object
Returns the value of attribute model.
-
#payment_purpose_code ⇒ Object
Returns the value of attribute payment_purpose_code.
-
#reference_number ⇒ Object
Returns the value of attribute reference_number.
-
#seller_address ⇒ Object
Returns the value of attribute seller_address.
-
#seller_city ⇒ Object
Returns the value of attribute seller_city.
-
#seller_iban ⇒ Object
Returns the value of attribute seller_iban.
-
#seller_name ⇒ Object
Returns the value of attribute seller_name.
-
#seller_postal_code ⇒ Object
Returns the value of attribute seller_postal_code.
-
#total_cents ⇒ Object
Returns the value of attribute total_cents.
Instance Method Summary collapse
- #barcode ⇒ Object
- #data ⇒ Object
-
#initialize(**options) ⇒ PaymentBarcode
constructor
A new instance of PaymentBarcode.
- #to_png ⇒ Object
- #to_svg ⇒ Object
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(**) .each do |key, value| public_send("#{key}=", value) end end |
Instance Attribute Details
#buyer_address ⇒ Object
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_city ⇒ Object
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_name ⇒ Object
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_code ⇒ Object
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 |
#currency ⇒ Object
Returns the value of attribute currency.
23 24 25 |
# File 'lib/croatia/payment_barcode.rb', line 23 def currency @currency end |
#description ⇒ Object
Returns the value of attribute description.
23 24 25 |
# File 'lib/croatia/payment_barcode.rb', line 23 def description @description end |
#model ⇒ Object
Returns the value of attribute model.
23 24 25 |
# File 'lib/croatia/payment_barcode.rb', line 23 def model @model end |
#payment_purpose_code ⇒ Object
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_number ⇒ Object
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_address ⇒ Object
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_city ⇒ Object
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_iban ⇒ Object
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_name ⇒ Object
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_code ⇒ Object
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_cents ⇒ Object
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
#barcode ⇒ Object
83 84 85 86 |
# File 'lib/croatia/payment_barcode.rb', line 83 def Croatia::PDF417.ensure_supported! Croatia::PDF417.new(data) end |
#data ⇒ Object
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_png ⇒ Object
88 89 90 |
# File 'lib/croatia/payment_barcode.rb', line 88 def to_png(...) .to_png(...) end |
#to_svg ⇒ Object
92 93 94 |
# File 'lib/croatia/payment_barcode.rb', line 92 def to_svg(...) .to_svg(...) end |