tauri_plugin_iap/
lib.rs

1use tauri::{
2    plugin::{Builder, TauriPlugin},
3    Manager, Runtime,
4};
5
6pub use models::*;
7
8#[cfg(any(
9    target_os = "windows",
10    target_os = "linux",
11    all(target_os = "macos", not(feature = "unstable"))
12))]
13mod desktop;
14#[cfg(all(target_os = "macos", feature = "unstable"))]
15mod macos;
16#[cfg(mobile)]
17mod mobile;
18
19mod commands;
20mod error;
21mod models;
22
23pub use error::{Error, Result};
24
25#[cfg(any(
26    target_os = "windows",
27    target_os = "linux",
28    all(target_os = "macos", not(feature = "unstable"))
29))]
30use desktop::Iap;
31#[cfg(all(target_os = "macos", feature = "unstable"))]
32use macos::Iap;
33#[cfg(mobile)]
34use mobile::Iap;
35
36/// Extensions to [`tauri::App`], [`tauri::AppHandle`] and [`tauri::Window`] to access the iap APIs.
37pub trait IapExt<R: Runtime> {
38    fn iap(&self) -> &Iap<R>;
39}
40
41impl<R: Runtime, T: Manager<R>> crate::IapExt<R> for T {
42    fn iap(&self) -> &Iap<R> {
43        self.state::<Iap<R>>().inner()
44    }
45}
46
47/// Initializes the plugin.
48pub fn init<R: Runtime>() -> TauriPlugin<R> {
49    Builder::new("iap")
50        .invoke_handler(tauri::generate_handler![
51            commands::initialize,
52            commands::get_products,
53            commands::purchase,
54            commands::restore_purchases,
55            commands::acknowledge_purchase,
56            commands::get_product_status,
57        ])
58        .setup(|app, api| {
59            #[cfg(all(target_os = "macos", feature = "unstable"))]
60            let iap = macos::init(app, api)?;
61            #[cfg(mobile)]
62            let iap = mobile::init(app, api)?;
63            #[cfg(any(
64                target_os = "windows",
65                target_os = "linux",
66                all(target_os = "macos", not(feature = "unstable"))
67            ))]
68            let iap = desktop::init(app, api)?;
69            app.manage(iap);
70            Ok(())
71        })
72        .build()
73}