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.



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

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

Instance Attribute Details

#checksumObject

Returns the value of attribute checksum.



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

def checksum
  @checksum
end

#encodedObject

Returns the value of attribute encoded.



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

def encoded
  @encoded
end

Instance Method Details

#[](key) ⇒ Object



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

def [](key)
  to_hash[key]
end

#decodedObject



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

def decoded
  Base64.strict_decode64(@encoded)
end

#invoiceObject



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

def invoice
  to_hash["INVOICE"]
end


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

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

#response_for(status) ⇒ Object



40
41
42
43
44
# File 'lib/epaybg/response.rb', line 40

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

#statusObject



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

def status
  to_hash["STATUS"]
end

#to_hashObject



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

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

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  Epaybg::hmac(@encoded) == @checksum
end