creating sync with billing

This commit is contained in:
olegphenomenon 2023-01-11 12:25:49 +02:00
parent 162647acba
commit 0a4444b556
9 changed files with 577 additions and 60 deletions

View file

@ -0,0 +1,71 @@
module EisBilling
class InvoicesController < BaseController
TYPE = 'PaymentOrders::EveryPay'.freeze
before_action :load_invoice, only: :update
def update
if @invoice.update(modified_params)
payment_orders_handler
render json: {
message: 'Invoice data was successfully updated'
}, status: :ok
else
render json: {
error: @message.errors.full_messages
}, status: :unprocessable_entity
end
end
private
def payment_orders_handler
if @invoice.payment_orders.present?
return if (@invoice.paid? && status == 'paid') || (@invoice.cancelled? && status == 'cancelled')
if status == 'cancelled' || status == 'failed'
@invoice.cancel_manualy
elsif status == 'paid'
@invoice.autobind_manually
end
else
return unless status == 'paid'
@invoice.autobind_manually
end
end
def status
case params[:invoice][:status]
when 'paid'
'paid'
when 'cancelled'
'cancelled'
when 'failed'
'failed'
else
'issued'
end
end
def load_invoice
@invoice = Invoice.find_by(number: params[:invoice][:invoice_number])
if @invoice.nil?
render json: {
error: {
message: "Invoice with #{params[:invoice][:invoice_number]} number not found"
}
}, status: :not_found and return
end
end
def modified_params
{
in_directo: params[:invoice][:in_directo],
e_invoice_sent_at: params[:invoice][:sent_at_omniva]
}
end
end
end

View file

@ -3,19 +3,20 @@ module EisBilling
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?
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]
)
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
render json: { message: 'Payment is proccessing' }, status: :ok
end
end
@ -26,30 +27,5 @@ module EisBilling
: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