Sha256: 690b4512197480c4ac9f78ba02ff1d20887969b1247e9d5dff9d256f3da39408

Contents?: true

Size: 850 Bytes

Versions: 3

Compression:

Stored size: 850 Bytes

Contents

module Spree
  class PagseguroCheckout < ActiveRecord::Base
    validates_presence_of :transaction_id

    has_many :payments, as: :source

    def process!
      payments.pending.each do |payment|
        raise RuntimeError.new('Payment and Order not belongs to same transaction') unless payment.order.id === transaction.reference.to_i

        case
          when status_paid? then payment.complete!
          when status_cancelled?
            payment.failure
            payment.order.cancel!
        end
      end
    end

    private
    def transaction
      @transaction ||= PagSeguro::Transaction.find_by_code transaction_id
    end

    def status
      transaction.status
    end

    def status_paid?
      status.paid? || status.available?
    end

    def status_cancelled?
      status.cancelled? || status.refunded?
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
solidus_pagseguro_oficial-1.1.2 app/models/spree/pagseguro_checkout.rb
solidus_pagseguro_oficial-1.1.1 app/models/spree/pagseguro_checkout.rb
solidus_pagseguro_oficial-1.1.0 app/models/spree/pagseguro_checkout.rb