1use serde::{Deserialize, Serialize};
2
3use crate::{
4 api::{Method, Payload},
5 types::{Chat, Gift, Integer, PaidMedia, User},
6};
7
8#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
10pub struct StarTransactions {
11 pub transactions: Vec<StarTransaction>,
13}
14
15impl<T> From<T> for StarTransactions
16where
17 T: IntoIterator<Item = StarTransaction>,
18{
19 fn from(value: T) -> Self {
20 Self {
21 transactions: value.into_iter().collect(),
22 }
23 }
24}
25
26#[serde_with::skip_serializing_none]
28#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
29pub struct StarTransaction {
30 amount: Integer,
31 date: Integer,
32 id: String,
33 nanostar_amount: Option<Integer>,
34 source: Option<TransactionPartner>,
35 receiver: Option<TransactionPartner>,
36}
37
38impl StarTransaction {
39 pub fn new<T>(amount: Integer, date: Integer, id: T) -> Self
50 where
51 T: Into<String>,
52 {
53 Self {
54 amount,
55 date,
56 id: id.into(),
57 nanostar_amount: None,
58 source: None,
59 receiver: None,
60 }
61 }
62
63 pub fn with_nanostar_amount(mut self, value: Integer) -> Self {
70 self.nanostar_amount = Some(value);
71 self
72 }
73
74 pub fn with_source(mut self, value: TransactionPartner) -> Self {
82 self.source = Some(value);
83 self
84 }
85
86 pub fn with_receiver(mut self, value: TransactionPartner) -> Self {
94 self.receiver = Some(value);
95 self
96 }
97}
98
99#[serde_with::skip_serializing_none]
101#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
102pub struct TransactionPartnerAffiliateProgramParameters {
103 pub commission_per_mille: Integer,
106 pub sponsor_user: Option<User>,
108}
109
110impl TransactionPartnerAffiliateProgramParameters {
111 pub fn new(commission_per_mille: Integer) -> Self {
117 Self {
118 commission_per_mille,
119 sponsor_user: None,
120 }
121 }
122
123 pub fn with_sponsor_user(mut self, value: User) -> Self {
129 self.sponsor_user = Some(value);
130 self
131 }
132}
133
134#[serde_with::skip_serializing_none]
136#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
137pub struct AffiliateInfo {
138 pub amount: Integer,
141 pub commission_per_mille: Integer,
144 pub affiliate_chat: Option<Chat>,
146 pub affiliate_user: Option<User>,
148 pub nanostar_amount: Option<Integer>,
151}
152
153impl AffiliateInfo {
154 pub fn new(amount: Integer, commission_per_mille: Integer) -> Self {
161 Self {
162 amount,
163 commission_per_mille,
164 affiliate_chat: None,
165 affiliate_user: None,
166 nanostar_amount: None,
167 }
168 }
169
170 pub fn with_affiliate_chat<T>(mut self, value: T) -> Self
176 where
177 T: Into<Chat>,
178 {
179 self.affiliate_chat = Some(value.into());
180 self
181 }
182
183 pub fn with_affiliate_user(mut self, value: User) -> Self {
189 self.affiliate_user = Some(value);
190 self
191 }
192
193 pub fn with_nanostar_amount(mut self, value: Integer) -> Self {
199 self.nanostar_amount = Some(value);
200 self
201 }
202}
203
204#[serde_with::skip_serializing_none]
206#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
207pub struct TransactionPartnerChatParameters {
208 pub chat: Chat,
210 pub gift: Option<Gift>,
212}
213
214impl TransactionPartnerChatParameters {
215 pub fn new<T>(chat: T) -> Self
221 where
222 T: Into<Chat>,
223 {
224 Self {
225 chat: chat.into(),
226 gift: None,
227 }
228 }
229
230 pub fn with_gift(mut self, value: Gift) -> Self {
236 self.gift = Some(value);
237 self
238 }
239}
240
241#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Serialize)]
243#[serde(rename_all = "snake_case")]
244pub enum TransactionPartnerUserType {
245 BusinessAccountTransfer,
247 GiftPurchase,
249 InvoicePayment,
251 PaidMediaPayment,
253 PremiumPurchase,
255}
256
257#[serde_with::skip_serializing_none]
259#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
260pub struct TransactionPartnerUserParameters {
261 pub transaction_type: TransactionPartnerUserType,
263 pub user: User,
265 pub affiliate: Option<AffiliateInfo>,
267 pub gift: Option<String>,
269 pub invoice_payload: Option<String>,
271 pub paid_media: Option<Vec<PaidMedia>>,
273 pub paid_media_payload: Option<String>,
275 pub premium_subscription_duration: Option<Integer>,
278 pub subscription_period: Option<Integer>,
280}
281
282impl TransactionPartnerUserParameters {
283 pub fn new(transaction_type: TransactionPartnerUserType, user: User) -> Self {
290 Self {
291 transaction_type,
292 user,
293 affiliate: None,
294 gift: None,
295 invoice_payload: None,
296 paid_media: None,
297 paid_media_payload: None,
298 premium_subscription_duration: None,
299 subscription_period: None,
300 }
301 }
302
303 pub fn with_affiliate(mut self, value: AffiliateInfo) -> Self {
309 self.affiliate = Some(value);
310 self
311 }
312
313 pub fn with_gift<T>(mut self, value: T) -> Self
319 where
320 T: Into<String>,
321 {
322 self.gift = Some(value.into());
323 self
324 }
325
326 pub fn with_invoice_payload<T>(mut self, value: T) -> Self
332 where
333 T: Into<String>,
334 {
335 self.invoice_payload = Some(value.into());
336 self
337 }
338
339 pub fn with_paid_media<T>(mut self, value: T) -> Self
345 where
346 T: IntoIterator<Item = PaidMedia>,
347 {
348 self.paid_media = Some(value.into_iter().collect());
349 self
350 }
351
352 pub fn with_paid_media_payload<T>(mut self, value: T) -> Self
358 where
359 T: Into<String>,
360 {
361 self.paid_media_payload = Some(value.into());
362 self
363 }
364
365 pub fn with_premium_subscription_duration(mut self, value: Integer) -> Self {
372 self.premium_subscription_duration = Some(value);
373 self
374 }
375
376 pub fn with_subscription_period(mut self, value: Integer) -> Self {
382 self.subscription_period = Some(value);
383 self
384 }
385}
386
387#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
389#[serde(from = "RawTransactionPartner", into = "RawTransactionPartner")]
390#[allow(clippy::large_enum_variant)]
391pub enum TransactionPartner {
392 AffiliateProgram(TransactionPartnerAffiliateProgramParameters),
394 Chat(TransactionPartnerChatParameters),
396 Fragment(Option<RevenueWithdrawalState>),
398 Other,
400 TelegramAds,
402 TelegramApi {
404 request_count: Integer,
406 },
407 User(TransactionPartnerUserParameters),
409}
410
411#[serde_with::skip_serializing_none]
412#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
413#[serde(tag = "type", rename_all = "snake_case")]
414#[allow(clippy::large_enum_variant)]
415enum RawTransactionPartner {
416 AffiliateProgram(TransactionPartnerAffiliateProgramParameters),
417 Chat(TransactionPartnerChatParameters),
418 Fragment {
419 withdrawal_state: Option<RevenueWithdrawalState>,
420 },
421 Other {},
422 TelegramAds {},
423 TelegramApi {
424 request_count: Integer,
425 },
426 User(TransactionPartnerUserParameters),
427}
428
429impl From<RawTransactionPartner> for TransactionPartner {
430 fn from(value: RawTransactionPartner) -> Self {
431 match value {
432 RawTransactionPartner::AffiliateProgram(parameters) => Self::AffiliateProgram(parameters),
433 RawTransactionPartner::Chat(parameters) => Self::Chat(parameters),
434 RawTransactionPartner::Fragment { withdrawal_state } => Self::Fragment(withdrawal_state),
435 RawTransactionPartner::Other {} => Self::Other,
436 RawTransactionPartner::TelegramAds {} => Self::TelegramAds,
437 RawTransactionPartner::TelegramApi { request_count } => Self::TelegramApi { request_count },
438 RawTransactionPartner::User(parameters) => Self::User(parameters),
439 }
440 }
441}
442
443impl From<TransactionPartner> for RawTransactionPartner {
444 fn from(value: TransactionPartner) -> Self {
445 match value {
446 TransactionPartner::AffiliateProgram(parameters) => Self::AffiliateProgram(parameters),
447 TransactionPartner::Chat(parameters) => Self::Chat(parameters),
448 TransactionPartner::Fragment(withdrawal_state) => Self::Fragment { withdrawal_state },
449 TransactionPartner::Other => Self::Other {},
450 TransactionPartner::TelegramAds => Self::TelegramAds {},
451 TransactionPartner::TelegramApi { request_count } => Self::TelegramApi { request_count },
452 TransactionPartner::User(parameters) => Self::User(parameters),
453 }
454 }
455}
456
457#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
459#[serde(from = "RawRevenueWithdrawalState", into = "RawRevenueWithdrawalState")]
460pub enum RevenueWithdrawalState {
461 Failed,
463 Pending,
465 Succeeded {
467 date: Integer,
469 url: String,
471 },
472}
473
474#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
475#[serde(tag = "type", rename_all = "snake_case")]
476enum RawRevenueWithdrawalState {
477 Failed {},
478 Pending {},
479 Succeeded { date: Integer, url: String },
480}
481
482impl From<RawRevenueWithdrawalState> for RevenueWithdrawalState {
483 fn from(value: RawRevenueWithdrawalState) -> Self {
484 use self::RawRevenueWithdrawalState::*;
485 match value {
486 Failed {} => Self::Failed,
487 Pending {} => Self::Pending,
488 Succeeded { date, url } => Self::Succeeded { date, url },
489 }
490 }
491}
492
493impl From<RevenueWithdrawalState> for RawRevenueWithdrawalState {
494 fn from(value: RevenueWithdrawalState) -> Self {
495 use self::RevenueWithdrawalState::*;
496 match value {
497 Failed => Self::Failed {},
498 Pending => Self::Pending {},
499 Succeeded { date, url } => Self::Succeeded { date, url },
500 }
501 }
502}
503
504#[serde_with::skip_serializing_none]
506#[derive(Clone, Copy, Debug, Default, Serialize)]
507pub struct GetStarTransactions {
508 offset: Option<Integer>,
509 limit: Option<Integer>,
510}
511
512impl GetStarTransactions {
513 pub fn with_offset(mut self, value: Integer) -> Self {
519 self.offset = Some(value);
520 self
521 }
522
523 pub fn with_limit(mut self, value: Integer) -> Self {
532 self.limit = Some(value);
533 self
534 }
535}
536
537impl Method for GetStarTransactions {
538 type Response = StarTransactions;
539
540 fn into_payload(self) -> Payload {
541 Payload::json("getStarTransactions", self)
542 }
543}