Module: Stripe

Defined in:
lib/stripe.rb,
lib/stripe/util.rb,
lib/stripe/json.rb,
lib/stripe/plan.rb,
lib/stripe/event.rb,
lib/stripe/token.rb,
lib/stripe/coupon.rb,
lib/stripe/charge.rb,
lib/stripe/account.rb,
lib/stripe/version.rb,
lib/stripe/invoice.rb,
lib/stripe/transfer.rb,
lib/stripe/customer.rb,
lib/stripe/list_object.rb,
lib/stripe/api_resource.rb,
lib/stripe/invoice_item.rb,
lib/stripe/stripe_object.rb,
lib/stripe/errors/api_error.rb,
lib/stripe/errors/card_error.rb,
lib/stripe/errors/stripe_error.rb,
lib/stripe/api_operations/list.rb,
lib/stripe/api_operations/update.rb,
lib/stripe/api_operations/create.rb,
lib/stripe/api_operations/delete.rb,
lib/stripe/singleton_api_resource.rb,
lib/stripe/errors/api_connection_error.rb,
lib/stripe/errors/authentication_error.rb,
lib/stripe/errors/invalid_request_error.rb

Defined Under Namespace

Modules: APIOperations, JSON, Util Classes: APIConnectionError, APIError, APIResource, Account, AuthenticationError, CardError, Charge, Coupon, Customer, Event, InvalidRequestError, Invoice, InvoiceItem, ListObject, Plan, SingletonAPIResource, StripeError, StripeObject, Token, Transfer

Constant Summary

VERSION =
'1.7.11'
@@ssl_bundle_path =
File.join(File.dirname(__FILE__), 'data/ca-certificates.crt')
@@api_key =
nil
@@api_base =
'https://api.stripe.com'
@@verify_ssl_certs =
true
@@api_version =
nil

Class Method Summary (collapse)

Class Method Details

+ (Object) api_base



67
68
69
# File 'lib/stripe.rb', line 67

def self.api_base
  @@api_base
end

+ (Object) api_base=(api_base)



63
64
65
# File 'lib/stripe.rb', line 63

def self.api_base=(api_base)
  @@api_base = api_base
end

+ (Object) api_key



59
60
61
# File 'lib/stripe.rb', line 59

def self.api_key
  @@api_key
end

+ (Object) api_key=(api_key)



55
56
57
# File 'lib/stripe.rb', line 55

def self.api_key=(api_key)
  @@api_key = api_key
end

+ (Object) api_url(url = '')



51
52
53
# File 'lib/stripe.rb', line 51

def self.api_url(url='')
  @@api_base + url
end

+ (Object) api_version



83
84
85
# File 'lib/stripe.rb', line 83

def self.api_version
  @@api_version
end

+ (Object) api_version=(version)



79
80
81
# File 'lib/stripe.rb', line 79

def self.api_version=(version)
  @@api_version = version
end

+ (Object) request(method, url, api_key, params = {}, headers = {})



87
88
89
90
91
92
93
94
95
96
97
98
99
100
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/stripe.rb', line 87

def self.request(method, url, api_key, params={}, headers={})
  api_key ||= @@api_key
  raise AuthenticationError.new('No API key provided.  (HINT: set your API key using "Stripe.api_key = <API-KEY>".  You can generate API keys from the Stripe web interface.  See https://stripe.com/api for details, or email support@stripe.com if you have any questions.)') unless api_key

  if !verify_ssl_certs
    unless @no_verify
      $stderr.puts "WARNING: Running without SSL cert verification.  Execute 'Stripe.verify_ssl_certs = true' to enable verification."
      @no_verify = true
    end
    ssl_opts = { :verify_ssl => false }
  elsif !Util.file_readable(@@ssl_bundle_path)
    unless @no_bundle
      $stderr.puts "WARNING: Running without SSL cert verification because #{@@ssl_bundle_path} isn't readable"
      @no_bundle = true
    end
    ssl_opts = { :verify_ssl => false }
  else
    ssl_opts = {
      :verify_ssl => OpenSSL::SSL::VERIFY_PEER,
      :ssl_ca_file => @@ssl_bundle_path
    }
  end
  uname = (@@uname ||= RUBY_PLATFORM =~ /linux|darwin/i ? `uname -a 2>/dev/null`.strip : nil)
  lang_version = "#{RUBY_VERSION} p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE})"
  ua = {
    :bindings_version => Stripe::VERSION,
    :lang => 'ruby',
    :lang_version => lang_version,
    :platform => RUBY_PLATFORM,
    :publisher => 'stripe',
    :uname => uname
  }

  params = Util.objects_to_ids(params)
  url = self.api_url(url)
  case method.to_s.downcase.to_sym
  when :get, :head, :delete
    # Make params into GET parameters
    if params && params.count > 0
      query_string = Util.flatten_params(params).collect{|key, value| "#{key}=#{Util.url_encode(value)}"}.join('&')
      url += "?#{query_string}"
    end
    payload = nil
  else
    payload = Util.flatten_params(params).collect{|(key, value)| "#{key}=#{Util.url_encode(value)}"}.join('&')
  end

  begin
    headers = { :x_stripe_client_user_agent => Stripe::JSON.dump(ua) }.merge(headers)
  rescue => e
    headers = {
      :x_stripe_client_raw_user_agent => ua.inspect,
      :error => "#{e} (#{e.class})"
    }.merge(headers)
  end

  headers = {
    :user_agent => "Stripe/v1 RubyBindings/#{Stripe::VERSION}",
    :authorization => "Bearer #{api_key}",
    :content_type => 'application/x-www-form-urlencoded'
  }.merge(headers)

  if self.api_version
    headers[:stripe_version] = self.api_version
  end

  opts = {
    :method => method,
    :url => url,
    :headers => headers,
    :open_timeout => 30,
    :payload => payload,
    :timeout => 80
  }.merge(ssl_opts)

  begin
    response = execute_request(opts)
  rescue SocketError => e
    self.handle_restclient_error(e)
  rescue NoMethodError => e
    # Work around RestClient bug
    if e.message =~ /\WRequestFailed\W/
      e = APIConnectionError.new('Unexpected HTTP response code')
      self.handle_restclient_error(e)
    else
      raise
    end
  rescue RestClient::ExceptionWithResponse => e
    if rcode = e.http_code and rbody = e.http_body
      self.handle_api_error(rcode, rbody)
    else
      self.handle_restclient_error(e)
    end
  rescue RestClient::Exception, Errno::ECONNREFUSED => e
    self.handle_restclient_error(e)
  end

  rbody = response.body
  rcode = response.code
  begin
    # Would use :symbolize_names => true, but apparently there is
    # some library out there that makes symbolize_names not work.
    resp = Stripe::JSON.load(rbody)
  rescue MultiJson::DecodeError
    raise APIError.new("Invalid response object from API: #{rbody.inspect} (HTTP response code was #{rcode})", rcode, rbody)
  end

  resp = Util.symbolize_names(resp)
  [resp, api_key]
end

+ (Object) verify_ssl_certs



75
76
77
# File 'lib/stripe.rb', line 75

def self.verify_ssl_certs
  @@verify_ssl_certs
end

+ (Object) verify_ssl_certs=(verify)



71
72
73
# File 'lib/stripe.rb', line 71

def self.verify_ssl_certs=(verify)
  @@verify_ssl_certs = verify
end