Sha256: b5c9545080829645d3c6863add3796abc73a9622a9b08ad8d0ffb633e0490de9

Contents?: true

Size: 1.28 KB

Versions: 2

Compression:

Stored size: 1.28 KB

Contents

module Paytureman

  class Payment

    attr_accessor :gateway
    attr_reader :order_id
    attr_writer :configuration

    def initialize(order_id, amount, gateway = nil)
      @order_id = order_id
      @amount = amount
      @gateway = gateway
    end

    def save_to_memento(memento)
      memento.order_id = order_id
      memento.amount = amount
      memento.gateway = gateway
    end

    def self.new_from_memento(memento)
      new(memento.order_id, memento.amount, memento.gateway)
    end

    def self.new_from_payment(donor)
      memento = OpenStruct.new
      donor.save_to_memento(memento)
      new_from_memento(memento)
    end

    def current
      current_payment_type = {
        :new => PaymentNew,
        :prepared => PaymentPrepared,
        :authorized => PaymentBlocked,
        :voided => PaymentCancelled,
        :charged => PaymentCharged,
        :refund => PaymentRefunded
      }[payture.status(order_id)] || PaymentUnknown
      current_payment_type.new_from_payment(self)
    end

  protected

    attr_accessor :amount
    attr_writer   :order_id

    def amount_in_cents
      (amount * 100).round
    end

    def configuration
      @configuration ||= Configuration.instance
    end

    def payture
      @payture ||= configuration.api_for(gateway)
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
paytureman-1.0.0 lib/payments/payment.rb
paytureman-0.7.0 lib/payments/payment.rb