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