Class: WillPaypal

Inherits:
Object
  • Object
show all
Defined in:
lib/will_paypal.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  :version => "72.0",
  :sandbox => false,
  :params => {}
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ WillPaypal

Returns a new instance of WillPaypal.



15
16
17
18
19
# File 'lib/will_paypal.rb', line 15

def initialize(config={})
  self.config = DEFAULT_OPTIONS.merge(config)
  self.logger = config.delete(:logger) || Logger.new(STDOUT)
  self.config[:url] ||= self.config[:sandbox] ? "https://api-3t.sandbox.paypal.com/nvp" : "https://api-3t.paypal.com/nvp"
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



7
8
9
# File 'lib/will_paypal.rb', line 7

def config
  @config
end

#loggerObject

Returns the value of attribute logger.



7
8
9
# File 'lib/will_paypal.rb', line 7

def logger
  @logger
end

Instance Method Details

#call_paypal(data) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/will_paypal.rb', line 45

def call_paypal(data)
  uri = URI.parse(self.config[:url])

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  rootCA = '/etc/ssl/certs'
  if File.directory? rootCA
    http.ca_path = rootCA
    http.verify_mode = OpenSSL::SSL::VERIFY_PEER
    http.verify_depth = 5
  else
    self.logger.warn "WARNING: no ssl certs found. Paypal communication will be insecure. DO NOT DEPLOY"
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end

  response = http.request_post(uri.path, self.query_string_for(data))
  response_hash = { :status => response.code, :body => response.body, :parsed_body => {} }

  if response.kind_of? Net::HTTPSuccess
    response_hash[:parsed_body].merge! self.hash_from_query_string(response.body)
  end
  response_hash
end

#hash_from_query_string(query_string) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/will_paypal.rb', line 36

def hash_from_query_string(query_string)
  hash = {}
  query_string.split("&").each do |element|
    a = element.split("=")
    hash[a[0]] = CGI.unescape(a[1]) if a.size == 2
  end
  hash
end

#query_string_for(data) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/will_paypal.rb', line 21

def query_string_for(data)
  data.merge!({
   "USER"       => self.config[:user],
   "PWD"        => self.config[:password],
   "SIGNATURE"  => self.config[:signature],
   "VERSION"    => self.config[:version]
  })
  data.merge!(self.config[:params])
  query = []
  data.each do |key, value|
    query << "#{key.to_s.upcase}=#{CGI.escape(value.to_s)}"
  end
  query.sort.join("&")
end