Skip to main content

tauri_plugin_revenuecat/
lib.rs

1//! # tauri-plugin-revenuecat
2//!
3//! Bridges the platform store (StoreKit 2 on iOS, Play Billing on Android)
4//! into the `revenuecat` crate's [`StoreBilling`] seam through a Tauri 2
5//! mobile plugin. Desktop targets have no store — configure the SDK with a
6//! `test_` Test Store key there, or sell via web billing.
7//!
8//! ```ignore
9//! tauri::Builder::default()
10//!     .plugin(tauri_plugin_revenuecat::init())
11//!     .setup(|app| {
12//!         let billing = tauri_plugin_revenuecat::store_billing(app.handle())?;
13//!         let purchases = revenuecat::Purchases::configure(
14//!             revenuecat::Configuration::builder("goog_or_appl_KEY")
15//!                 .store_billing(billing)
16//!                 .build()?,
17//!         )?;
18//!         // manage(purchases) ...
19//!         Ok(())
20//!     })
21//! ```
22//!
23//! Status: the Rust side and native shims are code-complete; end-to-end
24//! purchases still need store accounts + devices to verify (see README).
25
26mod commands;
27#[cfg(mobile)]
28mod mobile;
29mod models;
30
31use std::sync::Arc;
32
33use revenuecat::StoreBilling;
34use tauri::plugin::{Builder, TauriPlugin};
35use tauri::{AppHandle, Manager, Runtime};
36
37pub use commands::{ConfigureOptions, LoginResult, SessionInfo};
38
39pub use models::{PluginProduct, PluginTransaction};
40
41#[cfg(target_os = "ios")]
42tauri::ios_plugin_binding!(init_plugin_revenuecat);
43
44/// State managed by the plugin on mobile targets.
45#[cfg(mobile)]
46struct RevenuecatExt<R: Runtime>(tauri::plugin::PluginHandle<R>);
47
48/// Initializes the plugin; registers the native shim on mobile targets and
49/// is a no-op on desktop.
50pub fn init<R: Runtime>() -> TauriPlugin<R> {
51    Builder::new("revenuecat")
52        .invoke_handler(tauri::generate_handler![
53            commands::configure,
54            commands::session_info,
55            commands::get_offerings,
56            commands::get_customer_info,
57            commands::purchase_package,
58            commands::restore,
59            commands::sync_pending_purchases,
60            commands::sync_purchases,
61            commands::log_in,
62            commands::log_out,
63            commands::set_email,
64            commands::manage_subscriptions,
65        ])
66        .setup(|app, _api| {
67            // The configured SDK lives here; `configure` populates it.
68            app.manage(commands::Sdk::default());
69            #[cfg(target_os = "android")]
70            {
71                let handle =
72                    _api.register_android_plugin("app.tauri.revenuecat", "RevenueCatPlugin")?;
73                app.manage(RevenuecatExt(handle));
74            }
75            #[cfg(target_os = "ios")]
76            {
77                let handle = _api.register_ios_plugin(init_plugin_revenuecat)?;
78                app.manage(RevenuecatExt(handle));
79            }
80            Ok(())
81        })
82        .build()
83}
84
85/// Returns a [`StoreBilling`] backed by the platform store, for
86/// `ConfigurationBuilder::store_billing`. Errors on desktop targets, where
87/// no store exists.
88pub fn store_billing<R: Runtime>(app: &AppHandle<R>) -> revenuecat::Result<Arc<dyn StoreBilling>> {
89    #[cfg(mobile)]
90    {
91        let ext = app.try_state::<RevenuecatExt<R>>().ok_or_else(|| {
92            revenuecat::Error::new(
93                revenuecat::ErrorCode::ConfigurationError,
94                "tauri-plugin-revenuecat is not initialized; add .plugin(tauri_plugin_revenuecat::init()).",
95            )
96        })?;
97        Ok(Arc::new(mobile::MobileStoreBilling::new(ext.0.clone())))
98    }
99    #[cfg(not(mobile))]
100    {
101        let _ = app;
102        Err(revenuecat::Error::new(
103            revenuecat::ErrorCode::UnsupportedError,
104            "No platform store on desktop. Use a `test_` Test Store key, or sell via \
105             RevenueCat Web Billing and attach purchases with redeem_web_purchase.",
106        ))
107    }
108}
109
110#[cfg(all(test, not(mobile)))]
111mod tests {
112    #[test]
113    fn desktop_has_no_store_billing() {
114        let app = tauri::test::mock_app();
115        let error = match super::store_billing(app.handle()) {
116            Err(error) => error,
117            Ok(_) => panic!("desktop must not provide a store"),
118        };
119        assert_eq!(error.code, revenuecat::ErrorCode::UnsupportedError);
120    }
121}