Sha256: dc840d7aa654e9d4ae089eab533f4abed80696f18e78f13a9fc678877d9a8531

Contents?: true

Size: 1.81 KB

Versions: 1

Compression:

Stored size: 1.81 KB

Contents

class PaymentOrder < ApplicationRecord
  belongs_to :order, inverse_of: :payment_orders
  belongs_to :payment, inverse_of: :payment_orders

  validate :for_check_amount

  enum state: [
    :init,
    :confirmed
  ]

  def for_check_amount
    if (same_payment_amount + self.check_amount.to_d) >= self.payment.total_amount.floor + 0.99
      self.errors.add(:check_amount, 'The Amount Large than the Total Payment')
    end

    if (same_order_amount + self.check_amount.to_d) >= self.order.amount.floor + 0.99
      self.errors.add(:check_amount, 'The Amount Large than the Total Order')
    end
  end

  def same_payment_amount
    PaymentOrder.where.not(id: self.id).where(payment_id: self.payment_id).sum(:check_amount)
  end

  def same_order_amount
    received = PaymentOrder.where.not(id: self.id).where(order_id: self.order_id).sum(:check_amount)
    refund = Refund.where(payment_id: payment_id, order_id: order_id).sum(:total_amount)
    received - refund
  end

  def payment_amount
    PaymentOrder.where(payment_id: self.payment_id, state: 'confirmed').sum(:check_amount)
  end

  def order_amount
    PaymentOrder.where(order_id: self.order_id, state: 'confirmed').sum(:check_amount)
  end

  def confirm!
    self.state = 'confirmed'

    begin
      self.save!

      self.class.transaction do
        update_order_state
        update_payment_state
      end
    rescue => e
      raise e
    end
  end

  def revert_confirm!
    self.state = 'init'
    self.save!

    self.class.transaction do
      update_order_state
      update_payment_state
    end
  end

  def update_order_state
    order.received_amount = order_amount
    order.check_state!
  end

  def update_payment_state
    payment.checked_amount = payment_amount
    payment.check_state!
  end

end unless RailsTrade.config.disabled_models.include?('PaymentOrder')

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rails_trade-0.0.1 app/models/rails_trade/payment_order.rb