cranpose_services/purchases.rs
1//! In-app purchases: products, prices and owned entitlements.
2//!
3//! The shape is the one every mobile store agrees on — ask for a set of
4//! product ids, get back localized prices, start a purchase, and be told what
5//! the account owns — with the store-specific parts (StoreKit, Play Billing)
6//! living in platform backends installed via [`set_platform_purchases`].
7//!
8//! **The default backend reports [`StorePhase::Unavailable`] and owns
9//! nothing.** It deliberately does *not* grant entitlements: a desktop build
10//! with no store must not silently unlock paid features because a backend
11//! failed to register. An app that ships free on storeless platforms decides
12//! that itself, e.g.
13//!
14//! ```no_run
15//! # use cranpose_services::purchases::{store_state, StorePhase};
16//! let unlocked = match store_state().phase {
17//! // No store on this platform — this app is free there.
18//! StorePhase::Unavailable => true,
19//! _ => store_state().owns("com.example.pro"),
20//! };
21//! ```
22//!
23//! # Reading state
24//!
25//! [`store_state`] is a cheap snapshot, safe to call every frame: backends
26//! keep the state and hand out a clone. State changes arrive asynchronously
27//! (the store answers over the network, another device restores a purchase,
28//! a parent approves an Ask-to-Buy request), so read it from the frame loop
29//! rather than expecting a reply to [`purchase`].
30//!
31//! [`take_event`] drains one-shot events — the things a snapshot cannot
32//! express, like "the user cancelled" — for showing a message once.
33
34use std::cell::RefCell;
35use std::collections::BTreeSet;
36use std::rc::Rc;
37
38/// A product as the store describes it, in the user's locale and currency.
39#[derive(Clone, Debug, PartialEq, Eq)]
40pub struct Product {
41 /// Store product identifier, as configured in App Store Connect or the
42 /// Play Console.
43 pub id: String,
44 /// Price formatted by the store for the user's storefront — "$34.99",
45 /// "34,99 €", "¥5,000". **Always display this string**; never format a
46 /// price yourself, and never hard-code one. Stores localize currency,
47 /// separators and placement, and they apply regional price tiers.
48 pub display_price: String,
49 /// Display name configured in the store.
50 pub title: String,
51 /// Description configured in the store.
52 pub description: String,
53}
54
55/// How far along the store connection is.
56#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
57pub enum StorePhase {
58 /// No store on this platform, or no backend installed. Nothing is owned
59 /// and nothing can be bought.
60 #[default]
61 Unavailable,
62 /// A backend is installed and still talking to the store. Prices are not
63 /// known yet; owned entitlements may not be known yet either.
64 Connecting,
65 /// Product and entitlement information has been received at least once.
66 Ready,
67}
68
69/// Snapshot of everything known about the store right now.
70#[derive(Clone, Debug, Default, PartialEq, Eq)]
71pub struct StoreState {
72 /// How far along the connection is.
73 pub phase: StorePhase,
74 /// Products the backend was configured with and the store answered for.
75 /// A configured product missing here is one the store does not know —
76 /// usually a typo in the id, or a product not yet approved.
77 pub products: Vec<Product>,
78 /// Product ids the account currently owns. For non-consumables and
79 /// subscriptions this is the entitlement; consumables never appear.
80 pub owned: BTreeSet<String>,
81 /// Last error reported by the store, for diagnostics. A store being
82 /// briefly unreachable is normal and not worth showing to the user.
83 pub error: Option<String>,
84 /// True while a purchase or restore the user asked for is still running,
85 /// so the UI can disable the buy button and show a spinner.
86 pub busy: bool,
87}
88
89impl StoreState {
90 /// Whether `product_id` is currently owned.
91 pub fn owns(&self, product_id: &str) -> bool {
92 self.owned.contains(product_id)
93 }
94
95 /// The product with `product_id`, if the store answered for it.
96 pub fn product(&self, product_id: &str) -> Option<&Product> {
97 self.products.iter().find(|p| p.id == product_id)
98 }
99
100 /// The localized price of `product_id`, if known.
101 pub fn display_price(&self, product_id: &str) -> Option<&str> {
102 self.product(product_id).map(|p| p.display_price.as_str())
103 }
104}
105
106/// A one-shot thing that happened, which a snapshot cannot express.
107#[derive(Clone, Debug, PartialEq, Eq)]
108pub enum PurchaseEvent {
109 /// The purchase completed and the entitlement is in [`StoreState::owned`].
110 Purchased(String),
111 /// The user dismissed the payment sheet. Not an error; say nothing.
112 Cancelled,
113 /// The purchase needs someone else to finish it — Ask to Buy, or a
114 /// bank-side confirmation. It may complete minutes or days later, so tell
115 /// the user it is pending rather than that it failed.
116 Pending,
117 /// The purchase failed. The string is for the user.
118 Failed(String),
119 /// A restore finished. `restored` is how many entitlements it found —
120 /// zero means "nothing to restore on this account", which is worth
121 /// saying, because the user asked.
122 Restored {
123 /// Number of owned entitlements the restore turned up.
124 restored: usize,
125 },
126}
127
128/// A store backend.
129///
130/// Implementations are installed with [`set_platform_purchases`] and must be
131/// non-blocking: every method returns immediately and reports back by
132/// updating the snapshot returned from [`Purchases::state`].
133pub trait Purchases {
134 /// Declare the product ids this app sells and start talking to the store.
135 /// Called again on relaunch; backends should treat it as idempotent.
136 fn configure(&self, product_ids: &[&str]);
137
138 /// The current snapshot. Called every frame — keep it cheap.
139 fn state(&self) -> StoreState;
140
141 /// Begin a purchase. Presents the store's own payment sheet.
142 fn purchase(&self, product_id: &str);
143
144 /// Re-query what the account owns. Stores restore silently at launch, so
145 /// this is for the explicit "Restore purchases" button that Apple
146 /// requires a paid app to provide.
147 fn restore(&self);
148
149 /// Take the next pending one-shot event, if any.
150 fn take_event(&self) -> Option<PurchaseEvent>;
151}
152
153/// Shared handle to the active [`Purchases`] backend.
154pub type PurchasesRef = Rc<dyn Purchases>;
155
156/// The no-store backend: nothing is for sale and nothing is owned.
157struct NoPurchases;
158
159impl Purchases for NoPurchases {
160 fn configure(&self, _product_ids: &[&str]) {}
161
162 fn state(&self) -> StoreState {
163 StoreState::default()
164 }
165
166 fn purchase(&self, _product_id: &str) {}
167
168 fn restore(&self) {}
169
170 fn take_event(&self) -> Option<PurchaseEvent> {
171 None
172 }
173}
174
175thread_local! {
176 static PLATFORM_PURCHASES: RefCell<Option<PurchasesRef>> = const { RefCell::new(None) };
177}
178
179/// Installs a platform purchase backend, replacing any previous one.
180pub fn set_platform_purchases(purchases: PurchasesRef) {
181 PLATFORM_PURCHASES.with(|cell| *cell.borrow_mut() = Some(purchases));
182}
183
184/// Removes any registered purchase backend (tests and teardown).
185pub fn clear_platform_purchases() {
186 PLATFORM_PURCHASES.with(|cell| *cell.borrow_mut() = None);
187}
188
189/// The active backend: the platform one if installed, else the no-store
190/// backend.
191pub fn purchases() -> PurchasesRef {
192 PLATFORM_PURCHASES
193 .with(|cell| cell.borrow().clone())
194 .unwrap_or_else(|| Rc::new(NoPurchases))
195}
196
197/// Whether a real store backend is installed on this platform.
198pub fn store_available() -> bool {
199 PLATFORM_PURCHASES.with(|cell| cell.borrow().is_some())
200}
201
202/// Convenience: declare the products this app sells and connect to the store.
203pub fn configure(product_ids: &[&str]) {
204 purchases().configure(product_ids);
205}
206
207/// Convenience: the current store snapshot.
208pub fn store_state() -> StoreState {
209 purchases().state()
210}
211
212/// Convenience: begin a purchase.
213pub fn purchase(product_id: &str) {
214 purchases().purchase(product_id);
215}
216
217/// Convenience: re-query owned entitlements.
218pub fn restore() {
219 purchases().restore();
220}
221
222/// Convenience: take the next one-shot purchase event.
223pub fn take_event() -> Option<PurchaseEvent> {
224 purchases().take_event()
225}
226
227#[cfg(test)]
228mod tests {
229 use super::*;
230
231 #[test]
232 fn default_backend_sells_nothing_and_owns_nothing() {
233 clear_platform_purchases();
234 let state = store_state();
235 assert_eq!(state.phase, StorePhase::Unavailable);
236 assert!(state.owned.is_empty());
237 assert!(!state.owns("com.example.pro"));
238 assert!(!store_available());
239 // Calling through with no backend must not panic.
240 configure(&["com.example.pro"]);
241 purchase("com.example.pro");
242 restore();
243 assert_eq!(take_event(), None);
244 }
245
246 #[test]
247 fn installed_backend_answers_prices_and_ownership() {
248 struct Fake;
249 impl Purchases for Fake {
250 fn configure(&self, _product_ids: &[&str]) {}
251 fn state(&self) -> StoreState {
252 StoreState {
253 phase: StorePhase::Ready,
254 products: vec![Product {
255 id: "com.example.pro".into(),
256 display_price: "34,99 €".into(),
257 title: "Pro".into(),
258 description: "Everything unlocked".into(),
259 }],
260 owned: BTreeSet::from(["com.example.pro".to_string()]),
261 error: None,
262 busy: false,
263 }
264 }
265 fn purchase(&self, _product_id: &str) {}
266 fn restore(&self) {}
267 fn take_event(&self) -> Option<PurchaseEvent> {
268 Some(PurchaseEvent::Purchased("com.example.pro".into()))
269 }
270 }
271 set_platform_purchases(Rc::new(Fake));
272 let state = store_state();
273 assert_eq!(state.phase, StorePhase::Ready);
274 assert!(state.owns("com.example.pro"));
275 assert_eq!(state.display_price("com.example.pro"), Some("34,99 €"));
276 assert_eq!(state.display_price("com.example.nope"), None);
277 assert!(store_available());
278 assert_eq!(
279 take_event(),
280 Some(PurchaseEvent::Purchased("com.example.pro".into()))
281 );
282 clear_platform_purchases();
283 }
284}