Skip to main content

storekit/
app_store.rs

1use core::ptr;
2
3use crate::error::StoreKitError;
4use crate::ffi;
5use crate::private::{error_from_status, take_string};
6
7#[derive(Debug, Clone, PartialEq, Eq)]
8/// Wraps `StoreKit.AppStore.Environment`.
9pub enum AppStoreEnvironment {
10    /// Represents the `Production` `StoreKit` case.
11    Production,
12    /// Represents the `Sandbox` `StoreKit` case.
13    Sandbox,
14    /// Represents the `Xcode` `StoreKit` case.
15    Xcode,
16    /// Preserves an unrecognized `StoreKit` case.
17    Unknown(String),
18}
19
20impl AppStoreEnvironment {
21    /// Returns the raw `StoreKit` string for this App Store environment.
22    pub fn as_str(&self) -> &str {
23        match self {
24            Self::Production => "production",
25            Self::Sandbox => "sandbox",
26            Self::Xcode => "xcode",
27            Self::Unknown(value) => value.as_str(),
28        }
29    }
30
31    pub(crate) fn from_raw(raw: String) -> Self {
32        match raw.as_str() {
33            "production" => Self::Production,
34            "sandbox" => Self::Sandbox,
35            "xcode" => Self::Xcode,
36            _ => Self::Unknown(raw),
37        }
38    }
39}
40
41#[derive(Debug, Clone, PartialEq, Eq)]
42/// Wraps the platform value associated with `StoreKit.AppTransaction`.
43pub enum AppStorePlatform {
44    /// Represents the `IOS` `StoreKit` case.
45    IOS,
46    /// Represents the `MacOS` `StoreKit` case.
47    MacOS,
48    /// Represents the `TvOS` `StoreKit` case.
49    TvOS,
50    /// Represents the `VisionOS` `StoreKit` case.
51    VisionOS,
52    /// Preserves an unrecognized `StoreKit` case.
53    Unknown(String),
54}
55
56impl AppStorePlatform {
57    /// Returns the raw `StoreKit` string for this App Store platform.
58    pub fn as_str(&self) -> &str {
59        match self {
60            Self::IOS => "iOS",
61            Self::MacOS => "macOS",
62            Self::TvOS => "tvOS",
63            Self::VisionOS => "visionOS",
64            Self::Unknown(value) => value.as_str(),
65        }
66    }
67
68    pub(crate) fn from_raw(raw: String) -> Self {
69        match raw.as_str() {
70            "iOS" => Self::IOS,
71            "macOS" => Self::MacOS,
72            "tvOS" => Self::TvOS,
73            "visionOS" => Self::VisionOS,
74            _ => Self::Unknown(raw),
75        }
76    }
77}
78
79/// Helpers backed by `StoreKit.AppStore`.
80#[derive(Debug, Clone, Copy, Default)]
81pub struct AppStore;
82
83impl AppStore {
84    /// Calls `StoreKit.AppStore.canMakePayments`.
85    pub fn can_make_payments() -> Result<bool, StoreKitError> {
86        let mut raw_value = 0;
87        let mut error_message = ptr::null_mut();
88        let status = unsafe {
89            ffi::sk_app_store_can_make_payments(&raw mut raw_value, &raw mut error_message)
90        };
91        if status == ffi::status::OK {
92            Ok(raw_value != 0)
93        } else {
94            Err(unsafe { error_from_status(status, error_message) })
95        }
96    }
97
98    /// Calls the `StoreKit` device verification identifier API.
99    pub fn device_verification_id() -> Result<Option<String>, StoreKitError> {
100        let mut uuid_ptr = ptr::null_mut();
101        let mut error_message = ptr::null_mut();
102        let status = unsafe {
103            ffi::sk_app_store_device_verification_id(&raw mut uuid_ptr, &raw mut error_message)
104        };
105        if status == ffi::status::OK {
106            Ok(unsafe { take_string(uuid_ptr) })
107        } else {
108            Err(unsafe { error_from_status(status, error_message) })
109        }
110    }
111
112    /// Calls `StoreKit.AppStore.sync()`.
113    pub fn sync() -> Result<(), StoreKitError> {
114        let mut error_message = ptr::null_mut();
115        let status = unsafe { ffi::sk_app_store_sync(&raw mut error_message) };
116        if status == ffi::status::OK {
117            Ok(())
118        } else {
119            Err(unsafe { error_from_status(status, error_message) })
120        }
121    }
122
123    /// Calls the `StoreKit` manage-subscriptions API.
124    pub fn show_manage_subscriptions() -> Result<(), StoreKitError> {
125        let mut error_message = ptr::null_mut();
126        let status = unsafe { ffi::sk_app_store_show_manage_subscriptions(&raw mut error_message) };
127        if status == ffi::status::OK {
128            Ok(())
129        } else {
130            Err(unsafe { error_from_status(status, error_message) })
131        }
132    }
133
134    /// Calls the `StoreKit` review-request API.
135    pub fn request_review() -> Result<(), StoreKitError> {
136        let mut error_message = ptr::null_mut();
137        let status = unsafe { ffi::sk_app_store_request_review(&raw mut error_message) };
138        if status == ffi::status::OK {
139            Ok(())
140        } else {
141            Err(unsafe { error_from_status(status, error_message) })
142        }
143    }
144
145    /// Calls `StoreKit.AppStore.presentOfferCodeRedeemSheet()`.
146    pub fn present_offer_code_redeem_sheet() -> Result<(), StoreKitError> {
147        let mut error_message = ptr::null_mut();
148        let status =
149            unsafe { ffi::sk_app_store_present_offer_code_redeem_sheet(&raw mut error_message) };
150        if status == ffi::status::OK {
151            Ok(())
152        } else {
153            Err(unsafe { error_from_status(status, error_message) })
154        }
155    }
156}