Sha256: ed9ed5ce3e3b0187a993d237d482730a26368129fd313a2c20afbcad4590b305

Contents?: true

Size: 796 Bytes

Versions: 3

Compression:

Stored size: 796 Bytes

Contents

module JdPay
  class Result < ::Hash
    SUCCESS_FLAG = '000000'.freeze

    def initialize(result, options = {})
      super nil

      self['jdpay'] = result['jdpay']

      if result['jdpay'].class == Hash && (decrypt = self.decrypt_verify(options = {})).class == Hash
        self['jdpay'] = decrypt['jdpay']
      end
    end

    def success?
      self['jdpay']['result']['code'] == SUCCESS_FLAG
    end

    def decrypt_verify(options = {})
      if self.success?
        content_hash = Hash.from_xml JdPay::Des.decrypt_3des(Base64.decode64(self['jdpay']['encrypt']), options)
        JdPay::Sign.rsa_verify?(content_hash, options) ? content_hash : (raise "JdPay_verify_err:#{content_hash}")
      else
        raise "JdPay::Result#decrypt_verify_err:#{self}"
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
jdpay-0.1.3 lib/jd_pay/result.rb
jdpay-0.1.2 lib/jd_pay/result.rb
jdpay-0.1.1 lib/jd_pay/result.rb