Class: Paypal::Request

Inherits:
Object
  • Object
show all
Includes:
Formatters
Defined in:
lib/paypal_api/support/request.rb

Direct Known Subclasses

AdaptivePaymentsRequest, MassPayApiRequest

Constant Summary collapse

PAYPAL_VERSION =
"84.0"
@@paypal_info =
nil
@@paypal_endpoint =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Formatters

#escape_uri_component

Constructor Details

#initialize(payload = {}) ⇒ Request

Returns a new instance of Request.



17
18
19
20
21
22
23
24
# File 'lib/paypal_api/support/request.rb', line 17

def initialize(payload = {})
  config

  @payload = payload
  @payload.each do |k,v|
    self.send("#{k}=", v)
  end
end

Instance Attribute Details

#error_messageObject

Returns the value of attribute error_message.



15
16
17
# File 'lib/paypal_api/support/request.rb', line 15

def error_message
  @error_message
end

#payloadObject

Returns the value of attribute payload.



15
16
17
# File 'lib/paypal_api/support/request.rb', line 15

def payload
  @payload
end

#test_requestObject

Returns the value of attribute test_request.



15
16
17
# File 'lib/paypal_api/support/request.rb', line 15

def test_request
  @test_request
end

Class Method Details

.required_keysObject



37
38
39
# File 'lib/paypal_api/support/request.rb', line 37

def self.required_keys
  @required
end

.sequential_keysObject



41
42
43
# File 'lib/paypal_api/support/request.rb', line 41

def self.sequential_keys
  @sequential
end

Instance Method Details

#make(&block) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/paypal_api/support/request.rb', line 101

def make(&block)
  params_fulfilled?
  validate!

  response = make_request

  if block
    yield response
  else
    return response
  end

  do_request = lambda {

  }

  if @test_request
    do_request.call
  else
    begin
      do_request.call
    rescue OpenURI::HTTPError => error
      # status_code = error.io.status[0]
      # Rails.logger.info "[ERROR][Paypal] #{error.message } : #{error.backtrace} " if @@rails
      raise $!
    # rescue Timeout::Error => time_out_error
    #   Rails.logger.info "[ERROR][Timeout Error] #{time_out_error.message} : #{time_out_error.backtrace}" if @@rails
    #   raise $!
    rescue => err
      # Rails.logger.info "[ERROR][Something went wrong] #{err.message} : #{err.backtrace}" if @@rails
      raise $!
    end
  end
end

#make_requestObject

separated out so as not to stub Kernel.open in tests



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/paypal_api/support/request.rb', line 72

def make_request
  response = nil
  if self.respond_to? :headers
    uri = URI.parse(request_string)

    http = Net::HTTP.new(uri.host, uri.port)
    http.set_debug_output $stderr if @test_request
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE

    request = Net::HTTP::Get.new(uri.request_uri)
    headers.each do |k,v|
      request[k] = v
    end

    response = http.request(request).body
  else
    $stderr.puts(request_string) if @test_request

    response = open(request_string)
  end

  return process_response(response)
end

#paypal_endpoint_with_defaultsObject



49
50
51
52
53
54
# File 'lib/paypal_api/support/request.rb', line 49

def paypal_endpoint_with_defaults
  return "#{@@paypal_endpoint}?PWD=#{password}" +
    "&USER=#{user}" +
    "&SIGNATURE=#{signature}" +
    "&VERSION=#{version}"
end

#process_response(response) ⇒ Object



97
98
99
# File 'lib/paypal_api/support/request.rb', line 97

def process_response(response)
  Paypal::Response.new(response)
end

#request_hostObject



45
46
47
# File 'lib/paypal_api/support/request.rb', line 45

def request_host
  URI.parse(@@paypal_endpoint).host
end

#request_stringObject



64
65
66
67
68
69
# File 'lib/paypal_api/support/request.rb', line 64

def request_string
  (@payload.keys | self.class.required_keys).inject(paypal_endpoint_with_defaults + sequentials_string) do |acc, key|
    # if key signature is hash or optional...
    "#{acc}&#{to_key(key)}=#{escape_uri_component(self.send(key))}"
  end
end

#sequentials_stringObject



56
57
58
# File 'lib/paypal_api/support/request.rb', line 56

def sequentials_string
  self.class.sequential_keys.map{|k| self.send(k).to_query_string }.join
end

#to_key(symbol) ⇒ Object



60
61
62
# File 'lib/paypal_api/support/request.rb', line 60

def to_key(symbol)
  return symbol.to_s.gsub(/[^a-z0-9]/i, "").upcase
end

#valid?Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
34
35
# File 'lib/paypal_api/support/request.rb', line 26

def valid?
  begin
    params_fulfilled?
    validate!
    return true
  rescue
    @error_message = $!.message
    return false
  end
end