Sha256: 8b64fa4b03184baf025a01127ccf140ef8124e3f1a6b82220be172eb33ce8948

Contents?: true

Size: 1 KB

Versions: 1

Compression:

Stored size: 1 KB

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? secret
      Epaybg.hmac(@encoded, secret) == @checksum
    end

    def status
      to_hash['STATUS']
    end

    def invoice
      to_hash['INVOICE']
    end

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

    def bcode
      to_hash['BCODE'] if status == 'PAID'
    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=#{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.3.0 lib/epaybg/response.rb