Skip to main content

shopify_client/services/subscription/
mod.rs

1pub mod remote;
2
3use std::sync::Arc;
4
5use crate::{
6    common::types::{APIError, RequestCallbacks},
7    types::subscription::{
8        ActiveSubscriptionsResp, CancelSubscriptionResp, CreateCombinedSubscriptionRequest,
9        CreateRecurringSubscriptionRequest, CreateSubscriptionResp, CreateUsageRecordRequest,
10        CreateUsageRecordResp, CreateUsageSubscriptionRequest, ExtendTrialResp, MoneyInput,
11        UpdateCappedAmountResp,
12    },
13};
14
15pub struct Subscription {
16    pub shop_url: Arc<String>,
17    pub version: Arc<String>,
18    pub access_token: Arc<String>,
19    pub callbacks: Arc<RequestCallbacks>,
20}
21
22impl Subscription {
23    pub fn new(
24        shop_url: Arc<String>,
25        version: Arc<String>,
26        access_token: Arc<String>,
27        callbacks: Arc<RequestCallbacks>,
28    ) -> Self {
29        Subscription {
30            shop_url,
31            version,
32            access_token,
33            callbacks,
34        }
35    }
36
37    pub async fn create_recurring(
38        &self,
39        request: &CreateRecurringSubscriptionRequest,
40    ) -> Result<CreateSubscriptionResp, APIError> {
41        remote::create_recurring_subscription(
42            &self.shop_url,
43            &self.version,
44            &self.access_token,
45            &self.callbacks,
46            request,
47        )
48        .await
49    }
50
51    pub async fn create_usage(
52        &self,
53        request: &CreateUsageSubscriptionRequest,
54    ) -> Result<CreateSubscriptionResp, APIError> {
55        remote::create_usage_subscription(
56            &self.shop_url,
57            &self.version,
58            &self.access_token,
59            &self.callbacks,
60            request,
61        )
62        .await
63    }
64
65    pub async fn create_combined(
66        &self,
67        request: &CreateCombinedSubscriptionRequest,
68    ) -> Result<CreateSubscriptionResp, APIError> {
69        remote::create_combined_subscription(
70            &self.shop_url,
71            &self.version,
72            &self.access_token,
73            &self.callbacks,
74            request,
75        )
76        .await
77    }
78
79    pub async fn cancel(
80        &self,
81        subscription_id: &String,
82        prorate: bool,
83    ) -> Result<CancelSubscriptionResp, APIError> {
84        remote::cancel_subscription(
85            &self.shop_url,
86            &self.version,
87            &self.access_token,
88            &self.callbacks,
89            subscription_id,
90            prorate,
91        )
92        .await
93    }
94
95    pub async fn extend_trial(
96        &self,
97        subscription_id: &String,
98        days: i32,
99    ) -> Result<ExtendTrialResp, APIError> {
100        remote::extend_trial(
101            &self.shop_url,
102            &self.version,
103            &self.access_token,
104            &self.callbacks,
105            subscription_id,
106            days,
107        )
108        .await
109    }
110
111    pub async fn update_capped_amount(
112        &self,
113        line_item_id: &String,
114        capped_amount: &MoneyInput,
115    ) -> Result<UpdateCappedAmountResp, APIError> {
116        remote::update_capped_amount(
117            &self.shop_url,
118            &self.version,
119            &self.access_token,
120            &self.callbacks,
121            line_item_id,
122            capped_amount,
123        )
124        .await
125    }
126
127    pub async fn create_usage_record(
128        &self,
129        request: &CreateUsageRecordRequest,
130    ) -> Result<CreateUsageRecordResp, APIError> {
131        remote::create_usage_record(
132            &self.shop_url,
133            &self.version,
134            &self.access_token,
135            &self.callbacks,
136            request,
137        )
138        .await
139    }
140
141    pub async fn get_active_subscriptions(&self) -> Result<ActiveSubscriptionsResp, APIError> {
142        remote::get_active_subscriptions(
143            &self.shop_url,
144            &self.version,
145            &self.access_token,
146            &self.callbacks,
147        )
148        .await
149    }
150}