Class: Dodopayments::Resources::Discounts

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

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:



213
214
215
# File 'lib/dodopayments/resources/discounts.rb', line 213

def initialize(client:)
  @client = client
end

Instance Method Details

#create(amount:, type:, code: nil, currency_options: nil, customer_eligibility: nil, expires_at: nil, metadata: nil, name: nil, per_customer_usage_limit: nil, preserve_on_plan_change: nil, restricted_to: nil, starts_at: 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 in basis points (e.g. 540 means 5.4%, 10000 means

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

    The discount type: percentage or flat (flat_per_unit stays blocked).

  • code (String, nil)

    Optionally supply a code (will be uppercased).

  • currency_options (Array<Dodopayments::Models::DiscountCreateParams::CurrencyOption>, nil)

    Per-currency options (flat deduction / percentage cap + minimum subtotal).

  • customer_eligibility (Symbol, Dodopayments::Models::DiscountCreateParams::CustomerEligibility, nil)

    Who may redeem this discount code. Defaults to any (unrestricted).

  • expires_at (Time, nil)

    When the discount expires, if ever.

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

    Additional metadata for the discount

  • name (String, nil)
  • per_customer_usage_limit (Integer, nil)

    Maximum number of times a single customer may redeem this discount.

  • 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).

  • starts_at (Time, nil)

    When the discount becomes active, if scheduled for the future.

  • 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:



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

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:



179
180
181
182
183
184
185
186
# File 'lib/dodopayments/resources/discounts.rb', line 179

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>

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

GET /discounts

Parameters:

  • active (Boolean)

    Filter by active status. true = currently redeemable (started, not

  • code (String)

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

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

    Filter by discount type

  • 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:



155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/dodopayments/resources/discounts.rb', line 155

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:



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

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:



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

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, currency_options: nil, customer_eligibility: nil, expires_at: nil, metadata: nil, name: nil, per_customer_usage_limit: nil, preserve_on_plan_change: nil, restricted_to: nil, starts_at: 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 in basis points (e.g., 540 = 5.4%

  • code (String, nil)

    If present, update the discount code (uppercase).

  • currency_options (Array<Dodopayments::Models::DiscountUpdateParams::CurrencyOption>, nil)

    If present, fully replaces the discount's currency options (replace-set

  • customer_eligibility (Symbol, Dodopayments::Models::DiscountUpdateParams::CustomerEligibility, nil)

    If present, update who may redeem this discount. Plain field (not

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

    Additional metadata for the discount

  • name (String, nil)
  • per_customer_usage_limit (Integer, nil)

    If present, update the per-customer usage limit (double-option: send

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

  • starts_at (Time, nil)

    If present, update starts_at (double-option: send null to clear it).

  • 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 (percentage or flat).

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

Returns:

See Also:



120
121
122
123
124
125
126
127
128
129
# File 'lib/dodopayments/resources/discounts.rb', line 120

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