storekit/
store_context.rs1use 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)]
14pub struct StoreContext {
16 pub bundle_identifier: Option<String>,
18 pub bundle_name: Option<String>,
20 pub bundle_version: Option<String>,
22 pub receipt_url: Option<String>,
24 pub can_make_payments: bool,
26 pub device_verification_id: Option<String>,
28 pub is_bundled: bool,
30 pub executable_path: Option<String>,
32}
33
34impl StoreContext {
35 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 pub fn can_make_payments() -> Result<bool, StoreKitError> {
52 AppStore::can_make_payments()
53 }
54
55 pub fn device_verification_id() -> Result<Option<String>, StoreKitError> {
57 AppStore::device_verification_id()
58 }
59
60 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 pub fn current_entitlements() -> Result<TransactionStream, StoreKitError> {
71 Transaction::current_entitlements()
72 }
73
74 pub fn transaction_updates() -> Result<TransactionStream, StoreKitError> {
76 Transaction::updates()
77 }
78
79 pub fn current_storefront() -> Result<Option<Storefront>, StoreKitError> {
81 Storefront::current()
82 }
83
84 pub fn app_transaction() -> Result<VerificationResult<AppTransaction>, StoreKitError> {
86 AppTransaction::shared()
87 }
88
89 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}