Sha256: 75e2e92764909a67383c94875629190b0f0d636098c731015971433a5520df4c

Contents?: true

Size: 1.13 KB

Versions: 6

Compression:

Stored size: 1.13 KB

Contents

module Bs2Api
  module Payment
    class Base
      attr_reader :payment

      def initialize
        raise NoMethodError, "Missing #{__method__} to #{self.class}"
      end

      def call
        response = post_request
        raise Bs2Api::Errors::BadRequest, ::Util::Response.parse_error(response) unless response.ok?

        @payment = Bs2Api::Entities::Payment.from_response(response)
        self
      end

      private

      def post_request
        HTTParty.post(url, headers: headers, body: payload.to_json)
      end

      def headers
        {
          'Content-Type': 'application/json',
          'Accept': 'application/json',
          'Authorization': "Bearer #{bearer_token}"
        }
      end

      def bearer_token
        Bs2Api::Request::Auth.token(
          client_id: @client_id || Bs2Api.configuration.client_id,
          client_secret: @client_secret || Bs2Api.configuration.client_secret
        )
      end

      def payload
        raise NoMethodError, "Missing #{__method__} to #{self.class}"
      end

      def url
        raise NoMethodError, "Missing #{__method__} to #{self.class}"
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
bs2_api-1.5.1 lib/bs2_api/payment/base.rb
bs2_api-1.5.0 lib/bs2_api/payment/base.rb
bs2_api-1.4.3 lib/bs2_api/payment/base.rb
bs2_api-1.4.2 lib/bs2_api/payment/base.rb
bs2_api-1.4.1 lib/bs2_api/payment/base.rb
bs2_api-1.4.0 lib/bs2_api/payment/base.rb