stripe_shared/
mandate.rs

1/// A Mandate is a record of the permission that your customer gives you to debit their payment method.
2///
3/// For more details see <<https://stripe.com/docs/api/mandates/object>>.
4#[derive(Clone, Debug)]
5#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
6pub struct Mandate {
7    pub customer_acceptance: stripe_shared::CustomerAcceptance,
8    /// Unique identifier for the object.
9    pub id: stripe_shared::MandateId,
10    /// Has the value `true` if the object exists in live mode or the value `false` if the object exists in test mode.
11    pub livemode: bool,
12    pub multi_use: Option<stripe_shared::MandateMultiUse>,
13    /// The account (if any) that the mandate is intended for.
14    pub on_behalf_of: Option<String>,
15    /// ID of the payment method associated with this mandate.
16    pub payment_method: stripe_types::Expandable<stripe_shared::PaymentMethod>,
17    pub payment_method_details: stripe_shared::MandatePaymentMethodDetails,
18    pub single_use: Option<stripe_shared::MandateSingleUse>,
19    /// The mandate status indicates whether or not you can use it to initiate a payment.
20    pub status: MandateStatus,
21    /// The type of the mandate.
22    #[cfg_attr(feature = "deserialize", serde(rename = "type"))]
23    pub type_: MandateType,
24}
25#[doc(hidden)]
26pub struct MandateBuilder {
27    customer_acceptance: Option<stripe_shared::CustomerAcceptance>,
28    id: Option<stripe_shared::MandateId>,
29    livemode: Option<bool>,
30    multi_use: Option<Option<stripe_shared::MandateMultiUse>>,
31    on_behalf_of: Option<Option<String>>,
32    payment_method: Option<stripe_types::Expandable<stripe_shared::PaymentMethod>>,
33    payment_method_details: Option<stripe_shared::MandatePaymentMethodDetails>,
34    single_use: Option<Option<stripe_shared::MandateSingleUse>>,
35    status: Option<MandateStatus>,
36    type_: Option<MandateType>,
37}
38
39#[allow(
40    unused_variables,
41    irrefutable_let_patterns,
42    clippy::let_unit_value,
43    clippy::match_single_binding,
44    clippy::single_match
45)]
46const _: () = {
47    use miniserde::de::{Map, Visitor};
48    use miniserde::json::Value;
49    use miniserde::{Deserialize, Result, make_place};
50    use stripe_types::miniserde_helpers::FromValueOpt;
51    use stripe_types::{MapBuilder, ObjectDeser};
52
53    make_place!(Place);
54
55    impl Deserialize for Mandate {
56        fn begin(out: &mut Option<Self>) -> &mut dyn Visitor {
57            Place::new(out)
58        }
59    }
60
61    struct Builder<'a> {
62        out: &'a mut Option<Mandate>,
63        builder: MandateBuilder,
64    }
65
66    impl Visitor for Place<Mandate> {
67        fn map(&mut self) -> Result<Box<dyn Map + '_>> {
68            Ok(Box::new(Builder { out: &mut self.out, builder: MandateBuilder::deser_default() }))
69        }
70    }
71
72    impl MapBuilder for MandateBuilder {
73        type Out = Mandate;
74        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
75            Ok(match k {
76                "customer_acceptance" => Deserialize::begin(&mut self.customer_acceptance),
77                "id" => Deserialize::begin(&mut self.id),
78                "livemode" => Deserialize::begin(&mut self.livemode),
79                "multi_use" => Deserialize::begin(&mut self.multi_use),
80                "on_behalf_of" => Deserialize::begin(&mut self.on_behalf_of),
81                "payment_method" => Deserialize::begin(&mut self.payment_method),
82                "payment_method_details" => Deserialize::begin(&mut self.payment_method_details),
83                "single_use" => Deserialize::begin(&mut self.single_use),
84                "status" => Deserialize::begin(&mut self.status),
85                "type" => Deserialize::begin(&mut self.type_),
86                _ => <dyn Visitor>::ignore(),
87            })
88        }
89
90        fn deser_default() -> Self {
91            Self {
92                customer_acceptance: Deserialize::default(),
93                id: Deserialize::default(),
94                livemode: Deserialize::default(),
95                multi_use: Deserialize::default(),
96                on_behalf_of: Deserialize::default(),
97                payment_method: Deserialize::default(),
98                payment_method_details: Deserialize::default(),
99                single_use: Deserialize::default(),
100                status: Deserialize::default(),
101                type_: Deserialize::default(),
102            }
103        }
104
105        fn take_out(&mut self) -> Option<Self::Out> {
106            let (
107                Some(customer_acceptance),
108                Some(id),
109                Some(livemode),
110                Some(multi_use),
111                Some(on_behalf_of),
112                Some(payment_method),
113                Some(payment_method_details),
114                Some(single_use),
115                Some(status),
116                Some(type_),
117            ) = (
118                self.customer_acceptance.take(),
119                self.id.take(),
120                self.livemode,
121                self.multi_use,
122                self.on_behalf_of.take(),
123                self.payment_method.take(),
124                self.payment_method_details.take(),
125                self.single_use.take(),
126                self.status.take(),
127                self.type_.take(),
128            )
129            else {
130                return None;
131            };
132            Some(Self::Out {
133                customer_acceptance,
134                id,
135                livemode,
136                multi_use,
137                on_behalf_of,
138                payment_method,
139                payment_method_details,
140                single_use,
141                status,
142                type_,
143            })
144        }
145    }
146
147    impl Map for Builder<'_> {
148        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
149            self.builder.key(k)
150        }
151
152        fn finish(&mut self) -> Result<()> {
153            *self.out = self.builder.take_out();
154            Ok(())
155        }
156    }
157
158    impl ObjectDeser for Mandate {
159        type Builder = MandateBuilder;
160    }
161
162    impl FromValueOpt for Mandate {
163        fn from_value(v: Value) -> Option<Self> {
164            let Value::Object(obj) = v else {
165                return None;
166            };
167            let mut b = MandateBuilder::deser_default();
168            for (k, v) in obj {
169                match k.as_str() {
170                    "customer_acceptance" => b.customer_acceptance = FromValueOpt::from_value(v),
171                    "id" => b.id = FromValueOpt::from_value(v),
172                    "livemode" => b.livemode = FromValueOpt::from_value(v),
173                    "multi_use" => b.multi_use = FromValueOpt::from_value(v),
174                    "on_behalf_of" => b.on_behalf_of = FromValueOpt::from_value(v),
175                    "payment_method" => b.payment_method = FromValueOpt::from_value(v),
176                    "payment_method_details" => {
177                        b.payment_method_details = FromValueOpt::from_value(v)
178                    }
179                    "single_use" => b.single_use = FromValueOpt::from_value(v),
180                    "status" => b.status = FromValueOpt::from_value(v),
181                    "type" => b.type_ = FromValueOpt::from_value(v),
182                    _ => {}
183                }
184            }
185            b.take_out()
186        }
187    }
188};
189#[cfg(feature = "serialize")]
190impl serde::Serialize for Mandate {
191    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
192        use serde::ser::SerializeStruct;
193        let mut s = s.serialize_struct("Mandate", 11)?;
194        s.serialize_field("customer_acceptance", &self.customer_acceptance)?;
195        s.serialize_field("id", &self.id)?;
196        s.serialize_field("livemode", &self.livemode)?;
197        s.serialize_field("multi_use", &self.multi_use)?;
198        s.serialize_field("on_behalf_of", &self.on_behalf_of)?;
199        s.serialize_field("payment_method", &self.payment_method)?;
200        s.serialize_field("payment_method_details", &self.payment_method_details)?;
201        s.serialize_field("single_use", &self.single_use)?;
202        s.serialize_field("status", &self.status)?;
203        s.serialize_field("type", &self.type_)?;
204
205        s.serialize_field("object", "mandate")?;
206        s.end()
207    }
208}
209/// The mandate status indicates whether or not you can use it to initiate a payment.
210#[derive(Clone, Eq, PartialEq)]
211#[non_exhaustive]
212pub enum MandateStatus {
213    Active,
214    Inactive,
215    Pending,
216    /// An unrecognized value from Stripe. Should not be used as a request parameter.
217    Unknown(String),
218}
219impl MandateStatus {
220    pub fn as_str(&self) -> &str {
221        use MandateStatus::*;
222        match self {
223            Active => "active",
224            Inactive => "inactive",
225            Pending => "pending",
226            Unknown(v) => v,
227        }
228    }
229}
230
231impl std::str::FromStr for MandateStatus {
232    type Err = std::convert::Infallible;
233    fn from_str(s: &str) -> Result<Self, Self::Err> {
234        use MandateStatus::*;
235        match s {
236            "active" => Ok(Active),
237            "inactive" => Ok(Inactive),
238            "pending" => Ok(Pending),
239            v => {
240                tracing::warn!("Unknown value '{}' for enum '{}'", v, "MandateStatus");
241                Ok(Unknown(v.to_owned()))
242            }
243        }
244    }
245}
246impl std::fmt::Display for MandateStatus {
247    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
248        f.write_str(self.as_str())
249    }
250}
251
252impl std::fmt::Debug for MandateStatus {
253    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
254        f.write_str(self.as_str())
255    }
256}
257#[cfg(feature = "serialize")]
258impl serde::Serialize for MandateStatus {
259    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
260    where
261        S: serde::Serializer,
262    {
263        serializer.serialize_str(self.as_str())
264    }
265}
266impl miniserde::Deserialize for MandateStatus {
267    fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
268        crate::Place::new(out)
269    }
270}
271
272impl miniserde::de::Visitor for crate::Place<MandateStatus> {
273    fn string(&mut self, s: &str) -> miniserde::Result<()> {
274        use std::str::FromStr;
275        self.out = Some(MandateStatus::from_str(s).expect("infallible"));
276        Ok(())
277    }
278}
279
280stripe_types::impl_from_val_with_from_str!(MandateStatus);
281#[cfg(feature = "deserialize")]
282impl<'de> serde::Deserialize<'de> for MandateStatus {
283    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
284        use std::str::FromStr;
285        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
286        Ok(Self::from_str(&s).expect("infallible"))
287    }
288}
289/// The type of the mandate.
290#[derive(Clone, Eq, PartialEq)]
291#[non_exhaustive]
292pub enum MandateType {
293    MultiUse,
294    SingleUse,
295    /// An unrecognized value from Stripe. Should not be used as a request parameter.
296    Unknown(String),
297}
298impl MandateType {
299    pub fn as_str(&self) -> &str {
300        use MandateType::*;
301        match self {
302            MultiUse => "multi_use",
303            SingleUse => "single_use",
304            Unknown(v) => v,
305        }
306    }
307}
308
309impl std::str::FromStr for MandateType {
310    type Err = std::convert::Infallible;
311    fn from_str(s: &str) -> Result<Self, Self::Err> {
312        use MandateType::*;
313        match s {
314            "multi_use" => Ok(MultiUse),
315            "single_use" => Ok(SingleUse),
316            v => {
317                tracing::warn!("Unknown value '{}' for enum '{}'", v, "MandateType");
318                Ok(Unknown(v.to_owned()))
319            }
320        }
321    }
322}
323impl std::fmt::Display for MandateType {
324    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
325        f.write_str(self.as_str())
326    }
327}
328
329impl std::fmt::Debug for MandateType {
330    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
331        f.write_str(self.as_str())
332    }
333}
334#[cfg(feature = "serialize")]
335impl serde::Serialize for MandateType {
336    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
337    where
338        S: serde::Serializer,
339    {
340        serializer.serialize_str(self.as_str())
341    }
342}
343impl miniserde::Deserialize for MandateType {
344    fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
345        crate::Place::new(out)
346    }
347}
348
349impl miniserde::de::Visitor for crate::Place<MandateType> {
350    fn string(&mut self, s: &str) -> miniserde::Result<()> {
351        use std::str::FromStr;
352        self.out = Some(MandateType::from_str(s).expect("infallible"));
353        Ok(())
354    }
355}
356
357stripe_types::impl_from_val_with_from_str!(MandateType);
358#[cfg(feature = "deserialize")]
359impl<'de> serde::Deserialize<'de> for MandateType {
360    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
361        use std::str::FromStr;
362        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
363        Ok(Self::from_str(&s).expect("infallible"))
364    }
365}
366impl stripe_types::Object for Mandate {
367    type Id = stripe_shared::MandateId;
368    fn id(&self) -> &Self::Id {
369        &self.id
370    }
371
372    fn into_id(self) -> Self::Id {
373        self.id
374    }
375}
376stripe_types::def_id!(MandateId);