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
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
use serde::Serialize;

use crate::{
    net,
    requests::{Request, ResponseResult},
    types::{InlineKeyboardMarkup, LabeledPrice, Message},
    Bot,
};
use std::sync::Arc;

/// Use this method to send invoices.
///
/// [The official docs](https://core.telegram.org/bots/api#sendinvoice).
#[serde_with_macros::skip_serializing_none]
#[derive(Debug, Clone, Serialize)]
pub struct SendInvoice {
    #[serde(skip_serializing)]
    bot: Arc<Bot>,
    chat_id: i32,
    title: String,
    description: String,
    payload: String,
    provider_token: String,
    start_parameter: String,
    currency: String,
    prices: Vec<LabeledPrice>,
    provider_data: Option<String>,
    photo_url: Option<String>,
    photo_size: Option<i32>,
    photo_width: Option<i32>,
    photo_height: Option<i32>,
    need_name: Option<bool>,
    need_phone_number: Option<bool>,
    need_email: Option<bool>,
    need_shipping_address: Option<bool>,
    send_phone_number_to_provider: Option<bool>,
    send_email_to_provider: Option<bool>,
    is_flexible: Option<bool>,
    disable_notification: Option<bool>,
    reply_to_message_id: Option<i32>,
    reply_markup: Option<InlineKeyboardMarkup>,
}

#[async_trait::async_trait]
impl Request for SendInvoice {
    type Output = Message;

    async fn send(&self) -> ResponseResult<Message> {
        net::request_json(
            self.bot.client(),
            self.bot.token(),
            "sendInvoice",
            &self,
        )
        .await
    }
}

impl SendInvoice {
    #[allow(clippy::too_many_arguments)]
    pub(crate) fn new<T, D, Pl, Pt, S, C, Pr>(
        bot: Arc<Bot>,
        chat_id: i32,
        title: T,
        description: D,
        payload: Pl,
        provider_token: Pt,
        start_parameter: S,
        currency: C,
        prices: Pr,
    ) -> Self
    where
        T: Into<String>,
        D: Into<String>,
        Pl: Into<String>,
        Pt: Into<String>,
        S: Into<String>,
        C: Into<String>,
        Pr: Into<Vec<LabeledPrice>>,
    {
        let title = title.into();
        let description = description.into();
        let payload = payload.into();
        let provider_token = provider_token.into();
        let start_parameter = start_parameter.into();
        let currency = currency.into();
        let prices = prices.into();
        Self {
            bot,
            chat_id,
            title,
            description,
            payload,
            provider_token,
            start_parameter,
            currency,
            prices,
            provider_data: None,
            photo_url: None,
            photo_size: None,
            photo_width: None,
            photo_height: None,
            need_name: None,
            need_phone_number: None,
            need_email: None,
            need_shipping_address: None,
            send_phone_number_to_provider: None,
            send_email_to_provider: None,
            is_flexible: None,
            disable_notification: None,
            reply_to_message_id: None,
            reply_markup: None,
        }
    }

    /// Unique identifier for the target private chat.
    pub fn chat_id(mut self, val: i32) -> Self {
        self.chat_id = val;
        self
    }

    /// Product name, 1-32 characters.
    pub fn title<T>(mut self, val: T) -> Self
    where
        T: Into<String>,
    {
        self.title = val.into();
        self
    }

    /// Product description, 1-255 characters.
    pub fn description<T>(mut self, val: T) -> Self
    where
        T: Into<String>,
    {
        self.description = val.into();
        self
    }

    /// Bot-defined invoice payload, 1-128 bytes. This will not be displayed to
    /// the user, use for your internal processes.
    pub fn payload<T>(mut self, val: T) -> Self
    where
        T: Into<String>,
    {
        self.payload = val.into();
        self
    }

    /// Payments provider token, obtained via [@Botfather].
    ///
    /// [@Botfather]: https://t.me/botfather
    pub fn provider_token<T>(mut self, val: T) -> Self
    where
        T: Into<String>,
    {
        self.provider_token = val.into();
        self
    }

    /// Unique deep-linking parameter that can be used to generate this invoice
    /// when used as a start parameter.
    pub fn start_parameter<T>(mut self, val: T) -> Self
    where
        T: Into<String>,
    {
        self.start_parameter = val.into();
        self
    }

    /// Three-letter ISO 4217 currency code, see [more on currencies].
    ///
    /// [more on currencies]: https://core.telegram.org/bots/payments#supported-currencies
    pub fn currency<T>(mut self, val: T) -> Self
    where
        T: Into<String>,
    {
        self.currency = val.into();
        self
    }

    /// Price breakdown, a list of components (e.g. product price, tax,
    /// discount, delivery cost, delivery tax, bonus, etc.).
    pub fn prices<T>(mut self, val: T) -> Self
    where
        T: Into<Vec<LabeledPrice>>,
    {
        self.prices = val.into();
        self
    }

    /// JSON-encoded data about the invoice, which will be shared with the
    /// payment provider.
    ///
    /// A detailed description of required fields should be provided by the
    /// payment provider.
    pub fn provider_data<T>(mut self, val: T) -> Self
    where
        T: Into<String>,
    {
        self.provider_data = Some(val.into());
        self
    }

    /// URL of the product photo for the invoice.
    ///
    /// Can be a photo of the goods or a marketing image for a service. People
    /// like it better when they see what they are paying for.
    pub fn photo_url<T>(mut self, val: T) -> Self
    where
        T: Into<String>,
    {
        self.photo_url = Some(val.into());
        self
    }

    /// Photo size.
    pub fn photo_size(mut self, val: i32) -> Self {
        self.photo_size = Some(val);
        self
    }

    /// Photo width.
    pub fn photo_width(mut self, val: i32) -> Self {
        self.photo_width = Some(val);
        self
    }

    /// Photo height.
    pub fn photo_height(mut self, val: i32) -> Self {
        self.photo_height = Some(val);
        self
    }

    /// Pass `true`, if you require the user's full name to complete the order.
    pub fn need_name(mut self, val: bool) -> Self {
        self.need_name = Some(val);
        self
    }

    /// Pass `true`, if you require the user's phone number to complete the
    /// order.
    pub fn need_phone_number(mut self, val: bool) -> Self {
        self.need_phone_number = Some(val);
        self
    }

    /// Pass `true`, if you require the user's email address to complete the
    /// order.
    pub fn need_email(mut self, val: bool) -> Self {
        self.need_email = Some(val);
        self
    }

    /// Pass `true`, if you require the user's shipping address to complete the
    /// order.
    pub fn need_shipping_address(mut self, val: bool) -> Self {
        self.need_shipping_address = Some(val);
        self
    }

    /// Pass `true`, if user's phone number should be sent to provider.
    pub fn send_phone_number_to_provider(mut self, val: bool) -> Self {
        self.send_phone_number_to_provider = Some(val);
        self
    }

    /// Pass `true`, if user's email address should be sent to provider.
    pub fn send_email_to_provider(mut self, val: bool) -> Self {
        self.send_email_to_provider = Some(val);
        self
    }

    /// Pass `true`, if the final price depends on the shipping method.
    #[allow(clippy::wrong_self_convention)]
    pub fn is_flexible(mut self, val: bool) -> Self {
        self.is_flexible = Some(val);
        self
    }

    /// Sends the message [silently]. Users will receive a notification with no
    /// sound.
    ///
    /// [silently]: https://telegram.org/blog/channels-2-0#silent-messages
    pub fn disable_notification(mut self, val: bool) -> Self {
        self.disable_notification = Some(val);
        self
    }

    /// If the message is a reply, ID of the original message.
    pub fn reply_to_message_id(mut self, val: i32) -> Self {
        self.reply_to_message_id = Some(val);
        self
    }

    /// A JSON-serialized object for an [inline keyboard].
    ///
    /// If empty, one 'Pay `total price`' button will be shown. If not empty,
    /// the first button must be a Pay button.
    ///
    /// [inlint keyboard]: https://core.telegram.org/bots#inline-keyboards-and-on-the-fly-updating
    pub fn reply_markup(mut self, val: InlineKeyboardMarkup) -> Self {
        self.reply_markup = Some(val);
        self
    }
}