Module: Stripe

Defined in:
lib/stripe.rb,
lib/stripe/util.rb,
lib/stripe/plan.rb,
lib/stripe/card.rb,
lib/stripe/event.rb,
lib/stripe/token.rb,
lib/stripe/refund.rb,
lib/stripe/coupon.rb,
lib/stripe/charge.rb,
lib/stripe/invoice.rb,
lib/stripe/account.rb,
lib/stripe/balance.rb,
lib/stripe/version.rb,
lib/stripe/customer.rb,
lib/stripe/transfer.rb,
lib/stripe/recipient.rb,
lib/stripe/list_object.rb,
lib/stripe/file_upload.rb,
lib/stripe/api_resource.rb,
lib/stripe/invoice_item.rb,
lib/stripe/subscription.rb,
lib/stripe/stripe_object.rb,
lib/stripe/application_fee.rb,
lib/stripe/errors/api_error.rb,
lib/stripe/errors/card_error.rb,
lib/stripe/balance_transaction.rb,
lib/stripe/api_operations/list.rb,
lib/stripe/errors/stripe_error.rb,
lib/stripe/api_operations/update.rb,
lib/stripe/api_operations/create.rb,
lib/stripe/api_operations/delete.rb,
lib/stripe/certificate_blacklist.rb,
lib/stripe/singleton_api_resource.rb,
lib/stripe/application_fee_refund.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, CertificateBlacklist, Util Classes: APIConnectionError, APIError, APIResource, Account, ApplicationFee, ApplicationFeeRefund, AuthenticationError, Balance, BalanceTransaction, Card, CardError, Charge, Coupon, Customer, Event, FileUpload, InvalidRequestError, Invoice, InvoiceItem, ListObject, Plan, Recipient, Refund, SingletonAPIResource, StripeError, StripeObject, Subscription, Token, Transfer

Constant Summary

DEFAULT_CA_BUNDLE_PATH =
File.dirname(__FILE__) + '/data/ca-certificates.crt'
VERSION =
'1.17.3'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.api_baseObject

Returns the value of attribute api_base



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

def api_base
  @api_base
end

.api_keyObject

Returns the value of attribute api_key



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

def api_key
  @api_key
end

.api_versionObject

Returns the value of attribute api_version



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

def api_version
  @api_version
end

.connect_baseObject

Returns the value of attribute connect_base



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

def connect_base
  @connect_base
end

.verify_ssl_certsObject

Returns the value of attribute verify_ssl_certs



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

def verify_ssl_certs
  @verify_ssl_certs
end

Class Method Details

.api_url(url = '', api_base_url = nil) ⇒ Object



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

def self.api_url(url='', api_base_url=nil)
  (api_base_url || @api_base) + url
end

.request(method, url, api_key, params = {}, headers = {}, api_base_url = nil) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
# File 'lib/stripe.rb', line 71

def self.request(method, url, api_key, params={}, headers={}, api_base_url=nil)
  api_base_url = api_base_url || @api_base

  unless api_key ||= @api_key
    raise AuthenticationError.new('No API key provided. ' +
      '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.')
  end

  if api_key =~ /\s/
    raise AuthenticationError.new('Your API key is invalid, as it contains ' +
      'whitespace. (HINT: You can double-check your API key from the ' +
      'Stripe web interface. See https://stripe.com/api for details, or ' +
      'email support@stripe.com if you have any questions.)')
  end

  request_opts = { :verify_ssl => false }

  if ssl_preflight_passed?
    request_opts.update(:verify_ssl => OpenSSL::SSL::VERIFY_PEER,
                        :ssl_ca_file => @ssl_bundle_path)
  end

  if @verify_ssl_certs and !@CERTIFICATE_VERIFIED
    @CERTIFICATE_VERIFIED = CertificateBlacklist.check_ssl_cert(api_base_url, @ssl_bundle_path)
  end

  params = Util.objects_to_ids(params)
  url = api_url(url, api_base_url)

  case method.to_s.downcase.to_sym
  when :get, :head, :delete
    # Make params into GET parameters
    url += "#{URI.parse(url).query ? '&' : '?'}#{uri_encode(params)}" if params && params.any?
    payload = nil
  else
    if headers[:content_type] && headers[:content_type] == "multipart/form-data"
      payload = params
    else
      payload = uri_encode(params)
    end
  end

  request_opts.update(:headers => request_headers(api_key).update(headers),
                      :method => method, :open_timeout => 30,
                      :payload => payload, :url => url, :timeout => 80)

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

  [parse(response), api_key]
end