stripe_shared/
charge_outcome.rs

1#[derive(Clone, Debug)]
2#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
3#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
4pub struct ChargeOutcome {
5    /// An enumerated value providing a more detailed explanation on [how to proceed with an error](https://stripe.com/docs/declines#retrying-issuer-declines).
6    pub advice_code: Option<ChargeOutcomeAdviceCode>,
7    /// For charges declined by the network, a 2 digit code which indicates the advice returned by the network on how to proceed with an error.
8    pub network_advice_code: Option<String>,
9    /// For charges declined by the network, an alphanumeric code which indicates the reason the charge failed.
10    pub network_decline_code: Option<String>,
11    /// Possible values are `approved_by_network`, `declined_by_network`, `not_sent_to_network`, and `reversed_after_approval`.
12    /// The value `reversed_after_approval` indicates the payment was [blocked by Stripe](https://stripe.com/docs/declines#blocked-payments) after bank authorization, and may temporarily appear as "pending" on a cardholder's statement.
13    pub network_status: Option<String>,
14    /// An enumerated value providing a more detailed explanation of the outcome's `type`.
15    /// Charges blocked by Radar's default block rule have the value `highest_risk_level`.
16    /// Charges placed in review by Radar's default review rule have the value `elevated_risk_level`.
17    /// Charges blocked because the payment is unlikely to be authorized have the value `low_probability_of_authorization`.
18    /// Charges authorized, blocked, or placed in review by custom rules have the value `rule`.
19    /// See [understanding declines](https://stripe.com/docs/declines) for more details.
20    pub reason: Option<String>,
21    /// Stripe Radar's evaluation of the riskiness of the payment.
22    /// Possible values for evaluated payments are `normal`, `elevated`, `highest`.
23    /// For non-card payments, and card-based payments predating the public assignment of risk levels, this field will have the value `not_assessed`.
24    /// In the event of an error in the evaluation, this field will have the value `unknown`.
25    /// This field is only available with Radar.
26    pub risk_level: Option<String>,
27    /// Stripe Radar's evaluation of the riskiness of the payment.
28    /// Possible values for evaluated payments are between 0 and 100.
29    /// For non-card payments, card-based payments predating the public assignment of risk scores, or in the event of an error during evaluation, this field will not be present.
30    /// This field is only available with Radar for Fraud Teams.
31    pub risk_score: Option<i64>,
32    /// The ID of the Radar rule that matched the payment, if applicable.
33    pub rule: Option<stripe_types::Expandable<stripe_shared::Rule>>,
34    /// A human-readable description of the outcome type and reason, designed for you (the recipient of the payment), not your customer.
35    pub seller_message: Option<String>,
36    /// Possible values are `authorized`, `manual_review`, `issuer_declined`, `blocked`, and `invalid`.
37    /// See [understanding declines](https://stripe.com/docs/declines) and [Radar reviews](https://stripe.com/docs/radar/reviews) for details.
38    #[cfg_attr(any(feature = "deserialize", feature = "serialize"), serde(rename = "type"))]
39    pub type_: String,
40}
41#[doc(hidden)]
42pub struct ChargeOutcomeBuilder {
43    advice_code: Option<Option<ChargeOutcomeAdviceCode>>,
44    network_advice_code: Option<Option<String>>,
45    network_decline_code: Option<Option<String>>,
46    network_status: Option<Option<String>>,
47    reason: Option<Option<String>>,
48    risk_level: Option<Option<String>>,
49    risk_score: Option<Option<i64>>,
50    rule: Option<Option<stripe_types::Expandable<stripe_shared::Rule>>>,
51    seller_message: Option<Option<String>>,
52    type_: Option<String>,
53}
54
55#[allow(
56    unused_variables,
57    irrefutable_let_patterns,
58    clippy::let_unit_value,
59    clippy::match_single_binding,
60    clippy::single_match
61)]
62const _: () = {
63    use miniserde::de::{Map, Visitor};
64    use miniserde::json::Value;
65    use miniserde::{make_place, Deserialize, Result};
66    use stripe_types::miniserde_helpers::FromValueOpt;
67    use stripe_types::{MapBuilder, ObjectDeser};
68
69    make_place!(Place);
70
71    impl Deserialize for ChargeOutcome {
72        fn begin(out: &mut Option<Self>) -> &mut dyn Visitor {
73            Place::new(out)
74        }
75    }
76
77    struct Builder<'a> {
78        out: &'a mut Option<ChargeOutcome>,
79        builder: ChargeOutcomeBuilder,
80    }
81
82    impl Visitor for Place<ChargeOutcome> {
83        fn map(&mut self) -> Result<Box<dyn Map + '_>> {
84            Ok(Box::new(Builder {
85                out: &mut self.out,
86                builder: ChargeOutcomeBuilder::deser_default(),
87            }))
88        }
89    }
90
91    impl MapBuilder for ChargeOutcomeBuilder {
92        type Out = ChargeOutcome;
93        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
94            Ok(match k {
95                "advice_code" => Deserialize::begin(&mut self.advice_code),
96                "network_advice_code" => Deserialize::begin(&mut self.network_advice_code),
97                "network_decline_code" => Deserialize::begin(&mut self.network_decline_code),
98                "network_status" => Deserialize::begin(&mut self.network_status),
99                "reason" => Deserialize::begin(&mut self.reason),
100                "risk_level" => Deserialize::begin(&mut self.risk_level),
101                "risk_score" => Deserialize::begin(&mut self.risk_score),
102                "rule" => Deserialize::begin(&mut self.rule),
103                "seller_message" => Deserialize::begin(&mut self.seller_message),
104                "type" => Deserialize::begin(&mut self.type_),
105
106                _ => <dyn Visitor>::ignore(),
107            })
108        }
109
110        fn deser_default() -> Self {
111            Self {
112                advice_code: Deserialize::default(),
113                network_advice_code: Deserialize::default(),
114                network_decline_code: Deserialize::default(),
115                network_status: Deserialize::default(),
116                reason: Deserialize::default(),
117                risk_level: Deserialize::default(),
118                risk_score: Deserialize::default(),
119                rule: Deserialize::default(),
120                seller_message: Deserialize::default(),
121                type_: Deserialize::default(),
122            }
123        }
124
125        fn take_out(&mut self) -> Option<Self::Out> {
126            let (
127                Some(advice_code),
128                Some(network_advice_code),
129                Some(network_decline_code),
130                Some(network_status),
131                Some(reason),
132                Some(risk_level),
133                Some(risk_score),
134                Some(rule),
135                Some(seller_message),
136                Some(type_),
137            ) = (
138                self.advice_code,
139                self.network_advice_code.take(),
140                self.network_decline_code.take(),
141                self.network_status.take(),
142                self.reason.take(),
143                self.risk_level.take(),
144                self.risk_score,
145                self.rule.take(),
146                self.seller_message.take(),
147                self.type_.take(),
148            )
149            else {
150                return None;
151            };
152            Some(Self::Out {
153                advice_code,
154                network_advice_code,
155                network_decline_code,
156                network_status,
157                reason,
158                risk_level,
159                risk_score,
160                rule,
161                seller_message,
162                type_,
163            })
164        }
165    }
166
167    impl Map for Builder<'_> {
168        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
169            self.builder.key(k)
170        }
171
172        fn finish(&mut self) -> Result<()> {
173            *self.out = self.builder.take_out();
174            Ok(())
175        }
176    }
177
178    impl ObjectDeser for ChargeOutcome {
179        type Builder = ChargeOutcomeBuilder;
180    }
181
182    impl FromValueOpt for ChargeOutcome {
183        fn from_value(v: Value) -> Option<Self> {
184            let Value::Object(obj) = v else {
185                return None;
186            };
187            let mut b = ChargeOutcomeBuilder::deser_default();
188            for (k, v) in obj {
189                match k.as_str() {
190                    "advice_code" => b.advice_code = FromValueOpt::from_value(v),
191                    "network_advice_code" => b.network_advice_code = FromValueOpt::from_value(v),
192                    "network_decline_code" => b.network_decline_code = FromValueOpt::from_value(v),
193                    "network_status" => b.network_status = FromValueOpt::from_value(v),
194                    "reason" => b.reason = FromValueOpt::from_value(v),
195                    "risk_level" => b.risk_level = FromValueOpt::from_value(v),
196                    "risk_score" => b.risk_score = FromValueOpt::from_value(v),
197                    "rule" => b.rule = FromValueOpt::from_value(v),
198                    "seller_message" => b.seller_message = FromValueOpt::from_value(v),
199                    "type" => b.type_ = FromValueOpt::from_value(v),
200
201                    _ => {}
202                }
203            }
204            b.take_out()
205        }
206    }
207};
208/// An enumerated value providing a more detailed explanation on [how to proceed with an error](https://stripe.com/docs/declines#retrying-issuer-declines).
209#[derive(Copy, Clone, Eq, PartialEq)]
210pub enum ChargeOutcomeAdviceCode {
211    ConfirmCardData,
212    DoNotTryAgain,
213    TryAgainLater,
214}
215impl ChargeOutcomeAdviceCode {
216    pub fn as_str(self) -> &'static str {
217        use ChargeOutcomeAdviceCode::*;
218        match self {
219            ConfirmCardData => "confirm_card_data",
220            DoNotTryAgain => "do_not_try_again",
221            TryAgainLater => "try_again_later",
222        }
223    }
224}
225
226impl std::str::FromStr for ChargeOutcomeAdviceCode {
227    type Err = stripe_types::StripeParseError;
228    fn from_str(s: &str) -> Result<Self, Self::Err> {
229        use ChargeOutcomeAdviceCode::*;
230        match s {
231            "confirm_card_data" => Ok(ConfirmCardData),
232            "do_not_try_again" => Ok(DoNotTryAgain),
233            "try_again_later" => Ok(TryAgainLater),
234            _ => Err(stripe_types::StripeParseError),
235        }
236    }
237}
238impl std::fmt::Display for ChargeOutcomeAdviceCode {
239    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
240        f.write_str(self.as_str())
241    }
242}
243
244impl std::fmt::Debug for ChargeOutcomeAdviceCode {
245    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
246        f.write_str(self.as_str())
247    }
248}
249#[cfg(feature = "serialize")]
250impl serde::Serialize for ChargeOutcomeAdviceCode {
251    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
252    where
253        S: serde::Serializer,
254    {
255        serializer.serialize_str(self.as_str())
256    }
257}
258impl miniserde::Deserialize for ChargeOutcomeAdviceCode {
259    fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
260        crate::Place::new(out)
261    }
262}
263
264impl miniserde::de::Visitor for crate::Place<ChargeOutcomeAdviceCode> {
265    fn string(&mut self, s: &str) -> miniserde::Result<()> {
266        use std::str::FromStr;
267        self.out = Some(ChargeOutcomeAdviceCode::from_str(s).map_err(|_| miniserde::Error)?);
268        Ok(())
269    }
270}
271
272stripe_types::impl_from_val_with_from_str!(ChargeOutcomeAdviceCode);
273#[cfg(feature = "deserialize")]
274impl<'de> serde::Deserialize<'de> for ChargeOutcomeAdviceCode {
275    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
276        use std::str::FromStr;
277        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
278        Self::from_str(&s)
279            .map_err(|_| serde::de::Error::custom("Unknown value for ChargeOutcomeAdviceCode"))
280    }
281}