cranpose-storekit 0.1.103

StoreKit 2 in-app purchases for Cranpose (iOS/macOS)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//! The Apple half: FFI to `swift/storekit.swift` plus the
//! [`cranpose_services::purchases::Purchases`] implementation.
//!
//! The reviewed FFI boundary for this crate — the crate root denies unsafe
//! code and this is the only module that opts back in.
#![allow(unsafe_code)]

use std::{
    collections::{BTreeMap, BTreeSet, VecDeque},
    ffi::{CStr, CString, c_char, c_void},
    sync::{Arc, Mutex},
};

use cranpose_services::purchases::{
    Product, PurchaseEvent, Purchases, StorePhase, StoreState, set_platform_purchases,
};

const KIND_BEGIN: i32 = 0;
const KIND_PRODUCT: i32 = 1;
const KIND_OWNED: i32 = 2;
const KIND_PHASE: i32 = 3;
const KIND_EVENT: i32 = 4;
const KIND_BUSY: i32 = 5;

const PHASE_UNAVAILABLE: i32 = 0;
const PHASE_CONNECTING: i32 = 1;
const PHASE_READY: i32 = 2;
const PHASE_BLOCKED: i32 = 3;

const EVENT_PURCHASED: i32 = 0;
const EVENT_CANCELLED: i32 = 1;
const EVENT_PENDING: i32 = 2;
const EVENT_FAILED: i32 = 3;
const EVENT_RESTORED: i32 = 4;

/// `(ctx, kind, arg0, arg1, a, b, c, d)` — see `swift/storekit.swift`.
type StoreCallback = unsafe extern "C" fn(
    *mut c_void,
    i32,
    i32,
    i32,
    *const c_char,
    *const c_char,
    *const c_char,
    *const c_char,
);

unsafe extern "C" {
    fn cranpose_storekit_start(product_ids: *const c_char, ctx: *mut c_void, cb: StoreCallback);
    fn cranpose_storekit_is_connected() -> bool;
    fn cranpose_storekit_purchase(product_id: *const c_char);
    fn cranpose_storekit_restore();
}

/// Everything the Swift side has told us.
///
/// A snapshot arrives as `begin`, a run of `product`/`owned` rows, then
/// `phase`, which swaps the staged rows into `live` in one step — so a reader
/// never observes a half-built product list.
struct Shared {
    staging: bool,
    staged_products: Vec<Product>,
    staged_owned: BTreeSet<String>,
    staged_orders: BTreeMap<String, String>,
    live: StoreState,
    events: VecDeque<PurchaseEvent>,
}

impl Shared {
    const fn new() -> Self {
        Self {
            staging: false,
            staged_products: Vec::new(),
            staged_owned: BTreeSet::new(),
            staged_orders: BTreeMap::new(),
            live: StoreState {
                phase: StorePhase::Unavailable,
                products: Vec::new(),
                owned: BTreeSet::new(),
                orders: BTreeMap::new(),
                error: None,
                busy: false,
            },
            events: VecDeque::new(),
        }
    }
}

static SHARED: Mutex<Shared> = Mutex::new(Shared::new());

/// A poisoned mutex would mean a panic inside the callback; the state is
/// plain data, so recovering and carrying on is strictly better for the user
/// than propagating the panic through a StoreKit executor.
fn shared() -> std::sync::MutexGuard<'static, Shared> {
    SHARED.lock().unwrap_or_else(|e| e.into_inner())
}

/// Borrow a C string as an owned `String`. `None` for null.
///
/// # Safety
/// `ptr` is either null or a NUL-terminated string valid for this call, which
/// is what the Swift side guarantees (its buffers live for the callback's
/// duration only, so the copy here is required, not an optimization).
unsafe fn take(ptr: *const c_char) -> Option<String> {
    unsafe {
        if ptr.is_null() {
            return None;
        }
        Some(CStr::from_ptr(ptr).to_string_lossy().into_owned())
    }
}

/// The one callback the Swift side pushes everything through.
unsafe extern "C" fn on_message(
    _ctx: *mut c_void,
    kind: i32,
    arg0: i32,
    arg1: i32,
    a: *const c_char,
    b: *const c_char,
    c: *const c_char,
    d: *const c_char,
) {
    unsafe {
        let mut state = shared();
        match kind {
            KIND_BEGIN => {
                state.staging = true;
                state.staged_products.clear();
                state.staged_owned.clear();
                state.staged_orders.clear();
            }
            KIND_PRODUCT => {
                let (Some(id), Some(display_price)) = (take(a), take(b)) else {
                    return;
                };
                state.staged_products.push(Product {
                    id,
                    display_price,
                    title: take(c).unwrap_or_default(),
                    description: take(d).unwrap_or_default(),
                });
            }
            KIND_OWNED => {
                if let Some(id) = take(a) {
                    if let Some(order) = take(b) {
                        state.staged_orders.insert(id.clone(), order);
                    }
                    state.staged_owned.insert(id);
                }
            }
            KIND_PHASE => {
                if state.staging {
                    state.live.products = std::mem::take(&mut state.staged_products);
                    state.live.owned = std::mem::take(&mut state.staged_owned);
                    state.live.orders = std::mem::take(&mut state.staged_orders);
                    state.staging = false;
                }
                state.live.phase = match arg0 {
                    PHASE_READY => StorePhase::Ready,
                    PHASE_CONNECTING => StorePhase::Connecting,
                    PHASE_BLOCKED => StorePhase::Blocked,
                    PHASE_UNAVAILABLE => StorePhase::Unavailable,
                    _ => StorePhase::Unavailable,
                };
                state.live.error = take(a);
            }
            KIND_BUSY => state.live.busy = arg0 != 0,
            KIND_EVENT => {
                let event = match arg0 {
                    EVENT_PURCHASED => PurchaseEvent::Purchased(take(a).unwrap_or_default()),
                    EVENT_CANCELLED => PurchaseEvent::Cancelled,
                    EVENT_PENDING => PurchaseEvent::Pending,
                    EVENT_FAILED => PurchaseEvent::Failed(
                        take(a)
                            .unwrap_or_else(|| "The purchase could not be completed".to_string()),
                    ),
                    EVENT_RESTORED => PurchaseEvent::Restored {
                        restored: arg1.max(0) as usize,
                    },
                    _ => return,
                };
                if state.events.len() >= 32 {
                    state.events.pop_front();
                }
                state.events.push_back(event);
                drop(state);
                cranpose_services::note_store_news();
                return;
            }
            _ => {}
        }
        drop(state);
        cranpose_services::note_store_news();
    }
}

/// The App Store backend. Install it with [`register`].
pub struct StoreKitPurchases;

static STOREKIT_PRODUCT_IDS: Mutex<Vec<String>> = Mutex::new(Vec::new());

impl Purchases for StoreKitPurchases {
    fn configure(&self, product_ids: &[&str]) {
        *STOREKIT_PRODUCT_IDS
            .lock()
            .unwrap_or_else(|error| error.into_inner()) =
            product_ids.iter().map(|id| (*id).to_owned()).collect();
        let joined = product_ids.join("\n");
        let Ok(joined) = CString::new(joined) else {
            return;
        };
        unsafe {
            cranpose_storekit_start(joined.as_ptr(), std::ptr::null_mut(), on_message);
        }
    }

    fn state(&self) -> StoreState {
        shared().live.clone()
    }

    fn purchase(&self, product_id: &str) {
        let Ok(id) = CString::new(product_id) else {
            return;
        };
        unsafe { cranpose_storekit_purchase(id.as_ptr()) }
    }

    fn restore(&self) {
        unsafe { cranpose_storekit_restore() }
    }

    fn take_event(&self) -> Option<PurchaseEvent> {
        shared().events.pop_front()
    }

    fn is_connected(&self) -> bool {
        unsafe { cranpose_storekit_is_connected() }
    }

    fn reconnect(&self) {
        let ids = STOREKIT_PRODUCT_IDS
            .lock()
            .unwrap_or_else(|error| error.into_inner())
            .clone();
        let refs = ids.iter().map(String::as_str).collect::<Vec<_>>();
        self.configure(&refs);
    }
}

/// Installs StoreKit as the platform purchase backend.
pub fn register() {
    set_platform_purchases(Arc::new(StoreKitPurchases));
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Drive the callback exactly as Swift does and assert the snapshot is
    /// committed atomically, with prices surviving a bare phase update.
    #[test]
    fn snapshot_commits_on_phase_and_survives_a_bare_ping() {
        let id = CString::new("com.example.pro").unwrap();
        let price = CString::new("34,99 €").unwrap();
        let title = CString::new("Pro").unwrap();
        let body = CString::new("Everything unlocked").unwrap();
        let null = std::ptr::null();

        unsafe {
            on_message(
                std::ptr::null_mut(),
                KIND_BEGIN,
                0,
                0,
                null,
                null,
                null,
                null,
            );
            on_message(
                std::ptr::null_mut(),
                KIND_PRODUCT,
                0,
                0,
                id.as_ptr(),
                price.as_ptr(),
                title.as_ptr(),
                body.as_ptr(),
            );
            assert!(StoreKitPurchases.state().products.is_empty());
            on_message(
                std::ptr::null_mut(),
                KIND_OWNED,
                0,
                0,
                id.as_ptr(),
                null,
                null,
                null,
            );
            on_message(
                std::ptr::null_mut(),
                KIND_PHASE,
                PHASE_READY,
                0,
                null,
                null,
                null,
                null,
            );
        }

        let state = StoreKitPurchases.state();
        assert_eq!(state.phase, StorePhase::Ready);
        assert_eq!(state.display_price("com.example.pro"), Some("34,99 €"));
        assert!(state.owns("com.example.pro"));

        unsafe {
            on_message(
                std::ptr::null_mut(),
                KIND_PHASE,
                PHASE_CONNECTING,
                0,
                null,
                null,
                null,
                null,
            );
        }
        let state = StoreKitPurchases.state();
        assert_eq!(state.phase, StorePhase::Connecting);
        assert_eq!(state.display_price("com.example.pro"), Some("34,99 €"));
        assert!(state.owns("com.example.pro"));
    }

    #[test]
    fn events_queue_and_drain_in_order_and_are_bounded() {
        while StoreKitPurchases.take_event().is_some() {}
        let msg = CString::new("card declined").unwrap();
        let null = std::ptr::null();
        unsafe {
            on_message(
                std::ptr::null_mut(),
                KIND_EVENT,
                EVENT_CANCELLED,
                0,
                null,
                null,
                null,
                null,
            );
            on_message(
                std::ptr::null_mut(),
                KIND_EVENT,
                EVENT_FAILED,
                0,
                msg.as_ptr(),
                null,
                null,
                null,
            );
            on_message(
                std::ptr::null_mut(),
                KIND_EVENT,
                EVENT_RESTORED,
                3,
                null,
                null,
                null,
                null,
            );
        }
        assert_eq!(
            StoreKitPurchases.take_event(),
            Some(PurchaseEvent::Cancelled)
        );
        assert_eq!(
            StoreKitPurchases.take_event(),
            Some(PurchaseEvent::Failed("card declined".into()))
        );
        assert_eq!(
            StoreKitPurchases.take_event(),
            Some(PurchaseEvent::Restored { restored: 3 })
        );
        assert_eq!(StoreKitPurchases.take_event(), None);

        for _ in 0..100 {
            unsafe {
                on_message(
                    std::ptr::null_mut(),
                    KIND_EVENT,
                    EVENT_PENDING,
                    0,
                    null,
                    null,
                    null,
                    null,
                );
            }
        }
        let mut drained = 0;
        while StoreKitPurchases.take_event().is_some() {
            drained += 1;
        }
        assert_eq!(drained, 32);
    }
}