Skip to main content

rustigram_api/methods/
payments.rs

1use crate::client::BotClient;
2use crate::error::Result;
3use rustigram_types::payments::{LabeledPrice, StarAmount, StarTransactions};
4use rustigram_types::user::ChatId;
5use serde::Serialize;
6use std::future::{Future, IntoFuture};
7use std::pin::Pin;
8
9#[derive(Serialize)]
10struct SendInvoiceParams {
11    chat_id: ChatId,
12    title: String,
13    description: String,
14    payload: String,
15    currency: String,
16    prices: Vec<LabeledPrice>,
17    #[serde(skip_serializing_if = "Option::is_none")]
18    provider_token: Option<String>,
19    #[serde(skip_serializing_if = "Option::is_none")]
20    max_tip_amount: Option<i64>,
21    #[serde(skip_serializing_if = "Option::is_none")]
22    suggested_tip_amounts: Option<Vec<i64>>,
23    #[serde(skip_serializing_if = "Option::is_none")]
24    photo_url: Option<String>,
25    #[serde(skip_serializing_if = "Option::is_none")]
26    need_name: Option<bool>,
27    #[serde(skip_serializing_if = "Option::is_none")]
28    need_phone_number: Option<bool>,
29    #[serde(skip_serializing_if = "Option::is_none")]
30    need_email: Option<bool>,
31    #[serde(skip_serializing_if = "Option::is_none")]
32    need_shipping_address: Option<bool>,
33    #[serde(skip_serializing_if = "Option::is_none")]
34    is_flexible: Option<bool>,
35    #[serde(skip_serializing_if = "Option::is_none")]
36    disable_notification: Option<bool>,
37    #[serde(skip_serializing_if = "Option::is_none")]
38    protect_content: Option<bool>,
39    #[serde(skip_serializing_if = "Option::is_none")]
40    reply_markup: Option<rustigram_types::keyboard::InlineKeyboardMarkup>,
41}
42
43/// Builder for the [`sendInvoice`](https://core.telegram.org/bots/api#sendinvoice) method.
44pub struct SendInvoice {
45    client: BotClient,
46    params: SendInvoiceParams,
47}
48impl SendInvoice {
49    pub(crate) fn new(
50        client: BotClient,
51        chat_id: impl Into<ChatId>,
52        title: impl Into<String>,
53        description: impl Into<String>,
54        payload: impl Into<String>,
55        currency: impl Into<String>,
56        prices: Vec<LabeledPrice>,
57    ) -> Self {
58        Self {
59            client,
60            params: SendInvoiceParams {
61                chat_id: chat_id.into(),
62                title: title.into(),
63                description: description.into(),
64                payload: payload.into(),
65                currency: currency.into(),
66                prices,
67                provider_token: None,
68                max_tip_amount: None,
69                suggested_tip_amounts: None,
70                photo_url: None,
71                need_name: None,
72                need_phone_number: None,
73                need_email: None,
74                need_shipping_address: None,
75                is_flexible: None,
76                disable_notification: None,
77                protect_content: None,
78                reply_markup: None,
79            },
80        }
81    }
82    /// Sets the payment provider token. Not required for Telegram Stars (`XTR`).
83    pub fn provider_token(mut self, t: impl Into<String>) -> Self {
84        self.params.provider_token = Some(t.into());
85        self
86    }
87    /// Requests the buyer's full name during checkout.
88    pub fn need_name(mut self, v: bool) -> Self {
89        self.params.need_name = Some(v);
90        self
91    }
92    /// Requests the buyer's shipping address during checkout.
93    pub fn need_shipping_address(mut self, v: bool) -> Self {
94        self.params.need_shipping_address = Some(v);
95        self
96    }
97    /// Indicates that the final price depends on the shipping method.
98    pub fn is_flexible(mut self, v: bool) -> Self {
99        self.params.is_flexible = Some(v);
100        self
101    }
102    /// Attaches a reply markup (inline keyboard, reply keyboard, etc.).
103    pub fn reply_markup(mut self, m: rustigram_types::keyboard::InlineKeyboardMarkup) -> Self {
104        self.params.reply_markup = Some(m);
105        self
106    }
107}
108impl IntoFuture for SendInvoice {
109    type Output = Result<rustigram_types::message::Message>;
110    type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
111    fn into_future(self) -> Self::IntoFuture {
112        Box::pin(async move { self.client.post_json("sendInvoice", &self.params).await })
113    }
114}
115
116/// Builder for the [`getMyStarBalance`](https://core.telegram.org/bots/api#getmystarbalance) method.
117pub struct GetMyStarBalance {
118    client: BotClient,
119}
120impl GetMyStarBalance {
121    pub(crate) fn new(client: BotClient) -> Self {
122        Self { client }
123    }
124}
125impl IntoFuture for GetMyStarBalance {
126    type Output = Result<StarAmount>;
127    type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
128    fn into_future(self) -> Self::IntoFuture {
129        Box::pin(async move {
130            self.client
131                .post_json("getMyStarBalance", &serde_json::json!({}))
132                .await
133        })
134    }
135}
136
137#[derive(Serialize, Default)]
138struct GetStarTransactionsParams {
139    #[serde(skip_serializing_if = "Option::is_none")]
140    offset: Option<u32>,
141    #[serde(skip_serializing_if = "Option::is_none")]
142    limit: Option<u32>,
143}
144
145/// Builder for the [`getStarTransactions`](https://core.telegram.org/bots/api#getstartransactions) method.
146pub struct GetStarTransactions {
147    client: BotClient,
148    params: GetStarTransactionsParams,
149}
150impl GetStarTransactions {
151    pub(crate) fn new(client: BotClient) -> Self {
152        Self {
153            client,
154            params: Default::default(),
155        }
156    }
157    /// Skips the first N transactions in the result.
158    pub fn offset(mut self, v: u32) -> Self {
159        self.params.offset = Some(v);
160        self
161    }
162    /// Limits the number of transactions returned.
163    pub fn limit(mut self, v: u32) -> Self {
164        self.params.limit = Some(v);
165        self
166    }
167}
168impl IntoFuture for GetStarTransactions {
169    type Output = Result<StarTransactions>;
170    type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
171    fn into_future(self) -> Self::IntoFuture {
172        Box::pin(async move {
173            self.client
174                .post_json("getStarTransactions", &self.params)
175                .await
176        })
177    }
178}