stripe/resources/generated/
promotion_code.rs

1// ======================================
2// This file was automatically generated.
3// ======================================
4
5use crate::client::{Client, Response};
6use crate::ids::{CouponId, CustomerId, PromotionCodeId};
7use crate::params::{
8    CurrencyMap, Expand, Expandable, List, Metadata, Object, Paginable, RangeQuery, Timestamp,
9};
10use crate::resources::{Coupon, Currency, Customer};
11use serde::{Deserialize, Serialize};
12
13/// The resource representing a Stripe "PromotionCode".
14///
15/// For more details see <https://stripe.com/docs/api/promotion_codes/object>
16#[derive(Clone, Debug, Default, Deserialize, Serialize)]
17pub struct PromotionCode {
18    /// Unique identifier for the object.
19    pub id: PromotionCodeId,
20
21    /// Whether the promotion code is currently active.
22    ///
23    /// A promotion code is only active if the coupon is also valid.
24    pub active: bool,
25
26    /// The customer-facing code.
27    ///
28    /// Regardless of case, this code must be unique across all active promotion codes for each customer.
29    pub code: String,
30
31    pub coupon: Coupon,
32
33    /// Time at which the object was created.
34    ///
35    /// Measured in seconds since the Unix epoch.
36    pub created: Timestamp,
37
38    /// The customer that this promotion code can be used by.
39    pub customer: Option<Expandable<Customer>>,
40
41    /// Date at which the promotion code can no longer be redeemed.
42    pub expires_at: Option<Timestamp>,
43
44    /// Has the value `true` if the object exists in live mode or the value `false` if the object exists in test mode.
45    pub livemode: bool,
46
47    /// Maximum number of times this promotion code can be redeemed.
48    pub max_redemptions: Option<i64>,
49
50    /// Set of [key-value pairs](https://stripe.com/docs/api/metadata) that you can attach to an object.
51    ///
52    /// This can be useful for storing additional information about the object in a structured format.
53    pub metadata: Option<Metadata>,
54
55    pub restrictions: PromotionCodesResourceRestrictions,
56
57    /// Number of times this promotion code has been used.
58    pub times_redeemed: i64,
59}
60
61impl PromotionCode {
62    /// Returns a list of your promotion codes.
63    pub fn list(client: &Client, params: &ListPromotionCodes<'_>) -> Response<List<PromotionCode>> {
64        client.get_query("/promotion_codes", params)
65    }
66
67    /// Retrieves the promotion code with the given ID.
68    ///
69    /// In order to retrieve a promotion code by the customer-facing `code` use [list](https://stripe.com/docs/api/promotion_codes/list) with the desired `code`.
70    pub fn retrieve(
71        client: &Client,
72        id: &PromotionCodeId,
73        expand: &[&str],
74    ) -> Response<PromotionCode> {
75        client.get_query(&format!("/promotion_codes/{}", id), Expand { expand })
76    }
77
78    /// Updates the specified promotion code by setting the values of the parameters passed.
79    ///
80    /// Most fields are, by design, not editable.
81    pub fn update(
82        client: &Client,
83        id: &PromotionCodeId,
84        params: UpdatePromotionCode<'_>,
85    ) -> Response<PromotionCode> {
86        #[allow(clippy::needless_borrows_for_generic_args)]
87        client.post_form(&format!("/promotion_codes/{}", id), &params)
88    }
89}
90
91impl Object for PromotionCode {
92    type Id = PromotionCodeId;
93    fn id(&self) -> Self::Id {
94        self.id.clone()
95    }
96    fn object(&self) -> &'static str {
97        "promotion_code"
98    }
99}
100
101#[derive(Clone, Debug, Default, Deserialize, Serialize)]
102pub struct PromotionCodesResourceRestrictions {
103    /// Promotion code restrictions defined in each available currency option.
104    ///
105    /// Each key must be a three-letter [ISO currency code](https://www.iso.org/iso-4217-currency-codes.html) and a [supported currency](https://stripe.com/docs/currencies).
106    #[serde(skip_serializing_if = "Option::is_none")]
107    pub currency_options: Option<CurrencyMap<PromotionCodeCurrencyOption>>,
108
109    /// A Boolean indicating if the Promotion Code should only be redeemed for Customers without any successful payments or invoices.
110    pub first_time_transaction: bool,
111
112    /// Minimum amount required to redeem this Promotion Code into a Coupon (e.g., a purchase must be $100 or more to work).
113    pub minimum_amount: Option<i64>,
114
115    /// Three-letter [ISO code](https://stripe.com/docs/currencies) for minimum_amount.
116    pub minimum_amount_currency: Option<Currency>,
117}
118
119#[derive(Clone, Debug, Default, Deserialize, Serialize)]
120pub struct PromotionCodeCurrencyOption {
121    /// Minimum amount required to redeem this Promotion Code into a Coupon (e.g., a purchase must be $100 or more to work).
122    pub minimum_amount: i64,
123}
124
125/// The parameters for `PromotionCode::list`.
126#[derive(Clone, Debug, Serialize, Default)]
127pub struct ListPromotionCodes<'a> {
128    /// Filter promotion codes by whether they are active.
129    #[serde(skip_serializing_if = "Option::is_none")]
130    pub active: Option<bool>,
131
132    /// Only return promotion codes that have this case-insensitive code.
133    #[serde(skip_serializing_if = "Option::is_none")]
134    pub code: Option<&'a str>,
135
136    /// Only return promotion codes for this coupon.
137    #[serde(skip_serializing_if = "Option::is_none")]
138    pub coupon: Option<CouponId>,
139
140    /// A filter on the list, based on the object `created` field.
141    ///
142    /// The value can be a string with an integer Unix timestamp, or it can be a dictionary with a number of different query options.
143    #[serde(skip_serializing_if = "Option::is_none")]
144    pub created: Option<RangeQuery<Timestamp>>,
145
146    /// Only return promotion codes that are restricted to this customer.
147    #[serde(skip_serializing_if = "Option::is_none")]
148    pub customer: Option<CustomerId>,
149
150    /// A cursor for use in pagination.
151    ///
152    /// `ending_before` is an object ID that defines your place in the list.
153    /// For instance, if you make a list request and receive 100 objects, starting with `obj_bar`, your subsequent call can include `ending_before=obj_bar` in order to fetch the previous page of the list.
154    #[serde(skip_serializing_if = "Option::is_none")]
155    pub ending_before: Option<PromotionCodeId>,
156
157    /// Specifies which fields in the response should be expanded.
158    #[serde(skip_serializing_if = "Expand::is_empty")]
159    pub expand: &'a [&'a str],
160
161    /// A limit on the number of objects to be returned.
162    ///
163    /// Limit can range between 1 and 100, and the default is 10.
164    #[serde(skip_serializing_if = "Option::is_none")]
165    pub limit: Option<u64>,
166
167    /// A cursor for use in pagination.
168    ///
169    /// `starting_after` is an object ID that defines your place in the list.
170    /// For instance, if you make a list request and receive 100 objects, ending with `obj_foo`, your subsequent call can include `starting_after=obj_foo` in order to fetch the next page of the list.
171    #[serde(skip_serializing_if = "Option::is_none")]
172    pub starting_after: Option<PromotionCodeId>,
173}
174
175impl<'a> ListPromotionCodes<'a> {
176    pub fn new() -> Self {
177        ListPromotionCodes {
178            active: Default::default(),
179            code: Default::default(),
180            coupon: Default::default(),
181            created: Default::default(),
182            customer: Default::default(),
183            ending_before: Default::default(),
184            expand: Default::default(),
185            limit: Default::default(),
186            starting_after: Default::default(),
187        }
188    }
189}
190impl Paginable for ListPromotionCodes<'_> {
191    type O = PromotionCode;
192    fn set_last(&mut self, item: Self::O) {
193        self.starting_after = Some(item.id());
194    }
195}
196/// The parameters for `PromotionCode::update`.
197#[derive(Clone, Debug, Serialize, Default)]
198pub struct UpdatePromotionCode<'a> {
199    /// Whether the promotion code is currently active.
200    ///
201    /// A promotion code can only be reactivated when the coupon is still valid and the promotion code is otherwise redeemable.
202    #[serde(skip_serializing_if = "Option::is_none")]
203    pub active: Option<bool>,
204
205    /// Specifies which fields in the response should be expanded.
206    #[serde(skip_serializing_if = "Expand::is_empty")]
207    pub expand: &'a [&'a str],
208
209    /// Set of [key-value pairs](https://stripe.com/docs/api/metadata) that you can attach to an object.
210    ///
211    /// This can be useful for storing additional information about the object in a structured format.
212    /// Individual keys can be unset by posting an empty value to them.
213    /// All keys can be unset by posting an empty value to `metadata`.
214    #[serde(skip_serializing_if = "Option::is_none")]
215    pub metadata: Option<Metadata>,
216
217    /// Settings that restrict the redemption of the promotion code.
218    #[serde(skip_serializing_if = "Option::is_none")]
219    pub restrictions: Option<UpdatePromotionCodeRestrictions>,
220}
221
222impl<'a> UpdatePromotionCode<'a> {
223    pub fn new() -> Self {
224        UpdatePromotionCode {
225            active: Default::default(),
226            expand: Default::default(),
227            metadata: Default::default(),
228            restrictions: Default::default(),
229        }
230    }
231}
232
233#[derive(Clone, Debug, Default, Deserialize, Serialize)]
234pub struct UpdatePromotionCodeRestrictions {
235    /// Promotion codes defined in each available currency option.
236    ///
237    /// Each key must be a three-letter [ISO currency code](https://www.iso.org/iso-4217-currency-codes.html) and a [supported currency](https://stripe.com/docs/currencies).
238    #[serde(skip_serializing_if = "Option::is_none")]
239    pub currency_options: Option<CurrencyMap<UpdatePromotionCodeRestrictionsCurrencyOptions>>,
240}
241
242#[derive(Clone, Debug, Default, Deserialize, Serialize)]
243pub struct UpdatePromotionCodeRestrictionsCurrencyOptions {
244    /// Minimum amount required to redeem this Promotion Code into a Coupon (e.g., a purchase must be $100 or more to work).
245    #[serde(skip_serializing_if = "Option::is_none")]
246    pub minimum_amount: Option<i64>,
247}