Class: Dodopayments::Resources::Subscriptions

Inherits:
Object
  • Object
show all
Defined in:
lib/dodopayments/resources/subscriptions.rb

Instance Method Summary collapse

Constructor Details

#initialize(client:) ⇒ Subscriptions

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Subscriptions.

Parameters:



315
316
317
# File 'lib/dodopayments/resources/subscriptions.rb', line 315

def initialize(client:)
  @client = client
end

Instance Method Details

#change_plan(subscription_id, product_id: , proration_billing_mode: , quantity: , addons: nil, request_options: {}) ⇒ nil

Some parameter documentations has been truncated, see Models::SubscriptionChangePlanParams for more details.

Parameters:

Returns:

  • (nil)

See Also:



166
167
168
169
170
171
172
173
174
175
# File 'lib/dodopayments/resources/subscriptions.rb', line 166

def change_plan(subscription_id, params)
  parsed, options = Dodopayments::SubscriptionChangePlanParams.dump_request(params)
  @client.request(
    method: :post,
    path: ["subscriptions/%1$s/change-plan", subscription_id],
    body: parsed,
    model: NilClass,
    options: options
  )
end

#charge(subscription_id, product_price: , adaptive_currency_fees_inclusive: nil, customer_balance_config: nil, metadata: nil, product_currency: nil, product_description: nil, request_options: {}) ⇒ Dodopayments::Models::SubscriptionChargeResponse

Some parameter documentations has been truncated, see Models::SubscriptionChargeParams for more details.

Parameters:

  • subscription_id (String)

    Subscription Id

  • product_price (Integer)

    The product price. Represented in the lowest denomination of the currency (e.g.,

  • adaptive_currency_fees_inclusive (Boolean, nil)

    Whether adaptive currency fees should be included in the product_price (true) or

  • customer_balance_config (Dodopayments::Models::SubscriptionChargeParams::CustomerBalanceConfig, nil)

    Specify how customer balance is used for the payment

  • metadata (Hash{Symbol=>String}, nil)

    Metadata for the payment. If not passed, the metadata of the subscription will b

  • product_currency (Symbol, Dodopayments::Models::Currency, nil)

    Optional currency of the product price. If not specified, defaults to the curren

  • product_description (String, nil)

    Optional product description override for billing and line items.

  • request_options (Dodopayments::RequestOptions, Hash{Symbol=>Object}, nil)

Returns:

See Also:



201
202
203
204
205
206
207
208
209
210
# File 'lib/dodopayments/resources/subscriptions.rb', line 201

def charge(subscription_id, params)
  parsed, options = Dodopayments::SubscriptionChargeParams.dump_request(params)
  @client.request(
    method: :post,
    path: ["subscriptions/%1$s/charge", subscription_id],
    body: parsed,
    model: Dodopayments::Models::SubscriptionChargeResponse,
    options: options
  )
end

#create(billing: , customer: , product_id: , quantity: , addons: nil, allowed_payment_method_types: nil, billing_currency: nil, discount_code: nil, force_3ds: nil, metadata: nil, on_demand: nil, payment_link: nil, return_url: nil, show_saved_payment_methods: nil, tax_id: nil, trial_period_days: nil, request_options: {}) ⇒ Dodopayments::Models::SubscriptionCreateResponse

Some parameter documentations has been truncated, see Models::SubscriptionCreateParams for more details.

Parameters:

  • billing (Dodopayments::Models::BillingAddress)

    Billing address information for the subscription

  • customer (Dodopayments::Models::AttachExistingCustomer, Dodopayments::Models::NewCustomer)

    Customer details for the subscription

  • product_id (String)

    Unique identifier of the product to subscribe to

  • quantity (Integer)

    Number of units to subscribe for. Must be at least 1.

  • addons (Array<Dodopayments::Models::AttachAddon>, nil)

    Attach addons to this subscription

  • allowed_payment_method_types (Array<Symbol, Dodopayments::Models::PaymentMethodTypes>, nil)

    List of payment methods allowed during checkout.

  • billing_currency (Symbol, Dodopayments::Models::Currency, nil)

    Fix the currency in which the end customer is billed.

  • discount_code (String, nil)

    Discount Code to apply to the subscription

  • force_3ds (Boolean, nil)

    Override merchant default 3DS behaviour for this subscription

  • metadata (Hash{Symbol=>String})

    Additional metadata for the subscription

  • on_demand (Dodopayments::Models::OnDemandSubscription, nil)
  • payment_link (Boolean, nil)

    If true, generates a payment link.

  • return_url (String, nil)

    Optional URL to redirect after successful subscription creation

  • show_saved_payment_methods (Boolean)

    Display saved payment methods of a returning customer

  • tax_id (String, nil)

    Tax ID in case the payment is B2B. If tax id validation fails the payment creati

  • trial_period_days (Integer, nil)

    Optional trial period in days

  • request_options (Dodopayments::RequestOptions, Hash{Symbol=>Object}, nil)

Returns:

See Also:



48
49
50
51
52
53
54
55
56
57
# File 'lib/dodopayments/resources/subscriptions.rb', line 48

def create(params)
  parsed, options = Dodopayments::SubscriptionCreateParams.dump_request(params)
  @client.request(
    method: :post,
    path: "subscriptions",
    body: parsed,
    model: Dodopayments::Models::SubscriptionCreateResponse,
    options: options
  )
end

#list(brand_id: nil, created_at_gte: nil, created_at_lte: nil, customer_id: nil, page_number: nil, page_size: nil, status: nil, request_options: {}) ⇒ Dodopayments::Internal::DefaultPageNumberPagination<Dodopayments::Models::SubscriptionListResponse>

Parameters:

  • brand_id (String)

    filter by Brand id

  • created_at_gte (Time)

    Get events after this created time

  • created_at_lte (Time)

    Get events created before this time

  • customer_id (String)

    Filter by customer id

  • page_number (Integer)

    Page number default is 0

  • page_size (Integer)

    Page size default is 10 max is 100

  • status (Symbol, Dodopayments::Models::SubscriptionListParams::Status)

    Filter by status

  • request_options (Dodopayments::RequestOptions, Hash{Symbol=>Object}, nil)

Returns:

See Also:



134
135
136
137
138
139
140
141
142
143
144
# File 'lib/dodopayments/resources/subscriptions.rb', line 134

def list(params = {})
  parsed, options = Dodopayments::SubscriptionListParams.dump_request(params)
  @client.request(
    method: :get,
    path: "subscriptions",
    query: parsed,
    page: Dodopayments::Internal::DefaultPageNumberPagination,
    model: Dodopayments::Models::SubscriptionListResponse,
    options: options
  )
end

#retrieve(subscription_id, request_options: {}) ⇒ Dodopayments::Models::Subscription

Parameters:

Returns:

See Also:



68
69
70
71
72
73
74
75
# File 'lib/dodopayments/resources/subscriptions.rb', line 68

def retrieve(subscription_id, params = {})
  @client.request(
    method: :get,
    path: ["subscriptions/%1$s", subscription_id],
    model: Dodopayments::Subscription,
    options: params[:request_options]
  )
end

#retrieve_usage_history(subscription_id, end_date: nil, meter_id: nil, page_number: nil, page_size: nil, start_date: nil, request_options: {}) ⇒ Dodopayments::Internal::DefaultPageNumberPagination<Dodopayments::Models::SubscriptionRetrieveUsageHistoryResponse>

Get detailed usage history for a subscription that includes usage-based billing (metered components). This endpoint provides insights into customer usage patterns and billing calculations over time.

## What You’ll Get:

  • **Billing periods**: Each item represents a billing cycle with start and end dates

  • **Meter usage**: Detailed breakdown of usage for each meter configured on the subscription

  • **Usage calculations**: Total units consumed, free threshold units, and chargeable units

  • **Historical tracking**: Complete audit trail of usage-based charges

## Use Cases:

  • **Customer support**: Investigate billing questions and usage discrepancies

  • **Usage analytics**: Analyze customer consumption patterns over time

  • **Billing transparency**: Provide customers with detailed usage breakdowns

  • **Revenue optimization**: Identify usage trends to optimize pricing strategies

## Filtering Options:

  • **Date range filtering**: Get usage history for specific time periods

  • **Meter-specific filtering**: Focus on usage for a particular meter

  • Pagination: Navigate through large usage histories efficiently

## Important Notes:

  • Only returns data for subscriptions with usage-based (metered) components

  • Usage history is organized by billing periods (subscription cycles)

  • Free threshold units are calculated and displayed separately from chargeable units

  • Historical data is preserved even if meter configurations change

## Example Query Patterns:

  • Get last 3 months: ‘?start_date=2024-01-01T00:00:00Z&end_date=2024-03-31T23:59:59Z`

  • Filter by meter: ‘?meter_id=mtr_api_requests`

  • Paginate results: ‘?page_size=20&page_number=1`

  • Recent usage: ‘?start_date=2024-03-01T00:00:00Z` (from March 1st to now)

Parameters:

  • subscription_id (String)

    Unique subscription identifier

  • end_date (Time, nil)

    Filter by end date (inclusive)

  • meter_id (String, nil)

    Filter by specific meter ID

  • page_number (Integer, nil)

    Page number (default: 0)

  • page_size (Integer, nil)

    Page size (default: 10, max: 100)

  • start_date (Time, nil)

    Filter by start date (inclusive)

  • request_options (Dodopayments::RequestOptions, Hash{Symbol=>Object}, nil)

Returns:

See Also:



274
275
276
277
278
279
280
281
282
283
284
# File 'lib/dodopayments/resources/subscriptions.rb', line 274

def retrieve_usage_history(subscription_id, params = {})
  parsed, options = Dodopayments::SubscriptionRetrieveUsageHistoryParams.dump_request(params)
  @client.request(
    method: :get,
    path: ["subscriptions/%1$s/usage-history", subscription_id],
    query: parsed,
    page: Dodopayments::Internal::DefaultPageNumberPagination,
    model: Dodopayments::Models::SubscriptionRetrieveUsageHistoryResponse,
    options: options
  )
end

#update(subscription_id, billing: nil, cancel_at_next_billing_date: nil, customer_name: nil, disable_on_demand: nil, metadata: nil, next_billing_date: nil, status: nil, tax_id: nil, request_options: {}) ⇒ Dodopayments::Models::Subscription

Parameters:

Returns:

See Also:



102
103
104
105
106
107
108
109
110
111
# File 'lib/dodopayments/resources/subscriptions.rb', line 102

def update(subscription_id, params = {})
  parsed, options = Dodopayments::SubscriptionUpdateParams.dump_request(params)
  @client.request(
    method: :patch,
    path: ["subscriptions/%1$s", subscription_id],
    body: parsed,
    model: Dodopayments::Subscription,
    options: options
  )
end

#update_payment_method(subscription_id, type: , payment_method_id: , return_url: nil, request_options: {}) ⇒ Dodopayments::Models::SubscriptionUpdatePaymentMethodResponse

Parameters:

Returns:

See Also:



301
302
303
304
305
306
307
308
309
310
# File 'lib/dodopayments/resources/subscriptions.rb', line 301

def update_payment_method(subscription_id, params)
  parsed, options = Dodopayments::SubscriptionUpdatePaymentMethodParams.dump_request(params)
  @client.request(
    method: :post,
    path: ["subscriptions/%1$s/update-payment-method", subscription_id],
    body: parsed,
    model: Dodopayments::Models::SubscriptionUpdatePaymentMethodResponse,
    options: options
  )
end