Sha256: a495d5bb6572089dfbb5de1d576dd7e03b49cceeb67270cd647c8aaa550fafa2

Contents?: true

Size: 1.09 KB

Versions: 1

Compression:

Stored size: 1.09 KB

Contents

require 'openssl'

module Ipay
  module Rest
    class Resource
      attr_reader :client

      def initialize(client)
        @client = client
      end

      def post(url, body:, headers: {})
        body[:hash] = create_hash(body.values.join(""))
        handle_response client.connection.post(url, body, default_headers.merge(headers))
      end

      private

      def default_headers
        {}
      end

      def handle_response(response)
        status = response.body['status'] || response.body['header_status']

        case status
        when 0
          raise Error, response.body['text']
        when "fe2707etr5s4wq"
          raise FailedTransactionError
        when "bdi6p2yy76etrs"
          raise PendingTransactionError
        when "cr5i3pgy9867e1"
          raise UsedCodeError
        when "dtfi4p7yty45wq"
          raise PaymentDeficitError
        when "eq3i7p5yt7645e"
          raise OverPaymentError
        end

        response
      end

      def create_hash(data_string)
        OpenSSL::HMAC.hexdigest('sha256', Ipay::Rest::SECRET_KEY, data_string)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ipay-rest-0.1.1 lib/ipay/rest/resource.rb