Class: Privatbank::P24::AccountStatement

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/privatbank/p24/account_statement.rb

Instance Method Summary collapse

Constructor Details

#initialize(card_number, options = {}) ⇒ AccountStatement

Returns a new instance of AccountStatement.



15
16
17
18
19
20
21
# File 'lib/privatbank/p24/account_statement.rb', line 15

def initialize card_number, options={}
  @card_number       = card_number
  @from_date         = options[:from_date]
  @till_date         = options[:till_date]
  @merchant_id       = options[:merchant_id]
  @merchant_password = options[:merchant_password]
end

Instance Method Details

#from_dateObject



77
78
79
# File 'lib/privatbank/p24/account_statement.rb', line 77

def from_date
  @from_date.strftime('%d.%m.%Y')
end

#outgoing_xmlObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/privatbank/p24/account_statement.rb', line 39

def outgoing_xml
  builder = Builder::XmlMarkup.new
  builder.instruct!
  builder.request(version: '1.0') do |req|
    req.merchant do |merch|
      merch.id(@merchant_id)
      merch.signature(signature)
    end
    req.data do |d|
      d.oper('cmt')
      d.wait(0)
      d.payment(id: '') do |p|
        p.prop(name: 'sd',   value: from_date)
        p.prop(name: 'ed',   value: till_date)
        p.prop(name: 'card', value: @card_number)
      end
    end
  end
  builder.target!
end

#requestObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/privatbank/p24/account_statement.rb', line 23

def request
  response = self.class.post('/p24api/rest_fiz', body: outgoing_xml)
  return [] if response.fetch('error', nil)
  statements = response['response']['data']['info']['statements']['statement']
  statements = [statements] if statements.is_a?(Hash)
  statements.map do |operation|
    Items::Transaction.new( date:           operation['trandate'],
                            time:           operation['trantime'],
                            amount:         operation['amount'],
                            card_amount:    operation['cardamount'],
                            rest:           operation['rest'],
                            description:    operation['terminal'],
                            transaction_id: operation['appcode'])
  end
end

#request_xml_dataObject



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/privatbank/p24/account_statement.rb', line 60

def request_xml_data
  builder = Builder::XmlMarkup.new
  builder.oper('cmt')
  builder.wait(0)
  builder.payment(id: '') do |p|
    p.prop(name: 'sd',   value: from_date)
    p.prop(name: 'ed',   value: till_date)
    p.prop(name: 'card', value: @card_number)
  end
  builder.target!
end

#signatureObject



72
73
74
# File 'lib/privatbank/p24/account_statement.rb', line 72

def signature
  Privatbank::Signature.generate(request_xml_data, @merchant_password)
end

#till_dateObject



81
82
83
# File 'lib/privatbank/p24/account_statement.rb', line 81

def till_date
  @till_date.strftime('%d.%m.%Y')
end