1use serde::{Deserialize, Serialize};
2
3use crate::{
4 api::{Method, Payload},
5 types::{Chat, Gift, Integer, PaidMedia, User},
6};
7
8#[cfg(test)]
9mod tests;
10
11#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
13pub struct StarTransactions {
14 pub transactions: Vec<StarTransaction>,
16}
17
18impl<T> From<T> for StarTransactions
19where
20 T: IntoIterator<Item = StarTransaction>,
21{
22 fn from(value: T) -> Self {
23 Self {
24 transactions: value.into_iter().collect(),
25 }
26 }
27}
28
29#[serde_with::skip_serializing_none]
31#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
32pub struct StarTransaction {
33 amount: Integer,
34 date: Integer,
35 id: String,
36 nanostar_amount: Option<Integer>,
37 source: Option<TransactionPartner>,
38 receiver: Option<TransactionPartner>,
39}
40
41impl StarTransaction {
42 pub fn new<T>(amount: Integer, date: Integer, id: T) -> Self
53 where
54 T: Into<String>,
55 {
56 Self {
57 amount,
58 date,
59 id: id.into(),
60 nanostar_amount: None,
61 source: None,
62 receiver: None,
63 }
64 }
65
66 pub fn with_nanostar_amount(mut self, value: Integer) -> Self {
73 self.nanostar_amount = Some(value);
74 self
75 }
76
77 pub fn with_source(mut self, value: TransactionPartner) -> Self {
85 self.source = Some(value);
86 self
87 }
88
89 pub fn with_receiver(mut self, value: TransactionPartner) -> Self {
97 self.receiver = Some(value);
98 self
99 }
100}
101
102#[serde_with::skip_serializing_none]
104#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
105pub struct TransactionPartnerAffiliateProgramParameters {
106 pub commission_per_mille: Integer,
109 pub sponsor_user: Option<User>,
111}
112
113impl TransactionPartnerAffiliateProgramParameters {
114 pub fn new(commission_per_mille: Integer) -> Self {
120 Self {
121 commission_per_mille,
122 sponsor_user: None,
123 }
124 }
125
126 pub fn with_sponsor_user(mut self, value: User) -> Self {
132 self.sponsor_user = Some(value);
133 self
134 }
135}
136
137#[serde_with::skip_serializing_none]
139#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
140pub struct AffiliateInfo {
141 pub amount: Integer,
144 pub commission_per_mille: Integer,
147 pub affiliate_chat: Option<Chat>,
149 pub affiliate_user: Option<User>,
151 pub nanostar_amount: Option<Integer>,
154}
155
156impl AffiliateInfo {
157 pub fn new(amount: Integer, commission_per_mille: Integer) -> Self {
164 Self {
165 amount,
166 commission_per_mille,
167 affiliate_chat: None,
168 affiliate_user: None,
169 nanostar_amount: None,
170 }
171 }
172
173 pub fn with_affiliate_chat<T>(mut self, value: T) -> Self
179 where
180 T: Into<Chat>,
181 {
182 self.affiliate_chat = Some(value.into());
183 self
184 }
185
186 pub fn with_affiliate_user(mut self, value: User) -> Self {
192 self.affiliate_user = Some(value);
193 self
194 }
195
196 pub fn with_nanostar_amount(mut self, value: Integer) -> Self {
202 self.nanostar_amount = Some(value);
203 self
204 }
205}
206
207#[serde_with::skip_serializing_none]
209#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
210pub struct TransactionPartnerChatParameters {
211 pub chat: Chat,
213 pub gift: Option<Gift>,
215}
216
217impl TransactionPartnerChatParameters {
218 pub fn new<T>(chat: T) -> Self
224 where
225 T: Into<Chat>,
226 {
227 Self {
228 chat: chat.into(),
229 gift: None,
230 }
231 }
232
233 pub fn with_gift(mut self, value: Gift) -> Self {
239 self.gift = Some(value);
240 self
241 }
242}
243
244#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Serialize)]
246#[serde(rename_all = "snake_case")]
247pub enum TransactionPartnerUserType {
248 BusinessAccountTransfer,
250 GiftPurchase,
252 InvoicePayment,
254 PaidMediaPayment,
256 PremiumPurchase,
258}
259
260#[serde_with::skip_serializing_none]
262#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
263pub struct TransactionPartnerUserParameters {
264 pub transaction_type: TransactionPartnerUserType,
266 pub user: User,
268 pub affiliate: Option<AffiliateInfo>,
270 pub gift: Option<String>,
272 pub invoice_payload: Option<String>,
274 pub paid_media: Option<Vec<PaidMedia>>,
276 pub paid_media_payload: Option<String>,
278 pub premium_subscription_duration: Option<Integer>,
281 pub subscription_period: Option<Integer>,
283}
284
285impl TransactionPartnerUserParameters {
286 pub fn new(transaction_type: TransactionPartnerUserType, user: User) -> Self {
293 Self {
294 transaction_type,
295 user,
296 affiliate: None,
297 gift: None,
298 invoice_payload: None,
299 paid_media: None,
300 paid_media_payload: None,
301 premium_subscription_duration: None,
302 subscription_period: None,
303 }
304 }
305
306 pub fn with_affiliate(mut self, value: AffiliateInfo) -> Self {
312 self.affiliate = Some(value);
313 self
314 }
315
316 pub fn with_gift<T>(mut self, value: T) -> Self
322 where
323 T: Into<String>,
324 {
325 self.gift = Some(value.into());
326 self
327 }
328
329 pub fn with_invoice_payload<T>(mut self, value: T) -> Self
335 where
336 T: Into<String>,
337 {
338 self.invoice_payload = Some(value.into());
339 self
340 }
341
342 pub fn with_paid_media<T>(mut self, value: T) -> Self
348 where
349 T: IntoIterator<Item = PaidMedia>,
350 {
351 self.paid_media = Some(value.into_iter().collect());
352 self
353 }
354
355 pub fn with_paid_media_payload<T>(mut self, value: T) -> Self
361 where
362 T: Into<String>,
363 {
364 self.paid_media_payload = Some(value.into());
365 self
366 }
367
368 pub fn with_premium_subscription_duration(mut self, value: Integer) -> Self {
375 self.premium_subscription_duration = Some(value);
376 self
377 }
378
379 pub fn with_subscription_period(mut self, value: Integer) -> Self {
385 self.subscription_period = Some(value);
386 self
387 }
388}
389
390#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
392#[serde(from = "RawTransactionPartner", into = "RawTransactionPartner")]
393#[allow(clippy::large_enum_variant)]
394pub enum TransactionPartner {
395 AffiliateProgram(TransactionPartnerAffiliateProgramParameters),
397 Chat(TransactionPartnerChatParameters),
399 Fragment(Option<RevenueWithdrawalState>),
401 Other,
403 TelegramAds,
405 TelegramApi {
407 request_count: Integer,
409 },
410 User(TransactionPartnerUserParameters),
412}
413
414#[serde_with::skip_serializing_none]
415#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
416#[serde(tag = "type", rename_all = "snake_case")]
417#[allow(clippy::large_enum_variant)]
418enum RawTransactionPartner {
419 AffiliateProgram(TransactionPartnerAffiliateProgramParameters),
420 Chat(TransactionPartnerChatParameters),
421 Fragment {
422 withdrawal_state: Option<RevenueWithdrawalState>,
423 },
424 Other {},
425 TelegramAds {},
426 TelegramApi {
427 request_count: Integer,
428 },
429 User(TransactionPartnerUserParameters),
430}
431
432impl From<RawTransactionPartner> for TransactionPartner {
433 fn from(value: RawTransactionPartner) -> Self {
434 match value {
435 RawTransactionPartner::AffiliateProgram(parameters) => Self::AffiliateProgram(parameters),
436 RawTransactionPartner::Chat(parameters) => Self::Chat(parameters),
437 RawTransactionPartner::Fragment { withdrawal_state } => Self::Fragment(withdrawal_state),
438 RawTransactionPartner::Other {} => Self::Other,
439 RawTransactionPartner::TelegramAds {} => Self::TelegramAds,
440 RawTransactionPartner::TelegramApi { request_count } => Self::TelegramApi { request_count },
441 RawTransactionPartner::User(parameters) => Self::User(parameters),
442 }
443 }
444}
445
446impl From<TransactionPartner> for RawTransactionPartner {
447 fn from(value: TransactionPartner) -> Self {
448 match value {
449 TransactionPartner::AffiliateProgram(parameters) => Self::AffiliateProgram(parameters),
450 TransactionPartner::Chat(parameters) => Self::Chat(parameters),
451 TransactionPartner::Fragment(withdrawal_state) => Self::Fragment { withdrawal_state },
452 TransactionPartner::Other => Self::Other {},
453 TransactionPartner::TelegramAds => Self::TelegramAds {},
454 TransactionPartner::TelegramApi { request_count } => Self::TelegramApi { request_count },
455 TransactionPartner::User(parameters) => Self::User(parameters),
456 }
457 }
458}
459
460#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
462#[serde(from = "RawRevenueWithdrawalState", into = "RawRevenueWithdrawalState")]
463pub enum RevenueWithdrawalState {
464 Failed,
466 Pending,
468 Succeeded {
470 date: Integer,
472 url: String,
474 },
475}
476
477#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
478#[serde(tag = "type", rename_all = "snake_case")]
479enum RawRevenueWithdrawalState {
480 Failed {},
481 Pending {},
482 Succeeded { date: Integer, url: String },
483}
484
485impl From<RawRevenueWithdrawalState> for RevenueWithdrawalState {
486 fn from(value: RawRevenueWithdrawalState) -> Self {
487 use self::RawRevenueWithdrawalState::*;
488 match value {
489 Failed {} => Self::Failed,
490 Pending {} => Self::Pending,
491 Succeeded { date, url } => Self::Succeeded { date, url },
492 }
493 }
494}
495
496impl From<RevenueWithdrawalState> for RawRevenueWithdrawalState {
497 fn from(value: RevenueWithdrawalState) -> Self {
498 use self::RevenueWithdrawalState::*;
499 match value {
500 Failed => Self::Failed {},
501 Pending => Self::Pending {},
502 Succeeded { date, url } => Self::Succeeded { date, url },
503 }
504 }
505}
506
507#[serde_with::skip_serializing_none]
509#[derive(Clone, Copy, Debug, Default, Serialize)]
510pub struct GetStarTransactions {
511 offset: Option<Integer>,
512 limit: Option<Integer>,
513}
514
515impl GetStarTransactions {
516 pub fn with_offset(mut self, value: Integer) -> Self {
522 self.offset = Some(value);
523 self
524 }
525
526 pub fn with_limit(mut self, value: Integer) -> Self {
535 self.limit = Some(value);
536 self
537 }
538}
539
540impl Method for GetStarTransactions {
541 type Response = StarTransactions;
542
543 fn into_payload(self) -> Payload {
544 Payload::json("getStarTransactions", self)
545 }
546}