Skip to content

Commit

Permalink
Merge pull request #94 from internetee/93-lhv-connect
Browse files Browse the repository at this point in the history
added support lhv connect for auction
  • Loading branch information
vohmar authored Dec 1, 2023
2 parents 93821f7 + 2040aa8 commit 55d4532
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
53 changes: 33 additions & 20 deletions app/jobs/payment_lhv_connect_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand All @@ -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}"
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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?
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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
19 changes: 9 additions & 10 deletions app/jobs/save_reference_data_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,30 @@ 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

[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
end
1 change: 1 addition & 0 deletions app/views/references/_reference.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
<td class="px-6 py-1 border-b border-gray-200"><%= reference.reference_number %></td>
<td class="px-6 py-1 border-b border-gray-200"><%= reference.initiator %></td>
<td class="px-6 py-1 border-b border-gray-200"><%= reference.owner %></td>
<td class="px-6 py-1 border-b border-gray-200"><%= reference.email %></td>
<td class="px-6 py-1 border-b border-gray-200"><%= reference.created_at %></td>
<% end %>
1 change: 1 addition & 0 deletions app/views/references/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<th class="px-6 py-1 border-b border-gray-200 bg-gray-50 text-left text-xs text-gray-500 uppercase"><%= sort_link_to "Reference number", "reference_number" %></th>
<th class="px-6 py-1 border-b border-gray-200 bg-gray-50 text-left text-xs text-gray-500 uppercase"><%= sort_link_to "Related", "initiator" %></th>
<th class="px-6 py-1 border-b border-gray-200 bg-gray-50 text-left text-xs text-gray-500 uppercase"><%= sort_link_to "Owner", "owner" %></th>
<th class="px-6 py-1 border-b border-gray-200 bg-gray-50 text-left text-xs text-gray-500 uppercase"><%= sort_link_to "Email", "email" %></th>
<th class="px-6 py-1 border-b border-gray-200 bg-gray-50 text-left text-xs text-gray-500 uppercase"><%= sort_link_to "Created at", "created_at" %></th>
</tr>
</thead>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddOwnerEmailForReferencesTable < ActiveRecord::Migration[7.0]
def change
add_column :references, :email, :string
end
end
6 changes: 3 additions & 3 deletions spec/jobs/payment_lhv_connect_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 55d4532

Please sign in to comment.