change logic, added tests

This commit is contained in:
olegphenomenon 2023-02-07 16:10:20 +02:00
parent e00e87a15c
commit d28afca00f
5 changed files with 115 additions and 52 deletions

View file

@ -1,15 +1,8 @@
module EisBilling
class InvoicesController < BaseController
TYPE = 'PaymentOrders::EveryPay'.freeze
PAID = 'paid'.freeze
CANCELLED = 'cancelled'.freeze
ISSUED = 'unpaid'.freeze
FAILED = 'failed'.freeze
before_action :load_invoice, only: :update
def update
# 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: {
@ -26,42 +19,6 @@ 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?)
# 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
# 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])
return if @invoice.present?

View file

@ -1,5 +1,3 @@
# enum status: %i[unpaid paid cancelled failed]
class InvoiceStateMachine
attr_reader :invoice, :status
@ -24,21 +22,24 @@ class InvoiceStateMachine
private
def mark_as_paid
return push_error unless invoice.payable? || invoice.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? || invoice.cancelled?
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.cancellable?
return push_error if invoice.paid? && invoice.payment_orders.last.payment_reference? || invoice.cancelled?
return true unless invoice.paid?
invoice.cancel_manualy
invoice

View file

@ -64,6 +64,10 @@ class PaymentOrder < ApplicationRecord
false
end
def payment_reference?
response && response['payment_reference'].present?
end
def base_transaction(sum:, paid_at:, buyer_name:)
BankTransaction.new(
description: invoice.order,

View file

@ -30,10 +30,6 @@ 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