Class: Stripe::StripeClient

Inherits:
Object
  • Object
show all
Extended by:
Gem::Deprecate
Defined in:
lib/stripe/stripe_client.rb

Constant Summary collapse

CLIENT_OPTIONS =

For internal use only. Does not provide a stable API and may be broken with future non-major changes.

Set.new(%i[api_key stripe_account stripe_context api_version api_base uploads_base connect_base meter_events_base client_id])

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, stripe_account: nil, stripe_context: nil, stripe_version: nil, api_base: nil, uploads_base: nil, connect_base: nil, meter_events_base: nil, client_id: nil) ⇒ StripeClient

Initializes a new StripeClient



22
23
24
25
26
27
28
29
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
# File 'lib/stripe/stripe_client.rb', line 22

def initialize(api_key,
               stripe_account: nil,
               stripe_context: nil,
               stripe_version: nil,
               api_base: nil,
               uploads_base: nil,
               connect_base: nil,
               meter_events_base: nil,
               client_id: nil)
  unless api_key
    raise AuthenticationError, "No API key provided. " \
                               'Set your API key using "client = Stripe::StripeClient.new(<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

  config_opts = {
    api_key: api_key,
    stripe_account: ,
    stripe_context: stripe_context,
    api_version: stripe_version,
    api_base: api_base,
    uploads_base: uploads_base,
    connect_base: connect_base,
    meter_events_base: meter_events_base,
    client_id: client_id,
  }.compact

  config = StripeConfiguration.client_init(config_opts)
  @requestor = APIRequestor.new(config)

  # top-level services: The beginning of the section generated from our OpenAPI spec
  @v1 = Stripe::V1Services.new(@requestor)
  @v2 = Stripe::V2Services.new(@requestor)
  # top-level services: The end of the section generated from our OpenAPI spec
end

Instance Attribute Details

#requestorObject (readonly)

For internal use only. Does not provide a stable API and may be broken with future non-major changes.



15
16
17
# File 'lib/stripe/stripe_client.rb', line 15

def requestor
  @requestor
end

#v1Object (readonly)

attr_readers: The beginning of the section generated from our OpenAPI spec



9
10
11
# File 'lib/stripe/stripe_client.rb', line 9

def v1
  @v1
end

#v2Object (readonly)

Returns the value of attribute v2.



10
11
12
# File 'lib/stripe/stripe_client.rb', line 10

def v2
  @v2
end

Instance Method Details

#deserialize(data, api_mode: :v1) ⇒ Object



120
121
122
123
# File 'lib/stripe/stripe_client.rb', line 120

def deserialize(data, api_mode: :v1)
  data = JSON.parse(data) if data.is_a?(String)
  Util.convert_to_stripe_object(data, {}, api_mode: api_mode, requestor: @requestor)
end

#notification_handler(webhook_secret, &fallback_callback) ⇒ Object



125
126
127
# File 'lib/stripe/stripe_client.rb', line 125

def notification_handler(webhook_secret, &fallback_callback)
  ::Stripe::StripeEventNotificationHandler.new(self, webhook_secret, &fallback_callback)
end

#parse_event_notification(payload, sig_header, secret, tolerance: Webhook::DEFAULT_TOLERANCE) ⇒ Object

Constructs a thin event notification from an incoming webhook after verifying its authenticity. To work with a webhook that has already been verified (i.e. one from a cloud provider, an asynchronous queue, or during testing), see parse_event_notification_without_verification.



70
71
72
73
74
75
76
# File 'lib/stripe/stripe_client.rb', line 70

def parse_event_notification(payload, sig_header, secret, tolerance: Webhook::DEFAULT_TOLERANCE)
  payload = payload.force_encoding("UTF-8") if payload.respond_to?(:force_encoding)

  # v2 events use the same signing mechanism as v1 events
  Webhook::Signature.verify_header(payload, sig_header, secret, tolerance: tolerance)
  build_event_notification(payload)
end

#parse_event_notification_without_verification(payload) ⇒ Object

Constructs a thin event notification from an incoming webhook without first verifying its authenticity. Should be used after calling Webhook::Signature.verify_header or with input from a trusted source (such as AWS EventBridge, or Azure Event Grid payload). Or, to verify & parse in a single call, use parse_event_notification instead.



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

def parse_event_notification_without_verification(payload)
  build_event_notification(Webhook.send(:_maybe_extract_from_cloud_provider_envelope, payload))
end

#raw_request(method, url, base_address: :api, params: {}, opts: {}, usage: nil) ⇒ Object



110
111
112
113
114
115
116
117
118
# File 'lib/stripe/stripe_client.rb', line 110

def raw_request(method, url, base_address: :api, params: {}, opts: {}, usage: nil)
  opts = Util.normalize_opts(opts)
  req_opts = RequestOptions.extract_opts_from_hash(opts)

  params = params.to_h if params.is_a?(Stripe::RequestParams)
  resp, = @requestor.send(:execute_request_internal, method, url, base_address, params, req_opts, usage: usage || ["raw_request"])

  @requestor.interpret_response(resp)
end

#request(&block) ⇒ Object



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

def request(&block)
  @requestor.request(&block)
end