Class: Stripe::Subscription

Inherits:
APIResource show all
Extended by:
APIOperations::Create, APIOperations::List, APIOperations::Search
Includes:
APIOperations::Save
Defined in:
lib/stripe/resources/subscription.rb

Overview

Subscriptions allow you to charge a customer on a recurring basis.

Related guide: [Creating subscriptions](docs.stripe.com/billing/subscriptions/creating)

Defined Under Namespace

Classes: AutomaticTax, BillingCycleAnchorConfig, BillingMode, BillingSchedule, BillingThresholds, CancellationDetails, InvoiceSettings, LastPriceMigrationError, ManagedPayments, PauseCollection, PaymentSettings, PendingInvoiceItemInterval, PendingUpdate, Prebilling, PresentmentDetails, StatusDetails, TransferData, TrialSettings

Constant Summary collapse

OBJECT_NAME =
"subscription"

Constants inherited from StripeObject

Stripe::StripeObject::RESERVED_FIELD_NAMES

Instance Attribute Summary collapse

Attributes inherited from APIResource

#save_with_parent

Attributes inherited from StripeObject

#last_response

Class Method Summary collapse

Instance Method Summary collapse

Methods included from APIOperations::Create

create

Methods included from APIOperations::List

list

Methods included from APIOperations::Search

_search

Methods included from APIOperations::Save

included, #save

Methods inherited from APIResource

class_name, custom_method, #refresh, #request_stripe_object, resource_url, #resource_url, retrieve, save_nested_resource

Methods included from APIOperations::Request

included

Methods inherited from StripeObject

#==, #[], #[]=, #_get_inner_class_type, additive_object_param, additive_object_param?, #as_json, construct_from, #deleted?, #dirty!, #each, #eql?, field_encodings, #hash, #initialize, #inspect, #keys, #marshal_dump, #marshal_load, protected_fields, #serialize_params, #to_hash, #to_json, #to_s, #update_attributes, #values

Constructor Details

This class inherits a constructor from Stripe::StripeObject

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Stripe::StripeObject

Instance Attribute Details

#applicationObject (readonly)

ID of the Connect Application that created the subscription.



875
876
877
# File 'lib/stripe/resources/subscription.rb', line 875

def application
  @application
end

#application_fee_percentObject (readonly)

A non-negative decimal between 0 and 100, with at most two decimal places. This represents the percentage of the subscription invoice total that will be transferred to the application owner’s Stripe account.



877
878
879
# File 'lib/stripe/resources/subscription.rb', line 877

def application_fee_percent
  @application_fee_percent
end

#automatic_taxObject (readonly)

Attribute for field automatic_tax



879
880
881
# File 'lib/stripe/resources/subscription.rb', line 879

def automatic_tax
  @automatic_tax
end

#billing_cadenceObject (readonly)

The Billing Cadence which controls the timing of recurring invoice generation for this subscription.If unset, the subscription will bill according to its own configured schedule and create its own invoices.If set, this subscription will be billed by the cadence instead, potentially sharing invoices with the other subscriptions linked to that Cadence.



881
882
883
# File 'lib/stripe/resources/subscription.rb', line 881

def billing_cadence
  @billing_cadence
end

#billing_cycle_anchorObject (readonly)

The reference point that aligns future [billing cycle](docs.stripe.com/subscriptions/billing-cycle) dates. It sets the day of week for ‘week` intervals, the day of month for `month` and `year` intervals, and the month of year for `year` intervals. The timestamp is in UTC format.



883
884
885
# File 'lib/stripe/resources/subscription.rb', line 883

def billing_cycle_anchor
  @billing_cycle_anchor
end

#billing_cycle_anchor_configObject (readonly)

The fixed values used to calculate the ‘billing_cycle_anchor`.



885
886
887
# File 'lib/stripe/resources/subscription.rb', line 885

def billing_cycle_anchor_config
  @billing_cycle_anchor_config
end

#billing_modeObject (readonly)

The billing mode of the subscription.



887
888
889
# File 'lib/stripe/resources/subscription.rb', line 887

def billing_mode
  @billing_mode
end

#billing_schedulesObject (readonly)

Billing schedules for this subscription.



889
890
891
# File 'lib/stripe/resources/subscription.rb', line 889

def billing_schedules
  @billing_schedules
end

#billing_thresholdsObject (readonly)

Define thresholds at which an invoice will be sent, and the subscription advanced to a new billing period



891
892
893
# File 'lib/stripe/resources/subscription.rb', line 891

def billing_thresholds
  @billing_thresholds
end

#cancel_atObject (readonly)

A date in the future at which the subscription will automatically get canceled



893
894
895
# File 'lib/stripe/resources/subscription.rb', line 893

def cancel_at
  @cancel_at
end

#cancel_at_period_endObject (readonly)

Whether this subscription will (if ‘status=active`) or did (if `status=canceled`) cancel at the end of the current billing period.



895
896
897
# File 'lib/stripe/resources/subscription.rb', line 895

def cancel_at_period_end
  @cancel_at_period_end
end

#canceled_atObject (readonly)

If the subscription has been canceled, the date of that cancellation. If the subscription was canceled with ‘cancel_at_period_end`, `canceled_at` will reflect the time of the most recent update request, not the end of the subscription period when the subscription is automatically moved to a canceled state.



897
898
899
# File 'lib/stripe/resources/subscription.rb', line 897

def canceled_at
  @canceled_at
end

#cancellation_detailsObject (readonly)

Details about why this subscription was cancelled



899
900
901
# File 'lib/stripe/resources/subscription.rb', line 899

def cancellation_details
  @cancellation_details
end

#collection_methodObject (readonly)

Either ‘charge_automatically`, or `send_invoice`. When charging automatically, Stripe will attempt to pay this subscription at the end of the cycle using the default source attached to the customer. When sending an invoice, Stripe will email your customer an invoice with payment instructions and mark the subscription as `active`.



901
902
903
# File 'lib/stripe/resources/subscription.rb', line 901

def collection_method
  @collection_method
end

#createdObject (readonly)

Time at which the object was created. Measured in seconds since the Unix epoch.



903
904
905
# File 'lib/stripe/resources/subscription.rb', line 903

def created
  @created
end

#currencyObject (readonly)

Three-letter [ISO currency code](www.iso.org/iso-4217-currency-codes.html), in lowercase. Must be a [supported currency](stripe.com/docs/currencies).



905
906
907
# File 'lib/stripe/resources/subscription.rb', line 905

def currency
  @currency
end

#customerObject (readonly)

ID of the customer who owns the subscription.



907
908
909
# File 'lib/stripe/resources/subscription.rb', line 907

def customer
  @customer
end

#customer_accountObject (readonly)

ID of the account representing the customer who owns the subscription.



909
910
911
# File 'lib/stripe/resources/subscription.rb', line 909

def 
  @customer_account
end

#days_until_dueObject (readonly)

Number of days a customer has to pay invoices generated by this subscription. This value will be ‘null` for subscriptions where `collection_method=charge_automatically`.



911
912
913
# File 'lib/stripe/resources/subscription.rb', line 911

def days_until_due
  @days_until_due
end

#default_payment_methodObject (readonly)

ID of the default payment method for the subscription. It must belong to the customer associated with the subscription. This takes precedence over ‘default_source`. If neither are set, invoices will use the customer’s [invoice_settings.default_payment_method](docs.stripe.com/api/customers/object#customer_object-invoice_settings-default_payment_method) or [default_source](docs.stripe.com/api/customers/object#customer_object-default_source).



913
914
915
# File 'lib/stripe/resources/subscription.rb', line 913

def default_payment_method
  @default_payment_method
end

#default_sourceObject (readonly)

ID of the default payment source for the subscription. It must belong to the customer associated with the subscription and be in a chargeable state. If ‘default_payment_method` is also set, `default_payment_method` will take precedence. If neither are set, invoices will use the customer’s [invoice_settings.default_payment_method](docs.stripe.com/api/customers/object#customer_object-invoice_settings-default_payment_method) or [default_source](docs.stripe.com/api/customers/object#customer_object-default_source).



915
916
917
# File 'lib/stripe/resources/subscription.rb', line 915

def default_source
  @default_source
end

#default_tax_ratesObject (readonly)

The tax rates that will apply to any subscription item that does not have ‘tax_rates` set. Invoices created will have their `default_tax_rates` populated from the subscription.



917
918
919
# File 'lib/stripe/resources/subscription.rb', line 917

def default_tax_rates
  @default_tax_rates
end

#descriptionObject (readonly)

The subscription’s description, meant to be displayable to the customer. Use this field to optionally store an explanation of the subscription for rendering in Stripe surfaces and certain local payment methods UIs.



919
920
921
# File 'lib/stripe/resources/subscription.rb', line 919

def description
  @description
end

#discountsObject (readonly)

The discounts applied to the subscription. Subscription item discounts are applied before subscription discounts. Use ‘expand[]=discounts` to expand each discount.



921
922
923
# File 'lib/stripe/resources/subscription.rb', line 921

def discounts
  @discounts
end

#ended_atObject (readonly)

If the subscription has ended, the date the subscription ended.



923
924
925
# File 'lib/stripe/resources/subscription.rb', line 923

def ended_at
  @ended_at
end

#idObject (readonly)

Unique identifier for the object.



925
926
927
# File 'lib/stripe/resources/subscription.rb', line 925

def id
  @id
end

#invoice_settingsObject (readonly)

Attribute for field invoice_settings



927
928
929
# File 'lib/stripe/resources/subscription.rb', line 927

def invoice_settings
  @invoice_settings
end

#itemsObject (readonly)

List of subscription items, each with an attached price.



929
930
931
# File 'lib/stripe/resources/subscription.rb', line 929

def items
  @items
end

#last_price_migration_errorObject (readonly)

Details of the most recent price migration that failed for the subscription.



931
932
933
# File 'lib/stripe/resources/subscription.rb', line 931

def last_price_migration_error
  @last_price_migration_error
end

#latest_invoiceObject (readonly)

The most recent invoice this subscription has generated over its lifecycle (for example, when it cycles or is updated).



933
934
935
# File 'lib/stripe/resources/subscription.rb', line 933

def latest_invoice
  @latest_invoice
end

#livemodeObject (readonly)

If the object exists in live mode, the value is ‘true`. If the object exists in test mode, the value is `false`.



935
936
937
# File 'lib/stripe/resources/subscription.rb', line 935

def livemode
  @livemode
end

#managed_paymentsObject (readonly)

Settings for Managed Payments for this Subscription and resulting [Invoices](/api/invoices/object) and [PaymentIntents](/api/payment_intents/object).



937
938
939
# File 'lib/stripe/resources/subscription.rb', line 937

def managed_payments
  @managed_payments
end

#metadataObject (readonly)

Set of [key-value pairs](docs.stripe.com/api/metadata) that you can attach to an object. This can be useful for storing additional information about the object in a structured format.



939
940
941
# File 'lib/stripe/resources/subscription.rb', line 939

def 
  @metadata
end

#next_pending_invoice_item_invoiceObject (readonly)

Specifies the approximate timestamp on which any pending invoice items will be billed according to the schedule provided at ‘pending_invoice_item_interval`.



941
942
943
# File 'lib/stripe/resources/subscription.rb', line 941

def next_pending_invoice_item_invoice
  @next_pending_invoice_item_invoice
end

#objectObject (readonly)

String representing the object’s type. Objects of the same type share the same value.



943
944
945
# File 'lib/stripe/resources/subscription.rb', line 943

def object
  @object
end

#on_behalf_ofObject (readonly)

The account (if any) the charge was made on behalf of for charges associated with this subscription. See the [Connect documentation](docs.stripe.com/connect/subscriptions#on-behalf-of) for details.



945
946
947
# File 'lib/stripe/resources/subscription.rb', line 945

def on_behalf_of
  @on_behalf_of
end

#pause_collectionObject (readonly)

If specified, payment collection for this subscription will be paused. Note that the subscription status will be unchanged and will not be updated to ‘paused`. Learn more about [pausing collection](docs.stripe.com/billing/subscriptions/pause-payment).



947
948
949
# File 'lib/stripe/resources/subscription.rb', line 947

def pause_collection
  @pause_collection
end

#payment_settingsObject (readonly)

Payment settings passed on to invoices created by the subscription.



949
950
951
# File 'lib/stripe/resources/subscription.rb', line 949

def payment_settings
  @payment_settings
end

#pending_invoice_item_intervalObject (readonly)

Specifies an interval for how often to bill for any pending invoice items. It is analogous to calling [Create an invoice](/api/invoices/create) for the given subscription at the specified interval.



951
952
953
# File 'lib/stripe/resources/subscription.rb', line 951

def pending_invoice_item_interval
  @pending_invoice_item_interval
end

#pending_setup_intentObject (readonly)

You can use this [SetupIntent](docs.stripe.com/api/setup_intents) to collect user authentication when creating a subscription without immediate payment or updating a subscription’s payment method, allowing you to optimize for off-session payments. Learn more in the [SCA Migration Guide](docs.stripe.com/billing/migration/strong-customer-authentication#scenario-2).



953
954
955
# File 'lib/stripe/resources/subscription.rb', line 953

def pending_setup_intent
  @pending_setup_intent
end

#pending_updateObject (readonly)

If specified, [pending updates](docs.stripe.com/billing/subscriptions/pending-updates) that will be applied to the subscription once the ‘latest_invoice` has been paid.



955
956
957
# File 'lib/stripe/resources/subscription.rb', line 955

def pending_update
  @pending_update
end

#prebillingObject (readonly)

Time period and invoice for a Subscription billed in advance.



957
958
959
# File 'lib/stripe/resources/subscription.rb', line 957

def prebilling
  @prebilling
end

#presentment_detailsObject (readonly)

Attribute for field presentment_details



959
960
961
# File 'lib/stripe/resources/subscription.rb', line 959

def presentment_details
  @presentment_details
end

#scheduleObject (readonly)

The schedule attached to the subscription



961
962
963
# File 'lib/stripe/resources/subscription.rb', line 961

def schedule
  @schedule
end

#start_dateObject (readonly)

Date when the subscription was first created. The date might differ from the ‘created` date due to backdating.



963
964
965
# File 'lib/stripe/resources/subscription.rb', line 963

def start_date
  @start_date
end

#statusObject (readonly)

Possible values are ‘incomplete`, `incomplete_expired`, `trialing`, `active`, `past_due`, `canceled`, `unpaid`, or `paused`.

For ‘collection_method=charge_automatically` a subscription moves into `incomplete` if the initial payment attempt fails. A subscription in this status can only have metadata and default_source updated. Once the first invoice is paid, the subscription moves into an `active` status. If the first invoice is not paid within 23 hours, the subscription transitions to `incomplete_expired`. This is a terminal status, the open invoice will be voided and no further invoices will be generated.

A subscription that is currently in a trial period is ‘trialing` and moves to `active` when the trial period is over.

A subscription can only enter a ‘paused` status [when a trial ends without a payment method](docs.stripe.com/billing/subscriptions/trials#create-free-trials-without-payment). A `paused` subscription doesn’t generate invoices and can be resumed after your customer adds their payment method. The ‘paused` status is different from [pausing collection](docs.stripe.com/billing/subscriptions/pause-payment), which still generates invoices and leaves the subscription’s status unchanged.

If subscription ‘collection_method=charge_automatically`, it becomes `past_due` when payment is required but cannot be paid (due to failed payment or awaiting additional user actions). Once Stripe has exhausted all payment retry attempts, the subscription will become `canceled` or `unpaid` (depending on your subscriptions settings).

If subscription ‘collection_method=send_invoice` it becomes `past_due` when its invoice is not paid by the due date, and `canceled` or `unpaid` if it is still not paid by an additional deadline after that. Note that when a subscription has a status of `unpaid`, no subsequent invoices will be attempted (invoices will be created, but then immediately automatically closed). After receiving updated payment information from a customer, you may choose to reopen and pay their closed invoices.



975
976
977
# File 'lib/stripe/resources/subscription.rb', line 975

def status
  @status
end

#status_detailsObject (readonly)

Describes changes to the subscription’s status.



977
978
979
# File 'lib/stripe/resources/subscription.rb', line 977

def status_details
  @status_details
end

#test_clockObject (readonly)

ID of the test clock this subscription belongs to.



979
980
981
# File 'lib/stripe/resources/subscription.rb', line 979

def test_clock
  @test_clock
end

#transfer_dataObject (readonly)

The account (if any) the subscription’s payments will be attributed to for tax reporting, and where funds from each payment will be transferred to for each of the subscription’s invoices.



981
982
983
# File 'lib/stripe/resources/subscription.rb', line 981

def transfer_data
  @transfer_data
end

#trial_endObject (readonly)

If the subscription has a trial, the end of that trial.



983
984
985
# File 'lib/stripe/resources/subscription.rb', line 983

def trial_end
  @trial_end
end

#trial_settingsObject (readonly)

Settings related to subscription trials.



985
986
987
# File 'lib/stripe/resources/subscription.rb', line 985

def trial_settings
  @trial_settings
end

#trial_startObject (readonly)

If the subscription has a trial, the beginning of that trial.



987
988
989
# File 'lib/stripe/resources/subscription.rb', line 987

def trial_start
  @trial_start
end

Class Method Details

.attach_cadence(subscription, params = {}, opts = {}) ⇒ Object

Attach a Billing Cadence to an existing subscription. When attached, the subscription is billed by the Billing Cadence, potentially sharing invoices with the other subscriptions linked to the Billing Cadence.



1000
1001
1002
1003
1004
1005
1006
1007
# File 'lib/stripe/resources/subscription.rb', line 1000

def self.attach_cadence(subscription, params = {}, opts = {})
  request_stripe_object(
    method: :post,
    path: format("/v1/subscriptions/%<subscription>s/attach_cadence", { subscription: CGI.escape(subscription) }),
    params: params,
    opts: opts
  )
end

.cancel(subscription_exposed_id, params = {}, opts = {}) ⇒ Object

Cancels a customer’s subscription immediately. The customer won’t be charged again for the subscription. After it’s canceled, you can no longer update the subscription or its [metadata](docs.stripe.com/metadata).

Any pending invoice items that you’ve created are still charged at the end of the period, unless manually [deleted](docs.stripe.com/api/invoiceitems/delete). If you’ve set the subscription to cancel at the end of the period, any pending prorations are also left in place and collected at the end of the period. But if the subscription is set to cancel immediately, pending prorations are removed if invoice_now and prorate are both set to true.

By default, upon subscription cancellation, Stripe stops automatic collection of all finalized invoices for the customer. This is intended to prevent unexpected payment attempts after the customer has canceled a subscription. However, you can resume automatic collection of the invoices manually after subscription cancellation to have us proceed. Or, you could check for unpaid invoices before allowing the customer to cancel the subscription at all.



1028
1029
1030
1031
1032
1033
1034
1035
# File 'lib/stripe/resources/subscription.rb', line 1028

def self.cancel(subscription_exposed_id, params = {}, opts = {})
  request_stripe_object(
    method: :delete,
    path: format("/v1/subscriptions/%<subscription_exposed_id>s", { subscription_exposed_id: CGI.escape(subscription_exposed_id) }),
    params: params,
    opts: opts
  )
end

.create(params = {}, opts = {}) ⇒ Object

Creates a new subscription on an existing customer. Each customer can have up to 500 active or scheduled subscriptions.

When you create a subscription with collection_method=charge_automatically, the first invoice is finalized as part of the request. The payment_behavior parameter determines the exact behavior of the initial payment.

To start subscriptions where the first invoice always begins in a draft status, use [subscription schedules](docs.stripe.com/docs/billing/subscriptions/subscription-schedules#managing) instead. Schedules provide the flexibility to model more complex billing configurations that change over time.



1044
1045
1046
# File 'lib/stripe/resources/subscription.rb', line 1044

def self.create(params = {}, opts = {})
  request_stripe_object(method: :post, path: "/v1/subscriptions", params: params, opts: opts)
end

.delete_discount(subscription_exposed_id, params = {}, opts = {}) ⇒ Object

Removes the currently applied discount on a subscription.



1059
1060
1061
1062
1063
1064
1065
1066
# File 'lib/stripe/resources/subscription.rb', line 1059

def self.delete_discount(subscription_exposed_id, params = {}, opts = {})
  request_stripe_object(
    method: :delete,
    path: format("/v1/subscriptions/%<subscription_exposed_id>s/discount", { subscription_exposed_id: CGI.escape(subscription_exposed_id) }),
    params: params,
    opts: opts
  )
end

.field_remappingsObject



1201
1202
1203
# File 'lib/stripe/resources/subscription.rb', line 1201

def self.field_remappings
  @field_remappings = {}
end

.inner_class_typesObject



1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
# File 'lib/stripe/resources/subscription.rb', line 1178

def self.inner_class_types
  @inner_class_types = {
    automatic_tax: AutomaticTax,
    billing_cycle_anchor_config: BillingCycleAnchorConfig,
    billing_mode: BillingMode,
    billing_schedules: BillingSchedule,
    billing_thresholds: BillingThresholds,
    cancellation_details: CancellationDetails,
    invoice_settings: InvoiceSettings,
    last_price_migration_error: LastPriceMigrationError,
    managed_payments: ManagedPayments,
    pause_collection: PauseCollection,
    payment_settings: PaymentSettings,
    pending_invoice_item_interval: PendingInvoiceItemInterval,
    pending_update: PendingUpdate,
    prebilling: Prebilling,
    presentment_details: PresentmentDetails,
    status_details: StatusDetails,
    transfer_data: TransferData,
    trial_settings: TrialSettings,
  }
end

.list(params = {}, opts = {}) ⇒ Object

By default, returns a list of subscriptions that have not been canceled. In order to list canceled subscriptions, specify status=canceled.



1069
1070
1071
# File 'lib/stripe/resources/subscription.rb', line 1069

def self.list(params = {}, opts = {})
  request_stripe_object(method: :get, path: "/v1/subscriptions", params: params, opts: opts)
end

.migrate(subscription, params = {}, opts = {}) ⇒ Object

Upgrade the billing_mode of an existing subscription.



1084
1085
1086
1087
1088
1089
1090
1091
# File 'lib/stripe/resources/subscription.rb', line 1084

def self.migrate(subscription, params = {}, opts = {})
  request_stripe_object(
    method: :post,
    path: format("/v1/subscriptions/%<subscription>s/migrate", { subscription: CGI.escape(subscription) }),
    params: params,
    opts: opts
  )
end

.object_nameObject



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

def self.object_name
  "subscription"
end

.pause(subscription, params = {}, opts = {}) ⇒ Object

Pauses a subscription by transitioning it to the paused status. A paused subscription does not generate invoices and will not advance to new billing periods. The subscription can be resumed later using the resume endpoint. Cannot pause subscriptions with attached schedules.



1104
1105
1106
1107
1108
1109
1110
1111
# File 'lib/stripe/resources/subscription.rb', line 1104

def self.pause(subscription, params = {}, opts = {})
  request_stripe_object(
    method: :post,
    path: format("/v1/subscriptions/%<subscription>s/pause", { subscription: CGI.escape(subscription) }),
    params: params,
    opts: opts
  )
end

.resume(subscription, params = {}, opts = {}) ⇒ Object

Initiates resumption of a paused subscription, optionally resetting the billing cycle anchor and creating prorations. If a resumption invoice is generated, it must be paid or marked uncollectible before the subscription will be unpaused. If payment succeeds the subscription will become active, and if payment fails the subscription will be past_due. The resumption invoice will void automatically if not paid by the expiration date.



1124
1125
1126
1127
1128
1129
1130
1131
# File 'lib/stripe/resources/subscription.rb', line 1124

def self.resume(subscription, params = {}, opts = {})
  request_stripe_object(
    method: :post,
    path: format("/v1/subscriptions/%<subscription>s/resume", { subscription: CGI.escape(subscription) }),
    params: params,
    opts: opts
  )
end

.search(params = {}, opts = {}) ⇒ Object



1133
1134
1135
1136
1137
1138
1139
1140
# File 'lib/stripe/resources/subscription.rb', line 1133

def self.search(params = {}, opts = {})
  request_stripe_object(
    method: :get,
    path: "/v1/subscriptions/search",
    params: params,
    opts: opts
  )
end

.search_auto_paging_each(params = {}, opts = {}, &blk) ⇒ Object



1142
1143
1144
# File 'lib/stripe/resources/subscription.rb', line 1142

def self.search_auto_paging_each(params = {}, opts = {}, &blk)
  search(params, opts).auto_paging_each(&blk)
end

.update(subscription_exposed_id, params = {}, opts = {}) ⇒ Object

Updates an existing subscription to match the specified parameters. When changing prices or quantities, we optionally prorate the price we charge next month to make up for any price changes. To preview how the proration is calculated, use the [create preview](docs.stripe.com/docs/api/invoices/create_preview) endpoint.

By default, we prorate subscription changes. For example, if a customer signs up on May 1 for a 100 price, they’ll be billed 100 immediately. If on May 15 they switch to a 200 price, then on June 1 they’ll be billed 250 (200 for a renewal of her subscription, plus a 50 prorating adjustment for half of the previous month’s 100 difference). Similarly, a downgrade generates a credit that is applied to the next invoice. We also prorate when you make quantity changes.

Switching prices does not normally change the billing date or generate an immediate charge unless:

The billing interval is changed (for example, from monthly to yearly). The subscription moves from free to paid. A trial starts or ends.

In these cases, we apply a credit for the unused time on the previous price, immediately charge the customer using the new price, and reset the billing date. Learn about how [Stripe immediately attempts payment for subscription changes](docs.stripe.com/docs/billing/subscriptions/upgrade-downgrade#immediate-payment).

If you want to charge for an upgrade immediately, pass proration_behavior as always_invoice to create prorations, automatically invoice the customer for those proration adjustments, and attempt to collect payment. If you pass create_prorations, the prorations are created but not automatically invoiced. If you want to bill the customer for the prorations before the subscription’s renewal date, you need to manually [invoice the customer](docs.stripe.com/docs/api/invoices/create).

If you don’t want to prorate, set the proration_behavior option to none. With this option, the customer is billed 100 on May 1 and 200 on June 1. Similarly, if you set proration_behavior to none when switching between different billing intervals (for example, from monthly to yearly), we don’t generate any credits for the old subscription’s unused time. We still reset the billing date and bill immediately for the new subscription.

Updating the quantity on a subscription many times in an hour may result in [rate limiting. If you need to bill for a frequently changing quantity, consider integrating <a href=“/docs/billing/subscriptions/usage-based”>usage-based billing](docs.stripe.com/docs/rate-limits) instead.



1167
1168
1169
1170
1171
1172
1173
1174
# File 'lib/stripe/resources/subscription.rb', line 1167

def self.update(subscription_exposed_id, params = {}, opts = {})
  request_stripe_object(
    method: :post,
    path: format("/v1/subscriptions/%<subscription_exposed_id>s", { subscription_exposed_id: CGI.escape(subscription_exposed_id) }),
    params: params,
    opts: opts
  )
end

Instance Method Details

#attach_cadence(params = {}, opts = {}) ⇒ Object

Attach a Billing Cadence to an existing subscription. When attached, the subscription is billed by the Billing Cadence, potentially sharing invoices with the other subscriptions linked to the Billing Cadence.



990
991
992
993
994
995
996
997
# File 'lib/stripe/resources/subscription.rb', line 990

def attach_cadence(params = {}, opts = {})
  request_stripe_object(
    method: :post,
    path: format("/v1/subscriptions/%<subscription>s/attach_cadence", { subscription: CGI.escape(self["id"]) }),
    params: params,
    opts: opts
  )
end

#cancel(params = {}, opts = {}) ⇒ Object

Cancels a customer’s subscription immediately. The customer won’t be charged again for the subscription. After it’s canceled, you can no longer update the subscription or its [metadata](docs.stripe.com/metadata).

Any pending invoice items that you’ve created are still charged at the end of the period, unless manually [deleted](docs.stripe.com/api/invoiceitems/delete). If you’ve set the subscription to cancel at the end of the period, any pending prorations are also left in place and collected at the end of the period. But if the subscription is set to cancel immediately, pending prorations are removed if invoice_now and prorate are both set to true.

By default, upon subscription cancellation, Stripe stops automatic collection of all finalized invoices for the customer. This is intended to prevent unexpected payment attempts after the customer has canceled a subscription. However, you can resume automatic collection of the invoices manually after subscription cancellation to have us proceed. Or, you could check for unpaid invoices before allowing the customer to cancel the subscription at all.



1014
1015
1016
1017
1018
1019
1020
1021
# File 'lib/stripe/resources/subscription.rb', line 1014

def cancel(params = {}, opts = {})
  request_stripe_object(
    method: :delete,
    path: format("/v1/subscriptions/%<subscription_exposed_id>s", { subscription_exposed_id: CGI.escape(self["id"]) }),
    params: params,
    opts: opts
  )
end

#delete_discount(params = {}, opts = {}) ⇒ Object

Removes the currently applied discount on a subscription.



1049
1050
1051
1052
1053
1054
1055
1056
# File 'lib/stripe/resources/subscription.rb', line 1049

def delete_discount(params = {}, opts = {})
  request_stripe_object(
    method: :delete,
    path: format("/v1/subscriptions/%<subscription_exposed_id>s/discount", { subscription_exposed_id: CGI.escape(self["id"]) }),
    params: params,
    opts: opts
  )
end

#migrate(params = {}, opts = {}) ⇒ Object

Upgrade the billing_mode of an existing subscription.



1074
1075
1076
1077
1078
1079
1080
1081
# File 'lib/stripe/resources/subscription.rb', line 1074

def migrate(params = {}, opts = {})
  request_stripe_object(
    method: :post,
    path: format("/v1/subscriptions/%<subscription>s/migrate", { subscription: CGI.escape(self["id"]) }),
    params: params,
    opts: opts
  )
end

#pause(params = {}, opts = {}) ⇒ Object

Pauses a subscription by transitioning it to the paused status. A paused subscription does not generate invoices and will not advance to new billing periods. The subscription can be resumed later using the resume endpoint. Cannot pause subscriptions with attached schedules.



1094
1095
1096
1097
1098
1099
1100
1101
# File 'lib/stripe/resources/subscription.rb', line 1094

def pause(params = {}, opts = {})
  request_stripe_object(
    method: :post,
    path: format("/v1/subscriptions/%<subscription>s/pause", { subscription: CGI.escape(self["id"]) }),
    params: params,
    opts: opts
  )
end

#resume(params = {}, opts = {}) ⇒ Object

Initiates resumption of a paused subscription, optionally resetting the billing cycle anchor and creating prorations. If a resumption invoice is generated, it must be paid or marked uncollectible before the subscription will be unpaused. If payment succeeds the subscription will become active, and if payment fails the subscription will be past_due. The resumption invoice will void automatically if not paid by the expiration date.



1114
1115
1116
1117
1118
1119
1120
1121
# File 'lib/stripe/resources/subscription.rb', line 1114

def resume(params = {}, opts = {})
  request_stripe_object(
    method: :post,
    path: format("/v1/subscriptions/%<subscription>s/resume", { subscription: CGI.escape(self["id"]) }),
    params: params,
    opts: opts
  )
end