stripe_shared/
promotion_code.rs

1/// A Promotion Code represents a customer-redeemable code for an underlying promotion.
2/// You can create multiple codes for a single promotion.
3///
4/// If you enable promotion codes in your [customer portal configuration](https://stripe.com/docs/customer-management/configure-portal), then customers can redeem a code themselves when updating a subscription in the portal.
5/// Customers can also view the currently active promotion codes and coupons on each of their subscriptions in the portal.
6///
7/// For more details see <<https://stripe.com/docs/api/promotion_codes/object>>.
8#[derive(Clone, Debug)]
9#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
10pub struct PromotionCode {
11    /// Whether the promotion code is currently active.
12    /// A promotion code is only active if the coupon is also valid.
13    pub active: bool,
14    /// The customer-facing code.
15    /// Regardless of case, this code must be unique across all active promotion codes for each customer.
16    /// Valid characters are lower case letters (a-z), upper case letters (A-Z), and digits (0-9).
17    pub code: String,
18    /// Time at which the object was created. Measured in seconds since the Unix epoch.
19    pub created: stripe_types::Timestamp,
20    /// The customer that this promotion code can be used by.
21    pub customer: Option<stripe_types::Expandable<stripe_shared::Customer>>,
22    /// Date at which the promotion code can no longer be redeemed.
23    pub expires_at: Option<stripe_types::Timestamp>,
24    /// Unique identifier for the object.
25    pub id: stripe_shared::PromotionCodeId,
26    /// Has the value `true` if the object exists in live mode or the value `false` if the object exists in test mode.
27    pub livemode: bool,
28    /// Maximum number of times this promotion code can be redeemed.
29    pub max_redemptions: Option<i64>,
30    /// Set of [key-value pairs](https://stripe.com/docs/api/metadata) that you can attach to an object.
31    /// This can be useful for storing additional information about the object in a structured format.
32    pub metadata: Option<std::collections::HashMap<String, String>>,
33    pub promotion: stripe_shared::PromotionCodesResourcePromotion,
34    pub restrictions: stripe_shared::PromotionCodesResourceRestrictions,
35    /// Number of times this promotion code has been used.
36    pub times_redeemed: i64,
37}
38#[doc(hidden)]
39pub struct PromotionCodeBuilder {
40    active: Option<bool>,
41    code: Option<String>,
42    created: Option<stripe_types::Timestamp>,
43    customer: Option<Option<stripe_types::Expandable<stripe_shared::Customer>>>,
44    expires_at: Option<Option<stripe_types::Timestamp>>,
45    id: Option<stripe_shared::PromotionCodeId>,
46    livemode: Option<bool>,
47    max_redemptions: Option<Option<i64>>,
48    metadata: Option<Option<std::collections::HashMap<String, String>>>,
49    promotion: Option<stripe_shared::PromotionCodesResourcePromotion>,
50    restrictions: Option<stripe_shared::PromotionCodesResourceRestrictions>,
51    times_redeemed: Option<i64>,
52}
53
54#[allow(
55    unused_variables,
56    irrefutable_let_patterns,
57    clippy::let_unit_value,
58    clippy::match_single_binding,
59    clippy::single_match
60)]
61const _: () = {
62    use miniserde::de::{Map, Visitor};
63    use miniserde::json::Value;
64    use miniserde::{Deserialize, Result, make_place};
65    use stripe_types::miniserde_helpers::FromValueOpt;
66    use stripe_types::{MapBuilder, ObjectDeser};
67
68    make_place!(Place);
69
70    impl Deserialize for PromotionCode {
71        fn begin(out: &mut Option<Self>) -> &mut dyn Visitor {
72            Place::new(out)
73        }
74    }
75
76    struct Builder<'a> {
77        out: &'a mut Option<PromotionCode>,
78        builder: PromotionCodeBuilder,
79    }
80
81    impl Visitor for Place<PromotionCode> {
82        fn map(&mut self) -> Result<Box<dyn Map + '_>> {
83            Ok(Box::new(Builder {
84                out: &mut self.out,
85                builder: PromotionCodeBuilder::deser_default(),
86            }))
87        }
88    }
89
90    impl MapBuilder for PromotionCodeBuilder {
91        type Out = PromotionCode;
92        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
93            Ok(match k {
94                "active" => Deserialize::begin(&mut self.active),
95                "code" => Deserialize::begin(&mut self.code),
96                "created" => Deserialize::begin(&mut self.created),
97                "customer" => Deserialize::begin(&mut self.customer),
98                "expires_at" => Deserialize::begin(&mut self.expires_at),
99                "id" => Deserialize::begin(&mut self.id),
100                "livemode" => Deserialize::begin(&mut self.livemode),
101                "max_redemptions" => Deserialize::begin(&mut self.max_redemptions),
102                "metadata" => Deserialize::begin(&mut self.metadata),
103                "promotion" => Deserialize::begin(&mut self.promotion),
104                "restrictions" => Deserialize::begin(&mut self.restrictions),
105                "times_redeemed" => Deserialize::begin(&mut self.times_redeemed),
106                _ => <dyn Visitor>::ignore(),
107            })
108        }
109
110        fn deser_default() -> Self {
111            Self {
112                active: Deserialize::default(),
113                code: Deserialize::default(),
114                created: Deserialize::default(),
115                customer: Deserialize::default(),
116                expires_at: Deserialize::default(),
117                id: Deserialize::default(),
118                livemode: Deserialize::default(),
119                max_redemptions: Deserialize::default(),
120                metadata: Deserialize::default(),
121                promotion: Deserialize::default(),
122                restrictions: Deserialize::default(),
123                times_redeemed: Deserialize::default(),
124            }
125        }
126
127        fn take_out(&mut self) -> Option<Self::Out> {
128            let (
129                Some(active),
130                Some(code),
131                Some(created),
132                Some(customer),
133                Some(expires_at),
134                Some(id),
135                Some(livemode),
136                Some(max_redemptions),
137                Some(metadata),
138                Some(promotion),
139                Some(restrictions),
140                Some(times_redeemed),
141            ) = (
142                self.active,
143                self.code.take(),
144                self.created,
145                self.customer.take(),
146                self.expires_at,
147                self.id.take(),
148                self.livemode,
149                self.max_redemptions,
150                self.metadata.take(),
151                self.promotion.take(),
152                self.restrictions.take(),
153                self.times_redeemed,
154            )
155            else {
156                return None;
157            };
158            Some(Self::Out {
159                active,
160                code,
161                created,
162                customer,
163                expires_at,
164                id,
165                livemode,
166                max_redemptions,
167                metadata,
168                promotion,
169                restrictions,
170                times_redeemed,
171            })
172        }
173    }
174
175    impl Map for Builder<'_> {
176        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
177            self.builder.key(k)
178        }
179
180        fn finish(&mut self) -> Result<()> {
181            *self.out = self.builder.take_out();
182            Ok(())
183        }
184    }
185
186    impl ObjectDeser for PromotionCode {
187        type Builder = PromotionCodeBuilder;
188    }
189
190    impl FromValueOpt for PromotionCode {
191        fn from_value(v: Value) -> Option<Self> {
192            let Value::Object(obj) = v else {
193                return None;
194            };
195            let mut b = PromotionCodeBuilder::deser_default();
196            for (k, v) in obj {
197                match k.as_str() {
198                    "active" => b.active = FromValueOpt::from_value(v),
199                    "code" => b.code = FromValueOpt::from_value(v),
200                    "created" => b.created = FromValueOpt::from_value(v),
201                    "customer" => b.customer = FromValueOpt::from_value(v),
202                    "expires_at" => b.expires_at = FromValueOpt::from_value(v),
203                    "id" => b.id = FromValueOpt::from_value(v),
204                    "livemode" => b.livemode = FromValueOpt::from_value(v),
205                    "max_redemptions" => b.max_redemptions = FromValueOpt::from_value(v),
206                    "metadata" => b.metadata = FromValueOpt::from_value(v),
207                    "promotion" => b.promotion = FromValueOpt::from_value(v),
208                    "restrictions" => b.restrictions = FromValueOpt::from_value(v),
209                    "times_redeemed" => b.times_redeemed = FromValueOpt::from_value(v),
210                    _ => {}
211                }
212            }
213            b.take_out()
214        }
215    }
216};
217#[cfg(feature = "serialize")]
218impl serde::Serialize for PromotionCode {
219    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
220        use serde::ser::SerializeStruct;
221        let mut s = s.serialize_struct("PromotionCode", 13)?;
222        s.serialize_field("active", &self.active)?;
223        s.serialize_field("code", &self.code)?;
224        s.serialize_field("created", &self.created)?;
225        s.serialize_field("customer", &self.customer)?;
226        s.serialize_field("expires_at", &self.expires_at)?;
227        s.serialize_field("id", &self.id)?;
228        s.serialize_field("livemode", &self.livemode)?;
229        s.serialize_field("max_redemptions", &self.max_redemptions)?;
230        s.serialize_field("metadata", &self.metadata)?;
231        s.serialize_field("promotion", &self.promotion)?;
232        s.serialize_field("restrictions", &self.restrictions)?;
233        s.serialize_field("times_redeemed", &self.times_redeemed)?;
234
235        s.serialize_field("object", "promotion_code")?;
236        s.end()
237    }
238}
239impl stripe_types::Object for PromotionCode {
240    type Id = stripe_shared::PromotionCodeId;
241    fn id(&self) -> &Self::Id {
242        &self.id
243    }
244
245    fn into_id(self) -> Self::Id {
246        self.id
247    }
248}
249stripe_types::def_id!(PromotionCodeId);