Stripe Configuration
This guide explains how to configure Stripe in your Ever Works application with a complete subscription and payment system.
Overviewβ
Stripe is a comprehensive payment platform that supports:
- π³ One-time payments
- π Recurring subscriptions
- π Multiple payment methods (cards, Apple Pay, Google Pay)
- π° Multiple currencies
- π Advanced analytics and reporting
Required Environment Variablesβ
Add these variables to your .env.local file:
# Stripe Configuration
STRIPE_SECRET_KEY=sk_test_your_stripe_secret_key_here
STRIPE_WEBHOOK_SECRET=whsec_your_stripe_webhook_secret_here
NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=pk_test_your_stripe_publishable_key_here
# Stripe Price IDs
NEXT_PUBLIC_STRIPE_SUBSCRIPTION_PRICE_ID=price_subscription_id_here
NEXT_PUBLIC_STRIPE_ONETIME_PRICE_ID=price_onetime_id_here
NEXT_PUBLIC_STRIPE_FREE_PRICE_ID=price_free_id_here
# Product Pricing (for display purposes)
NEXT_PUBLIC_PRODUCT_PRICE_PRO=10.00
NEXT_PUBLIC_PRODUCT_PRICE_SPONSOR=20.00
NEXT_PUBLIC_PRODUCT_PRICE_FREE=0.00
Never commit your secret keys to version control. Keep .env.local in your .gitignore file.
Stripe Dashboard Configurationβ
Step 1: Create Productsβ
In your Stripe Dashboard:
- Navigate to Products β Add Product
- Create the following products:
| Product | Price | Type | Description |
|---|---|---|---|
| Free Plan | $0.00 | One-time | Basic features |
| Pro Plan | $10.00 | Monthly subscription | Advanced features |
| Sponsor Plan | $20.00 | One-time | Premium support |
- Copy the Price ID for each product (starts with
price_)
Step 2: Configure Webhooksβ
Webhooks allow Stripe to notify your application about payment events.
-
Go to Developers β Webhooks β Add endpoint
-
Set the endpoint URL:
- Development:
http://localhost:3000/api/stripe/webhook - Production:
https://your-domain.com/api/stripe/webhook
- Development:
-
Select events to listen for:
payment_intent.succeededpayment_intent.payment_failedcustomer.subscription.createdcustomer.subscription.updatedcustomer.subscription.deletedcustomer.subscription.trial_will_endinvoice.payment_succeededinvoice.payment_failed
-
Copy the Signing secret (starts with
whsec_)
Step 3: Retrieve API Keysβ
In your Stripe dashboard:
- Secret Key: Developers β API keys β Secret key (starts with
sk_) - Publishable Key: Developers β API keys β Publishable key (starts with
pk_) - Webhook Secret: Developers β Webhooks β Select your webhook β Signing secret
Use test mode keys during development (they start with sk_test_ and pk_test_). Switch to live mode keys for production.
Payment System Architectureβ
Stripe Providerβ
The Stripe provider (lib/payment/lib/providers/stripe-provider.ts) implements:
- β Customer management
- β Payment intent creation
- β Subscription management
- β Webhook handling
- β Setup intent support
- β Refunds and cancellations
API Routesβ
The following API routes are available:
| Route | Method | Description |
|---|---|---|
/api/stripe/webhook | POST | Handle Stripe webhooks |
/api/stripe/subscription | POST | Create subscription |
/api/stripe/subscription | PUT | Update subscription |
/api/stripe/subscription | DELETE | Cancel subscription |
/api/stripe/payment-intent | POST | Create payment intent |
/api/stripe/payment-intent | GET | Verify payment |
/api/stripe/setup-intent | POST | Setup payment method |
UI Componentsβ
The system uses Stripe Elements for secure payment forms:
StripeElementsWrapper- Main wrapper componentStripePaymentForm- Payment form with validation- Support for Apple Pay and Google Pay
- Responsive design for mobile and desktop
Usage Examplesβ
Create a Subscriptionβ
import { StripeProvider } from '@/lib/payment/providers/stripe-provider';
const configs = createProviderConfigs({
apiKey: process.env.STRIPE_SECRET_KEY!,
webhookSecret: process.env.STRIPE_WEBHOOK_SECRET!,
options: {
publishableKey: process.env.NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY!,
apiVersion: '2023-10-16'
}
});
const stripeProvider = new StripeProvider(configs.stripe);
const subscription = await stripeProvider.createSubscription({
customerId: 'cus_customer_id',
priceId: 'price_subscription_id',
paymentMethodId: 'pm_payment_method_id',
trialPeriodDays: 7
});
Use the Payment Componentβ
import { PaymentForm } from '@/lib/payment';
function PaymentPage() {
return (
<PaymentForm
amount={1000} // 10.00 USD in cents
currency="usd"
isSubscription={true}
onSuccess={(paymentId) => {
console.log('Payment succeeded:', paymentId);
// Redirect to success page or update UI
}}
onError={(error) => {
console.error('Payment error:', error);
// Show error message to user
}}
/>
);
}
Testing Your Integrationβ
Test Modeβ
-
Use test API keys (start with
sk_test_andpk_test_) -
Use test card numbers:
- Success:
4242 4242 4242 4242 - Decline:
4000 0000 0000 0002 - 3D Secure:
4000 0025 0000 3155
- Success:
-
Test webhooks locally with Stripe CLI:
stripe listen --forward-to localhost:3000/api/stripe/webhook
Webhook Testingβ
# Install Stripe CLI
brew install stripe/stripe-cli/stripe
# Login to your Stripe account
stripe login
# Forward webhooks to your local server
stripe listen --forward-to localhost:3000/api/stripe/webhook
# Trigger test events
stripe trigger payment_intent.succeeded
Error Handlingβ
The system automatically handles common errors:
| Error Type | Handling |
|---|---|
| Card declined | User-friendly error message |
| Insufficient funds | Retry with different card |
| Network issues | Automatic retry logic |
| Webhook failures | Logged for manual review |
| Validation errors | Form field highlighting |
Security Best Practicesβ
-
API Keys:
- Never expose secret keys in client-side code
- Use environment variables
- Rotate keys regularly
-
Webhook Verification:
- Always verify webhook signatures
- Validate event data before processing
-
Payment Data:
- Never store card numbers
- Use Stripe's tokenization
- Implement PCI compliance
-
User Sessions:
- Verify user authentication
- Validate user permissions
- Log all payment activities
Dependenciesβ
Required packages (already included in Ever Works):
{
"@stripe/react-stripe-js": "^3.7.0",
"@stripe/stripe-js": "^7.3.0",
"stripe": "^18.1.0"
}
Troubleshootingβ
Common Issuesβ
Issue: Webhook not receiving events
- Solution: Check webhook URL is publicly accessible
- Use Stripe CLI for local testing
- Verify webhook secret is correct
Issue: Payment fails silently
- Solution: Check browser console for errors
- Verify API keys are correct
- Check Stripe dashboard logs
Issue: 3D Secure not working
- Solution: Ensure you're handling
requires_actionstatus - Implement proper redirect flow
- Test with 3D Secure test cards
Next Stepsβ
- LemonSqueezy Configuration - Alternative payment provider
- Environment Variables - Complete environment setup
- Deployment - Deploy your payment integration
Resourcesβ
Supportβ
Need help with Stripe integration? Check our support page or join our community.