Sha256: d083573b25ad641133f61a8d710e90940279f2e8303af900b266e794c9b8d582

Contents?: true

Size: 888 Bytes

Versions: 1

Compression:

Stored size: 888 Bytes

Contents

module Epaybg

  class Response
    attr_accessor :encoded, :checksum

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

    def decoded
      Base64.strict_decode64(@encoded)
    end

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

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

    def status
      to_hash["STATUS"]
    end

    def invoice
      to_hash["INVOICE"]
    end

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

    def [](key)
      to_hash[key]
    end

    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

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
epaybg-0.1.2 lib/epaybg/response.rb