Module: ActiveMerchant::PostsData

Included in:
Billing::Gateway, Billing::Integrations::Nochex::Notification, Billing::Integrations::Paypal::Notification
Defined in:
lib/active_merchant/lib/posts_data.rb

Overview

:nodoc:

Constant Summary collapse

MAX_RETRIES =
3
OPEN_TIMEOUT =
60
READ_TIMEOUT =
60

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/active_merchant/lib/posts_data.rb', line 13

def self.included(base)
  base.superclass_delegating_accessor :ssl_strict
  base.ssl_strict = true
  
  base.class_inheritable_accessor :pem_password
  base.pem_password = false
  
  base.class_inheritable_accessor :retry_safe
  base.retry_safe = false

  base.superclass_delegating_accessor :open_timeout
  base.open_timeout = OPEN_TIMEOUT

  base.superclass_delegating_accessor :read_timeout
  base.read_timeout = READ_TIMEOUT
end

Instance Method Details

#retry_exceptionsObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/active_merchant/lib/posts_data.rb', line 74

def retry_exceptions
  retries = MAX_RETRIES
  begin
    yield
  rescue RetriableConnectionError => e
    retries -= 1
    retry unless retries.zero?
    raise ConnectionError, e.message
  rescue ConnectionError
    retries -= 1
    retry if retry_safe && !retries.zero?
    raise
  end
end

#ssl_post(url, data, headers = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/active_merchant/lib/posts_data.rb', line 30

def ssl_post(url, data, headers = {})
  # Ruby 1.8.4 doesn't automatically set this header
  headers['Content-Type'] ||= "application/x-www-form-urlencoded"
  
  uri   = URI.parse(url)

  http = Net::HTTP.new(uri.host, uri.port) 
  http.open_timeout = self.class.open_timeout
  http.read_timeout = self.class.read_timeout
  http.use_ssl      = true
  
  if ssl_strict
    http.verify_mode    = OpenSSL::SSL::VERIFY_PEER
    http.ca_file        = File.dirname(__FILE__) + '/../../certs/cacert.pem'
  else
    http.verify_mode    = OpenSSL::SSL::VERIFY_NONE
  end
  
  if @options && !@options[:pem].blank?
    http.cert           = OpenSSL::X509::Certificate.new(@options[:pem])
    
    if pem_password
      raise ArgumentError, "The private key requires a password" if @options[:pem_password].blank?
      http.key            = OpenSSL::PKey::RSA.new(@options[:pem], @options[:pem_password])
    else
      http.key            = OpenSSL::PKey::RSA.new(@options[:pem])
    end
  end

  retry_exceptions do 
    begin
      http.post(uri.request_uri, data, headers).body
    rescue EOFError => e
      raise ConnectionError, "The remote server dropped the connection"
    rescue Errno::ECONNRESET => e
      raise ConnectionError, "The remote server reset the connection"
    rescue Errno::ECONNREFUSED => e
      raise RetriableConnectionError, "The remote server refused the connection"
    rescue Timeout::Error, Errno::ETIMEDOUT => e
      raise ConnectionError, "The connection to the remote server timed out"
    end
  end
end