Appearance
Subscription Functions Mixin
File Information
- Path:
mixins/subscription-functions.js - Purpose: Stripe subscription management and workspace switching functionality
Overview
The Subscription Functions mixin provides functionality for managing Stripe subscriptions, adding payment methods, and handling workspace switching with subscription updates. It integrates with Stripe Elements for secure payment processing.
Key Features
Stripe Integration
- Stripe Elements setup
- Payment method addition
- Setup intent creation
- Payment confirmation
Workspace Switching
- Workspace change handling
- Subscription update on switch
- User profile update
- Session refresh
Card Management
- Add card dialog
- Card element mounting
- Payment method saving
Methods
addCardDialog()
Opens dialog for adding payment card:
- Creates Stripe setup intent
- Initializes Stripe Elements
- Mounts payment element
- Shows add card dialog
Process:
- Checks if dialog already open
- Creates setup intent via API
- Configures Stripe Elements
- Mounts payment element to
#payment-element-stripe - Shows dialog after element loads
Requirements:
- Stripe instance must be initialized
- Component must have
showAddCarddata property - Component must have
stripeElemLoadeddata property - Component must have
stripeCardElementsdata property
addCard()
Saves payment card:
- Confirms Stripe setup
- Updates subscription with card
- Handles errors
- Redirects on success
Process:
- Updates subscription trial with customer ID
- Confirms Stripe setup intent
- Handles errors or redirects
- Customer redirected to return URL
API Calls:
POST /subscription/user-trial- Update subscription- Stripe
confirmSetup()- Confirm payment method
onSwitchWorkspace(workspace)
Handles workspace switching:
- Updates user profile with new workspace
- Updates subscription context
- Refreshes authentication
- Logs out and reloads
Parameters:
workspace(Object): Workspace object to switch toid: Workspace IDdepartment_id: Department IDworkspace_unique_id: Workspace unique ID
Process:
- Prevents multiple switches
- Updates user profile via API
- Shows success message
- Updates current workspace
- Updates DAM branding
- Refreshes user data
- Logs out and reloads page
Data Properties Required
Components using this mixin should have:
javascript
{
showAddCard: boolean, // Add card dialog visibility
stripeElemLoaded: boolean, // Stripe element loaded state
stripeCardElements: object, // Stripe Elements instance
stripe: object, // Stripe instance
stripeCustomerId: string, // Stripe customer ID
stripe_subscription_id: string, // Stripe subscription ID
subscription_id: number, // Subscription ID
subscription: object, // Subscription object
addCardError: string, // Add card error message
switchingWP: boolean // Workspace switching state
}Stripe Integration
Setup Intent Creation
javascript
POST /stripe/init-setup-intent
{
customer: stripe_customer_id
}
Response: {
client_secret: "seti_xxx_secret_xxx"
}Stripe Elements Configuration
javascript
{
clientSecret: data.client_secret,
appearance: {
theme: 'stripe'
}
}Usage Examples
Add Card Functionality
vue
<template>
<div>
<v-dialog v-model="showAddCard">
<div id="payment-element-stripe"></div>
<button @click="addCard" :disabled="!stripeElemLoaded">
Save Card
</button>
<p v-if="addCardError" class="error">{{ addCardError }}</p>
</v-dialog>
<button @click="addCardDialog">Add Payment Method</button>
</div>
</template>
<script>
import { loadStripe } from '@stripe/stripe-js'
import subscriptionFunctions from '~/mixins/subscription-functions'
export default {
mixins: [subscriptionFunctions],
async mounted() {
this.stripe = await loadStripe(process.env.STRIPE_PUBLIC_KEY)
},
data() {
return {
showAddCard: false,
stripeElemLoaded: false,
stripeCardElements: null,
stripe: null,
addCardError: '',
subscription: null
}
}
}
</script>Workspace Switching
vue
<template>
<div>
<v-select
v-model="selectedWorkspace"
:items="workspaces"
@change="handleWorkspaceSwitch"
/>
</div>
</template>
<script>
import subscriptionFunctions from '~/mixins/subscription-functions'
export default {
mixins: [subscriptionFunctions],
data() {
return {
selectedWorkspace: null,
workspaces: [],
switchingWP: false
}
},
methods: {
async handleWorkspaceSwitch(workspace) {
await this.onSwitchWorkspace(workspace)
// User will be logged out and page reloaded
}
}
}
</script>API Endpoints
POST /stripe/init-setup-intent- Create Stripe setup intent- Request Body:json
{ "customer": "cus_xxx" } - Response: Setup intent with client secret
- Request Body:
POST /subscription/user-trial- Update subscription trial- Request Body:json
{ "stripe_customer_id": "cus_xxx", "stripe_subscription_id": "sub_xxx", "subscription_id": 123 }
- Request Body:
POST /user/update-profile- Update user profile- Request Body:json
{ "default_department": 456, "default_workspace": "workspace-slug", "email": "[email protected]", "name": "User Name", "phone": "+1234567890", "timezone": "America/New_York", "user_id": 789 }
- Request Body:
Integration Points
- Stripe.js: Payment processing
- Workspace Settings: Workspace management
- Account & Billing: Subscription management
- Authentication: User session
- DAM Store: Branding updates
Notes for Development
- Mixin requires Stripe.js to be loaded
- Payment element must be mounted to specific ID
- Workspace switch triggers full page reload
- Subscription updates are handled automatically
- Error handling for Stripe operations
- Workspace switch updates branding
Related Documentation
- Workspace Settings Pages - Workspace management
- Account & Billing Page - Billing
- Multi-Workspace Feature - Workspace system