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")]
390pub enum TransactionPartner {
391 AffiliateProgram(TransactionPartnerAffiliateProgramParameters),
393 Chat(TransactionPartnerChatParameters),
395 Fragment(Option<RevenueWithdrawalState>),
397 Other,
399 TelegramAds,
401 TelegramApi {
403 request_count: Integer,
405 },
406 User(TransactionPartnerUserParameters),
408}
409
410#[serde_with::skip_serializing_none]
411#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
412#[serde(tag = "type", rename_all = "snake_case")]
413enum RawTransactionPartner {
414 AffiliateProgram(TransactionPartnerAffiliateProgramParameters),
415 Chat(TransactionPartnerChatParameters),
416 Fragment {
417 withdrawal_state: Option<RevenueWithdrawalState>,
418 },
419 Other {},
420 TelegramAds {},
421 TelegramApi {
422 request_count: Integer,
423 },
424 User(TransactionPartnerUserParameters),
425}
426
427impl From<RawTransactionPartner> for TransactionPartner {
428 fn from(value: RawTransactionPartner) -> Self {
429 match value {
430 RawTransactionPartner::AffiliateProgram(parameters) => Self::AffiliateProgram(parameters),
431 RawTransactionPartner::Chat(parameters) => Self::Chat(parameters),
432 RawTransactionPartner::Fragment { withdrawal_state } => Self::Fragment(withdrawal_state),
433 RawTransactionPartner::Other {} => Self::Other,
434 RawTransactionPartner::TelegramAds {} => Self::TelegramAds,
435 RawTransactionPartner::TelegramApi { request_count } => Self::TelegramApi { request_count },
436 RawTransactionPartner::User(parameters) => Self::User(parameters),
437 }
438 }
439}
440
441impl From<TransactionPartner> for RawTransactionPartner {
442 fn from(value: TransactionPartner) -> Self {
443 match value {
444 TransactionPartner::AffiliateProgram(parameters) => Self::AffiliateProgram(parameters),
445 TransactionPartner::Chat(parameters) => Self::Chat(parameters),
446 TransactionPartner::Fragment(withdrawal_state) => Self::Fragment { withdrawal_state },
447 TransactionPartner::Other => Self::Other {},
448 TransactionPartner::TelegramAds => Self::TelegramAds {},
449 TransactionPartner::TelegramApi { request_count } => Self::TelegramApi { request_count },
450 TransactionPartner::User(parameters) => Self::User(parameters),
451 }
452 }
453}
454
455#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
457#[serde(from = "RawRevenueWithdrawalState", into = "RawRevenueWithdrawalState")]
458pub enum RevenueWithdrawalState {
459 Failed,
461 Pending,
463 Succeeded {
465 date: Integer,
467 url: String,
469 },
470}
471
472#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
473#[serde(tag = "type", rename_all = "snake_case")]
474enum RawRevenueWithdrawalState {
475 Failed {},
476 Pending {},
477 Succeeded { date: Integer, url: String },
478}
479
480impl From<RawRevenueWithdrawalState> for RevenueWithdrawalState {
481 fn from(value: RawRevenueWithdrawalState) -> Self {
482 use self::RawRevenueWithdrawalState::*;
483 match value {
484 Failed {} => Self::Failed,
485 Pending {} => Self::Pending,
486 Succeeded { date, url } => Self::Succeeded { date, url },
487 }
488 }
489}
490
491impl From<RevenueWithdrawalState> for RawRevenueWithdrawalState {
492 fn from(value: RevenueWithdrawalState) -> Self {
493 use self::RevenueWithdrawalState::*;
494 match value {
495 Failed => Self::Failed {},
496 Pending => Self::Pending {},
497 Succeeded { date, url } => Self::Succeeded { date, url },
498 }
499 }
500}
501
502#[serde_with::skip_serializing_none]
504#[derive(Clone, Copy, Debug, Default, Serialize)]
505pub struct GetStarTransactions {
506 offset: Option<Integer>,
507 limit: Option<Integer>,
508}
509
510impl GetStarTransactions {
511 pub fn with_offset(mut self, value: Integer) -> Self {
517 self.offset = Some(value);
518 self
519 }
520
521 pub fn with_limit(mut self, value: Integer) -> Self {
530 self.limit = Some(value);
531 self
532 }
533}
534
535impl Method for GetStarTransactions {
536 type Response = StarTransactions;
537
538 fn into_payload(self) -> Payload {
539 Payload::json("getStarTransactions", self)
540 }
541}