mirror of
https://github.com/internetee/registry.git
synced 2025-07-27 21:16:12 +02:00
added state machine for invoice
This commit is contained in:
parent
c9ed31771b
commit
a4bf967e1d
3 changed files with 83 additions and 34 deletions
|
@ -9,8 +9,9 @@ module EisBilling
|
|||
before_action :load_invoice, only: :update
|
||||
|
||||
def update
|
||||
if @invoice.update(modified_params) && payment_orders_handler
|
||||
|
||||
# if @invoice.update(modified_params) && payment_orders_handler
|
||||
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
|
||||
|
@ -25,41 +26,41 @@ module EisBilling
|
|||
|
||||
private
|
||||
|
||||
def payment_orders_handler
|
||||
return false if @invoice.cancelled? && status.paid? || @invoice.cancelled? && status.issued?
|
||||
return false if @invoice.paid? && (status.failed? || status.cancelled?)
|
||||
# def payment_orders_handler
|
||||
# return false if @invoice.cancelled? && status.paid? || @invoice.cancelled? && status.issued?
|
||||
# return false if @invoice.paid? && (status.failed? || status.cancelled?)
|
||||
|
||||
case
|
||||
when @invoice.paid? && status.paid?
|
||||
true
|
||||
when @invoice.unpaid? && status.issued?
|
||||
true
|
||||
when @invoice.cancelled? && (status.cancelled? || status.failed?)
|
||||
true
|
||||
when status.issued?
|
||||
@invoice.cancel_manualy
|
||||
when status.paid?
|
||||
@invoice.autobind_manually
|
||||
else
|
||||
@invoice.cancel
|
||||
end
|
||||
end
|
||||
# case
|
||||
# when @invoice.paid? && status.paid?
|
||||
# true
|
||||
# when @invoice.unpaid? && status.issued?
|
||||
# true
|
||||
# when @invoice.cancelled? && (status.cancelled? || status.failed?)
|
||||
# true
|
||||
# when status.issued?
|
||||
# @invoice.cancel_manualy
|
||||
# when status.paid?
|
||||
# @invoice.autobind_manually
|
||||
# else
|
||||
# @invoice.cancel
|
||||
# end
|
||||
# end
|
||||
|
||||
def status
|
||||
status = case params[:status]
|
||||
when 'paid'
|
||||
'paid'
|
||||
when 'cancelled'
|
||||
'cancelled'
|
||||
when 'failed'
|
||||
'failed'
|
||||
else
|
||||
'unpaid'
|
||||
end
|
||||
# def status
|
||||
# status = case params[:status]
|
||||
# when 'paid'
|
||||
# 'paid'
|
||||
# when 'cancelled'
|
||||
# 'cancelled'
|
||||
# when 'failed'
|
||||
# 'failed'
|
||||
# else
|
||||
# 'unpaid'
|
||||
# end
|
||||
|
||||
Struct.new(:paid?, :cancelled?, :issued?, :failed?)
|
||||
.new(status == PAID, status == CANCELLED, status == ISSUED, status == FAILED)
|
||||
end
|
||||
# Struct.new(:paid?, :cancelled?, :issued?, :failed?)
|
||||
# .new(status == PAID, status == CANCELLED, status == ISSUED, status == FAILED)
|
||||
# end
|
||||
|
||||
def load_invoice
|
||||
@invoice = Invoice.find_by(number: params[:invoice][:invoice_number])
|
||||
|
|
44
app/models/invoice_state_machine.rb
Normal file
44
app/models/invoice_state_machine.rb
Normal file
|
@ -0,0 +1,44 @@
|
|||
# enum status: %i[unpaid paid cancelled failed]
|
||||
|
||||
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
|
||||
raise "Inavalid state #{invoice.status}"
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def mark_as_paid
|
||||
raise "Inavalid state #{invoice.status}" unless invoice.unpaid? || invoice.paid?
|
||||
|
||||
invoice.autobind_manually
|
||||
end
|
||||
|
||||
def mark_as_cancel
|
||||
# Paid invoice cannot be cancelled?
|
||||
raise "Inavalid state #{invoice.status}" unless invoice.cancellable? || invoice.cancelled?
|
||||
|
||||
invoice.cancel
|
||||
end
|
||||
|
||||
def mark_as_unpaid
|
||||
raise "Inavalid state #{invoice.status}" unless invoice.paid? && invoice.payment_orders.present? || invoice.unpaid?
|
||||
|
||||
invoice.cancel_manualy
|
||||
end
|
||||
end
|
|
@ -30,6 +30,10 @@ module PaymentOrders
|
|||
valid_hmac? && valid_amount? && valid_account?
|
||||
end
|
||||
|
||||
def payment_reference?
|
||||
response['payment_reference'].present?
|
||||
end
|
||||
|
||||
def settled_payment?
|
||||
SUCCESSFUL_PAYMENT.include?(response['payment_state'])
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue