1use crate::Lb;
2use crate::io::docs::LocalDocuments;
3use crate::io::network::ApiError;
4use crate::io::server::Server;
5use crate::model::api::{
6 CancelSubscriptionError, CancelSubscriptionRequest, GetSubscriptionInfoError,
7 GetSubscriptionInfoRequestV2, StripeBillingPortalError, StripeBillingPortalRequest,
8 StripeCheckoutError, StripeCheckoutRequest, SubscriptionInfo, UpgradeAccountAppStoreError,
9 UpgradeAccountAppStoreRequest, UpgradeAccountGooglePlayError, UpgradeAccountGooglePlayRequest,
10};
11use crate::model::errors::{LbErrKind, LbResult, Unexpected, core_err_unexpected};
12
13impl<S: Server, D: LocalDocuments> Lb<S, D> {
15 #[instrument(level = "debug", skip(self), err(Debug))]
17 pub async fn stripe_checkout_url(&self) -> LbResult<String> {
18 let account = self.get_account()?;
19
20 let response = self
21 .client
22 .stripe_checkout(account, StripeCheckoutRequest {})
23 .await
24 .map_err(|err| match err {
25 ApiError::Endpoint(err) => match err {
26 StripeCheckoutError::AlreadyPremium => LbErrKind::AlreadyPremium,
27 StripeCheckoutError::ExistingRequestPending => {
28 LbErrKind::ExistingRequestPending
29 }
30 StripeCheckoutError::UserNotFound => LbErrKind::AccountNonexistent,
31 StripeCheckoutError::NotPermissioned => LbErrKind::InsufficientPermission,
32 },
33 ApiError::SendFailed(_) => LbErrKind::ServerUnreachable,
34 ApiError::ClientUpdateRequired => LbErrKind::ClientUpdateRequired,
35 _ => core_err_unexpected(err),
36 })?;
37
38 Ok(response.url)
39 }
40
41 #[instrument(level = "debug", skip(self), err(Debug))]
43 pub async fn stripe_billing_portal_url(&self) -> LbResult<String> {
44 let account = self.get_account()?;
45
46 let response = self
47 .client
48 .stripe_billing_portal(account, StripeBillingPortalRequest {})
49 .await
50 .map_err(|err| match err {
51 ApiError::Endpoint(StripeBillingPortalError::NotStripeCustomer) => {
52 LbErrKind::NotPremium
53 }
54 ApiError::Endpoint(StripeBillingPortalError::UserNotFound) => {
55 LbErrKind::AccountNonexistent
56 }
57 ApiError::Endpoint(StripeBillingPortalError::NotPermissioned) => {
58 LbErrKind::InsufficientPermission
59 }
60 ApiError::SendFailed(_) => LbErrKind::ServerUnreachable,
61 ApiError::ClientUpdateRequired => LbErrKind::ClientUpdateRequired,
62 _ => core_err_unexpected(err),
63 })?;
64
65 Ok(response.url)
66 }
67
68 #[instrument(level = "debug", skip(self), err(Debug))]
69 pub async fn upgrade_account_google_play(
70 &self, purchase_token: &str, account_id: &str,
71 ) -> LbResult<()> {
72 let account = self.get_account()?;
73
74 self.client
75 .upgrade_account_google_play(
76 account,
77 UpgradeAccountGooglePlayRequest {
78 purchase_token: purchase_token.to_string(),
79 account_id: account_id.to_string(),
80 },
81 )
82 .await
83 .map_err(|err| match err {
84 ApiError::Endpoint(err) => match err {
85 UpgradeAccountGooglePlayError::AlreadyPremium => LbErrKind::AlreadyPremium,
86 UpgradeAccountGooglePlayError::InvalidPurchaseToken => {
87 LbErrKind::InvalidPurchaseToken
88 }
89 UpgradeAccountGooglePlayError::ExistingRequestPending => {
90 LbErrKind::ExistingRequestPending
91 }
92 UpgradeAccountGooglePlayError::UserNotFound => core_err_unexpected(err),
93 UpgradeAccountGooglePlayError::NotPermissioned => {
94 LbErrKind::InsufficientPermission
95 }
96 },
97 ApiError::SendFailed(_) => LbErrKind::ServerUnreachable,
98 ApiError::ClientUpdateRequired => LbErrKind::ClientUpdateRequired,
99 _ => core_err_unexpected(err),
100 })?;
101
102 self.refresh_usage().await.log_and_ignore();
103
104 Ok(())
105 }
106
107 #[instrument(level = "debug", skip(self), err(Debug))]
108 pub async fn upgrade_account_app_store(
109 &self, original_transaction_id: String, app_account_token: String,
110 ) -> LbResult<()> {
111 let account = self.get_account()?;
112
113 self.client
114 .upgrade_account_app_store(
115 account,
116 UpgradeAccountAppStoreRequest { original_transaction_id, app_account_token },
117 )
118 .await
119 .map_err(|err| match err {
120 ApiError::Endpoint(err) => match err {
121 UpgradeAccountAppStoreError::AlreadyPremium => LbErrKind::AlreadyPremium,
122 UpgradeAccountAppStoreError::InvalidAuthDetails => {
123 LbErrKind::InvalidAuthDetails
124 }
125 UpgradeAccountAppStoreError::ExistingRequestPending => {
126 LbErrKind::ExistingRequestPending
127 }
128 UpgradeAccountAppStoreError::AppStoreAccountAlreadyLinked => {
129 LbErrKind::AppStoreAccountAlreadyLinked
130 }
131 UpgradeAccountAppStoreError::UserNotFound => core_err_unexpected(err),
132 UpgradeAccountAppStoreError::NotPermissioned => {
133 LbErrKind::InsufficientPermission
134 }
135 },
136 ApiError::SendFailed(_) => LbErrKind::ServerUnreachable,
137 ApiError::ClientUpdateRequired => LbErrKind::ClientUpdateRequired,
138 _ => core_err_unexpected(err),
139 })?;
140
141 self.refresh_usage().await.log_and_ignore();
142
143 Ok(())
144 }
145
146 #[instrument(level = "debug", skip(self), err(Debug))]
147 pub async fn cancel_subscription(&self) -> LbResult<()> {
148 let account = self.get_account()?;
149
150 self.client
151 .cancel_subscription(account, CancelSubscriptionRequest {})
152 .await
153 .map_err(|err| match err {
154 ApiError::Endpoint(CancelSubscriptionError::NotPremium) => LbErrKind::NotPremium,
155 ApiError::Endpoint(CancelSubscriptionError::AlreadyCanceled) => {
156 LbErrKind::AlreadyCanceled
157 }
158 ApiError::Endpoint(CancelSubscriptionError::UsageIsOverFreeTierDataCap) => {
159 LbErrKind::UsageIsOverFreeTierDataCap
160 }
161 ApiError::Endpoint(CancelSubscriptionError::ExistingRequestPending) => {
162 LbErrKind::ExistingRequestPending
163 }
164 ApiError::Endpoint(CancelSubscriptionError::CannotCancelForAppStore) => {
165 LbErrKind::CannotCancelSubscriptionForAppStore
166 }
167 ApiError::Endpoint(CancelSubscriptionError::NotPermissioned) => {
168 LbErrKind::InsufficientPermission
169 }
170 ApiError::SendFailed(_) => LbErrKind::ServerUnreachable,
171 ApiError::ClientUpdateRequired => LbErrKind::ClientUpdateRequired,
172 _ => core_err_unexpected(err),
173 })?;
174
175 self.refresh_usage().await.log_and_ignore();
176
177 Ok(())
178 }
179
180 #[instrument(level = "debug", skip(self), err(Debug))]
181 pub async fn get_subscription_info(&self) -> LbResult<Option<SubscriptionInfo>> {
182 let account = self.get_account()?;
183
184 let response = self
185 .client
186 .get_subscription_info_v2(account, GetSubscriptionInfoRequestV2 {})
187 .await
188 .map_err(|err| match err {
189 ApiError::Endpoint(GetSubscriptionInfoError::NotPermissioned) => {
190 LbErrKind::InsufficientPermission
191 }
192 ApiError::SendFailed(_) => LbErrKind::ServerUnreachable,
193 ApiError::ClientUpdateRequired => LbErrKind::ClientUpdateRequired,
194 _ => core_err_unexpected(err),
195 })?;
196 Ok(response.subscription_info.map(Into::into))
197 }
198}