Sha256: 4dc591da0793afeeeaa7201ab53f8a5a349df1b9bed79b216c23c05cc6954dc9

Contents?: true

Size: 1.14 KB

Versions: 3

Compression:

Stored size: 1.14 KB

Contents

module Moneytree
  class Refund < Moneytree::Transaction
    belongs_to :payment, class_name: 'Moneytree::Payment'

    before_validation :set_order, :set_payment_gateway

    validates_presence_of :payment

    validates_numericality_of :amount, less_than: 0
    validates_numericality_of :app_fee_amount, less_than_or_equal_to: 0

    validate :order_matches_payment, :gateway_matches_payment

    private

    def execute_transaction(metadata: {})
      process_response(
        payment_gateway.refund(
          amount,
          payment.details,
          metadata: metadata.merge(moneytree_transaction_id: id)
        )
      )
    end

    # validates_presence_of :payment
    def set_order
      self.order ||= payment&.order
    end

    # validates_presence_of :payment
    def set_payment_gateway
      self.payment_gateway ||= payment&.payment_gateway
    end

    # validates
    def order_matches_payment
      errors.add(:order_id, :mismatch) if order_id != payment&.order_id
    end

    # validates
    def gateway_matches_payment
      errors.add(:payment_gateway_id, :mismatch) if payment_gateway_id != payment&.payment_gateway_id
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
moneytree-rails-0.1.11 app/models/moneytree/refund.rb
moneytree-rails-0.1.10 app/models/moneytree/refund.rb
moneytree-rails-0.1.9 app/models/moneytree/refund.rb