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
90                _ => <dyn Visitor>::ignore(),
91            })
92        }
93
94        fn deser_default() -> Self {
95            Self { id: Deserialize::default(), rates: Deserialize::default() }
96        }
97
98        fn take_out(&mut self) -> Option<Self::Out> {
99            let (Some(id), Some(rates)) = (self.id.take(), self.rates.take()) else {
100                return None;
101            };
102            Some(Self::Out { id, rates })
103        }
104    }
105
106    impl Map for Builder<'_> {
107        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
108            self.builder.key(k)
109        }
110
111        fn finish(&mut self) -> Result<()> {
112            *self.out = self.builder.take_out();
113            Ok(())
114        }
115    }
116
117    impl ObjectDeser for ExchangeRate {
118        type Builder = ExchangeRateBuilder;
119    }
120
121    impl FromValueOpt for ExchangeRate {
122        fn from_value(v: Value) -> Option<Self> {
123            let Value::Object(obj) = v else {
124                return None;
125            };
126            let mut b = ExchangeRateBuilder::deser_default();
127            for (k, v) in obj {
128                match k.as_str() {
129                    "id" => b.id = FromValueOpt::from_value(v),
130                    "rates" => b.rates = FromValueOpt::from_value(v),
131
132                    _ => {}
133                }
134            }
135            b.take_out()
136        }
137    }
138};
139#[cfg(feature = "serialize")]
140impl serde::Serialize for ExchangeRate {
141    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
142        use serde::ser::SerializeStruct;
143        let mut s = s.serialize_struct("ExchangeRate", 3)?;
144        s.serialize_field("id", &self.id)?;
145        s.serialize_field("rates", &self.rates)?;
146
147        s.serialize_field("object", "exchange_rate")?;
148        s.end()
149    }
150}
151impl stripe_types::Object for ExchangeRate {
152    type Id = stripe_misc::ExchangeRateId;
153    fn id(&self) -> &Self::Id {
154        &self.id
155    }
156
157    fn into_id(self) -> Self::Id {
158        self.id
159    }
160}
161stripe_types::def_id!(ExchangeRateId);