mercadopago_sdk_rust/
common_types.rs

1use serde::{Deserialize, Serialize};
2
3use crate::errors::ValidationError;
4use crate::helpers::option_stringify;
5use crate::payments::requests::DocumentType;
6use crate::SDKError;
7
8/// ID of the currency used in the payment accepted by MercadoPago
9/// ARS: Argentine peso.
10/// BRL: Brazilian real.
11/// CLP: Chilean peso.
12/// MXN: Mexican peso.
13/// COP: Colombian peso.
14/// PEN: Peruvian sol.
15/// UYU: Uruguayan peso.
16#[derive(
17    Copy, Clone, Deserialize, Serialize, PartialEq, Debug, strum::IntoStaticStr, strum::AsRefStr,
18)]
19pub enum CurrencyId {
20    ARS,
21    BRL,
22    CLP,
23    MXN,
24    COP,
25    PEN,
26    UYU,
27}
28
29#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
30pub struct Phone {
31    #[serde(
32        default,
33        deserialize_with = "serde_aux::field_attributes::deserialize_option_number_from_string"
34    )]
35    pub area_code: Option<i64>,
36    pub number: Option<String>,
37}
38
39/// Redirection URLs after customer pays.
40///
41/// Used by the Checkout and CheckoutPro.
42#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
43pub struct BackUrls {
44    pub success: Option<String>,
45    pub failure: Option<String>,
46    pub pending: Option<String>,
47}
48
49#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
50pub struct PaymentMethods {
51    /// Method that excludes specific credit and debit card brands, such as Visa, Mastercard,
52    /// American Express, among others. Note that these are DEBIT/CREDIT CARD brands.
53    pub excluded_payment_methods: Vec<ExcludedPaymentMethod>,
54
55    /// Method that excludes undesired payment methods for your operation, such as credit card,
56    /// ticket, among others. Note that these are PAYMENT TYPES.
57    pub excluded_payment_types: Vec<ExcludedPaymentType>,
58
59    /// Method that defines the maximum number of installments to be offered.
60    #[serde(skip_serializing_if = "Option::is_none")]
61    pub installments: Option<i64>,
62}
63
64#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
65pub struct ExcludedPaymentMethod {
66    pub id: Option<String>,
67}
68
69#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
70pub struct ExcludedPaymentType {
71    pub id: Option<String>,
72}
73
74/// An item processed by MercadoPago.
75#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
76pub struct Item {
77    pub title: String,
78    pub description: String,
79    pub quantity: i32,
80    pub unit_price: f64,
81
82    /// Item ID/sku, identified in your platform.
83    pub id: Option<String>,
84    pub currency_id: Option<CurrencyId>,
85    pub picture_url: Option<String>,
86    pub category_id: Option<String>,
87}
88
89impl Item {
90    pub fn minimal_item(
91        name: String,
92        description: String,
93        price: f64,
94        quantity: i32,
95    ) -> Result<Item, SDKError> {
96        if quantity < 1 {
97            return Err(ValidationError::ItemError(
98                "You can't have zero of something.".to_string(),
99            )
100            .into());
101        }
102
103        Ok(Self {
104            title: name,
105            description,
106            quantity,
107            unit_price: price,
108
109            id: None,
110            currency_id: None,
111            picture_url: None,
112            category_id: None,
113        })
114    }
115}
116
117#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
118pub struct Shipments {
119    pub receiver_address: Option<Address>,
120}
121
122#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
123pub struct Address {
124    pub zip_code: Option<String>,
125    pub state_name: Option<String>,
126    pub city_name: Option<String>,
127    pub street_name: Option<String>,
128    pub street_number: Option<i64>,
129}
130
131/// A payer will ALWAYS have a `PersonalIdentification`, and an `email` since it's the bare minimum.
132#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
133pub struct CheckoutProPayer {
134    pub(crate) email: Option<String>,
135    pub identification: PersonalIdentification,
136
137    pub name: Option<String>,
138    pub surname: Option<String>,
139    pub phone: Option<Phone>,
140    pub address: Option<Address>,
141}
142
143impl CheckoutProPayer {
144    pub fn validate(&self) -> bool {
145        if self.email.is_none()
146            || self.identification.number.is_none()
147            || self.identification.document_type.is_none()
148        {
149            return false;
150        }
151        true
152    }
153
154    pub fn standard_payer<II>(
155        email: String,
156        document_type: DocumentType,
157        document_number: II,
158    ) -> Self
159    where
160        II: Into<Option<i64>>,
161    {
162        Self {
163            email: Some(email),
164            identification: PersonalIdentification {
165                document_type: Some(document_type),
166                number: document_number.into(),
167            },
168
169            name: None,
170            surname: None,
171            phone: None,
172            address: None,
173        }
174    }
175
176    pub fn minimal_payer<II>(
177        email: String,
178        document_type: DocumentType,
179        document_number: II,
180    ) -> Self
181    where
182        II: Into<Option<i64>>,
183    {
184        Self {
185            email: Some(email),
186            identification: PersonalIdentification {
187                document_type: Some(document_type),
188                number: document_number.into(),
189            },
190
191            name: None,
192            surname: None,
193            phone: None,
194            address: None,
195        }
196    }
197}
198
199/// Documents for personal identification, such as RG, CPF, CNH
200#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
201pub struct PersonalIdentification {
202    #[serde(rename = "type")]
203    pub document_type: Option<DocumentType>,
204    #[serde(
205        default,
206        serialize_with = "option_stringify",
207        deserialize_with = "serde_aux::field_attributes::deserialize_option_number_from_string"
208    )]
209    pub number: Option<i64>,
210}
211
212impl PersonalIdentification {
213    pub fn new(document_type: DocumentType, document_number: i64) -> Self {
214        Self {
215            document_type: Some(document_type),
216            number: Some(document_number),
217        }
218    }
219}
220
221#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
222pub struct Card {
223    pub first_six_digits: String,
224    pub last_four_digits: String,
225    pub expiration_month: i64,
226    pub expiration_year: i64,
227
228    pub card_number_length: i64,
229    pub security_code_length: i64,
230
231    pub cardholder: Cardholder,
232
233    #[serde(with = "time::serde::rfc3339")]
234    pub date_created: time::OffsetDateTime,
235    #[serde(with = "time::serde::rfc3339")]
236    pub date_last_updated: time::OffsetDateTime,
237    #[serde(with = "time::serde::rfc3339")]
238    pub date_due: time::OffsetDateTime,
239}
240
241/// Information of the Credit/Debit Card owner.
242#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
243pub struct Cardholder {
244    pub name: String,
245    pub identification: PersonalIdentification,
246}