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.take(),
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.take(),
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.take(),
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(Clone, Eq, PartialEq)]
305#[non_exhaustive]
306pub enum SubscriptionScheduleStatus {
307    Active,
308    Canceled,
309    Completed,
310    NotStarted,
311    Released,
312    /// An unrecognized value from Stripe. Should not be used as a request parameter.
313    Unknown(String),
314}
315impl SubscriptionScheduleStatus {
316    pub fn as_str(&self) -> &str {
317        use SubscriptionScheduleStatus::*;
318        match self {
319            Active => "active",
320            Canceled => "canceled",
321            Completed => "completed",
322            NotStarted => "not_started",
323            Released => "released",
324            Unknown(v) => v,
325        }
326    }
327}
328
329impl std::str::FromStr for SubscriptionScheduleStatus {
330    type Err = std::convert::Infallible;
331    fn from_str(s: &str) -> Result<Self, Self::Err> {
332        use SubscriptionScheduleStatus::*;
333        match s {
334            "active" => Ok(Active),
335            "canceled" => Ok(Canceled),
336            "completed" => Ok(Completed),
337            "not_started" => Ok(NotStarted),
338            "released" => Ok(Released),
339            v => {
340                tracing::warn!("Unknown value '{}' for enum '{}'", v, "SubscriptionScheduleStatus");
341                Ok(Unknown(v.to_owned()))
342            }
343        }
344    }
345}
346impl std::fmt::Display for SubscriptionScheduleStatus {
347    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
348        f.write_str(self.as_str())
349    }
350}
351
352impl std::fmt::Debug for SubscriptionScheduleStatus {
353    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
354        f.write_str(self.as_str())
355    }
356}
357#[cfg(feature = "serialize")]
358impl serde::Serialize for SubscriptionScheduleStatus {
359    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
360    where
361        S: serde::Serializer,
362    {
363        serializer.serialize_str(self.as_str())
364    }
365}
366impl miniserde::Deserialize for SubscriptionScheduleStatus {
367    fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
368        crate::Place::new(out)
369    }
370}
371
372impl miniserde::de::Visitor for crate::Place<SubscriptionScheduleStatus> {
373    fn string(&mut self, s: &str) -> miniserde::Result<()> {
374        use std::str::FromStr;
375        self.out = Some(SubscriptionScheduleStatus::from_str(s).expect("infallible"));
376        Ok(())
377    }
378}
379
380stripe_types::impl_from_val_with_from_str!(SubscriptionScheduleStatus);
381#[cfg(feature = "deserialize")]
382impl<'de> serde::Deserialize<'de> for SubscriptionScheduleStatus {
383    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
384        use std::str::FromStr;
385        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
386        Ok(Self::from_str(&s).expect("infallible"))
387    }
388}
389impl stripe_types::Object for SubscriptionSchedule {
390    type Id = stripe_shared::SubscriptionScheduleId;
391    fn id(&self) -> &Self::Id {
392        &self.id
393    }
394
395    fn into_id(self) -> Self::Id {
396        self.id
397    }
398}
399stripe_types::def_id!(SubscriptionScheduleId);
400#[derive(Clone, Eq, PartialEq)]
401#[non_exhaustive]
402pub enum SubscriptionScheduleEndBehavior {
403    Cancel,
404    None,
405    Release,
406    Renew,
407    /// An unrecognized value from Stripe. Should not be used as a request parameter.
408    Unknown(String),
409}
410impl SubscriptionScheduleEndBehavior {
411    pub fn as_str(&self) -> &str {
412        use SubscriptionScheduleEndBehavior::*;
413        match self {
414            Cancel => "cancel",
415            None => "none",
416            Release => "release",
417            Renew => "renew",
418            Unknown(v) => v,
419        }
420    }
421}
422
423impl std::str::FromStr for SubscriptionScheduleEndBehavior {
424    type Err = std::convert::Infallible;
425    fn from_str(s: &str) -> Result<Self, Self::Err> {
426        use SubscriptionScheduleEndBehavior::*;
427        match s {
428            "cancel" => Ok(Cancel),
429            "none" => Ok(None),
430            "release" => Ok(Release),
431            "renew" => Ok(Renew),
432            v => {
433                tracing::warn!(
434                    "Unknown value '{}' for enum '{}'",
435                    v,
436                    "SubscriptionScheduleEndBehavior"
437                );
438                Ok(Unknown(v.to_owned()))
439            }
440        }
441    }
442}
443impl std::fmt::Display for SubscriptionScheduleEndBehavior {
444    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
445        f.write_str(self.as_str())
446    }
447}
448
449impl std::fmt::Debug for SubscriptionScheduleEndBehavior {
450    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
451        f.write_str(self.as_str())
452    }
453}
454impl serde::Serialize for SubscriptionScheduleEndBehavior {
455    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
456    where
457        S: serde::Serializer,
458    {
459        serializer.serialize_str(self.as_str())
460    }
461}
462impl miniserde::Deserialize for SubscriptionScheduleEndBehavior {
463    fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
464        crate::Place::new(out)
465    }
466}
467
468impl miniserde::de::Visitor for crate::Place<SubscriptionScheduleEndBehavior> {
469    fn string(&mut self, s: &str) -> miniserde::Result<()> {
470        use std::str::FromStr;
471        self.out = Some(SubscriptionScheduleEndBehavior::from_str(s).expect("infallible"));
472        Ok(())
473    }
474}
475
476stripe_types::impl_from_val_with_from_str!(SubscriptionScheduleEndBehavior);
477#[cfg(feature = "deserialize")]
478impl<'de> serde::Deserialize<'de> for SubscriptionScheduleEndBehavior {
479    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
480        use std::str::FromStr;
481        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
482        Ok(Self::from_str(&s).expect("infallible"))
483    }
484}