refactored

This commit is contained in:
olegphenomenon 2022-01-17 14:07:04 +02:00
parent f22f16d720
commit fdc2882b8f
6 changed files with 58 additions and 38 deletions

View file

@ -3,23 +3,50 @@ module EisBilling
skip_authorization_check # Temporary solution skip_authorization_check # Temporary solution
skip_before_action :verify_authenticity_token # Temporary solution skip_before_action :verify_authenticity_token # Temporary solution
TYPE = "PaymentOrders::EveryPay".freeze TYPE = 'PaymentOrders::EveryPay'.freeze
def update def update
invoice_number = params[:order_reference] invoice_number = params[:order_reference]
paid_at = params[:transaction_time] paid_at = params[:transaction_time]
sum = params[:standing_amount] sum = params[:standing_amount]
everypay_response = params everypay_response = params
payment_status = nil
if PaymentOrders::EveryPay::SUCCESSFUL_PAYMENT.include? params[:payment_state] payment_status = define_payment_status(params[:payment_state])
payment_status = :paid
elsif params[:payment_state] == 'failed'
payment_status = :failed
end
invoice = Invoice.find_by(number: invoice_number) invoice = Invoice.find_by(number: invoice_number)
bank = create_bank_transfer(invoice: invoice, sum: sum, paid_at: paid_at)
create_payment_order(invoice: invoice, everypay_response: everypay_response, payment_status: payment_status)
registrar = Registrar.find_by(reference_no: params[:reference_number])
bank.create_activity(registrar, invoice)
render status: 200, json: { status: 'ok' }
end
private
def define_payment_status(status)
return :paid if PaymentOrders::EveryPay::SUCCESSFUL_PAYMENT.include? status
:failed
end
def create_payment_order(invoice:, everypay_response:, payment_status:)
payment = PaymentOrder.new
payment.type = TYPE
payment.invoice = invoice
payment.response = everypay_response
payment.status = payment_status
payment.save
logger.info '++++ PAYMENT ORDER ERRORS ? ++++'
logger.info payment.errors
payment
end
def create_bank_transfer(invoice:, sum:, paid_at:)
bank = BankTransaction.new bank = BankTransaction.new
bank.description = invoice.order bank.description = invoice.order
bank.reference_no = invoice.reference_no bank.reference_no = invoice.reference_no
@ -30,23 +57,14 @@ module EisBilling
bank.buyer_name = invoice.buyer_name bank.buyer_name = invoice.buyer_name
bank.save bank.save
p "++++ BANK TRANSACTION ERRORS ? ++++" logger.info '++++ BANK TRANSACTION ERRORS ? ++++'
p bank.errors looger.info bank.errors
payment = PaymentOrder.new bank
payment.type = TYPE end
payment.invoice = invoice
payment.response = everypay_response
payment.status = payment_status
payment.save
p "++++ PAYMENT ORDER ERRORS ? ++++" def logger
p bank.errors @logger ||= Rails.logger
registrar = Registrar.find_by(reference_no: params[:reference_number])
bank.create_activity(registrar, invoice)
render status: 200, json: { status: 'ok' }
end end
end end
end end

View file

@ -12,10 +12,7 @@ class Registrar
if @invoice if @invoice
flash[:notice] = t(:please_pay_the_following_invoice) flash[:notice] = t(:please_pay_the_following_invoice)
send_invoice_data_to_billing_system
add_invoice_instance = EisBilling::AddDeposits.new(@invoice)
add_invoice_instance.send_invoice
redirect_to [:registrar, @invoice] redirect_to [:registrar, @invoice]
else else
flash[:alert] = @deposit.errors.full_messages.join(', ') flash[:alert] = @deposit.errors.full_messages.join(', ')
@ -25,6 +22,11 @@ class Registrar
private private
def send_invoice_data_to_billing_system
add_invoice_instance = EisBilling::AddDeposits.new(@invoice)
add_invoice_instance.send_invoice
end
def deposit_params def deposit_params
params.require(:deposit).permit(:amount, :description) params.require(:deposit).permit(:amount, :description)
end end

View file

@ -18,7 +18,7 @@ class Registrar
link_handler = EisBilling::GetInvoiceLink.new(invoice.number) link_handler = EisBilling::GetInvoiceLink.new(invoice.number)
response = link_handler.send_request response = link_handler.send_request
@everypay_link = JSON.parse(response.body)["payment_link"] @everypay_link = JSON.parse(response.body)['payment_link']
end end
def cancel def cancel

View file

@ -20,14 +20,14 @@ module EisBilling
:items_attributes]) :items_attributes])
parsed_data = JSON.parse(invoice) parsed_data = JSON.parse(invoice)
parsed_data["role"] = "registrar" parsed_data['role'] = 'registrar'
parsed_data["description"] = "some" if parsed_data["description"] == '' parsed_data['description'] = 'some' if parsed_data['description'] == ''
parsed_data = replace_key(json_obj: parsed_data, old_key: "total", new_key: "transaction_amount") parsed_data = replace_key(json_obj: parsed_data, old_key: 'total', new_key: 'transaction_amount')
parsed_data = replace_key(json_obj: parsed_data, old_key: "reference_no", new_key: "reference_number") parsed_data = replace_key(json_obj: parsed_data, old_key: 'reference_no', new_key: 'reference_number')
invoice_items_json = @invoice.items.to_json(except: [:created_at, :updated_at]) invoice_items_json = @invoice.items.to_json(except: [ :created_at, :updated_at ])
parsed_data["items"] = JSON.parse(invoice_items_json) parsed_data['items'] = JSON.parse(invoice_items_json)
parsed_data parsed_data
end end
@ -42,9 +42,9 @@ module EisBilling
uri = URI(invoice_generator_url) uri = URI(invoice_generator_url)
http = Net::HTTP.new(uri.host, uri.port) http = Net::HTTP.new(uri.host, uri.port)
headers = { headers = {
'Authorization'=>'Bearer foobar', 'Authorization' => 'Bearer foobar',
'Content-Type' =>'application/json', 'Content-Type' => 'application/json',
'Accept'=> TOKEN 'Accept' => TOKEN
} }
res = http.post(invoice_generator_url, json_obj.to_json, headers) res = http.post(invoice_generator_url, json_obj.to_json, headers)

View file

@ -6,7 +6,7 @@ module EisBilling
# => "HFW8ADSIrjyD9cbH4H5Rk3MY/ZfhV85IlnGl7YI2CQ==--OvlWMMiTLLotgdfT--/ffejEDaIGFfz7FzzNSlYA==" # => "HFW8ADSIrjyD9cbH4H5Rk3MY/ZfhV85IlnGl7YI2CQ==--OvlWMMiTLLotgdfT--/ffejEDaIGFfz7FzzNSlYA=="
# irb(main):048:0> decrypted_back = crypt.decrypt_and_verify(encrypted_data) # irb(main):048:0> decrypted_back = crypt.decrypt_and_verify(encrypted_data)
# => "PLEASE CREATE INVOICE" # => "PLEASE CREATE INVOICE"
TOKEN = "Bearer WA9UvDmzR9UcE5rLqpWravPQtdS8eDMAIynzGdSOTw==--9ZShwwij3qmLeuMJ--NE96w2PnfpfyIuuNzDJTGw==".freeze TOKEN = 'Bearer WA9UvDmzR9UcE5rLqpWravPQtdS8eDMAIynzGdSOTw==--9ZShwwij3qmLeuMJ--NE96w2PnfpfyIuuNzDJTGw=='.freeze
BASE_URL = ENV['eis_billing_system_base_url'] BASE_URL = ENV['eis_billing_system_base_url']
protected protected

View file

@ -4,7 +4,7 @@ class BalanceTopUpTest < ApplicationSystemTestCase
setup do setup do
sign_in users(:api_bestnames) sign_in users(:api_bestnames)
@original_registry_vat_rate = Setting.registry_vat_prc @original_registry_vat_rate = Setting.registry_vat_prc
eis_response = OpenStruct.new(body: "{\"payment_link\":\"http://link.test\"}") eis_response = OpenStruct.new(body: "{\"payment_link\":\"http://link.test\"}")
Spy.on_instance_method(EisBilling::AddDeposits, :send_invoice).and_return(eis_response) Spy.on_instance_method(EisBilling::AddDeposits, :send_invoice).and_return(eis_response)
Spy.on_instance_method(EisBilling::GetInvoiceLink, :send_request).and_return(eis_response) Spy.on_instance_method(EisBilling::GetInvoiceLink, :send_request).and_return(eis_response)