- Updated import paths for credit-related actions and functions to improve module organization. - Removed redundant refresh trigger declaration in CreditPackages component. - Simplified success toast message in CreditPackages and PaymentForm components for clarity. - Introduced a new credits.ts file to centralize credit management logic and improve maintainability.
4.6 KiB
4.6 KiB
Credit Management System Implementation
Overview
This document describes the credit management system implementation for the mksaas-template project, which allows users to purchase credits using Stripe payments.
Features Implemented
1. Credit Packages
- Defined credit packages with different tiers (Basic, Standard, Premium, Enterprise)
- Each package includes credits amount, price, and description
- Popular package highlighting
2. Payment Integration
- Stripe PaymentIntent integration for credit purchases
- Secure payment processing with webhook verification
- Automatic credit addition upon successful payment
3. UI Components
- Credit balance display with refresh functionality
- Credit packages selection interface
- Stripe payment form integration
- Modern, responsive design
4. Database Integration
- Credit transaction recording
- User credit balance management
- Proper error handling and validation
Files Created/Modified
Core Components
src/components/dashboard/credit-packages.tsx
- Main credit packages interfacesrc/components/dashboard/credit-balance.tsx
- Credit balance displaysrc/components/dashboard/stripe-payment-form.tsx
- Stripe payment integration
Actions & API
src/actions/credits.action.ts
- Credit-related server actionssrc/app/api/webhooks/stripe/route.ts
- Stripe webhook handlersrc/payment/index.ts
- Payment provider interface (updated)src/payment/types.ts
- Payment types (updated)
Configuration
src/lib/constants.ts
- Credit packages configurationenv.example
- Environment variables template
Pages
src/app/[locale]/(protected)/settings/credits/page.tsx
- Credits management page
Environment Variables Required
Add these to your .env.local
file:
# Stripe Configuration
NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY="pk_test_..."
STRIPE_SECRET_KEY="sk_test_..."
STRIPE_WEBHOOK_SECRET="whsec_..."
Setup Instructions
1. Stripe Configuration
- Create a Stripe account at https://dashboard.stripe.com
- Get your API keys from the Stripe dashboard
- Set up a webhook endpoint pointing to
/api/webhooks/stripe
- Copy the webhook secret and add it to your environment variables
2. Database Setup
Make sure your database schema includes the required credit tables as defined in src/db/schema.ts
.
3. Environment Variables
Copy the required environment variables from env.example
to your .env.local
file and fill in the values.
Usage
For Users
- Navigate to
/settings/credits
- View current credit balance
- Select a credit package
- Complete payment using Stripe
- Credits are automatically added to account
For Developers
// Get user credits
const result = await getCreditsAction();
// Create payment intent
const paymentIntent = await createCreditPaymentIntent({
packageId: 'standard'
});
// Add credits manually
await addCredits({
userId: 'user-id',
amount: 100,
type: 'PURCHASE',
description: 'Credit purchase'
});
Credit Packages Configuration
Edit src/lib/constants.ts
to modify available credit packages:
export const CREDIT_PACKAGES = [
{
id: 'basic',
credits: 100,
price: 9.99,
popular: false,
description: 'Perfect for getting started',
},
// ... more packages
];
Webhook Events
The system handles these Stripe webhook events:
payment_intent.succeeded
- Adds credits to user account upon successful payment
Security Features
- Webhook Verification: All webhook requests are verified using Stripe signatures
- Payment Validation: Amount and package validation before processing
- User Authentication: All credit operations require authenticated users
- Metadata Validation: Payment metadata is validated before processing
Error Handling
The system includes comprehensive error handling for:
- Invalid payment attempts
- Network failures
- Database errors
- Authentication issues
- Webhook verification failures
Testing
To test the credit purchase flow:
- Use Stripe test cards (e.g.,
4242424242424242
) - Monitor webhook events in Stripe dashboard
- Check credit balance updates in the application
Integration Notes
This implementation:
- Uses Next.js server actions for secure server-side operations
- Integrates with existing Drizzle ORM schema
- Follows the existing payment provider pattern
- Maintains consistency with the existing codebase architecture
Future Enhancements
Potential improvements:
- Credit transaction history display
- Credit expiration management
- Bulk credit operations
- Credit usage analytics
- Subscription-based credit allocation