tauri_plugin_iap/
lib.rs

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