change plan

This commit is contained in:
Brandon Thomas committed 2025-09-16 03:10:55 -04:00
1 parent 6a0abfc177
commit f90ab5771a
13 files changed
+256 -42

No files matched your search

@@ -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,
@@ -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<StripeArtcraftCreateCustomerPortalSessionResponse, StorytellerError> {
Ok(basic_json_post_request(
api_host,
CREATE_CUSTOMER_PORTAL_URL_PATH,
maybe_creds,
request,
).await?)
}
@@ -1,2 +1,3 @@
pub mod create_credits_pack_checkout;
pub mod create_customer_portal_session;
pub mod create_subscription_checkout;
+2
View File
@@ -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,
]);
@@ -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;
@@ -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;
@@ -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;
@@ -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<String, String> {
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?;
@@ -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<PortalReason>,
}
#[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<String, String> {
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(())
}
@@ -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<String, String> {
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,
}
@@ -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<Url> {
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,
@@ -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 (
<>
<div className="space-y-4">
@@ -53,19 +51,14 @@ export const BillingSettingsPane = (args: BillingSettingsPaneProps) => {
</div>
<div className="flex gap-2">
{hasPaidPlan && (
<Button variant="secondary" className="h-[30px]">
Cancel plan
</Button>
<>
<CancelPlanButton />
<ChangePlanButton />
</>
)}
{!hasPaidPlan && (
<UpgradePlanButton />
)}
<Button
variant="primary"
className="h-[30px]"
onClick={() => {
toggleModal();
}}
>
Upgrade plan
</Button>
</div>
</div>
</div>
@@ -95,17 +88,95 @@ export const BillingSettingsPane = (args: BillingSettingsPaneProps) => {
</span>
</div>
<div className="flex gap-2">
<Button
variant="primary"
className="h-[30px]"
onClick={() => toggleCreditsModal()}
>
Buy credits
</Button>
<BuyCreditsButton />
</div>
</div>
<CreditsTally creditsStore={creditsStore} />
</div>
</div>
</>
);
};
const CancelPlanButton = () => {
const handleClick = async () => {
await invoke("storyteller_open_customer_portal_command", {
request: {
reason: "cancel_subscription",
}
});
}
return (
<Button
variant="secondary"
className="h-[30px]"
onClick={handleClick}
>
Cancel plan
</Button>
)
}
const ChangePlanButton = () => {
const handleClick = async () => {
await invoke("storyteller_open_customer_portal_command", {
request: {
reason: "update_subscription",
}
});
}
return (
<Button
variant="primary"
className="h-[30px]"
onClick={handleClick}
>
Change plan
</Button>
)
}
const UpgradePlanButton = () => {
const { toggleModal } = usePricingModalStore();
return (
<Button
variant="primary"
className="h-[30px]"
onClick={() => {
toggleModal();
}}
>
Upgrade plan
</Button>
)
}
const BuyCreditsButton = () => {
const { toggleModal: toggleCreditsModal } = useCreditsModalStore();
return (
<Button
variant="primary"
className="h-[30px]"
onClick={() => toggleCreditsModal()}
>
Buy credits
</Button>
)
}
const CreditsTally = ({creditsStore}: {creditsStore: CreditsState}) => {
return (
<div className="flex pl-5 pt-3">
<ul className="list-disc">
<li> {creditsStore.monthlyCredits} monthly credits (refilled monthly) </li>
<li> {creditsStore.bankedCredits} purchased credits </li>
</ul>
</div>
)
}
@@ -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,