1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use crate::{
    methods::AnswerPreCheckoutQuery,
    types::{pre_checkout_query, OrderInfo, PreCheckoutQuery, User},
    Bot,
};

common! {
    /// The context for [`pre_checkout`] handlers.
    ///
    /// [`pre_checkout`]: crate::EventLoop::pre_checkout
    struct PreCheckout {
        /// The ID of the query.
        id: pre_checkout_query::Id,
        /// The user who sent the query.
        from: User,
        /// The currency of of the invoice.
        currency: String,
        /// The total price.
        total_amount: u32,
        /// The invoice payload sent previously by the bot.
        invoice_payload: String,
        /// The ID of the chosen shipping option.
        shipping_option_id: Option<String>,
        /// The order information.
        order_info: Option<OrderInfo>,
    }
}

impl PreCheckout {
    #[allow(clippy::missing_const_for_fn)]
    pub(crate) fn new(bot: Bot, query: PreCheckoutQuery) -> Self {
        Self {
            bot,
            id: query.id,
            from: query.from,
            currency: query.currency,
            total_amount: query.total_amount,
            invoice_payload: query.invoice_payload,
            shipping_option_id: query.shipping_option_id,
            order_info: query.order_info,
        }
    }

    /// Reports if the checkout is possible.
    ///
    /// Note that this method suits better when you already deal with
    /// an `Option`. You might also want to use the [`ok`] and [`err`]
    /// methods from this context.
    ///
    /// [`ok`]: Self::ok
    /// [`err`]: Self::err
    pub fn answer(
        &self,
        result: Result<(), impl Into<String>>,
    ) -> AnswerPreCheckoutQuery<'_> {
        self.bot.answer_pre_checkout_query(self.id.clone(), result)
    }

    /// Reports that shipping is possible and shows possible shipping options.
    pub fn ok(&self) -> AnswerPreCheckoutQuery<'_> {
        let answer: Result<(), String> = Ok(());
        self.answer(answer)
    }

    /// Reports that shipping is impossible and shows the error message.
    pub fn err(&self, err: impl Into<String>) -> AnswerPreCheckoutQuery<'_> {
        self.answer(Err(err))
    }
}