Class: Dodopayments::Client

Inherits:
Internal::Transport::BaseClient show all
Defined in:
lib/dodopayments/client.rb

Constant Summary collapse

DEFAULT_MAX_RETRIES =

Default max number of retries to attempt after a failed retryable request.

2
DEFAULT_TIMEOUT_IN_SECONDS =

Default per-request timeout.

60.0
DEFAULT_INITIAL_RETRY_DELAY =

Default initial retry delay in seconds. Overall delay is calculated using exponential backoff + jitter.

0.5
DEFAULT_MAX_RETRY_DELAY =

Default max retry delay in seconds.

8.0
ENVIRONMENTS =

rubocop:disable Style/MutableConstant

{live_mode: "https://live.dodopayments.com", test_mode: "https://test.dodopayments.com"}

Constants inherited from Internal::Transport::BaseClient

Internal::Transport::BaseClient::MAX_REDIRECTS, Internal::Transport::BaseClient::PLATFORM_HEADERS

Instance Attribute Summary collapse

Attributes inherited from Internal::Transport::BaseClient

#base_url, #headers, #idempotency_header, #initial_retry_delay, #max_retries, #max_retry_delay, #requester, #timeout

Instance Method Summary collapse

Methods inherited from Internal::Transport::BaseClient

follow_redirect, #inspect, reap_connection!, #request, #send_request, should_retry?, validate!

Methods included from Internal::Util::SorbetRuntimeSupport

#const_missing, #define_sorbet_constant!, #sorbet_constant_defined?, #to_sorbet_type, to_sorbet_type

Constructor Details

#initialize(bearer_token: ENV["DODO_PAYMENTS_API_KEY"], webhook_key: ENV["DODO_PAYMENTS_WEBHOOK_KEY"], environment: nil, base_url: ENV["DODO_PAYMENTS_BASE_URL"], max_retries: self.class::DEFAULT_MAX_RETRIES, timeout: self.class::DEFAULT_TIMEOUT_IN_SECONDS, initial_retry_delay: self.class::DEFAULT_INITIAL_RETRY_DELAY, max_retry_delay: self.class::DEFAULT_MAX_RETRY_DELAY) ⇒ Client

Creates and returns a new client for interacting with the API.

Each environment maps to a different base URL:

‘“api.example.com/v2/”`. Defaults to `ENV`

Parameters:

  • bearer_token (String, nil) (defaults to: ENV["DODO_PAYMENTS_API_KEY"])

    Bearer Token for API authentication Defaults to ‘ENV`

  • webhook_key (String, nil) (defaults to: ENV["DODO_PAYMENTS_WEBHOOK_KEY"])

    Defaults to ‘ENV`

  • environment (:live_mode, :test_mode, nil) (defaults to: nil)

    Specifies the environment to use for the API.

  • base_url (String, nil) (defaults to: ENV["DODO_PAYMENTS_BASE_URL"])

    Override the default base URL for the API, e.g.,

  • max_retries (Integer) (defaults to: self.class::DEFAULT_MAX_RETRIES)

    Max number of retries to attempt after a failed retryable request.

  • timeout (Float) (defaults to: self.class::DEFAULT_TIMEOUT_IN_SECONDS)
  • initial_retry_delay (Float) (defaults to: self.class::DEFAULT_INITIAL_RETRY_DELAY)
  • max_retry_delay (Float) (defaults to: self.class::DEFAULT_MAX_RETRY_DELAY)


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
# File 'lib/dodopayments/client.rb', line 128

def initialize(
  bearer_token: ENV["DODO_PAYMENTS_API_KEY"],
  webhook_key: ENV["DODO_PAYMENTS_WEBHOOK_KEY"],
  environment: nil,
  base_url: ENV["DODO_PAYMENTS_BASE_URL"],
  max_retries: self.class::DEFAULT_MAX_RETRIES,
  timeout: self.class::DEFAULT_TIMEOUT_IN_SECONDS,
  initial_retry_delay: self.class::DEFAULT_INITIAL_RETRY_DELAY,
  max_retry_delay: self.class::DEFAULT_MAX_RETRY_DELAY
)
  base_url ||= Dodopayments::Client::ENVIRONMENTS.fetch(environment&.to_sym || :live_mode) do
    message = "environment must be one of #{Dodopayments::Client::ENVIRONMENTS.keys}, got #{environment}"
    raise ArgumentError.new(message)
  end

  if bearer_token.nil?
    raise ArgumentError.new("bearer_token is required, and can be set via environ: \"DODO_PAYMENTS_API_KEY\"")
  end

  @bearer_token = bearer_token.to_s
  @webhook_key = webhook_key&.to_s

  super(
    base_url: base_url,
    timeout: timeout,
    max_retries: max_retries,
    initial_retry_delay: initial_retry_delay,
    max_retry_delay: max_retry_delay
  )

  @checkout_sessions = Dodopayments::Resources::CheckoutSessions.new(client: self)
  @payments = Dodopayments::Resources::Payments.new(client: self)
  @subscriptions = Dodopayments::Resources::Subscriptions.new(client: self)
  @invoices = Dodopayments::Resources::Invoices.new(client: self)
  @licenses = Dodopayments::Resources::Licenses.new(client: self)
  @license_keys = Dodopayments::Resources::LicenseKeys.new(client: self)
  @license_key_instances = Dodopayments::Resources::LicenseKeyInstances.new(client: self)
  @customers = Dodopayments::Resources::Customers.new(client: self)
  @refunds = Dodopayments::Resources::Refunds.new(client: self)
  @disputes = Dodopayments::Resources::Disputes.new(client: self)
  @payouts = Dodopayments::Resources::Payouts.new(client: self)
  @products = Dodopayments::Resources::Products.new(client: self)
  @misc = Dodopayments::Resources::Misc.new(client: self)
  @discounts = Dodopayments::Resources::Discounts.new(client: self)
  @addons = Dodopayments::Resources::Addons.new(client: self)
  @brands = Dodopayments::Resources::Brands.new(client: self)
  @webhooks = Dodopayments::Resources::Webhooks.new(client: self)
  @webhook_events = Dodopayments::Resources::WebhookEvents.new(client: self)
  @usage_events = Dodopayments::Resources::UsageEvents.new(client: self)
  @meters = Dodopayments::Resources::Meters.new(client: self)
  @balances = Dodopayments::Resources::Balances.new(client: self)
  @credit_entitlements = Dodopayments::Resources::CreditEntitlements.new(client: self)
end

Instance Attribute Details

#addonsDodopayments::Resources::Addons (readonly)



73
74
75
# File 'lib/dodopayments/client.rb', line 73

def addons
  @addons
end

#balancesDodopayments::Resources::Balances (readonly)



91
92
93
# File 'lib/dodopayments/client.rb', line 91

def balances
  @balances
end

#bearer_tokenString (readonly)

Bearer Token for API authentication

Returns:

  • (String)


25
26
27
# File 'lib/dodopayments/client.rb', line 25

def bearer_token
  @bearer_token
end

#brandsDodopayments::Resources::Brands (readonly)



76
77
78
# File 'lib/dodopayments/client.rb', line 76

def brands
  @brands
end

#checkout_sessionsDodopayments::Resources::CheckoutSessions (readonly)



31
32
33
# File 'lib/dodopayments/client.rb', line 31

def checkout_sessions
  @checkout_sessions
end

#credit_entitlementsDodopayments::Resources::CreditEntitlements (readonly)



94
95
96
# File 'lib/dodopayments/client.rb', line 94

def credit_entitlements
  @credit_entitlements
end

#customersDodopayments::Resources::Customers (readonly)



52
53
54
# File 'lib/dodopayments/client.rb', line 52

def customers
  @customers
end

#discountsDodopayments::Resources::Discounts (readonly)



70
71
72
# File 'lib/dodopayments/client.rb', line 70

def discounts
  @discounts
end

#disputesDodopayments::Resources::Disputes (readonly)



58
59
60
# File 'lib/dodopayments/client.rb', line 58

def disputes
  @disputes
end

#invoicesDodopayments::Resources::Invoices (readonly)



40
41
42
# File 'lib/dodopayments/client.rb', line 40

def invoices
  @invoices
end

#license_key_instancesDodopayments::Resources::LicenseKeyInstances (readonly)



49
50
51
# File 'lib/dodopayments/client.rb', line 49

def license_key_instances
  @license_key_instances
end

#license_keysDodopayments::Resources::LicenseKeys (readonly)



46
47
48
# File 'lib/dodopayments/client.rb', line 46

def license_keys
  @license_keys
end

#licensesDodopayments::Resources::Licenses (readonly)



43
44
45
# File 'lib/dodopayments/client.rb', line 43

def licenses
  @licenses
end

#metersDodopayments::Resources::Meters (readonly)



88
89
90
# File 'lib/dodopayments/client.rb', line 88

def meters
  @meters
end

#miscDodopayments::Resources::Misc (readonly)



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

def misc
  @misc
end

#paymentsDodopayments::Resources::Payments (readonly)



34
35
36
# File 'lib/dodopayments/client.rb', line 34

def payments
  @payments
end

#payoutsDodopayments::Resources::Payouts (readonly)



61
62
63
# File 'lib/dodopayments/client.rb', line 61

def payouts
  @payouts
end

#productsDodopayments::Resources::Products (readonly)



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

def products
  @products
end

#refundsDodopayments::Resources::Refunds (readonly)



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

def refunds
  @refunds
end

#subscriptionsDodopayments::Resources::Subscriptions (readonly)



37
38
39
# File 'lib/dodopayments/client.rb', line 37

def subscriptions
  @subscriptions
end

#usage_eventsDodopayments::Resources::UsageEvents (readonly)



85
86
87
# File 'lib/dodopayments/client.rb', line 85

def usage_events
  @usage_events
end

#webhook_eventsDodopayments::Resources::WebhookEvents (readonly)



82
83
84
# File 'lib/dodopayments/client.rb', line 82

def webhook_events
  @webhook_events
end

#webhook_keyString? (readonly)

Returns:

  • (String, nil)


28
29
30
# File 'lib/dodopayments/client.rb', line 28

def webhook_key
  @webhook_key
end

#webhooksDodopayments::Resources::Webhooks (readonly)



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

def webhooks
  @webhooks
end