Skip to main content

storekit/
subscription.rs

1use serde::Deserialize;
2
3#[derive(Debug, Clone, PartialEq, Eq)]
4pub enum SubscriptionPeriodUnit {
5    Day,
6    Week,
7    Month,
8    Year,
9    Unknown(String),
10}
11
12impl SubscriptionPeriodUnit {
13    pub fn as_str(&self) -> &str {
14        match self {
15            Self::Day => "day",
16            Self::Week => "week",
17            Self::Month => "month",
18            Self::Year => "year",
19            Self::Unknown(value) => value.as_str(),
20        }
21    }
22
23    pub(crate) fn from_raw(raw: String) -> Self {
24        match raw.as_str() {
25            "day" => Self::Day,
26            "week" => Self::Week,
27            "month" => Self::Month,
28            "year" => Self::Year,
29            _ => Self::Unknown(raw),
30        }
31    }
32}
33
34#[derive(Debug, Clone, PartialEq, Eq)]
35pub struct SubscriptionPeriod {
36    pub unit: SubscriptionPeriodUnit,
37    pub value: i64,
38}
39
40#[derive(Debug, Clone, PartialEq, Eq)]
41pub enum SubscriptionOfferType {
42    Introductory,
43    Promotional,
44    WinBack,
45    Unknown(String),
46}
47
48impl SubscriptionOfferType {
49    pub fn as_str(&self) -> &str {
50        match self {
51            Self::Introductory => "introductory",
52            Self::Promotional => "promotional",
53            Self::WinBack => "winBack",
54            Self::Unknown(value) => value.as_str(),
55        }
56    }
57
58    pub(crate) fn from_raw(raw: String) -> Self {
59        match raw.as_str() {
60            "introductory" => Self::Introductory,
61            "promotional" => Self::Promotional,
62            "winBack" => Self::WinBack,
63            _ => Self::Unknown(raw),
64        }
65    }
66}
67
68#[derive(Debug, Clone, PartialEq, Eq)]
69pub enum SubscriptionPaymentMode {
70    PayAsYouGo,
71    PayUpFront,
72    FreeTrial,
73    Unknown(String),
74}
75
76impl SubscriptionPaymentMode {
77    pub fn as_str(&self) -> &str {
78        match self {
79            Self::PayAsYouGo => "payAsYouGo",
80            Self::PayUpFront => "payUpFront",
81            Self::FreeTrial => "freeTrial",
82            Self::Unknown(value) => value.as_str(),
83        }
84    }
85
86    pub(crate) fn from_raw(raw: String) -> Self {
87        match raw.as_str() {
88            "payAsYouGo" => Self::PayAsYouGo,
89            "payUpFront" => Self::PayUpFront,
90            "freeTrial" => Self::FreeTrial,
91            _ => Self::Unknown(raw),
92        }
93    }
94}
95
96#[derive(Debug, Clone, PartialEq, Eq)]
97pub struct SubscriptionOffer {
98    pub id: Option<String>,
99    pub offer_type: SubscriptionOfferType,
100    pub price: String,
101    pub display_price: String,
102    pub period: SubscriptionPeriod,
103    pub period_count: i64,
104    pub payment_mode: SubscriptionPaymentMode,
105}
106
107#[derive(Debug, Deserialize)]
108pub(crate) struct SubscriptionPeriodPayload {
109    unit: String,
110    value: i64,
111}
112
113impl SubscriptionPeriodPayload {
114    pub(crate) fn into_subscription_period(self) -> SubscriptionPeriod {
115        SubscriptionPeriod {
116            unit: SubscriptionPeriodUnit::from_raw(self.unit),
117            value: self.value,
118        }
119    }
120}
121
122#[derive(Debug, Deserialize)]
123pub(crate) struct SubscriptionOfferPayload {
124    id: Option<String>,
125    #[serde(rename = "type")]
126    offer_type: String,
127    price: String,
128    #[serde(rename = "displayPrice")]
129    display_price: String,
130    period: SubscriptionPeriodPayload,
131    #[serde(rename = "periodCount")]
132    period_count: i64,
133    #[serde(rename = "paymentMode")]
134    payment_mode: String,
135}
136
137impl SubscriptionOfferPayload {
138    pub(crate) fn into_subscription_offer(self) -> SubscriptionOffer {
139        SubscriptionOffer {
140            id: self.id,
141            offer_type: SubscriptionOfferType::from_raw(self.offer_type),
142            price: self.price,
143            display_price: self.display_price,
144            period: self.period.into_subscription_period(),
145            period_count: self.period_count,
146            payment_mode: SubscriptionPaymentMode::from_raw(self.payment_mode),
147        }
148    }
149}