Skip to main content

storekit/
store_context.rs

1use serde::Deserialize;
2
3use crate::app_store::AppStore;
4use crate::app_transaction::AppTransaction;
5use crate::error::StoreKitError;
6use crate::ffi;
7use crate::product::Product;
8use crate::receipt_validator::{AppReceipt, ReceiptValidator};
9use crate::storefront::Storefront;
10use crate::transaction::{Transaction, TransactionStream};
11use crate::verification_result::VerificationResult;
12
13#[derive(Debug, Clone, PartialEq, Eq)]
14/// Collects bundle context used alongside `StoreKit` APIs.
15pub struct StoreContext {
16    /// Bundle identifier reported by `StoreKit`.
17    pub bundle_identifier: Option<String>,
18    /// Bundle display name reported by `StoreKit`.
19    pub bundle_name: Option<String>,
20    /// Bundle version reported by `StoreKit`.
21    pub bundle_version: Option<String>,
22    /// Receipt URL reported by `StoreKit`.
23    pub receipt_url: Option<String>,
24    /// Whether `StoreKit` reports that payments can be made.
25    pub can_make_payments: bool,
26    /// Device verification identifier reported by `StoreKit`.
27    pub device_verification_id: Option<String>,
28    /// Whether `StoreKit` reported that the app is bundled.
29    pub is_bundled: bool,
30    /// Executable path reported by `StoreKit`.
31    pub executable_path: Option<String>,
32}
33
34impl StoreContext {
35    /// Fetches the current bundle context used alongside `StoreKit` APIs.
36    pub fn current() -> Result<Self, StoreKitError> {
37        let mut context_json = core::ptr::null_mut();
38        let mut error_message = core::ptr::null_mut();
39        let status =
40            unsafe { ffi::sk_store_context_json(&raw mut context_json, &raw mut error_message) };
41        if status != ffi::status::OK {
42            return Err(unsafe { crate::private::error_from_status(status, error_message) });
43        }
44        let payload = unsafe {
45            crate::private::parse_json_ptr::<StoreContextPayload>(context_json, "store context")
46        }?;
47        Ok(payload.into_store_context())
48    }
49
50    /// Returns whether `StoreKit` reports that payments can be made.
51    pub fn can_make_payments() -> Result<bool, StoreKitError> {
52        AppStore::can_make_payments()
53    }
54
55    /// Fetches the `StoreKit` device verification identifier.
56    pub fn device_verification_id() -> Result<Option<String>, StoreKitError> {
57        AppStore::device_verification_id()
58    }
59
60    /// Fetches current `StoreKit` products for the supplied identifiers.
61    pub fn current_products<I, S>(identifiers: I) -> Result<Vec<Product>, StoreKitError>
62    where
63        I: IntoIterator<Item = S>,
64        S: AsRef<str>,
65    {
66        Product::products_for(identifiers)
67    }
68
69    /// Creates a stream of current `StoreKit` entitlements.
70    pub fn current_entitlements() -> Result<TransactionStream, StoreKitError> {
71        Transaction::current_entitlements()
72    }
73
74    /// Creates a stream of `StoreKit` transaction updates.
75    pub fn transaction_updates() -> Result<TransactionStream, StoreKitError> {
76        Transaction::updates()
77    }
78
79    /// Fetches the current `StoreKit` storefront.
80    pub fn current_storefront() -> Result<Option<Storefront>, StoreKitError> {
81        Storefront::current()
82    }
83
84    /// Fetches the shared `StoreKit` app transaction.
85    pub fn app_transaction() -> Result<VerificationResult<AppTransaction>, StoreKitError> {
86        AppTransaction::shared()
87    }
88
89    /// Fetches the current app receipt used by `StoreKit`.
90    pub fn receipt() -> Result<Option<AppReceipt>, StoreKitError> {
91        ReceiptValidator::current_receipt()
92    }
93}
94
95#[derive(Debug, Deserialize)]
96struct StoreContextPayload {
97    #[serde(rename = "bundleIdentifier")]
98    bundle_identifier: Option<String>,
99    #[serde(rename = "bundleName")]
100    bundle_name: Option<String>,
101    #[serde(rename = "bundleVersion")]
102    bundle_version: Option<String>,
103    #[serde(rename = "receiptURL")]
104    receipt_url: Option<String>,
105    #[serde(rename = "canMakePayments")]
106    can_make_payments: bool,
107    #[serde(rename = "deviceVerificationID")]
108    device_verification_id: Option<String>,
109    #[serde(rename = "isBundled")]
110    is_bundled: bool,
111    #[serde(rename = "executablePath")]
112    executable_path: Option<String>,
113}
114
115impl StoreContextPayload {
116    fn into_store_context(self) -> StoreContext {
117        StoreContext {
118            bundle_identifier: self.bundle_identifier,
119            bundle_name: self.bundle_name,
120            bundle_version: self.bundle_version,
121            receipt_url: self.receipt_url,
122            can_make_payments: self.can_make_payments,
123            device_verification_id: self.device_verification_id,
124            is_bundled: self.is_bundled,
125            executable_path: self.executable_path,
126        }
127    }
128}