Sha256: df89ae983ba00e97188a41f3d935cd6de1adda35921cf41a116601bc56506cdb

Contents?: true

Size: 1.54 KB

Versions: 4

Compression:

Stored size: 1.54 KB

Contents

module Paypal
  module NVP
    class Request < Base
      attr_required :username, :password, :signature
      attr_optional :subject
      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 = Paypal.api_version
        super
      end

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

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

      private

      def post(method, params)
        rest_params = common_params.merge(params).merge(METHOD: method)
        RestClient.post(self.class.endpoint, rest_params)
      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

4 entries across 4 versions & 1 rubygems

Version Path
creative-paypal-express-1.0.1.dev2 lib/paypal/nvp/request.rb
creative-paypal-express-1.0.1.dev1 lib/paypal/nvp/request.rb
creative-paypal-express-1.0.0 lib/paypal/nvp/request.rb
creative-paypal-express-0.8.2.4 lib/paypal/nvp/request.rb