Skip to main content

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