added jobs for send e invoice data to billing system and received response

This commit is contained in:
olegphenomenon 2022-02-02 18:41:42 +02:00
parent 3242fb1da0
commit 5e0d1ff619
9 changed files with 117 additions and 3 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -8,5 +8,6 @@ module EisBilling
# =>
TOKEN = 'Bearer WA9UvDmzR9UcE5rLqpWravPQtdS8eDMAIynzGdSOTw==--9ZShwwij3qmLeuMJ--NE96w2PnfpfyIuuNzDJTGw=='.freeze
BASE_URL = ENV['eis_billing_system_base_url']
INITIATOR = 'registry'
end
end

View file

@ -8,7 +8,7 @@ module EisBilling
def self.obj_data
{
initiator: 'registry'
initiator: INITIATOR
}
end

View file

@ -9,7 +9,7 @@ module EisBilling
invoice_data: object_data,
monthly: monthly,
dry: dry,
initiator: 'registry'
initiator: INITIATOR
}
uri = URI(invoice_generator_url)

View file

@ -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

View file

@ -0,0 +1,2 @@
<h1>EisBilling::EInvoiceResponse#update</h1>
<p>Find me in app/views/eis_billing/e_invoice_response/update.html.erb</p>

View file

@ -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