Class: Qiwi::Transaction

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations, Observable
Defined in:
lib/qiwi/transaction.rb

Defined Under Namespace

Classes: CorrectAmountValidator

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(txn) ⇒ Transaction

Returns a new instance of Transaction.



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/qiwi/transaction.rb', line 32

def initialize(txn)
  @txn = txn

  # A logging observer
  add_observer(self, :log_transaction)

  if block_given?
    yield self
  else
    Qiwi.config.transaction_handler.call(self) if Qiwi.config.transaction_handler
  end
end

Instance Attribute Details

#finderObject

Finder should respond_to?(:find_by_txn) and return an object, which can respond_to?(:amount)



30
31
32
# File 'lib/qiwi/transaction.rb', line 30

def finder
  @finder
end

#txnObject (readonly)

Transaction id



26
27
28
# File 'lib/qiwi/transaction.rb', line 26

def txn
  @txn
end

Instance Method Details

#amountObject



64
65
66
# File 'lib/qiwi/transaction.rb', line 64

def amount
  persisted.amount if exists?
end

#commit!Object



50
51
52
53
# File 'lib/qiwi/transaction.rb', line 50

def commit!
  changed
  notify_observers(self)
end

#exists?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/qiwi/transaction.rb', line 55

def exists?
  !!persisted
end

#find(txn) ⇒ Object



80
81
82
# File 'lib/qiwi/transaction.rb', line 80

def find(txn)
  finder.find_by_txn(txn) if finder
end

#inspectObject



45
46
47
48
# File 'lib/qiwi/transaction.rb', line 45

def inspect
  error_msgs = errors.full_messages.join(', ')
  %{<Qiwi::Transaction id: #{txn}, remote: #{remote.inspect} persisted: #{persisted.inspect} errors: #{error_msgs}}
end

#log_transaction(transaction) ⇒ Object



68
69
70
# File 'lib/qiwi/transaction.rb', line 68

def log_transaction(transaction)
  Qiwi.logger.info "Transaction update: #{transaction.inspect}"
end

#persistedObject



76
77
78
# File 'lib/qiwi/transaction.rb', line 76

def persisted
  @persisted ||= find(txn)
end

#remoteObject



72
73
74
# File 'lib/qiwi/transaction.rb', line 72

def remote
  @remote ||= Qiwi::Client.new.check_bill(txn: txn)
end

#valid_amount?Boolean

Returns:

  • (Boolean)


59
60
61
62
# File 'lib/qiwi/transaction.rb', line 59

def valid_amount?
  valid?
  errors[:amount].empty?
end