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::{make_place, Deserialize, Result};
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
131                _ => <dyn Visitor>::ignore(),
132            })
133        }
134
135        fn deser_default() -> Self {
136            Self {
137                application: Deserialize::default(),
138                billing_mode: Deserialize::default(),
139                canceled_at: Deserialize::default(),
140                completed_at: Deserialize::default(),
141                created: Deserialize::default(),
142                current_phase: Deserialize::default(),
143                customer: Deserialize::default(),
144                default_settings: Deserialize::default(),
145                end_behavior: Deserialize::default(),
146                id: Deserialize::default(),
147                livemode: Deserialize::default(),
148                metadata: Deserialize::default(),
149                phases: Deserialize::default(),
150                released_at: Deserialize::default(),
151                released_subscription: Deserialize::default(),
152                status: Deserialize::default(),
153                subscription: Deserialize::default(),
154                test_clock: Deserialize::default(),
155            }
156        }
157
158        fn take_out(&mut self) -> Option<Self::Out> {
159            let (
160                Some(application),
161                Some(billing_mode),
162                Some(canceled_at),
163                Some(completed_at),
164                Some(created),
165                Some(current_phase),
166                Some(customer),
167                Some(default_settings),
168                Some(end_behavior),
169                Some(id),
170                Some(livemode),
171                Some(metadata),
172                Some(phases),
173                Some(released_at),
174                Some(released_subscription),
175                Some(status),
176                Some(subscription),
177                Some(test_clock),
178            ) = (
179                self.application.take(),
180                self.billing_mode,
181                self.canceled_at,
182                self.completed_at,
183                self.created,
184                self.current_phase,
185                self.customer.take(),
186                self.default_settings.take(),
187                self.end_behavior,
188                self.id.take(),
189                self.livemode,
190                self.metadata.take(),
191                self.phases.take(),
192                self.released_at,
193                self.released_subscription.take(),
194                self.status,
195                self.subscription.take(),
196                self.test_clock.take(),
197            )
198            else {
199                return None;
200            };
201            Some(Self::Out {
202                application,
203                billing_mode,
204                canceled_at,
205                completed_at,
206                created,
207                current_phase,
208                customer,
209                default_settings,
210                end_behavior,
211                id,
212                livemode,
213                metadata,
214                phases,
215                released_at,
216                released_subscription,
217                status,
218                subscription,
219                test_clock,
220            })
221        }
222    }
223
224    impl Map for Builder<'_> {
225        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
226            self.builder.key(k)
227        }
228
229        fn finish(&mut self) -> Result<()> {
230            *self.out = self.builder.take_out();
231            Ok(())
232        }
233    }
234
235    impl ObjectDeser for SubscriptionSchedule {
236        type Builder = SubscriptionScheduleBuilder;
237    }
238
239    impl FromValueOpt for SubscriptionSchedule {
240        fn from_value(v: Value) -> Option<Self> {
241            let Value::Object(obj) = v else {
242                return None;
243            };
244            let mut b = SubscriptionScheduleBuilder::deser_default();
245            for (k, v) in obj {
246                match k.as_str() {
247                    "application" => b.application = FromValueOpt::from_value(v),
248                    "billing_mode" => b.billing_mode = FromValueOpt::from_value(v),
249                    "canceled_at" => b.canceled_at = FromValueOpt::from_value(v),
250                    "completed_at" => b.completed_at = FromValueOpt::from_value(v),
251                    "created" => b.created = FromValueOpt::from_value(v),
252                    "current_phase" => b.current_phase = FromValueOpt::from_value(v),
253                    "customer" => b.customer = FromValueOpt::from_value(v),
254                    "default_settings" => b.default_settings = FromValueOpt::from_value(v),
255                    "end_behavior" => b.end_behavior = FromValueOpt::from_value(v),
256                    "id" => b.id = FromValueOpt::from_value(v),
257                    "livemode" => b.livemode = FromValueOpt::from_value(v),
258                    "metadata" => b.metadata = FromValueOpt::from_value(v),
259                    "phases" => b.phases = FromValueOpt::from_value(v),
260                    "released_at" => b.released_at = FromValueOpt::from_value(v),
261                    "released_subscription" => {
262                        b.released_subscription = FromValueOpt::from_value(v)
263                    }
264                    "status" => b.status = FromValueOpt::from_value(v),
265                    "subscription" => b.subscription = FromValueOpt::from_value(v),
266                    "test_clock" => b.test_clock = FromValueOpt::from_value(v),
267
268                    _ => {}
269                }
270            }
271            b.take_out()
272        }
273    }
274};
275#[cfg(feature = "serialize")]
276impl serde::Serialize for SubscriptionSchedule {
277    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
278        use serde::ser::SerializeStruct;
279        let mut s = s.serialize_struct("SubscriptionSchedule", 19)?;
280        s.serialize_field("application", &self.application)?;
281        s.serialize_field("billing_mode", &self.billing_mode)?;
282        s.serialize_field("canceled_at", &self.canceled_at)?;
283        s.serialize_field("completed_at", &self.completed_at)?;
284        s.serialize_field("created", &self.created)?;
285        s.serialize_field("current_phase", &self.current_phase)?;
286        s.serialize_field("customer", &self.customer)?;
287        s.serialize_field("default_settings", &self.default_settings)?;
288        s.serialize_field("end_behavior", &self.end_behavior)?;
289        s.serialize_field("id", &self.id)?;
290        s.serialize_field("livemode", &self.livemode)?;
291        s.serialize_field("metadata", &self.metadata)?;
292        s.serialize_field("phases", &self.phases)?;
293        s.serialize_field("released_at", &self.released_at)?;
294        s.serialize_field("released_subscription", &self.released_subscription)?;
295        s.serialize_field("status", &self.status)?;
296        s.serialize_field("subscription", &self.subscription)?;
297        s.serialize_field("test_clock", &self.test_clock)?;
298
299        s.serialize_field("object", "subscription_schedule")?;
300        s.end()
301    }
302}
303/// The present status of the subscription schedule.
304/// Possible values are `not_started`, `active`, `completed`, `released`, and `canceled`.
305/// You can read more about the different states in our [behavior guide](https://stripe.com/docs/billing/subscriptions/subscription-schedules).
306#[derive(Copy, Clone, Eq, PartialEq)]
307pub enum SubscriptionScheduleStatus {
308    Active,
309    Canceled,
310    Completed,
311    NotStarted,
312    Released,
313}
314impl SubscriptionScheduleStatus {
315    pub fn as_str(self) -> &'static str {
316        use SubscriptionScheduleStatus::*;
317        match self {
318            Active => "active",
319            Canceled => "canceled",
320            Completed => "completed",
321            NotStarted => "not_started",
322            Released => "released",
323        }
324    }
325}
326
327impl std::str::FromStr for SubscriptionScheduleStatus {
328    type Err = stripe_types::StripeParseError;
329    fn from_str(s: &str) -> Result<Self, Self::Err> {
330        use SubscriptionScheduleStatus::*;
331        match s {
332            "active" => Ok(Active),
333            "canceled" => Ok(Canceled),
334            "completed" => Ok(Completed),
335            "not_started" => Ok(NotStarted),
336            "released" => Ok(Released),
337            _ => Err(stripe_types::StripeParseError),
338        }
339    }
340}
341impl std::fmt::Display for SubscriptionScheduleStatus {
342    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
343        f.write_str(self.as_str())
344    }
345}
346
347impl std::fmt::Debug for SubscriptionScheduleStatus {
348    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
349        f.write_str(self.as_str())
350    }
351}
352#[cfg(feature = "serialize")]
353impl serde::Serialize for SubscriptionScheduleStatus {
354    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
355    where
356        S: serde::Serializer,
357    {
358        serializer.serialize_str(self.as_str())
359    }
360}
361impl miniserde::Deserialize for SubscriptionScheduleStatus {
362    fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
363        crate::Place::new(out)
364    }
365}
366
367impl miniserde::de::Visitor for crate::Place<SubscriptionScheduleStatus> {
368    fn string(&mut self, s: &str) -> miniserde::Result<()> {
369        use std::str::FromStr;
370        self.out = Some(SubscriptionScheduleStatus::from_str(s).map_err(|_| miniserde::Error)?);
371        Ok(())
372    }
373}
374
375stripe_types::impl_from_val_with_from_str!(SubscriptionScheduleStatus);
376#[cfg(feature = "deserialize")]
377impl<'de> serde::Deserialize<'de> for SubscriptionScheduleStatus {
378    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
379        use std::str::FromStr;
380        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
381        Self::from_str(&s)
382            .map_err(|_| serde::de::Error::custom("Unknown value for SubscriptionScheduleStatus"))
383    }
384}
385impl stripe_types::Object for SubscriptionSchedule {
386    type Id = stripe_shared::SubscriptionScheduleId;
387    fn id(&self) -> &Self::Id {
388        &self.id
389    }
390
391    fn into_id(self) -> Self::Id {
392        self.id
393    }
394}
395stripe_types::def_id!(SubscriptionScheduleId);
396#[derive(Copy, Clone, Eq, PartialEq)]
397pub enum SubscriptionScheduleEndBehavior {
398    Cancel,
399    None,
400    Release,
401    Renew,
402}
403impl SubscriptionScheduleEndBehavior {
404    pub fn as_str(self) -> &'static str {
405        use SubscriptionScheduleEndBehavior::*;
406        match self {
407            Cancel => "cancel",
408            None => "none",
409            Release => "release",
410            Renew => "renew",
411        }
412    }
413}
414
415impl std::str::FromStr for SubscriptionScheduleEndBehavior {
416    type Err = stripe_types::StripeParseError;
417    fn from_str(s: &str) -> Result<Self, Self::Err> {
418        use SubscriptionScheduleEndBehavior::*;
419        match s {
420            "cancel" => Ok(Cancel),
421            "none" => Ok(None),
422            "release" => Ok(Release),
423            "renew" => Ok(Renew),
424            _ => Err(stripe_types::StripeParseError),
425        }
426    }
427}
428impl std::fmt::Display for SubscriptionScheduleEndBehavior {
429    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
430        f.write_str(self.as_str())
431    }
432}
433
434impl std::fmt::Debug for SubscriptionScheduleEndBehavior {
435    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
436        f.write_str(self.as_str())
437    }
438}
439impl serde::Serialize for SubscriptionScheduleEndBehavior {
440    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
441    where
442        S: serde::Serializer,
443    {
444        serializer.serialize_str(self.as_str())
445    }
446}
447impl miniserde::Deserialize for SubscriptionScheduleEndBehavior {
448    fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
449        crate::Place::new(out)
450    }
451}
452
453impl miniserde::de::Visitor for crate::Place<SubscriptionScheduleEndBehavior> {
454    fn string(&mut self, s: &str) -> miniserde::Result<()> {
455        use std::str::FromStr;
456        self.out =
457            Some(SubscriptionScheduleEndBehavior::from_str(s).map_err(|_| miniserde::Error)?);
458        Ok(())
459    }
460}
461
462stripe_types::impl_from_val_with_from_str!(SubscriptionScheduleEndBehavior);
463#[cfg(feature = "deserialize")]
464impl<'de> serde::Deserialize<'de> for SubscriptionScheduleEndBehavior {
465    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
466        use std::str::FromStr;
467        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
468        Self::from_str(&s).map_err(|_| {
469            serde::de::Error::custom("Unknown value for SubscriptionScheduleEndBehavior")
470        })
471    }
472}