tauri_plugin_revenuecat/
lib.rs1mod 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#[cfg(mobile)]
46struct RevenuecatExt<R: Runtime>(tauri::plugin::PluginHandle<R>);
47
48pub 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 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
85pub 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}