Class: Epaybg::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/epaybg/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(encoded, checksum) ⇒ Response

Returns a new instance of Response.



5
6
7
8
# File 'lib/epaybg/response.rb', line 5

def initialize(encoded, checksum)
  @encoded = encoded
  @checksum = checksum
end

Instance Attribute Details

#checksumObject

Returns the value of attribute checksum.



3
4
5
# File 'lib/epaybg/response.rb', line 3

def checksum
  @checksum
end

#encodedObject

Returns the value of attribute encoded.



3
4
5
# File 'lib/epaybg/response.rb', line 3

def encoded
  @encoded
end

Instance Method Details

#[](key) ⇒ Object



43
44
45
# File 'lib/epaybg/response.rb', line 43

def [](key)
  to_hash[key]
end

#bcodeObject



35
36
37
# File 'lib/epaybg/response.rb', line 35

def bcode
  to_hash['BCODE'] if status == 'PAID'
end

#decodedObject



10
11
12
# File 'lib/epaybg/response.rb', line 10

def decoded
  Base64.strict_decode64(@encoded)
end

#invoiceObject



27
28
29
# File 'lib/epaybg/response.rb', line 27

def invoice
  to_hash['INVOICE']
end


39
40
41
# File 'lib/epaybg/response.rb', line 39

def paid_on
  DateTime.parse(to_hash['PAY_TIME']) if status == 'PAID'
end

#response_for(status) ⇒ Object



47
48
49
50
51
# File 'lib/epaybg/response.rb', line 47

def response_for(status)
  response_statuses = [:ok, :err, :no]
  raise "Status must be one of #{response_statuses}" unless response_statuses.include? status
  "INVOICE=#{to_hash['INVOICE']}:STATUS=#{status.to_s.upcase}"
end

#stanObject



31
32
33
# File 'lib/epaybg/response.rb', line 31

def stan
  to_hash['STAN'] if status == 'PAID'
end

#statusObject



23
24
25
# File 'lib/epaybg/response.rb', line 23

def status
  to_hash['STATUS']
end

#to_hashObject



14
15
16
17
# File 'lib/epaybg/response.rb', line 14

def to_hash
  i = decoded.strip.split(':').flat_map { |c| c.split('=') }
  Hash[*i]
end

#valid?(secret) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/epaybg/response.rb', line 19

def valid? secret
  Epaybg.hmac(@encoded, secret) == @checksum
end