diff --git a/app/controllers/eis_billing/e_invoice_response_controller.rb b/app/controllers/eis_billing/e_invoice_response_controller.rb new file mode 100644 index 000000000..8d50001ce --- /dev/null +++ b/app/controllers/eis_billing/e_invoice_response_controller.rb @@ -0,0 +1,16 @@ +class EisBilling::EInvoiceResponseController < ApplicationController + def update + invoice_number = params[:invoice_number] + date = params[:date] + + set_e_invoice_sent_at(date, invoice_number) + render status: 200, json: { messege: 'Response received', status: :ok } + end + + private + + def set_e_invoice_sent_at(date, invoice_number) + invoice = Invoice.find_by(number: invoice_number) + invoice.update(e_invoice_sent_at: Time.zone.now) + end +end diff --git a/app/jobs/send_e_invoice_two_job.rb b/app/jobs/send_e_invoice_two_job.rb new file mode 100644 index 000000000..7b896c77c --- /dev/null +++ b/app/jobs/send_e_invoice_two_job.rb @@ -0,0 +1,43 @@ +class SendEInvoiceTwoJob < ApplicationJob + discard_on HTTPClient::TimeoutError + + def perform(invoice_id, payable: true) + logger.info "Started to process e-invoice for invoice_id #{invoice_id}" + invoice = Invoice.find_by(id: invoice_id) + return unless need_to_process_invoice?(invoice: invoice, payable: payable) + + send_invoice_to_eis_billing(invoice: invoice, payable: payable) + rescue StandardError => e + log_error(invoice: invoice, error: e) + raise e + end + + private + + def need_to_process_invoice?(invoice:, payable:) + logger.info "Checking if need to process e-invoice #{invoice}, payable: #{payable}" + return false if invoice.blank? + return false if invoice.do_not_send_e_invoice? && payable + + true + end + + def send_invoice_to_eis_billing(invoice:, payable:) + result = EisBilling::SendEInvoice.send_request(invoice: invoice, payable: payable) + logger.info result.body + end + + def log_error(invoice:, error:) + id = invoice.try(:id) || invoice + message = <<~TEXT.squish + There was an error sending e-invoice for invoice with ID # #{id}. + The error message was the following: #{error} + This job will retry. + TEXT + logger.error message + end + + def logger + Rails.logger + end +end diff --git a/app/services/eis_billing/add_deposits.rb b/app/services/eis_billing/add_deposits.rb index 062a69519..c35cc9c7d 100644 --- a/app/services/eis_billing/add_deposits.rb +++ b/app/services/eis_billing/add_deposits.rb @@ -19,7 +19,7 @@ module EisBilling data[:customer_name] = invoice.buyer_name data[:customer_email] = invoice.buyer_email data[:custom_field_1] = invoice.description - data[:custom_field_2] = 'registry' + data[:custom_field_2] = INITIATOR data[:invoice_number] = invoice.number data diff --git a/app/services/eis_billing/base.rb b/app/services/eis_billing/base.rb index 78a57013b..7957b1c20 100644 --- a/app/services/eis_billing/base.rb +++ b/app/services/eis_billing/base.rb @@ -8,5 +8,6 @@ module EisBilling # => TOKEN = 'Bearer WA9UvDmzR9UcE5rLqpWravPQtdS8eDMAIynzGdSOTw==--9ZShwwij3qmLeuMJ--NE96w2PnfpfyIuuNzDJTGw=='.freeze BASE_URL = ENV['eis_billing_system_base_url'] + INITIATOR = 'registry' end end diff --git a/app/services/eis_billing/get_reference_number.rb b/app/services/eis_billing/get_reference_number.rb index e24ca3fb0..9a174c3a8 100644 --- a/app/services/eis_billing/get_reference_number.rb +++ b/app/services/eis_billing/get_reference_number.rb @@ -8,7 +8,7 @@ module EisBilling def self.obj_data { - initiator: 'registry' + initiator: INITIATOR } end diff --git a/app/services/eis_billing/send_data_to_directo.rb b/app/services/eis_billing/send_data_to_directo.rb index 615925990..47854be4c 100644 --- a/app/services/eis_billing/send_data_to_directo.rb +++ b/app/services/eis_billing/send_data_to_directo.rb @@ -9,7 +9,7 @@ module EisBilling invoice_data: object_data, monthly: monthly, dry: dry, - initiator: 'registry' + initiator: INITIATOR } uri = URI(invoice_generator_url) diff --git a/app/services/eis_billing/send_e_invoice.rb b/app/services/eis_billing/send_e_invoice.rb new file mode 100644 index 000000000..16b1f20f9 --- /dev/null +++ b/app/services/eis_billing/send_e_invoice.rb @@ -0,0 +1,51 @@ +module EisBilling + class SendEInvoice < EisBilling::Base + def self.send_request(invoice:, payable:) + base_request(invoice: invoice, payable: payable) + end + + def self.base_request(invoice:, payable:) + items = [] + prepared_data = { + invoice: invoice, + vat_amount: invoice.vat_amount, + invoice_subtotal: invoice.subtotal, + buyer_billing_email: invoice.buyer.billing_email, + buyer_e_invoice_iban: invoice.buyer.e_invoice_iban, + seller_country: invoice.seller_country, + buyer_country: invoice.buyer_country, + payable: payable, + initiator: INITIATOR + } + + invoice.items.each do |invoice_item| + items << { + description: invoice_item.description, + price: invoice_item.price, + quantity: invoice_item.quantity, + unit: invoice_item.unit, + subtotal: invoice_item.subtotal, + vat_rate: invoice_item.vat_rate, + vat_amount: invoice_item.vat_amount, + total: invoice_item.total + } + end + + prepared_data[:items] = items + + uri = URI(invoice_generator_url) + http = Net::HTTP.new(uri.host, uri.port) + headers = { + 'Authorization' => 'Bearer foobar', + 'Content-Type' => 'application/json', + 'Accept' => TOKEN + } + + http.post(invoice_generator_url, prepared_data.to_json, headers) + end + + def self.invoice_generator_url + "#{BASE_URL}/api/v1/e_invoice/e_invoice" + end + end +end diff --git a/app/views/eis_billing/e_invoice_response/update.html.erb b/app/views/eis_billing/e_invoice_response/update.html.erb new file mode 100644 index 000000000..32fb7d171 --- /dev/null +++ b/app/views/eis_billing/e_invoice_response/update.html.erb @@ -0,0 +1,2 @@ +
Find me in app/views/eis_billing/e_invoice_response/update.html.erb
diff --git a/config/routes.rb b/config/routes.rb index a91287b49..25a3f1b98 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -14,6 +14,7 @@ Rails.application.routes.draw do namespace :eis_billing do put '/payment_status', to: 'payment_status#update', as: 'payment_status' put '/directo_response', to: 'directo_response#update', as: 'directo_response' + put '/e_invoice_response', to: 'e_invoice_response#update', as: 'e_invoice_response' post '/lhv_connect_transactions', to: 'lhv_connect_transactions#create', as: 'lhv_connect_transactions' end