mirror of
https://github.com/internetee/registry.git
synced 2025-07-29 05:56:20 +02:00
Merge pull request #2531 from internetee/68-creating-syncronization-of-invoice-changes
creating sync with billing
This commit is contained in:
commit
da514f9a50
11 changed files with 715 additions and 60 deletions
40
app/controllers/eis_billing/invoices_controller.rb
Normal file
40
app/controllers/eis_billing/invoices_controller.rb
Normal file
|
@ -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
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue