tgbot/types/definitions/payment/
checkout.rs

1use serde::{Deserialize, Serialize};
2
3use crate::{
4    api::{Method, Payload},
5    types::{Integer, OrderInfo, User},
6};
7
8/// Represents an incoming pre-checkout query.
9#[serde_with::skip_serializing_none]
10#[derive(Clone, Debug, Deserialize, PartialOrd, PartialEq, Serialize)]
11pub struct PreCheckoutQuery {
12    /// Three-letter ISO 4217 currency code.
13    pub currency: String,
14    /// User who sent the query.
15    pub from: User,
16    /// Unique query identifier.
17    pub id: String,
18    /// Bot specified invoice payload.
19    pub invoice_payload: String,
20    /// Total price in the smallest units of the currency (integer, not float/double).
21    ///
22    /// For example, for a price of US$ 1.45 pass amount = 145.
23    /// See the exp parameter in [currencies.json][1], it shows the number of digits past the
24    /// decimal point for each currency (2 for the majority of currencies).
25    ///
26    /// [1]: https://core.telegram.org/bots/payments/currencies.json
27    pub total_amount: Integer,
28    /// Order info provided by the user.
29    pub order_info: Option<OrderInfo>,
30    /// Identifier of the shipping option chosen by the user.
31    pub shipping_option_id: Option<String>,
32}
33
34impl PreCheckoutQuery {
35    /// Creates a new `PreCheckoutQuery`.
36    ///
37    /// # Arguments
38    ///
39    /// * `currency` - Three-letter ISO 4217 currency code.
40    /// * `from` - User who sent the query.
41    /// * `id` - Unique query identifier.
42    /// * `invoice_payload` - Bot specified invoice payload.
43    /// * `total_amount` - Total price in the smallest units of the currency.
44    pub fn new<A, B, C>(currency: A, from: User, id: B, invoice_payload: C, total_amount: Integer) -> Self
45    where
46        A: Into<String>,
47        B: Into<String>,
48        C: Into<String>,
49    {
50        Self {
51            currency: currency.into(),
52            from,
53            id: id.into(),
54            invoice_payload: invoice_payload.into(),
55            total_amount,
56            order_info: None,
57            shipping_option_id: None,
58        }
59    }
60
61    /// Sets a new order info.
62    ///
63    /// # Arguments
64    ///
65    /// * `value` - Order info.
66    pub fn with_order_info(mut self, value: OrderInfo) -> Self {
67        self.order_info = Some(value);
68        self
69    }
70    /// Sets a new shipping option ID.
71    ///
72    /// # Arguments
73    ///
74    /// * `value` - Shipping option ID.
75    pub fn with_shipping_option_id<T>(mut self, value: T) -> Self
76    where
77        T: Into<String>,
78    {
79        self.shipping_option_id = Some(value.into());
80        self
81    }
82}
83
84/// Responds to a pre-checkout query.
85///
86/// Once the user has confirmed their payment and shipping details,
87/// the Bot API sends the final confirmation in the form of an
88/// [`crate::types::UpdateType::PreCheckoutQuery`].
89///
90/// # Notes
91///
92/// The Bot API must receive an answer within 10 seconds after the pre-checkout query was sent.
93#[serde_with::skip_serializing_none]
94#[derive(Clone, Debug, Serialize)]
95pub struct AnswerPreCheckoutQuery {
96    ok: bool,
97    pre_checkout_query_id: String,
98    error_message: Option<String>,
99}
100
101impl AnswerPreCheckoutQuery {
102    /// Creates a new `AnswerPreCheckoutQuery` with a success answer.
103    ///
104    /// # Arguments
105    ///
106    /// * `id` - Unique identifier of the query to be answered.
107    pub fn ok<T>(id: T) -> Self
108    where
109        T: Into<String>,
110    {
111        Self {
112            pre_checkout_query_id: id.into(),
113            ok: true,
114            error_message: None,
115        }
116    }
117
118    /// Creates a new `AnswerPreCheckoutQuery` with an error answer.
119    ///
120    /// # Arguments
121    ///
122    /// * `id` - Unique identifier of the query to be answered.
123    /// * `message` - Error message in human readable form
124    ///   that explains the reason for failure to proceed with the checkout.
125    pub fn error<A, B>(id: A, message: B) -> Self
126    where
127        A: Into<String>,
128        B: Into<String>,
129    {
130        Self {
131            ok: false,
132            pre_checkout_query_id: id.into(),
133            error_message: Some(message.into()),
134        }
135    }
136}
137
138impl Method for AnswerPreCheckoutQuery {
139    type Response = bool;
140
141    fn into_payload(self) -> Payload {
142        Payload::json("answerPreCheckoutQuery", self)
143    }
144}