Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

creating sync with billing #2531

Merged
merged 9 commits into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions app/controllers/eis_billing/invoices_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module EisBilling
class InvoicesController < BaseController
before_action :load_invoice, only: :update

def update
state = InvoiceStateMachine.new(invoice: @invoice, status: params[:status])
if @invoice.update(modified_params) && state.call
render json: {
message: 'Invoice data was successfully updated',
}, status: :ok
else
render json: {
error: {
message: @invoice.errors.full_messages
}
}, status: :unprocessable_entity
end
end

private

def load_invoice
@invoice = Invoice.find_by(number: params[:invoice][:invoice_number])
return if @invoice.present?

render json: {
error: {
message: "Invoice with #{params[:invoice][:invoice_number]} number not found",
}
}, status: :not_found and return
end

def modified_params
{
in_directo: params[:invoice][:in_directo],
e_invoice_sent_at: params[:invoice][:sent_at_omniva],
}
end
end
end
48 changes: 12 additions & 36 deletions app/controllers/eis_billing/payment_status_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ class PaymentStatusController < EisBilling::BaseController
TYPE = 'PaymentOrders::EveryPay'.freeze

def update
payment_status = define_payment_status(params[:payment_state])
invoice = Invoice.find_by(number: params[:order_reference])

return if invoice.paid?

bank = create_bank_transfer(invoice: invoice, sum: params[:standing_amount], paid_at: params[:transaction_time])
create_payment_order(invoice: invoice, everypay_response: params, payment_status: payment_status)
bank.bind_invoice(params[:order_reference])

respond_to do |format|
format.json do
render status: :ok, content_type: 'application/json', layout: false, json: { message: 'ok' }
end
if invoice.paid?
render json: { message: 'Invoice already paid' }, status: :ok
else
invoice.process_payment(
payment_type: TYPE,
everypay_response: params,
payment_status: define_payment_status(params[:payment_state]),
sum: params[:standing_amount],
transaction_time: params[:transaction_time]
)

render json: { message: 'Payment is proccessing' }, status: :ok
end
end

Expand All @@ -26,30 +27,5 @@ def define_payment_status(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

payment
end

def create_bank_transfer(invoice:, sum:, paid_at:)
bank = BankTransaction.new
bank.description = invoice.order
bank.reference_no = invoice.reference_no
bank.currency = invoice.currency
bank.iban = invoice.seller_iban
bank.sum = sum
bank.paid_at = paid_at
bank.buyer_name = invoice.buyer_name
bank.save

bank
end
end
end
18 changes: 18 additions & 0 deletions app/models/concerns/invoice/cancellable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,22 @@ def cancelled?
def not_cancelled?
!cancelled?
end

def cancel_manualy
account_activity = AccountActivity.find_by(invoice_id: id)
account_activity_dup = account_activity.dup
account_activity_dup.sum = -account_activity.sum.to_f
account_activity_dup.save
account_activity.update(invoice_id: nil)
account_activity_dup.update(invoice_id: nil)
mark_cancelled_payment_order
account_activity.save && account_activity_dup.save
end

private

def mark_cancelled_payment_order
payment_order = payment_orders.last
payment_order.update(notes: 'Cancelled')
end
end
30 changes: 30 additions & 0 deletions app/models/concerns/invoice/payable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,34 @@ def receipt_date
def unpaid?
!paid?
end

def process_payment(**options)
payment = options[:payment_type].constantize.new(invoice: self)
payment.response = options[:everypay_response]
payment.status = options[:payment_status]
payment.save!

bank_transaction = payment.base_transaction(sum: options[:sum],
paid_at: options[:transaction_time] || Time.zone.now,
buyer_name: buyer_name)
bank_transaction.bind_invoice(number)
end

def autobind_manually
return if paid?

bank_statement = BankStatement.new(
bank_code: Setting.registry_bank_code,
iban: Setting.registry_iban
)
bank_statement.bank_transactions.build(
description: description,
sum: total,
reference_no: reference_no,
paid_at: Time.zone.now.to_date,
currency: 'EUR'
)
bank_statement.save!
bank_statement.bind_invoices(manual: true)
end
end
53 changes: 53 additions & 0 deletions app/models/invoice_state_machine.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
class InvoiceStateMachine
attr_reader :invoice, :status

def initialize(invoice:, status:)
@invoice = invoice
@status = status.to_sym
end

def call
case status
when :paid
mark_as_paid
when :cancelled
mark_as_cancel
when :unpaid
mark_as_unpaid
else
push_error
end
end

private

def mark_as_paid
return push_error unless invoice.payable?
return true if invoice.paid?

invoice.autobind_manually
invoice
end

def mark_as_cancel
return push_error unless invoice.cancellable?
return true if invoice.cancelled?

invoice.cancel
invoice
end

def mark_as_unpaid
return push_error if invoice.paid? && invoice.payment_orders&.last&.payment_reference? || invoice.cancelled?
return true unless invoice.paid?

invoice.cancel_manualy
invoice
end

def push_error
invoice.errors.add(:base, "Inavalid state #{status}")

false
end
end
4 changes: 4 additions & 0 deletions app/models/payment_order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ def self.supported_method?(name, shortname: false)
false
end

def payment_reference?
response && response['payment_reference'].present?
end

def base_transaction(sum:, paid_at:, buyer_name:)
BankTransaction.new(
description: invoice.order,
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
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'
resource :invoices, only: [:update]
end

namespace :epp do
Expand Down
Loading