Class: Croatia::Fiscalizer
- Inherits:
-
Object
- Object
- Croatia::Fiscalizer
- Defined in:
- lib/croatia/fiscalizer.rb
Defined Under Namespace
Modules: XMLBuilder
Constant Summary collapse
- TZ =
TZInfo::Timezone.get("Europe/Zagreb")
- QR_CODE_BASE_URL =
"https://porezna.gov.hr/rn"
Instance Attribute Summary collapse
-
#certificate ⇒ Object
readonly
Returns the value of attribute certificate.
Instance Method Summary collapse
- #fiscalize(invoice:, message_id: SecureRandom.uuid) ⇒ Object
- #generate_issuer_protection_code(invoice) ⇒ Object
- #generate_verification_qr_code(invoice) ⇒ Object
-
#initialize(certificate: nil, password: nil) ⇒ Fiscalizer
constructor
A new instance of Fiscalizer.
Constructor Details
#initialize(certificate: nil, password: nil) ⇒ Fiscalizer
Returns a new instance of Fiscalizer.
17 18 19 20 21 22 |
# File 'lib/croatia/fiscalizer.rb', line 17 def initialize(certificate: nil, password: nil) certificate ||= Croatia.config.fiscalization[:certificate] password ||= Croatia.config.fiscalization[:password] @certificate = load_certificate(certificate, password) end |
Instance Attribute Details
#certificate ⇒ Object (readonly)
Returns the value of attribute certificate.
15 16 17 |
# File 'lib/croatia/fiscalizer.rb', line 15 def certificate @certificate end |
Instance Method Details
#fiscalize(invoice:, message_id: SecureRandom.uuid) ⇒ Object
24 25 26 27 |
# File 'lib/croatia/fiscalizer.rb', line 24 def fiscalize(invoice:, message_id: SecureRandom.uuid) _document = XMLBuilder.invoice_request(invoice: invoice, message_id: , timezone: TZ) raise NotImplementedError, "Fiscalization XML generation is not implemented yet" end |
#generate_issuer_protection_code(invoice) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/croatia/fiscalizer.rb', line 29 def generate_issuer_protection_code(invoice) buffer = [] buffer << invoice.issuer.pin buffer << TZ.to_local(invoice.issue_date).strftime("%d.%m.%Y %H:%M:%S") buffer << invoice.sequential_number buffer << invoice.business_location_identifier buffer << invoice.register_identifier buffer << invoice.total.to_f digest = OpenSSL::Digest::SHA1.new signature = certificate.sign(digest, buffer.join) Digest::MD5.hexdigest(signature).downcase end |
#generate_verification_qr_code(invoice) ⇒ Object
44 45 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 |
# File 'lib/croatia/fiscalizer.rb', line 44 def generate_verification_qr_code(invoice) Croatia::QRCode.ensure_supported! params = { datv: TZ.to_local(invoice.issue_date).strftime("%Y%m%d_%H%M"), izn: invoice.total_cents.to_s } if params[:izn].length > 10 raise ArgumentError, "Total amount exceeds 10 digits: #{params[:izn]}" end if invoice.unique_invoice_identifier params[:jir] = invoice.unique_invoice_identifier else params[:zki] = generate_issuer_protection_code(invoice) end if params[:jir].nil? && params[:zki].nil? raise ArgumentError, "Either unique_invoice_identifier or issuer_protection_code must be provided" end query_string = URI.encode_www_form(params) url = "#{QR_CODE_BASE_URL}?#{query_string}" Croatia::QRCode.new(url) end |