rust_tdlib/types/
invoice.rs

1use crate::errors::Result;
2use crate::types::*;
3use uuid::Uuid;
4
5/// Product invoice
6#[derive(Debug, Clone, Default, Serialize, Deserialize)]
7pub struct Invoice {
8    #[doc(hidden)]
9    #[serde(rename(serialize = "@extra", deserialize = "@extra"))]
10    extra: Option<String>,
11    #[serde(rename(serialize = "@client_id", deserialize = "@client_id"))]
12    client_id: Option<i32>,
13    /// ISO 4217 currency code
14
15    #[serde(default)]
16    currency: String,
17    /// A list of objects used to calculate the total price of the product
18
19    #[serde(default)]
20    price_parts: Vec<LabeledPricePart>,
21    /// The maximum allowed amount of tip in the smallest units of the currency
22
23    #[serde(default)]
24    max_tip_amount: i64,
25    /// Suggested amounts of tip in the smallest units of the currency
26
27    #[serde(default)]
28    suggested_tip_amounts: Vec<i64>,
29    /// True, if the payment is a test payment
30
31    #[serde(default)]
32    is_test: bool,
33    /// True, if the user's name is needed for payment
34
35    #[serde(default)]
36    need_name: bool,
37    /// True, if the user's phone number is needed for payment
38
39    #[serde(default)]
40    need_phone_number: bool,
41    /// True, if the user's email address is needed for payment
42
43    #[serde(default)]
44    need_email_address: bool,
45    /// True, if the user's shipping address is needed for payment
46
47    #[serde(default)]
48    need_shipping_address: bool,
49    /// True, if the user's phone number will be sent to the provider
50
51    #[serde(default)]
52    send_phone_number_to_provider: bool,
53    /// True, if the user's email address will be sent to the provider
54
55    #[serde(default)]
56    send_email_address_to_provider: bool,
57    /// True, if the total price depends on the shipping method
58
59    #[serde(default)]
60    is_flexible: bool,
61}
62
63impl RObject for Invoice {
64    #[doc(hidden)]
65    fn extra(&self) -> Option<&str> {
66        self.extra.as_deref()
67    }
68    #[doc(hidden)]
69    fn client_id(&self) -> Option<i32> {
70        self.client_id
71    }
72}
73
74impl Invoice {
75    pub fn from_json<S: AsRef<str>>(json: S) -> Result<Self> {
76        Ok(serde_json::from_str(json.as_ref())?)
77    }
78    pub fn builder() -> InvoiceBuilder {
79        let mut inner = Invoice::default();
80        inner.extra = Some(Uuid::new_v4().to_string());
81
82        InvoiceBuilder { inner }
83    }
84
85    pub fn currency(&self) -> &String {
86        &self.currency
87    }
88
89    pub fn price_parts(&self) -> &Vec<LabeledPricePart> {
90        &self.price_parts
91    }
92
93    pub fn max_tip_amount(&self) -> i64 {
94        self.max_tip_amount
95    }
96
97    pub fn suggested_tip_amounts(&self) -> &Vec<i64> {
98        &self.suggested_tip_amounts
99    }
100
101    pub fn is_test(&self) -> bool {
102        self.is_test
103    }
104
105    pub fn need_name(&self) -> bool {
106        self.need_name
107    }
108
109    pub fn need_phone_number(&self) -> bool {
110        self.need_phone_number
111    }
112
113    pub fn need_email_address(&self) -> bool {
114        self.need_email_address
115    }
116
117    pub fn need_shipping_address(&self) -> bool {
118        self.need_shipping_address
119    }
120
121    pub fn send_phone_number_to_provider(&self) -> bool {
122        self.send_phone_number_to_provider
123    }
124
125    pub fn send_email_address_to_provider(&self) -> bool {
126        self.send_email_address_to_provider
127    }
128
129    pub fn is_flexible(&self) -> bool {
130        self.is_flexible
131    }
132}
133
134#[doc(hidden)]
135pub struct InvoiceBuilder {
136    inner: Invoice,
137}
138
139#[deprecated]
140pub type RTDInvoiceBuilder = InvoiceBuilder;
141
142impl InvoiceBuilder {
143    pub fn build(&self) -> Invoice {
144        self.inner.clone()
145    }
146
147    pub fn currency<T: AsRef<str>>(&mut self, currency: T) -> &mut Self {
148        self.inner.currency = currency.as_ref().to_string();
149        self
150    }
151
152    pub fn price_parts(&mut self, price_parts: Vec<LabeledPricePart>) -> &mut Self {
153        self.inner.price_parts = price_parts;
154        self
155    }
156
157    pub fn max_tip_amount(&mut self, max_tip_amount: i64) -> &mut Self {
158        self.inner.max_tip_amount = max_tip_amount;
159        self
160    }
161
162    pub fn suggested_tip_amounts(&mut self, suggested_tip_amounts: Vec<i64>) -> &mut Self {
163        self.inner.suggested_tip_amounts = suggested_tip_amounts;
164        self
165    }
166
167    pub fn is_test(&mut self, is_test: bool) -> &mut Self {
168        self.inner.is_test = is_test;
169        self
170    }
171
172    pub fn need_name(&mut self, need_name: bool) -> &mut Self {
173        self.inner.need_name = need_name;
174        self
175    }
176
177    pub fn need_phone_number(&mut self, need_phone_number: bool) -> &mut Self {
178        self.inner.need_phone_number = need_phone_number;
179        self
180    }
181
182    pub fn need_email_address(&mut self, need_email_address: bool) -> &mut Self {
183        self.inner.need_email_address = need_email_address;
184        self
185    }
186
187    pub fn need_shipping_address(&mut self, need_shipping_address: bool) -> &mut Self {
188        self.inner.need_shipping_address = need_shipping_address;
189        self
190    }
191
192    pub fn send_phone_number_to_provider(
193        &mut self,
194        send_phone_number_to_provider: bool,
195    ) -> &mut Self {
196        self.inner.send_phone_number_to_provider = send_phone_number_to_provider;
197        self
198    }
199
200    pub fn send_email_address_to_provider(
201        &mut self,
202        send_email_address_to_provider: bool,
203    ) -> &mut Self {
204        self.inner.send_email_address_to_provider = send_email_address_to_provider;
205        self
206    }
207
208    pub fn is_flexible(&mut self, is_flexible: bool) -> &mut Self {
209        self.inner.is_flexible = is_flexible;
210        self
211    }
212}
213
214impl AsRef<Invoice> for Invoice {
215    fn as_ref(&self) -> &Invoice {
216        self
217    }
218}
219
220impl AsRef<Invoice> for InvoiceBuilder {
221    fn as_ref(&self) -> &Invoice {
222        &self.inner
223    }
224}