Sha256: e5a73b91dbe6f642352494ddeceb91d41a9ddc0d805fc0ebb51b2ace8e3b7cfd

Contents?: true

Size: 1.44 KB

Versions: 24

Compression:

Stored size: 1.44 KB

Contents

module Paypal
  module NVP
    class Request < Base
      attr_required :username, :password, :signature
      attr_accessor :version

      ENDPOINT = {
        :production => 'https://api-3t.paypal.com/nvp',
        :sandbox => 'https://api-3t.sandbox.paypal.com/nvp'
      }

      def self.endpoint
        if Paypal.sandbox?
          ENDPOINT[:sandbox]
        else
          ENDPOINT[:production]
        end
      end

      def initialize(attributes = {})
        @version = API_VERSION
        super
      end

      def common_params
        {
          :USER => self.username,
          :PWD => self.password,
          :SIGNATURE => self.signature,
          :VERSION => self.version
        }
      end

      def request(method, params = {})
        handle_response do
          post(method, params)
        end
      end

      private

      def post(method, params)
        RestClient.post(self.class.endpoint, common_params.merge(params).merge(:METHOD => method))
      end

      def handle_response
        response = yield
        response = CGI.parse(response).inject({}) do |res, (k, v)|
          res.merge!(k.to_sym => v.first)
        end
        case response[:ACK]
        when 'Success', 'SuccessWithWarning'
          response
        else
          raise Exception::APIError.new(response)
        end
      rescue RestClient::Exception => e
        raise Exception::HttpError.new(e.http_code, e.message, e.http_body)
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
paypal-express-0.5.0 lib/paypal/nvp/request.rb
paypal-express-0.4.7 lib/paypal/nvp/request.rb
paypal-express-0.5.0.alpha lib/paypal/nvp/request.rb
paypal-express-0.4.6 lib/paypal/nvp/request.rb
paypal-express-0.4.5 lib/paypal/nvp/request.rb
paypal-express-0.4.4 lib/paypal/nvp/request.rb
paypal-express-0.4.3 lib/paypal/nvp/request.rb
paypal-express-0.4.2 lib/paypal/nvp/request.rb
paypal-express-0.4.1 lib/paypal/nvp/request.rb
paypal-express-0.4.0 lib/paypal/nvp/request.rb
paypal-express-0.4.0.alpha lib/paypal/nvp/request.rb
paypal-express-0.3.1 lib/paypal/nvp/request.rb
paypal-express-0.3.0 lib/paypal/nvp/request.rb
paypal-express-0.2.10 lib/paypal/nvp/request.rb
paypal-express-0.2.9 lib/paypal/nvp/request.rb
paypal-express-0.2.8 lib/paypal/nvp/request.rb
paypal-express-0.2.7 lib/paypal/nvp/request.rb
paypal-express-0.2.6 lib/paypal/nvp/request.rb
paypal-express-0.2.5 lib/paypal/nvp/request.rb
paypal-express-0.2.4 lib/paypal/nvp/request.rb