stripe_shared/
subscription_schedule.rs

1/// A subscription schedule allows you to create and manage the lifecycle of a subscription by predefining expected changes.
2///
3/// Related guide: [Subscription schedules](https://stripe.com/docs/billing/subscriptions/subscription-schedules).
4///
5/// For more details see <<https://stripe.com/docs/api/subscription_schedules/object>>.
6#[derive(Clone, Debug)]
7#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
8pub struct SubscriptionSchedule {
9    /// ID of the Connect Application that created the schedule.
10    pub application: Option<stripe_types::Expandable<stripe_shared::Application>>,
11    pub billing_mode: stripe_shared::SubscriptionsResourceBillingMode,
12    /// Time at which the subscription schedule was canceled. Measured in seconds since the Unix epoch.
13    pub canceled_at: Option<stripe_types::Timestamp>,
14    /// Time at which the subscription schedule was completed. Measured in seconds since the Unix epoch.
15    pub completed_at: Option<stripe_types::Timestamp>,
16    /// Time at which the object was created. Measured in seconds since the Unix epoch.
17    pub created: stripe_types::Timestamp,
18    /// Object representing the start and end dates for the current phase of the subscription schedule, if it is `active`.
19    pub current_phase: Option<stripe_shared::SubscriptionScheduleCurrentPhase>,
20    /// ID of the customer who owns the subscription schedule.
21    pub customer: stripe_types::Expandable<stripe_shared::Customer>,
22    pub default_settings: stripe_shared::SubscriptionSchedulesResourceDefaultSettings,
23    /// Behavior of the subscription schedule and underlying subscription when it ends.
24    /// Possible values are `release` or `cancel` with the default being `release`.
25    /// `release` will end the subscription schedule and keep the underlying subscription running.
26    /// `cancel` will end the subscription schedule and cancel the underlying subscription.
27    pub end_behavior: stripe_shared::SubscriptionScheduleEndBehavior,
28    /// Unique identifier for the object.
29    pub id: stripe_shared::SubscriptionScheduleId,
30    /// Has the value `true` if the object exists in live mode or the value `false` if the object exists in test mode.
31    pub livemode: bool,
32    /// Set of [key-value pairs](https://stripe.com/docs/api/metadata) that you can attach to an object.
33    /// This can be useful for storing additional information about the object in a structured format.
34    pub metadata: Option<std::collections::HashMap<String, String>>,
35    /// Configuration for the subscription schedule's phases.
36    pub phases: Vec<stripe_shared::SubscriptionSchedulePhaseConfiguration>,
37    /// Time at which the subscription schedule was released. Measured in seconds since the Unix epoch.
38    pub released_at: Option<stripe_types::Timestamp>,
39    /// ID of the subscription once managed by the subscription schedule (if it is released).
40    pub released_subscription: Option<String>,
41    /// The present status of the subscription schedule.
42    /// Possible values are `not_started`, `active`, `completed`, `released`, and `canceled`.
43    /// You can read more about the different states in our [behavior guide](https://stripe.com/docs/billing/subscriptions/subscription-schedules).
44    pub status: SubscriptionScheduleStatus,
45    /// ID of the subscription managed by the subscription schedule.
46    pub subscription: Option<stripe_types::Expandable<stripe_shared::Subscription>>,
47    /// ID of the test clock this subscription schedule belongs to.
48    pub test_clock: Option<stripe_types::Expandable<stripe_shared::TestHelpersTestClock>>,
49}
50#[doc(hidden)]
51pub struct SubscriptionScheduleBuilder {
52    application: Option<Option<stripe_types::Expandable<stripe_shared::Application>>>,
53    billing_mode: Option<stripe_shared::SubscriptionsResourceBillingMode>,
54    canceled_at: Option<Option<stripe_types::Timestamp>>,
55    completed_at: Option<Option<stripe_types::Timestamp>>,
56    created: Option<stripe_types::Timestamp>,
57    current_phase: Option<Option<stripe_shared::SubscriptionScheduleCurrentPhase>>,
58    customer: Option<stripe_types::Expandable<stripe_shared::Customer>>,
59    default_settings: Option<stripe_shared::SubscriptionSchedulesResourceDefaultSettings>,
60    end_behavior: Option<stripe_shared::SubscriptionScheduleEndBehavior>,
61    id: Option<stripe_shared::SubscriptionScheduleId>,
62    livemode: Option<bool>,
63    metadata: Option<Option<std::collections::HashMap<String, String>>>,
64    phases: Option<Vec<stripe_shared::SubscriptionSchedulePhaseConfiguration>>,
65    released_at: Option<Option<stripe_types::Timestamp>>,
66    released_subscription: Option<Option<String>>,
67    status: Option<SubscriptionScheduleStatus>,
68    subscription: Option<Option<stripe_types::Expandable<stripe_shared::Subscription>>>,
69    test_clock: Option<Option<stripe_types::Expandable<stripe_shared::TestHelpersTestClock>>>,
70}
71
72#[allow(
73    unused_variables,
74    irrefutable_let_patterns,
75    clippy::let_unit_value,
76    clippy::match_single_binding,
77    clippy::single_match
78)]
79const _: () = {
80    use miniserde::de::{Map, Visitor};
81    use miniserde::json::Value;
82    use miniserde::{Deserialize, Result, make_place};
83    use stripe_types::miniserde_helpers::FromValueOpt;
84    use stripe_types::{MapBuilder, ObjectDeser};
85
86    make_place!(Place);
87
88    impl Deserialize for SubscriptionSchedule {
89        fn begin(out: &mut Option<Self>) -> &mut dyn Visitor {
90            Place::new(out)
91        }
92    }
93
94    struct Builder<'a> {
95        out: &'a mut Option<SubscriptionSchedule>,
96        builder: SubscriptionScheduleBuilder,
97    }
98
99    impl Visitor for Place<SubscriptionSchedule> {
100        fn map(&mut self) -> Result<Box<dyn Map + '_>> {
101            Ok(Box::new(Builder {
102                out: &mut self.out,
103                builder: SubscriptionScheduleBuilder::deser_default(),
104            }))
105        }
106    }
107
108    impl MapBuilder for SubscriptionScheduleBuilder {
109        type Out = SubscriptionSchedule;
110        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
111            Ok(match k {
112                "application" => Deserialize::begin(&mut self.application),
113                "billing_mode" => Deserialize::begin(&mut self.billing_mode),
114                "canceled_at" => Deserialize::begin(&mut self.canceled_at),
115                "completed_at" => Deserialize::begin(&mut self.completed_at),
116                "created" => Deserialize::begin(&mut self.created),
117                "current_phase" => Deserialize::begin(&mut self.current_phase),
118                "customer" => Deserialize::begin(&mut self.customer),
119                "default_settings" => Deserialize::begin(&mut self.default_settings),
120                "end_behavior" => Deserialize::begin(&mut self.end_behavior),
121                "id" => Deserialize::begin(&mut self.id),
122                "livemode" => Deserialize::begin(&mut self.livemode),
123                "metadata" => Deserialize::begin(&mut self.metadata),
124                "phases" => Deserialize::begin(&mut self.phases),
125                "released_at" => Deserialize::begin(&mut self.released_at),
126                "released_subscription" => Deserialize::begin(&mut self.released_subscription),
127                "status" => Deserialize::begin(&mut self.status),
128                "subscription" => Deserialize::begin(&mut self.subscription),
129                "test_clock" => Deserialize::begin(&mut self.test_clock),
130                _ => <dyn Visitor>::ignore(),
131            })
132        }
133
134        fn deser_default() -> Self {
135            Self {
136                application: Deserialize::default(),
137                billing_mode: Deserialize::default(),
138                canceled_at: Deserialize::default(),
139                completed_at: Deserialize::default(),
140                created: Deserialize::default(),
141                current_phase: Deserialize::default(),
142                customer: Deserialize::default(),
143                default_settings: Deserialize::default(),
144                end_behavior: Deserialize::default(),
145                id: Deserialize::default(),
146                livemode: Deserialize::default(),
147                metadata: Deserialize::default(),
148                phases: Deserialize::default(),
149                released_at: Deserialize::default(),
150                released_subscription: Deserialize::default(),
151                status: Deserialize::default(),
152                subscription: Deserialize::default(),
153                test_clock: Deserialize::default(),
154            }
155        }
156
157        fn take_out(&mut self) -> Option<Self::Out> {
158            let (
159                Some(application),
160                Some(billing_mode),
161                Some(canceled_at),
162                Some(completed_at),
163                Some(created),
164                Some(current_phase),
165                Some(customer),
166                Some(default_settings),
167                Some(end_behavior),
168                Some(id),
169                Some(livemode),
170                Some(metadata),
171                Some(phases),
172                Some(released_at),
173                Some(released_subscription),
174                Some(status),
175                Some(subscription),
176                Some(test_clock),
177            ) = (
178                self.application.take(),
179                self.billing_mode,
180                self.canceled_at,
181                self.completed_at,
182                self.created,
183                self.current_phase,
184                self.customer.take(),
185                self.default_settings.take(),
186                self.end_behavior,
187                self.id.take(),
188                self.livemode,
189                self.metadata.take(),
190                self.phases.take(),
191                self.released_at,
192                self.released_subscription.take(),
193                self.status,
194                self.subscription.take(),
195                self.test_clock.take(),
196            )
197            else {
198                return None;
199            };
200            Some(Self::Out {
201                application,
202                billing_mode,
203                canceled_at,
204                completed_at,
205                created,
206                current_phase,
207                customer,
208                default_settings,
209                end_behavior,
210                id,
211                livemode,
212                metadata,
213                phases,
214                released_at,
215                released_subscription,
216                status,
217                subscription,
218                test_clock,
219            })
220        }
221    }
222
223    impl Map for Builder<'_> {
224        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
225            self.builder.key(k)
226        }
227
228        fn finish(&mut self) -> Result<()> {
229            *self.out = self.builder.take_out();
230            Ok(())
231        }
232    }
233
234    impl ObjectDeser for SubscriptionSchedule {
235        type Builder = SubscriptionScheduleBuilder;
236    }
237
238    impl FromValueOpt for SubscriptionSchedule {
239        fn from_value(v: Value) -> Option<Self> {
240            let Value::Object(obj) = v else {
241                return None;
242            };
243            let mut b = SubscriptionScheduleBuilder::deser_default();
244            for (k, v) in obj {
245                match k.as_str() {
246                    "application" => b.application = FromValueOpt::from_value(v),
247                    "billing_mode" => b.billing_mode = FromValueOpt::from_value(v),
248                    "canceled_at" => b.canceled_at = FromValueOpt::from_value(v),
249                    "completed_at" => b.completed_at = FromValueOpt::from_value(v),
250                    "created" => b.created = FromValueOpt::from_value(v),
251                    "current_phase" => b.current_phase = FromValueOpt::from_value(v),
252                    "customer" => b.customer = FromValueOpt::from_value(v),
253                    "default_settings" => b.default_settings = FromValueOpt::from_value(v),
254                    "end_behavior" => b.end_behavior = FromValueOpt::from_value(v),
255                    "id" => b.id = FromValueOpt::from_value(v),
256                    "livemode" => b.livemode = FromValueOpt::from_value(v),
257                    "metadata" => b.metadata = FromValueOpt::from_value(v),
258                    "phases" => b.phases = FromValueOpt::from_value(v),
259                    "released_at" => b.released_at = FromValueOpt::from_value(v),
260                    "released_subscription" => {
261                        b.released_subscription = FromValueOpt::from_value(v)
262                    }
263                    "status" => b.status = FromValueOpt::from_value(v),
264                    "subscription" => b.subscription = FromValueOpt::from_value(v),
265                    "test_clock" => b.test_clock = FromValueOpt::from_value(v),
266                    _ => {}
267                }
268            }
269            b.take_out()
270        }
271    }
272};
273#[cfg(feature = "serialize")]
274impl serde::Serialize for SubscriptionSchedule {
275    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
276        use serde::ser::SerializeStruct;
277        let mut s = s.serialize_struct("SubscriptionSchedule", 19)?;
278        s.serialize_field("application", &self.application)?;
279        s.serialize_field("billing_mode", &self.billing_mode)?;
280        s.serialize_field("canceled_at", &self.canceled_at)?;
281        s.serialize_field("completed_at", &self.completed_at)?;
282        s.serialize_field("created", &self.created)?;
283        s.serialize_field("current_phase", &self.current_phase)?;
284        s.serialize_field("customer", &self.customer)?;
285        s.serialize_field("default_settings", &self.default_settings)?;
286        s.serialize_field("end_behavior", &self.end_behavior)?;
287        s.serialize_field("id", &self.id)?;
288        s.serialize_field("livemode", &self.livemode)?;
289        s.serialize_field("metadata", &self.metadata)?;
290        s.serialize_field("phases", &self.phases)?;
291        s.serialize_field("released_at", &self.released_at)?;
292        s.serialize_field("released_subscription", &self.released_subscription)?;
293        s.serialize_field("status", &self.status)?;
294        s.serialize_field("subscription", &self.subscription)?;
295        s.serialize_field("test_clock", &self.test_clock)?;
296
297        s.serialize_field("object", "subscription_schedule")?;
298        s.end()
299    }
300}
301/// The present status of the subscription schedule.
302/// Possible values are `not_started`, `active`, `completed`, `released`, and `canceled`.
303/// You can read more about the different states in our [behavior guide](https://stripe.com/docs/billing/subscriptions/subscription-schedules).
304#[derive(Copy, Clone, Eq, PartialEq)]
305pub enum SubscriptionScheduleStatus {
306    Active,
307    Canceled,
308    Completed,
309    NotStarted,
310    Released,
311}
312impl SubscriptionScheduleStatus {
313    pub fn as_str(self) -> &'static str {
314        use SubscriptionScheduleStatus::*;
315        match self {
316            Active => "active",
317            Canceled => "canceled",
318            Completed => "completed",
319            NotStarted => "not_started",
320            Released => "released",
321        }
322    }
323}
324
325impl std::str::FromStr for SubscriptionScheduleStatus {
326    type Err = stripe_types::StripeParseError;
327    fn from_str(s: &str) -> Result<Self, Self::Err> {
328        use SubscriptionScheduleStatus::*;
329        match s {
330            "active" => Ok(Active),
331            "canceled" => Ok(Canceled),
332            "completed" => Ok(Completed),
333            "not_started" => Ok(NotStarted),
334            "released" => Ok(Released),
335            _ => Err(stripe_types::StripeParseError),
336        }
337    }
338}
339impl std::fmt::Display for SubscriptionScheduleStatus {
340    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
341        f.write_str(self.as_str())
342    }
343}
344
345impl std::fmt::Debug for SubscriptionScheduleStatus {
346    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
347        f.write_str(self.as_str())
348    }
349}
350#[cfg(feature = "serialize")]
351impl serde::Serialize for SubscriptionScheduleStatus {
352    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
353    where
354        S: serde::Serializer,
355    {
356        serializer.serialize_str(self.as_str())
357    }
358}
359impl miniserde::Deserialize for SubscriptionScheduleStatus {
360    fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
361        crate::Place::new(out)
362    }
363}
364
365impl miniserde::de::Visitor for crate::Place<SubscriptionScheduleStatus> {
366    fn string(&mut self, s: &str) -> miniserde::Result<()> {
367        use std::str::FromStr;
368        self.out = Some(SubscriptionScheduleStatus::from_str(s).map_err(|_| miniserde::Error)?);
369        Ok(())
370    }
371}
372
373stripe_types::impl_from_val_with_from_str!(SubscriptionScheduleStatus);
374#[cfg(feature = "deserialize")]
375impl<'de> serde::Deserialize<'de> for SubscriptionScheduleStatus {
376    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
377        use std::str::FromStr;
378        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
379        Self::from_str(&s)
380            .map_err(|_| serde::de::Error::custom("Unknown value for SubscriptionScheduleStatus"))
381    }
382}
383impl stripe_types::Object for SubscriptionSchedule {
384    type Id = stripe_shared::SubscriptionScheduleId;
385    fn id(&self) -> &Self::Id {
386        &self.id
387    }
388
389    fn into_id(self) -> Self::Id {
390        self.id
391    }
392}
393stripe_types::def_id!(SubscriptionScheduleId);
394#[derive(Copy, Clone, Eq, PartialEq)]
395pub enum SubscriptionScheduleEndBehavior {
396    Cancel,
397    None,
398    Release,
399    Renew,
400}
401impl SubscriptionScheduleEndBehavior {
402    pub fn as_str(self) -> &'static str {
403        use SubscriptionScheduleEndBehavior::*;
404        match self {
405            Cancel => "cancel",
406            None => "none",
407            Release => "release",
408            Renew => "renew",
409        }
410    }
411}
412
413impl std::str::FromStr for SubscriptionScheduleEndBehavior {
414    type Err = stripe_types::StripeParseError;
415    fn from_str(s: &str) -> Result<Self, Self::Err> {
416        use SubscriptionScheduleEndBehavior::*;
417        match s {
418            "cancel" => Ok(Cancel),
419            "none" => Ok(None),
420            "release" => Ok(Release),
421            "renew" => Ok(Renew),
422            _ => Err(stripe_types::StripeParseError),
423        }
424    }
425}
426impl std::fmt::Display for SubscriptionScheduleEndBehavior {
427    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
428        f.write_str(self.as_str())
429    }
430}
431
432impl std::fmt::Debug for SubscriptionScheduleEndBehavior {
433    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
434        f.write_str(self.as_str())
435    }
436}
437impl serde::Serialize for SubscriptionScheduleEndBehavior {
438    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
439    where
440        S: serde::Serializer,
441    {
442        serializer.serialize_str(self.as_str())
443    }
444}
445impl miniserde::Deserialize for SubscriptionScheduleEndBehavior {
446    fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
447        crate::Place::new(out)
448    }
449}
450
451impl miniserde::de::Visitor for crate::Place<SubscriptionScheduleEndBehavior> {
452    fn string(&mut self, s: &str) -> miniserde::Result<()> {
453        use std::str::FromStr;
454        self.out =
455            Some(SubscriptionScheduleEndBehavior::from_str(s).map_err(|_| miniserde::Error)?);
456        Ok(())
457    }
458}
459
460stripe_types::impl_from_val_with_from_str!(SubscriptionScheduleEndBehavior);
461#[cfg(feature = "deserialize")]
462impl<'de> serde::Deserialize<'de> for SubscriptionScheduleEndBehavior {
463    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
464        use std::str::FromStr;
465        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
466        Self::from_str(&s).map_err(|_| {
467            serde::de::Error::custom("Unknown value for SubscriptionScheduleEndBehavior")
468        })
469    }
470}