Module: Payjp

Defined in:
lib/payjp.rb,
lib/payjp/card.rb,
lib/payjp/plan.rb,
lib/payjp/term.rb,
lib/payjp/util.rb,
lib/payjp/event.rb,
lib/payjp/token.rb,
lib/payjp/charge.rb,
lib/payjp/tenant.rb,
lib/payjp/account.rb,
lib/payjp/balance.rb,
lib/payjp/version.rb,
lib/payjp/customer.rb,
lib/payjp/transfer.rb,
lib/payjp/statement.rb,
lib/payjp/list_object.rb,
lib/payjp/api_resource.rb,
lib/payjp/payjp_object.rb,
lib/payjp/subscription.rb,
lib/payjp/errors/api_error.rb,
lib/payjp/errors/card_error.rb,
lib/payjp/errors/payjp_error.rb,
lib/payjp/api_operations/list.rb,
lib/payjp/api_operations/create.rb,
lib/payjp/api_operations/delete.rb,
lib/payjp/api_operations/update.rb,
lib/payjp/api_operations/request.rb,
lib/payjp/three_d_secure_request.rb,
lib/payjp/errors/api_connection_error.rb,
lib/payjp/errors/authentication_error.rb,
lib/payjp/errors/invalid_request_error.rb

Defined Under Namespace

Modules: APIOperations, Util Classes: APIConnectionError, APIError, APIResource, Account, AuthenticationError, Balance, Card, CardError, Charge, Customer, Event, InvalidRequestError, ListObject, PayjpError, PayjpObject, Plan, Statement, Subscription, Tenant, Term, ThreeDSecureRequest, Token, Transfer

Constant Summary collapse

VERSION =
'0.0.16'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.api_baseObject

Returns the value of attribute api_base.



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

def api_base
  @api_base
end

.api_keyObject

Returns the value of attribute api_key.



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

def api_key
  @api_key
end

.api_versionObject

Returns the value of attribute api_version.



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

def api_version
  @api_version
end

.connect_baseObject

Returns the value of attribute connect_base.



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

def connect_base
  @connect_base
end

.max_retryObject

Returns the value of attribute max_retry.



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

def max_retry
  @max_retry
end

.open_timeoutObject

Returns the value of attribute open_timeout.



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

def open_timeout
  @open_timeout
end

.read_timeoutObject

Returns the value of attribute read_timeout.



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

def read_timeout
  @read_timeout
end

.retry_initial_delayObject

Returns the value of attribute retry_initial_delay.



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

def retry_initial_delay
  @retry_initial_delay
end

.retry_max_delayObject

Returns the value of attribute retry_max_delay.



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

def retry_max_delay
  @retry_max_delay
end

.ssl_ca_fileObject

Returns the value of attribute ssl_ca_file.



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

def ssl_ca_file
  @ssl_ca_file
end

.ssl_ca_pathObject

Returns the value of attribute ssl_ca_path.



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

def ssl_ca_path
  @ssl_ca_path
end

.ssl_cert_storeObject

Returns the value of attribute ssl_cert_store.



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

def ssl_cert_store
  @ssl_cert_store
end

.uploads_baseObject

Returns the value of attribute uploads_base.



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

def uploads_base
  @uploads_base
end

Class Method Details

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



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

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

.get_retry_delay(retry_count, retry_initial_delay, retry_max_delay) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/payjp.rb', line 71

def self.get_retry_delay(retry_count, retry_initial_delay, retry_max_delay)
  # Get retry delay seconds.
  # Based on "Exponential backoff with equal jitter" algorithm.
  # https://aws.amazon.com/jp/blogs/architecture/exponential-backoff-and-jitter/

  wait = [retry_max_delay, retry_initial_delay * 2 ** retry_count].min
  random = Random.new()
  (wait / 2) + (random.rand(wait / 2.0))
end

.request(method, url, api_key, params = {}, headers = {}, api_base_url = nil, open_timeout = nil, read_timeout = nil, ssl_ca_file = nil, ssl_ca_path = nil, ssl_cert_store = nil, max_retry = nil, retry_initial_delay = nil, retry_max_delay = nil) ⇒ Object



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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/payjp.rb', line 81

def self.request(method, url, api_key, params = {}, headers = {}, api_base_url = nil, open_timeout = nil, read_timeout = nil, ssl_ca_file = nil, ssl_ca_path = nil, ssl_cert_store = nil, max_retry = nil, retry_initial_delay= nil, retry_max_delay = nil)
  api_base_url ||= @api_base
  open_timeout ||= @open_timeout
  read_timeout ||= @read_timeout
  ssl_ca_file ||= @ssl_ca_file
  ssl_ca_path ||= @ssl_ca_path
  ssl_cert_store ||= @ssl_cert_store
  max_retry ||= @max_retry
  retry_initial_delay ||= @retry_initial_delay
  retry_max_delay ||= @retry_max_delay

  unless api_key ||= @api_key
    raise AuthenticationError.new('No API key provided. ' \
      'Set your API key using "Payjp.api_key = <API-KEY>". ' \
      'You can generate API keys from the Payjp web interface. ' \
      'See https://pay.jp/api for details, or email [email protected] ' \
      '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 ' \
      'Payjp web interface. See https://pay.jp/api for details, or ' \
      'email [email protected] if you have any questions.)')
  end

  request_opts = {}

  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, :payload => payload, :url => url,
                      :open_timeout => open_timeout, :read_timeout => read_timeout,
                      :ssl_ca_file => ssl_ca_file, :ssl_ca_path => ssl_ca_path,
                      :ssl_cert_store => ssl_cert_store)

  retry_count = 1

  begin
    # $stderr.puts request_opts

    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 e.http_code == 429 and retry_count <= max_retry then
      sleep get_retry_delay(retry_count, retry_initial_delay, retry_max_delay)
      retry_count += 1
      retry
    end

    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