Sha256: c9a1e9276aba5c67682852242217b1a12fbda6587caca31a9aee5c6dfefa8846

Contents?: true

Size: 1.01 KB

Versions: 2

Compression:

Stored size: 1.01 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, :no]
      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

2 entries across 2 versions & 1 rubygems

Version Path
epaybg-1.0.0 lib/epaybg/response.rb
epaybg-0.3.1 lib/epaybg/response.rb