diff --git a/crates/api_clients/artcraft_api_defs/src/stripe_artcraft/create_customer_portal_session.rs b/crates/api_clients/artcraft_api_defs/src/stripe_artcraft/create_customer_portal_session.rs index 69171e1442..62f52a3310 100644 --- a/crates/api_clients/artcraft_api_defs/src/stripe_artcraft/create_customer_portal_session.rs +++ b/crates/api_clients/artcraft_api_defs/src/stripe_artcraft/create_customer_portal_session.rs @@ -4,7 +4,7 @@ use utoipa::ToSchema; pub const CREATE_CUSTOMER_PORTAL_URL_PATH: &str = "/v1/stripe_artcraft/portal/create_session"; /// See https://docs.stripe.com/customer-management/portal-deep-links -#[derive(Serialize, Deserialize, ToSchema, Copy, Clone)] +#[derive(Serialize, Deserialize, ToSchema, Copy, Clone, Debug)] pub enum StripeArtcraftCreateCustomerPortalFlowState { // subscription_update SubscriptionUpdate, diff --git a/crates/api_clients/storyteller_client/src/stripe_artcraft/create_customer_portal_session.rs b/crates/api_clients/storyteller_client/src/stripe_artcraft/create_customer_portal_session.rs new file mode 100644 index 0000000000..476ce80658 --- /dev/null +++ b/crates/api_clients/storyteller_client/src/stripe_artcraft/create_customer_portal_session.rs @@ -0,0 +1,18 @@ +use crate::credentials::storyteller_credential_set::StorytellerCredentialSet; +use crate::error::storyteller_error::StorytellerError; +use crate::utils::api_host::ApiHost; +use crate::utils::basic_json_post_request::basic_json_post_request; +use artcraft_api_defs::stripe_artcraft::create_customer_portal_session::{StripeArtcraftCreateCustomerPortalSessionRequest, StripeArtcraftCreateCustomerPortalSessionResponse, CREATE_CUSTOMER_PORTAL_URL_PATH}; + +pub async fn create_customer_portal_session( + api_host: &ApiHost, + maybe_creds: Option<&StorytellerCredentialSet>, + request: StripeArtcraftCreateCustomerPortalSessionRequest, +) -> Result { + Ok(basic_json_post_request( + api_host, + CREATE_CUSTOMER_PORTAL_URL_PATH, + maybe_creds, + request, + ).await?) +} diff --git a/crates/api_clients/storyteller_client/src/stripe_artcraft/mod.rs b/crates/api_clients/storyteller_client/src/stripe_artcraft/mod.rs index d0dd6bc5b7..cd40e2dc89 100644 --- a/crates/api_clients/storyteller_client/src/stripe_artcraft/mod.rs +++ b/crates/api_clients/storyteller_client/src/stripe_artcraft/mod.rs @@ -1,2 +1,3 @@ pub mod create_credits_pack_checkout; +pub mod create_customer_portal_session; pub mod create_subscription_checkout; diff --git a/crates/desktop/artcraft/src/lib.rs b/crates/desktop/artcraft/src/lib.rs index 4395035d5f..b5c055980a 100644 --- a/crates/desktop/artcraft/src/lib.rs +++ b/crates/desktop/artcraft/src/lib.rs @@ -48,6 +48,7 @@ use crate::services::sora::threads::sora_task_polling_thread::sora_task_polling_ use crate::services::storyteller::commands::storyteller_get_credits_command::storyteller_get_credits_command; use crate::services::storyteller::commands::storyteller_get_subscription_command::storyteller_get_subscription_command; use crate::services::storyteller::commands::storyteller_open_credits_purchase_command::storyteller_open_credits_purchase_command; +use crate::services::storyteller::commands::storyteller_open_customer_portal_command::storyteller_open_customer_portal_command; use crate::services::storyteller::commands::storyteller_open_subscription_purchase_command::storyteller_open_subscription_purchase_command; use crate::services::storyteller::state::storyteller_credential_manager::StorytellerCredentialManager; use log::error; @@ -190,6 +191,7 @@ pub fn run() { storyteller_get_credits_command, storyteller_get_subscription_command, storyteller_open_credits_purchase_command, + storyteller_open_customer_portal_command, storyteller_open_subscription_purchase_command, update_app_preferences_command, ]); diff --git a/crates/desktop/artcraft/src/services/storyteller/commands/mod.rs b/crates/desktop/artcraft/src/services/storyteller/commands/mod.rs index a3a8ed09d6..309015562e 100644 --- a/crates/desktop/artcraft/src/services/storyteller/commands/mod.rs +++ b/crates/desktop/artcraft/src/services/storyteller/commands/mod.rs @@ -1,4 +1,5 @@ pub mod storyteller_get_credits_command; pub mod storyteller_get_subscription_command; pub mod storyteller_open_credits_purchase_command; +pub mod storyteller_open_customer_portal_command; pub mod storyteller_open_subscription_purchase_command; \ No newline at end of file diff --git a/crates/desktop/artcraft/src/services/storyteller/commands/storyteller_get_credits_command.rs b/crates/desktop/artcraft/src/services/storyteller/commands/storyteller_get_credits_command.rs index b19afcac76..7b1940389c 100644 --- a/crates/desktop/artcraft/src/services/storyteller/commands/storyteller_get_credits_command.rs +++ b/crates/desktop/artcraft/src/services/storyteller/commands/storyteller_get_credits_command.rs @@ -7,7 +7,7 @@ use crate::services::midjourney::state::midjourney_credential_manager::Midjourne use crate::services::midjourney::windows::open_midjourney_login_window::open_midjourney_login_window; use crate::services::sora::windows::sora_login_window::open_sora_login_window::open_sora_login_window; use crate::services::storyteller::state::storyteller_credential_manager::StorytellerCredentialManager; -use crate::services::storyteller::windows::open_storyteller_billing_window::{open_storyteller_billing_window, OpenStorytellerBillingWindowArgs, ProductInfo}; +use crate::services::storyteller::windows::open_storyteller_billing_window::{open_storyteller_billing_window, OpenStorytellerBillingWindowArgs, BillingWindowCase}; use artcraft_api_defs::stripe_artcraft::create_subscription_checkout::PlanBillingCadence; use enums::common::artcraft_credits_pack_slug::ArtcraftCreditsPackSlug; use enums::common::artcraft_subscription_slug::ArtcraftSubscriptionSlug; diff --git a/crates/desktop/artcraft/src/services/storyteller/commands/storyteller_get_subscription_command.rs b/crates/desktop/artcraft/src/services/storyteller/commands/storyteller_get_subscription_command.rs index 2fa5294d33..a1d3e06ee1 100644 --- a/crates/desktop/artcraft/src/services/storyteller/commands/storyteller_get_subscription_command.rs +++ b/crates/desktop/artcraft/src/services/storyteller/commands/storyteller_get_subscription_command.rs @@ -9,7 +9,7 @@ use crate::services::midjourney::state::midjourney_credential_manager::Midjourne use crate::services::midjourney::windows::open_midjourney_login_window::open_midjourney_login_window; use crate::services::sora::windows::sora_login_window::open_sora_login_window::open_sora_login_window; use crate::services::storyteller::state::storyteller_credential_manager::StorytellerCredentialManager; -use crate::services::storyteller::windows::open_storyteller_billing_window::{open_storyteller_billing_window, OpenStorytellerBillingWindowArgs, ProductInfo}; +use crate::services::storyteller::windows::open_storyteller_billing_window::{open_storyteller_billing_window, OpenStorytellerBillingWindowArgs, BillingWindowCase}; use artcraft_api_defs::stripe_artcraft::create_subscription_checkout::PlanBillingCadence; use enums::common::artcraft_credits_pack_slug::ArtcraftCreditsPackSlug; use enums::common::artcraft_subscription_slug::ArtcraftSubscriptionSlug; diff --git a/crates/desktop/artcraft/src/services/storyteller/commands/storyteller_open_credits_purchase_command.rs b/crates/desktop/artcraft/src/services/storyteller/commands/storyteller_open_credits_purchase_command.rs index 81f8896ea0..99016654da 100644 --- a/crates/desktop/artcraft/src/services/storyteller/commands/storyteller_open_credits_purchase_command.rs +++ b/crates/desktop/artcraft/src/services/storyteller/commands/storyteller_open_credits_purchase_command.rs @@ -5,7 +5,7 @@ use crate::services::midjourney::state::midjourney_credential_manager::Midjourne use crate::services::midjourney::windows::open_midjourney_login_window::open_midjourney_login_window; use crate::services::sora::windows::sora_login_window::open_sora_login_window::open_sora_login_window; use crate::services::storyteller::state::storyteller_credential_manager::StorytellerCredentialManager; -use crate::services::storyteller::windows::open_storyteller_billing_window::{open_storyteller_billing_window, OpenStorytellerBillingWindowArgs, ProductInfo}; +use crate::services::storyteller::windows::open_storyteller_billing_window::{open_storyteller_billing_window, OpenStorytellerBillingWindowArgs, BillingWindowCase}; use artcraft_api_defs::stripe_artcraft::create_subscription_checkout::PlanBillingCadence; use enums::common::artcraft_credits_pack_slug::ArtcraftCreditsPackSlug; use enums::common::artcraft_subscription_slug::ArtcraftSubscriptionSlug; @@ -29,7 +29,7 @@ pub async fn storyteller_open_credits_purchase_command( app_env_configs: State<'_, AppEnvConfigs>, storyteller_creds_manager: State<'_, StorytellerCredentialManager>, ) -> Result { - info!("storyteller_open_billing_command called"); + info!("storyteller_open_credits_purchase_command called"); let credits_pack = request.credits_pack.ok_or("Credits pack is required")?; @@ -63,7 +63,7 @@ async fn do_open_billing( app_data_root, app_env_configs, storyteller_creds_manager, - product_info: ProductInfo::CreditsPack { + billing_window_case: BillingWindowCase::CreditsPack { credits_pack, } }).await?; diff --git a/crates/desktop/artcraft/src/services/storyteller/commands/storyteller_open_customer_portal_command.rs b/crates/desktop/artcraft/src/services/storyteller/commands/storyteller_open_customer_portal_command.rs new file mode 100644 index 0000000000..dcf7952318 --- /dev/null +++ b/crates/desktop/artcraft/src/services/storyteller/commands/storyteller_open_customer_portal_command.rs @@ -0,0 +1,83 @@ +use crate::core::commands::enqueue::image_edit::enqueue_contextual_edit_image_command::{ContextualImageEditModel, EditImageQuality, EditImageSize}; +use crate::core::state::app_env_configs::app_env_configs::AppEnvConfigs; +use crate::core::state::data_dir::app_data_root::AppDataRoot; +use crate::services::midjourney::state::midjourney_credential_manager::MidjourneyCredentialManager; +use crate::services::midjourney::windows::open_midjourney_login_window::open_midjourney_login_window; +use crate::services::sora::windows::sora_login_window::open_sora_login_window::open_sora_login_window; +use crate::services::storyteller::state::storyteller_credential_manager::StorytellerCredentialManager; +use crate::services::storyteller::windows::open_storyteller_billing_window::{open_storyteller_billing_window, BillingWindowCase, OpenStorytellerBillingWindowArgs}; +use artcraft_api_defs::stripe_artcraft::create_subscription_checkout::PlanBillingCadence; +use enums::common::artcraft_credits_pack_slug::ArtcraftCreditsPackSlug; +use enums::common::artcraft_subscription_slug::ArtcraftSubscriptionSlug; +use enums::tauri::ux::tauri_command_caller::TauriCommandCaller; +use errors::AnyhowResult; +use log::{error, info}; +use serde_derive::Deserialize; +use tauri::{AppHandle, State}; +use tokens::tokens::media_files::MediaFileToken; + +#[derive(Deserialize, Debug)] +pub struct StorytellerOpenCustomerPortalCommand { + pub reason: Option, +} + +#[derive(Deserialize, Debug, Clone, Copy)] +#[serde(rename_all = "snake_case")] +pub enum PortalReason { + UpdateSubscription, + CancelSubscription, +} + +#[tauri::command] +pub async fn storyteller_open_customer_portal_command( + app: AppHandle, + request: StorytellerOpenCustomerPortalCommand, + app_data_root: State<'_, AppDataRoot>, + app_env_configs: State<'_, AppEnvConfigs>, + storyteller_creds_manager: State<'_, StorytellerCredentialManager>, +) -> Result { + info!("storyteller_open_customer_portal_command called"); + + let reason = request.reason.ok_or("Reason is required")?; + + do_open_portal( + &app, + &app_data_root, + &app_env_configs, + &storyteller_creds_manager, + reason, + ) + .await + .map_err(|err| { + error!("Error opening customer portal window: {:?}", err); + format!("Error opening customer portal window: {:?}", err) + })?; + + Ok("result".to_string()) +} + +async fn do_open_portal( + app: &AppHandle, + app_data_root: &AppDataRoot, + app_env_configs: &AppEnvConfigs, + storyteller_creds_manager: &StorytellerCredentialManager, + reason: PortalReason, +) -> AnyhowResult<()> { + info!("Building billing window..."); + + let billing_window_case = match reason { + PortalReason::UpdateSubscription => BillingWindowCase::CustomerPortalUpdate, + PortalReason::CancelSubscription => BillingWindowCase::CustomerPortalCancel, + }; + + open_storyteller_billing_window(OpenStorytellerBillingWindowArgs { + app, + app_data_root, + app_env_configs, + storyteller_creds_manager, + billing_window_case, + }).await?; + + info!("Done."); + Ok(()) +} diff --git a/crates/desktop/artcraft/src/services/storyteller/commands/storyteller_open_subscription_purchase_command.rs b/crates/desktop/artcraft/src/services/storyteller/commands/storyteller_open_subscription_purchase_command.rs index 02bbcff740..a5809b4e49 100644 --- a/crates/desktop/artcraft/src/services/storyteller/commands/storyteller_open_subscription_purchase_command.rs +++ b/crates/desktop/artcraft/src/services/storyteller/commands/storyteller_open_subscription_purchase_command.rs @@ -5,7 +5,7 @@ use crate::services::midjourney::state::midjourney_credential_manager::Midjourne use crate::services::midjourney::windows::open_midjourney_login_window::open_midjourney_login_window; use crate::services::sora::windows::sora_login_window::open_sora_login_window::open_sora_login_window; use crate::services::storyteller::state::storyteller_credential_manager::StorytellerCredentialManager; -use crate::services::storyteller::windows::open_storyteller_billing_window::{open_storyteller_billing_window, OpenStorytellerBillingWindowArgs, ProductInfo}; +use crate::services::storyteller::windows::open_storyteller_billing_window::{open_storyteller_billing_window, OpenStorytellerBillingWindowArgs, BillingWindowCase}; use artcraft_api_defs::stripe_artcraft::create_subscription_checkout::PlanBillingCadence; use enums::common::artcraft_subscription_slug::ArtcraftSubscriptionSlug; use enums::tauri::ux::tauri_command_caller::TauriCommandCaller; @@ -29,7 +29,7 @@ pub async fn storyteller_open_subscription_purchase_command( app_env_configs: State<'_, AppEnvConfigs>, storyteller_creds_manager: State<'_, StorytellerCredentialManager>, ) -> Result { - info!("storyteller_open_billing_command called"); + info!("storyteller_open_subscription_purchase_command called"); let plan = request.plan.ok_or("Plan is required")?; let cadence = request.cadence.ok_or("Cadence is required")?; @@ -66,7 +66,7 @@ async fn do_open_billing( app_env_configs, app_data_root, storyteller_creds_manager, - product_info: ProductInfo::Subscription { + billing_window_case: BillingWindowCase::Subscription { plan, cadence, } diff --git a/crates/desktop/artcraft/src/services/storyteller/windows/open_storyteller_billing_window.rs b/crates/desktop/artcraft/src/services/storyteller/windows/open_storyteller_billing_window.rs index 65619d5376..d819b93bde 100644 --- a/crates/desktop/artcraft/src/services/storyteller/windows/open_storyteller_billing_window.rs +++ b/crates/desktop/artcraft/src/services/storyteller/windows/open_storyteller_billing_window.rs @@ -5,6 +5,7 @@ use crate::services::storyteller::state::storyteller_credential_manager::Storyte use crate::services::storyteller::windows::storyteller_billing_window_thread::storyteller_billing_window_thread; use anyhow::anyhow; use artcraft_api_defs::stripe_artcraft::create_credits_pack_checkout::StripeArtcraftCreateCreditsPackCheckoutRequest; +use artcraft_api_defs::stripe_artcraft::create_customer_portal_session::{StripeArtcraftCreateCustomerPortalFlowState, StripeArtcraftCreateCustomerPortalSessionRequest}; use artcraft_api_defs::stripe_artcraft::create_subscription_checkout::{PlanBillingCadence, StripeArtcraftCreateSubscriptionCheckoutRequest}; use enums::common::artcraft_credits_pack_slug::ArtcraftCreditsPackSlug; use enums::common::artcraft_subscription_slug::ArtcraftSubscriptionSlug; @@ -13,6 +14,7 @@ use log::info; use reqwest::Url; use storyteller_client::credentials::storyteller_credential_set::StorytellerCredentialSet; use storyteller_client::stripe_artcraft::create_credits_pack_checkout::create_credits_pack_checkout; +use storyteller_client::stripe_artcraft::create_customer_portal_session::create_customer_portal_session; use storyteller_client::stripe_artcraft::create_subscription_checkout::create_subscription_checkout; use tauri::{AppHandle, Manager, WebviewUrl, WebviewWindowBuilder}; @@ -23,10 +25,10 @@ pub struct OpenStorytellerBillingWindowArgs<'a> { pub app_env_configs: &'a AppEnvConfigs, pub app_data_root: &'a AppDataRoot, pub storyteller_creds_manager: &'a StorytellerCredentialManager, - pub product_info: ProductInfo, + pub billing_window_case: BillingWindowCase, } -pub enum ProductInfo { +pub enum BillingWindowCase { Subscription { plan: ArtcraftSubscriptionSlug, cadence: PlanBillingCadence, @@ -34,6 +36,8 @@ pub enum ProductInfo { CreditsPack { credits_pack: ArtcraftCreditsPackSlug, }, + CustomerPortalCancel, + CustomerPortalUpdate, } pub async fn open_storyteller_billing_window( @@ -48,24 +52,36 @@ pub async fn open_storyteller_billing_window( let creds = args.storyteller_creds_manager.get_credentials_required()?; - let checkout_url = match args.product_info { - ProductInfo::Subscription { plan, cadence } => { + let checkout_url = match args.billing_window_case { + BillingWindowCase::Subscription { plan, cadence } => { get_subscription_url( - args.app, args.app_env_configs, - args.app_data_root, &creds, plan, cadence, ).await? }, - ProductInfo::CreditsPack { credits_pack } => { + BillingWindowCase::CreditsPack { credits_pack } => { get_credits_pack_url( args.app_env_configs, &creds, credits_pack, ).await? }, + BillingWindowCase::CustomerPortalCancel => { + get_customer_portal_url( + args.app_env_configs, + &creds, + StripeArtcraftCreateCustomerPortalFlowState::SubscriptionCancel, + ).await? + }, + BillingWindowCase::CustomerPortalUpdate => { + get_customer_portal_url( + args.app_env_configs, + &creds, + StripeArtcraftCreateCustomerPortalFlowState::SubscriptionUpdate, + ).await? + }, }; do_open_window(args.app, args.app_data_root, checkout_url).await?; @@ -74,9 +90,7 @@ pub async fn open_storyteller_billing_window( } async fn get_subscription_url( - app: &AppHandle, app_env_configs: &AppEnvConfigs, - app_data_root: &AppDataRoot, storyteller_creds: &StorytellerCredentialSet, plan: ArtcraftSubscriptionSlug, cadence: PlanBillingCadence, @@ -123,6 +137,30 @@ async fn get_credits_pack_url( Ok(Url::parse(&result.stripe_checkout_redirect_url)?) } + +async fn get_customer_portal_url( + app_env_configs: &AppEnvConfigs, + storyteller_creds: &StorytellerCredentialSet, + flow_state: StripeArtcraftCreateCustomerPortalFlowState, +) -> AnyhowResult { + + info!("Getting customer portal session for flow {:?} ...", flow_state); + + let request = StripeArtcraftCreateCustomerPortalSessionRequest { + portal_config_id: None, + flow: Some(flow_state), + }; + + let result = create_customer_portal_session( + &app_env_configs.storyteller_host, + Some(&storyteller_creds), + request, + ).await?; + + info!("Customer portal session created..."); + Ok(Url::parse(&result.stripe_portal_url)?) +} + async fn do_open_window( app: &AppHandle, app_data_root: &AppDataRoot, diff --git a/frontend/libs/components/settings-modal/src/lib/panes/BillingSettingsPane.tsx b/frontend/libs/components/settings-modal/src/lib/panes/BillingSettingsPane.tsx index 237c2f8184..7b8fa6bd7a 100644 --- a/frontend/libs/components/settings-modal/src/lib/panes/BillingSettingsPane.tsx +++ b/frontend/libs/components/settings-modal/src/lib/panes/BillingSettingsPane.tsx @@ -9,9 +9,10 @@ import { import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { usePricingModalStore } from "@storyteller/ui-pricing-modal"; import { useCreditsModalStore } from "@storyteller/ui-pricing-modal"; -import { useCreditsState } from "@storyteller/credits"; +import { useCreditsState, CreditsState } from "@storyteller/credits"; import { FREE_PLAN, SubscriptionPlanDetails, useSubscriptionState } from "@storyteller/subscription"; import { SUBSCRIPTION_PLANS_BY_SLUG } from "@storyteller/subscription"; +import { invoke } from "@tauri-apps/api/core"; interface BillingSettingsPaneProps {} @@ -35,9 +36,6 @@ export const BillingSettingsPane = (args: BillingSettingsPaneProps) => { subscriptionStore.fetchFromServer(); }, []); - const { toggleModal } = usePricingModalStore(); - const { toggleModal: toggleCreditsModal } = useCreditsModalStore(); - return ( <>
@@ -53,19 +51,14 @@ export const BillingSettingsPane = (args: BillingSettingsPaneProps) => {
{hasPaidPlan && ( - + <> + + + + )} + {!hasPaidPlan && ( + )} -
@@ -95,17 +88,95 @@ export const BillingSettingsPane = (args: BillingSettingsPaneProps) => {
- +
+ + + ); }; + +const CancelPlanButton = () => { + const handleClick = async () => { + await invoke("storyteller_open_customer_portal_command", { + request: { + reason: "cancel_subscription", + } + }); + } + + return ( + + ) +} + +const ChangePlanButton = () => { + const handleClick = async () => { + await invoke("storyteller_open_customer_portal_command", { + request: { + reason: "update_subscription", + } + }); + } + + return ( + + ) +} + +const UpgradePlanButton = () => { + const { toggleModal } = usePricingModalStore(); + + return ( + + ) +} + +const BuyCreditsButton = () => { + const { toggleModal: toggleCreditsModal } = useCreditsModalStore(); + + return ( + + ) +} + +const CreditsTally = ({creditsStore}: {creditsStore: CreditsState}) => { + return ( +
+
    +
  • {creditsStore.monthlyCredits} monthly credits (refilled monthly)
  • +
  • {creditsStore.bankedCredits} purchased credits
  • +
+
+ ) +} diff --git a/frontend/libs/state/credits/src/lib/state/credits-state.ts b/frontend/libs/state/credits/src/lib/state/credits-state.ts index 1e10b39dfe..d3b3592b70 100644 --- a/frontend/libs/state/credits/src/lib/state/credits-state.ts +++ b/frontend/libs/state/credits/src/lib/state/credits-state.ts @@ -1,7 +1,7 @@ import { create } from 'zustand' import { ArtcraftGetCredits } from "@storyteller/tauri-api"; -interface CreditsState { +export interface CreditsState { // Daily free credits (if/when we offer them) freeCredits: number,