Class: Dodopayments::Resources::Discounts

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

Instance Method Summary collapse

Constructor Details

#initialize(client:) ⇒ Discounts

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 Discounts.

Parameters:



194
195
196
# File 'lib/dodopayments/resources/discounts.rb', line 194

def initialize(client:)
  @client = client
end

Instance Method Details

#create(amount:, type:, code: nil, expires_at: nil, metadata: nil, name: nil, preserve_on_plan_change: nil, restricted_to: nil, subscription_cycles: nil, usage_limit: nil, request_options: {}) ⇒ Dodopayments::Models::Discount

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

POST /discounts If ‘code` is omitted or empty, a random 16-char uppercase code is generated.

Parameters:

  • amount (Integer)

    The discount amount.

  • type (Symbol, Dodopayments::Models::DiscountType)

    The discount type (e.g. ‘percentage`, `flat`, or `flat_per_unit`).

  • code (String, nil)

    Optionally supply a code (will be uppercased).

  • expires_at (Time, nil)

    When the discount expires, if ever.

  • metadata (Hash{Symbol=>String})

    Additional metadata for the discount

  • name (String, nil)
  • preserve_on_plan_change (Boolean)

    Whether this discount should be preserved when a subscription changes plans.

  • restricted_to (Array<String>, nil)

    List of product IDs to restrict usage (if any).

  • subscription_cycles (Integer, nil)

    Number of subscription billing cycles this discount is valid for.

  • usage_limit (Integer, nil)

    How many times this discount can be used (if any).

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

Returns:

See Also:



39
40
41
42
43
44
45
46
47
48
# File 'lib/dodopayments/resources/discounts.rb', line 39

def create(params)
  parsed, options = Dodopayments::DiscountCreateParams.dump_request(params)
  @client.request(
    method: :post,
    path: "discounts",
    body: parsed,
    model: Dodopayments::Discount,
    options: options
  )
end

#delete(discount_id, request_options: {}) ⇒ nil

DELETE /discounts/discount_id

Parameters:

Returns:

  • (nil)

See Also:



160
161
162
163
164
165
166
167
# File 'lib/dodopayments/resources/discounts.rb', line 160

def delete(discount_id, params = {})
  @client.request(
    method: :delete,
    path: ["discounts/%1$s", discount_id],
    model: NilClass,
    options: params[:request_options]
  )
end

#list(active: nil, code: nil, discount_type: nil, page_number: nil, page_size: nil, product_id: nil, request_options: {}) ⇒ Dodopayments::Internal::DefaultPageNumberPagination<Dodopayments::Models::Discount>

GET /discounts

Parameters:

  • active (Boolean)

    Filter by active status (true = not expired, false = expired)

  • code (String)

    Filter by discount code (partial match, case-insensitive)

  • discount_type (Symbol, Dodopayments::Models::DiscountType)

    Filter by discount type (percentage)

  • page_number (Integer)

    Page number (default = 0).

  • page_size (Integer)

    Page size (default = 10, max = 100).

  • product_id (String)

    Filter by product restriction (only discounts that apply to this product)

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

Returns:

See Also:



136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/dodopayments/resources/discounts.rb', line 136

def list(params = {})
  parsed, options = Dodopayments::DiscountListParams.dump_request(params)
  query = Dodopayments::Internal::Util.encode_query_params(parsed)
  @client.request(
    method: :get,
    path: "discounts",
    query: query,
    page: Dodopayments::Internal::DefaultPageNumberPagination,
    model: Dodopayments::Discount,
    options: options
  )
end

#retrieve(discount_id, request_options: {}) ⇒ Dodopayments::Models::Discount

GET /discounts/discount_id

Parameters:

Returns:

See Also:



61
62
63
64
65
66
67
68
# File 'lib/dodopayments/resources/discounts.rb', line 61

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

#retrieve_by_code(code, request_options: {}) ⇒ Dodopayments::Models::Discount

Validate and fetch a discount by its code name (e.g., “SAVE20”). This allows real-time validation directly against the API using the human-readable discount code instead of requiring the internal discount_id.

Parameters:

Returns:

See Also:



182
183
184
185
186
187
188
189
# File 'lib/dodopayments/resources/discounts.rb', line 182

def retrieve_by_code(code, params = {})
  @client.request(
    method: :get,
    path: ["discounts/code/%1$s", code],
    model: Dodopayments::Discount,
    options: params[:request_options]
  )
end

#update(discount_id, amount: nil, code: nil, expires_at: nil, metadata: nil, name: nil, preserve_on_plan_change: nil, restricted_to: nil, subscription_cycles: nil, type: nil, usage_limit: nil, request_options: {}) ⇒ Dodopayments::Models::Discount

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

PATCH /discounts/discount_id

Parameters:

  • discount_id (String)

    Discount Id

  • amount (Integer, nil)

    If present, update the discount amount:

  • code (String, nil)

    If present, update the discount code (uppercase).

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

    Additional metadata for the discount

  • name (String, nil)
  • preserve_on_plan_change (Boolean, nil)

    Whether this discount should be preserved when a subscription changes plans.

  • restricted_to (Array<String>, nil)

    If present, replaces all restricted product IDs with this new set.

  • subscription_cycles (Integer, nil)

    Number of subscription billing cycles this discount is valid for.

  • type (Symbol, Dodopayments::Models::DiscountType, nil)

    If present, update the discount type.

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

Returns:

See Also:



104
105
106
107
108
109
110
111
112
113
# File 'lib/dodopayments/resources/discounts.rb', line 104

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