From 0fde9eb6bc3333bab3bb135709697d114017be4a Mon Sep 17 00:00:00 2001 From: Lev Khoroshansky Date: Mon, 18 Dec 2023 10:24:35 +0100 Subject: [PATCH 1/5] feat: Create promotion code --- src/resources.rs | 1 + src/resources/promotion_code_ext.rs | 110 ++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 src/resources/promotion_code_ext.rs diff --git a/src/resources.rs b/src/resources.rs index 3f38188af..04fc9fe23 100644 --- a/src/resources.rs +++ b/src/resources.rs @@ -62,6 +62,7 @@ mod products { #[cfg(feature = "checkout")] mod checkout { pub mod checkout_session_ext; + pub mod promotion_code_ext; } #[path = "resources"] diff --git a/src/resources/promotion_code_ext.rs b/src/resources/promotion_code_ext.rs new file mode 100644 index 000000000..98cfaa893 --- /dev/null +++ b/src/resources/promotion_code_ext.rs @@ -0,0 +1,110 @@ +use crate::client::{Client, Response}; +use crate::resources::{Currency, PromotionCode}; + +#[derive(Copy, Clone, Debug, serde::Serialize)] +pub struct CreatePromotionCode<'a> { + /// Whether the promotion code is currently active. + #[serde(skip_serializing_if = "Option::is_none")] + pub active: Option, + /// The customer-facing code. + /// + /// Regardless of case, this code must be unique across all active promotion codes for a specific customer. + /// If left blank, we will generate one automatically. + #[serde(skip_serializing_if = "Option::is_none")] + pub code: Option<&'a str>, + /// The coupon for this promotion code. + pub coupon: &'a str, + /// The customer that this promotion code can be used by. + /// + /// If not set, the promotion code can be used by all customers. + #[serde(skip_serializing_if = "Option::is_none")] + pub customer: Option<&'a str>, + /// Specifies which fields in the response should be expanded. + #[serde(skip_serializing_if = "Option::is_none")] + pub expand: Option<&'a [&'a str]>, + /// The timestamp at which this promotion code will expire. + /// + /// If the coupon has specified a `redeems_by`, then this value cannot be after the coupon's `redeems_by`. + #[serde(skip_serializing_if = "Option::is_none")] + pub expires_at: Option, + /// A positive integer specifying the number of times the promotion code can be redeemed. + /// + /// If the coupon has specified a `max_redemptions`, then this value cannot be greater than the coupon's `max_redemptions`. + #[serde(skip_serializing_if = "Option::is_none")] + pub max_redemptions: Option, + /// Set of [key-value pairs](https://stripe.com/docs/api/metadata) that you can attach to an object. + /// + /// This can be useful for storing additional information about the object in a structured format. + /// Individual keys can be unset by posting an empty value to them. + /// All keys can be unset by posting an empty value to `metadata`. + #[serde(skip_serializing_if = "Option::is_none")] + pub metadata: Option<&'a std::collections::HashMap>, + /// Settings that restrict the redemption of the promotion code. + #[serde(skip_serializing_if = "Option::is_none")] + pub restrictions: Option>, +} +impl<'a> CreatePromotionCode<'a> { + pub fn new(coupon: &'a str) -> Self { + Self { + active: None, + code: None, + coupon, + customer: None, + expand: None, + expires_at: None, + max_redemptions: None, + metadata: None, + restrictions: None, + } + } +} +/// Settings that restrict the redemption of the promotion code. +#[derive(Copy, Clone, Debug, Default, serde::Serialize)] +pub struct CreatePromotionCodeRestrictions<'a> { + /// Promotion codes defined in each available currency option. + /// + /// 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). + #[serde(skip_serializing_if = "Option::is_none")] + pub currency_options: Option< + &'a std::collections::HashMap< + Currency, + CreatePromotionCodeRestrictionsCurrencyOptions, + >, + >, + /// A Boolean indicating if the Promotion Code should only be redeemed for Customers without any successful payments or invoices. + #[serde(skip_serializing_if = "Option::is_none")] + pub first_time_transaction: Option, + /// Minimum amount required to redeem this Promotion Code into a Coupon (e.g., a purchase must be $100 or more to work). + #[serde(skip_serializing_if = "Option::is_none")] + pub minimum_amount: Option, + /// Three-letter [ISO code](https://stripe.com/docs/currencies) for minimum_amount. + #[serde(skip_serializing_if = "Option::is_none")] + pub minimum_amount_currency: Option, +} +impl<'a> CreatePromotionCodeRestrictions<'a> { + pub fn new() -> Self { + Self::default() + } +} +/// Promotion codes defined in each available currency option. +/// +/// 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). +#[derive(Copy, Clone, Debug, Default, serde::Serialize)] +pub struct CreatePromotionCodeRestrictionsCurrencyOptions { + /// Minimum amount required to redeem this Promotion Code into a Coupon (e.g., a purchase must be $100 or more to work). + #[serde(skip_serializing_if = "Option::is_none")] + pub minimum_amount: Option, +} +impl CreatePromotionCodeRestrictionsCurrencyOptions { + pub fn new() -> Self { + Self::default() + } +} +impl<'a> CreatePromotionCode<'a> { + /// A promotion code points to a coupon. + /// + /// You can optionally restrict the code to a specific customer, redemption limit, and expiration date. + pub fn send(&self, client: &Client) -> Response { + client.post("/promotion_codes", self) + } +} From bc3926039eb50d48cf8f20ddb8110c847e7f7f00 Mon Sep 17 00:00:00 2001 From: Lev Khoroshansky Date: Mon, 18 Dec 2023 10:26:15 +0100 Subject: [PATCH 2/5] fix: `post` -> `post_form` --- src/resources/promotion_code_ext.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/resources/promotion_code_ext.rs b/src/resources/promotion_code_ext.rs index 98cfaa893..ce3d0cc74 100644 --- a/src/resources/promotion_code_ext.rs +++ b/src/resources/promotion_code_ext.rs @@ -105,6 +105,6 @@ impl<'a> CreatePromotionCode<'a> { /// /// You can optionally restrict the code to a specific customer, redemption limit, and expiration date. pub fn send(&self, client: &Client) -> Response { - client.post("/promotion_codes", self) + client.post_form("/promotion_codes", self) } } From 24f17823b1812886bda32762f9de66dceb83559b Mon Sep 17 00:00:00 2001 From: Lev Khoroshansky Date: Mon, 18 Dec 2023 10:35:43 +0100 Subject: [PATCH 3/5] fix: Re-export --- src/resources.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/resources.rs b/src/resources.rs index 04fc9fe23..f75691776 100644 --- a/src/resources.rs +++ b/src/resources.rs @@ -187,6 +187,8 @@ pub use { #[rustfmt::skip] #[cfg(feature = "checkout")] pub use { + checkout::checkout_session_ext::*, + checkout::promotion_code_ext::*, generated::checkout::{ checkout_session::*, payment_link::*, From 3b4cde6968b0a484679965910ba3b0e214106a28 Mon Sep 17 00:00:00 2001 From: Lev Khoroshansky Date: Sun, 11 Aug 2024 23:47:30 +0200 Subject: [PATCH 4/5] style: Format --- src/resources/promotion_code_ext.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/resources/promotion_code_ext.rs b/src/resources/promotion_code_ext.rs index ce3d0cc74..858c3fd80 100644 --- a/src/resources/promotion_code_ext.rs +++ b/src/resources/promotion_code_ext.rs @@ -66,10 +66,7 @@ pub struct CreatePromotionCodeRestrictions<'a> { /// 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). #[serde(skip_serializing_if = "Option::is_none")] pub currency_options: Option< - &'a std::collections::HashMap< - Currency, - CreatePromotionCodeRestrictionsCurrencyOptions, - >, + &'a std::collections::HashMap, >, /// A Boolean indicating if the Promotion Code should only be redeemed for Customers without any successful payments or invoices. #[serde(skip_serializing_if = "Option::is_none")] From 32435f38c242ad47d8c992d1178736c460f107f2 Mon Sep 17 00:00:00 2001 From: Lev Khoroshansky Date: Tue, 13 Aug 2024 22:57:53 +0200 Subject: [PATCH 5/5] fix: Move `promotion_code_ext` to `products` feature --- src/resources.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/resources.rs b/src/resources.rs index f75691776..d8bcd3830 100644 --- a/src/resources.rs +++ b/src/resources.rs @@ -56,13 +56,13 @@ mod billing { mod products { pub mod price_ext; pub mod product_ext; + pub mod promotion_code_ext; } #[path = "resources"] #[cfg(feature = "checkout")] mod checkout { pub mod checkout_session_ext; - pub mod promotion_code_ext; } #[path = "resources"] @@ -187,8 +187,6 @@ pub use { #[rustfmt::skip] #[cfg(feature = "checkout")] pub use { - checkout::checkout_session_ext::*, - checkout::promotion_code_ext::*, generated::checkout::{ checkout_session::*, payment_link::*, @@ -202,6 +200,7 @@ pub use { products::{ product_ext::*, price_ext::*, + promotion_code_ext::*, } };