stripe_misc/exchange_rate/
types.rs

1/// \[Deprecated\] The `ExchangeRate` APIs are deprecated.
2/// Please use the [FX Quotes API](https://docs.stripe.com/payments/currencies/localize-prices/fx-quotes-api) instead.
3///
4/// `ExchangeRate` objects allow you to determine the rates that Stripe is currently
5/// using to convert from one currency to another. Since this number is variable
6/// throughout the day, there are various reasons why you might want to know the current
7/// rate (for example, to dynamically price an item for a user with a default
8/// payment in a foreign currency).
9///
10/// Please refer to our [Exchange Rates API](https://stripe.com/docs/fx-rates) guide for more details.
11///
12/// *[Note: this integration path is supported but no longer recommended]* Additionally,
13/// you can guarantee that a charge is made with an exchange rate that you expect is
14/// current. To do so, you must pass in the exchange_rate to charges endpoints. If the
15/// value is no longer up to date, the charge won't go through. Please refer to our
16/// [Using with charges](https://stripe.com/docs/exchange-rates) guide for more details.
17///
18/// -----
19///
20///  
21///
22/// *This Exchange Rates API is a Beta Service and is subject to Stripe's terms of service.
23/// You may use the API solely for the purpose of transacting on Stripe.
24/// For example, the API may be queried in order to:*.
25///
26/// - *localize prices for processing payments on Stripe*
27/// - *reconcile Stripe transactions*
28/// - *determine how much money to send to a connected account*
29/// - *determine app fees to charge a connected account*
30///
31/// *Using this Exchange Rates API beta for any purpose other than to transact on Stripe is strictly prohibited and constitutes a violation of Stripe's terms of service.*.
32#[derive(Clone, Debug)]
33#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
34pub struct ExchangeRate {
35    /// Unique identifier for the object.
36    /// Represented as the three-letter [ISO currency code](https://www.iso.org/iso-4217-currency-codes.html) in lowercase.
37    pub id: stripe_misc::ExchangeRateId,
38    /// Hash where the keys are supported currencies and the values are the exchange rate at which the base id currency converts to the key currency.
39    pub rates: std::collections::HashMap<String, f64>,
40}
41#[doc(hidden)]
42pub struct ExchangeRateBuilder {
43    id: Option<stripe_misc::ExchangeRateId>,
44    rates: Option<std::collections::HashMap<String, f64>>,
45}
46
47#[allow(
48    unused_variables,
49    irrefutable_let_patterns,
50    clippy::let_unit_value,
51    clippy::match_single_binding,
52    clippy::single_match
53)]
54const _: () = {
55    use miniserde::de::{Map, Visitor};
56    use miniserde::json::Value;
57    use miniserde::{Deserialize, Result, make_place};
58    use stripe_types::miniserde_helpers::FromValueOpt;
59    use stripe_types::{MapBuilder, ObjectDeser};
60
61    make_place!(Place);
62
63    impl Deserialize for ExchangeRate {
64        fn begin(out: &mut Option<Self>) -> &mut dyn Visitor {
65            Place::new(out)
66        }
67    }
68
69    struct Builder<'a> {
70        out: &'a mut Option<ExchangeRate>,
71        builder: ExchangeRateBuilder,
72    }
73
74    impl Visitor for Place<ExchangeRate> {
75        fn map(&mut self) -> Result<Box<dyn Map + '_>> {
76            Ok(Box::new(Builder {
77                out: &mut self.out,
78                builder: ExchangeRateBuilder::deser_default(),
79            }))
80        }
81    }
82
83    impl MapBuilder for ExchangeRateBuilder {
84        type Out = ExchangeRate;
85        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
86            Ok(match k {
87                "id" => Deserialize::begin(&mut self.id),
88                "rates" => Deserialize::begin(&mut self.rates),
89                _ => <dyn Visitor>::ignore(),
90            })
91        }
92
93        fn deser_default() -> Self {
94            Self { id: Deserialize::default(), rates: Deserialize::default() }
95        }
96
97        fn take_out(&mut self) -> Option<Self::Out> {
98            let (Some(id), Some(rates)) = (self.id.take(), self.rates.take()) else {
99                return None;
100            };
101            Some(Self::Out { id, rates })
102        }
103    }
104
105    impl Map for Builder<'_> {
106        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
107            self.builder.key(k)
108        }
109
110        fn finish(&mut self) -> Result<()> {
111            *self.out = self.builder.take_out();
112            Ok(())
113        }
114    }
115
116    impl ObjectDeser for ExchangeRate {
117        type Builder = ExchangeRateBuilder;
118    }
119
120    impl FromValueOpt for ExchangeRate {
121        fn from_value(v: Value) -> Option<Self> {
122            let Value::Object(obj) = v else {
123                return None;
124            };
125            let mut b = ExchangeRateBuilder::deser_default();
126            for (k, v) in obj {
127                match k.as_str() {
128                    "id" => b.id = FromValueOpt::from_value(v),
129                    "rates" => b.rates = FromValueOpt::from_value(v),
130                    _ => {}
131                }
132            }
133            b.take_out()
134        }
135    }
136};
137#[cfg(feature = "serialize")]
138impl serde::Serialize for ExchangeRate {
139    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
140        use serde::ser::SerializeStruct;
141        let mut s = s.serialize_struct("ExchangeRate", 3)?;
142        s.serialize_field("id", &self.id)?;
143        s.serialize_field("rates", &self.rates)?;
144
145        s.serialize_field("object", "exchange_rate")?;
146        s.end()
147    }
148}
149impl stripe_types::Object for ExchangeRate {
150    type Id = stripe_misc::ExchangeRateId;
151    fn id(&self) -> &Self::Id {
152        &self.id
153    }
154
155    fn into_id(self) -> Self::Id {
156        self.id
157    }
158}
159stripe_types::def_id!(ExchangeRateId);