diff --git a/app/controllers/api/v1/invoice_generator/reference_number_generator_controller.rb b/app/controllers/api/v1/invoice_generator/reference_number_generator_controller.rb index 6e515d2..de356ef 100644 --- a/app/controllers/api/v1/invoice_generator/reference_number_generator_controller.rb +++ b/app/controllers/api/v1/invoice_generator/reference_number_generator_controller.rb @@ -3,14 +3,17 @@ class Api::V1::InvoiceGenerator::ReferenceNumberGeneratorController < Applicatio def create initiator = params['initiator'] + owner = params['owner'] + email = params['email'] reference_number = nil + loop do reference_number = generate - reference = Reference.find_by(reference_number: reference_number) + reference = Reference.find_by(reference_number:) next unless reference.nil? - Reference.create!(reference_number: reference_number, initiator: initiator) + Reference.create!(reference_number:, initiator:, email:, owner:) break end diff --git a/app/jobs/payment_lhv_connect_job.rb b/app/jobs/payment_lhv_connect_job.rb index 7c5297d..4c0eec6 100644 --- a/app/jobs/payment_lhv_connect_job.rb +++ b/app/jobs/payment_lhv_connect_job.rb @@ -22,7 +22,7 @@ def payment_process api = Lhv::ConnectApi.new api.cert = cert api.key = key - # api.ca_file = ENV['lhv_ca_file'] + api.dev_mode = ENV['lhv_dev_mode'] == 'true' incoming_transactions = [] @@ -43,11 +43,11 @@ def payment_process sorted_by_ref_number = incoming_transactions.group_by { |x| x[:payment_reference_number] } sorted_by_ref_number.each do |s| - Rails.logger.info "=========== Sending to registry ===========" + Rails.logger.info '=========== Sending transaction ===========' Rails.logger.info s[1] - Rails.logger.info "===========================================" + Rails.logger.info '===========================================' - send_transactions_to_registry(params: s[1]) + send_transactions(params: s[1], payment_reference_number: s[0]) end Rails.logger.info "Transactions processed: #{incoming_transactions.size}" @@ -66,7 +66,7 @@ def parse_reference_number(credit_transaction) return unless valid_ref_no?(reference) ref = Reference.find_by(reference_number: reference) - inform_admin(reference: reference, body: credit_transaction) and return nil if ref.nil? + inform_admin(reference:, body: credit_transaction) and return nil if ref.nil? reference end @@ -77,12 +77,12 @@ def ref_number_from_description(description) end def valid_ref_no?(match) - return true if Billing::ReferenceNo.valid?(match) + true if Billing::ReferenceNo.valid?(match) end def inform_admin(reference:, body:) - Rails.logger.info "Inform to admin that reference number not found" - BillingMailer.inform_admin(reference_number: reference, body: body).deliver_now + Rails.logger.info 'Inform to admin that reference number not found' + BillingMailer.inform_admin(reference_number: reference, body:).deliver_now end def open_ssl_keystore @@ -92,8 +92,10 @@ def open_ssl_keystore end # https://registry.test/eis_billing/lhv_connect_transactions - def send_transactions_to_registry(params:) - uri = URI.parse(url_transaction) + def send_transactions(params:, payment_reference_number:) + reference = Reference.find_by(reference_number: payment_reference_number) + + uri = URI.parse(url[reference.initiator.to_sym]) http = Net::HTTP.new(uri.host, uri.port) if Rails.env.development? || Rails.env.test? @@ -103,13 +105,13 @@ def send_transactions_to_registry(params:) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_PEER end - - res = http.post(url_transaction, params.to_json, headers) - - Rails.logger.info ">>>>>>" + + res = http.post(url[reference.initiator.to_sym], params.to_json, headers) + + Rails.logger.info '>>>>>>' Rails.logger.info res.body - Rails.logger.info ">>>>>>" - end + Rails.logger.info '>>>>>>' + end def generate_token JWT.encode(payload, billing_secret) @@ -121,15 +123,26 @@ def payload def headers { - 'Authorization' => "Bearer #{generate_token}", - 'Content-Type' => 'application/json', + 'Authorization' => "Bearer #{generate_token}", + 'Content-Type' => 'application/json' } end - def url_transaction + def url + { + registry: registry_url_transaction, + auction: auction_url_transaction + } + end + + def registry_url_transaction "#{ENV['base_registry']}/eis_billing/lhv_connect_transactions" end + def auction_url_transaction + "#{ENV['base_auction']}/eis_billing/lhv_connect_transactions" + end + def billing_secret ENV['billing_secret'] end @@ -139,6 +152,6 @@ def test_transactions currency: 'EUR', date: Time.zone.today, payment_reference_number: '7366488', - payment_description: "description 7366488") + payment_description: 'description 7366488') end end diff --git a/app/jobs/save_reference_data_job.rb b/app/jobs/save_reference_data_job.rb index 44856e3..3415a1f 100644 --- a/app/jobs/save_reference_data_job.rb +++ b/app/jobs/save_reference_data_job.rb @@ -4,19 +4,18 @@ def perform(response) skipped_count = 0 response.each do |data| - reference_number = data["reference_number"] - initiator = data["initiator"] - registrar_name = data["registrar_name"] + reference_number = data['reference_number'] + initiator = data['initiator'] + registrar_name = data['registrar_name'] - unless Reference.find_by(reference_number: reference_number, initiator: initiator, owner: registrar_name).nil? + if Reference.find_by(reference_number:, initiator:, owner: registrar_name).present? skipped_count += 1 next end - log_request(reference_number: reference_number, initiator: initiator, owner: registrar_name) - - Reference.create!(reference_number: reference_number, initiator: initiator, owner: registrar_name) + Reference.create!(reference_number:, initiator:, owner: registrar_name) + log_request(reference_number:, initiator:, owner: registrar_name) added_count += 1 end @@ -24,11 +23,11 @@ def perform(response) [added_count, skipped_count] end - def log_request(reference_number:, initiator:, owner:) + def log_request(reference_number:, initiator:, owner:) Rails.logger.info reference_number Rails.logger.info initiator Rails.logger.info owner - Rails.logger.info "++++++++++++++++++" + Rails.logger.info '++++++++++++++++++' end -end \ No newline at end of file +end diff --git a/app/views/references/_reference.html.erb b/app/views/references/_reference.html.erb index 5c60b4f..7a8cdca 100644 --- a/app/views/references/_reference.html.erb +++ b/app/views/references/_reference.html.erb @@ -2,5 +2,6 @@ <%= reference.reference_number %> <%= reference.initiator %> <%= reference.owner %> + <%= reference.email %> <%= reference.created_at %> <% end %> diff --git a/app/views/references/index.html.erb b/app/views/references/index.html.erb index 17a5a89..f523f14 100644 --- a/app/views/references/index.html.erb +++ b/app/views/references/index.html.erb @@ -25,6 +25,7 @@ <%= sort_link_to "Reference number", "reference_number" %> <%= sort_link_to "Related", "initiator" %> <%= sort_link_to "Owner", "owner" %> + <%= sort_link_to "Email", "email" %> <%= sort_link_to "Created at", "created_at" %> diff --git a/db/migrate/20231031100825_add_owner_email_for_references_table.rb b/db/migrate/20231031100825_add_owner_email_for_references_table.rb new file mode 100644 index 0000000..3303cf7 --- /dev/null +++ b/db/migrate/20231031100825_add_owner_email_for_references_table.rb @@ -0,0 +1,5 @@ +class AddOwnerEmailForReferencesTable < ActiveRecord::Migration[7.0] + def change + add_column :references, :email, :string + end +end diff --git a/spec/jobs/payment_lhv_connect_job_spec.rb b/spec/jobs/payment_lhv_connect_job_spec.rb index e87414b..e870b82 100644 --- a/spec/jobs/payment_lhv_connect_job_spec.rb +++ b/spec/jobs/payment_lhv_connect_job_spec.rb @@ -48,7 +48,7 @@ allow_any_instance_of(Net::HTTP).to receive(:post).and_return('200 - ok') allow_any_instance_of(PaymentLhvConnectJob).to receive(:open_ssl_keystore).and_return(openssl_struct) - expect_any_instance_of(PaymentLhvConnectJob).to receive(:send_transactions_to_registry).with(params: [params_for_sending]) + expect_any_instance_of(PaymentLhvConnectJob).to receive(:send_transactions).with(params: [params_for_sending], payment_reference_number: '2') PaymentLhvConnectJob.perform_now end @@ -62,7 +62,7 @@ openssl_struct = OpenStruct.new(key: 'key', certificate: 'certificate') allow_any_instance_of(PaymentLhvConnectJob).to receive(:open_ssl_keystore).and_return(openssl_struct) - expect_any_instance_of(PaymentLhvConnectJob).not_to receive(:send_transactions_to_registry).with(params: []) + expect_any_instance_of(PaymentLhvConnectJob).not_to receive(:send_transactions).with(params: [], payment_reference_number: nil) PaymentLhvConnectJob.perform_now end end @@ -103,7 +103,7 @@ end end - expect_any_instance_of(PaymentLhvConnectJob).to receive(:send_transactions_to_registry).with(params: [params_for_sending]) + expect_any_instance_of(PaymentLhvConnectJob).to receive(:send_transactions).with(params: [params_for_sending], payment_reference_number: ref) PaymentLhvConnectJob.perform_now end end