Polar Configuration
This guide explains how to configure Polar as a payment provider in your Ever Works application.
Overviewβ
Polar is a modern payment platform designed for developers and creators that offers:
- π» Developer-friendly API and documentation
- π Subscription and one-time payment support
- π GitHub integration for sponsorships
- π° Transparent pricing structure
- π Secure payment processing
- π Built-in analytics and reporting
Polar is built specifically for developers and open-source projects, offering a clean API, excellent documentation, and seamless GitHub integration for sponsorships and monetization.
Required Environment Variablesβ
Add these variables to your .env.local file:
# Polar Configuration
POLAR_API_KEY=your_polar_api_key_here
POLAR_WEBHOOK_SECRET=your_webhook_secret_here
POLAR_APP_URL=https://your-app-url.com
# Product IDs (optional)
NEXT_PUBLIC_POLAR_SUBSCRIPTION_PRODUCT_ID=product_id_here
NEXT_PUBLIC_POLAR_ONETIME_PRODUCT_ID=product_id_here
Never commit your secret keys to version control. Keep .env.local in your .gitignore file.
Polar Dashboard Setupβ
Step 1: Create Your Accountβ
- Sign up at Polar
- Complete your account setup
- Verify your email address
Step 2: Create Productsβ
- Navigate to Products β New Product
- Create your pricing tiers:
| Product | Price | Type | Description |
|---|---|---|---|
| Pro Plan | $10/month | Subscription | Advanced features |
| Sponsor Plan | $20 | One-time | Premium support |
- Configure product settings:
- Set pricing and billing cycle
- Add product descriptions
- Configure access levels
- Copy the Product ID for each product
Step 3: Get API Keyβ
- Go to Settings β API Keys
- Create a new API key
- Copy the API key
- Add it to your
.env.localasPOLAR_API_KEY
Polar provides separate keys for development and production. Use test keys during development.
Step 4: Configure Webhooksβ
-
Go to Settings β Webhooks
-
Click Create Webhook
-
Configure the webhook:
- URL:
https://yourdomain.com/api/polar/webhook - Events: Select all payment and subscription events
- Secret: Generate a secret key
- URL:
-
Copy the Webhook Secret and add it to your
.env.local
Recommended Eventsβ
Select these events in your webhook configuration:
- β
payment.succeeded- Successful payment - β
payment.failed- Failed payment - β
subscription.created- New subscription - β
subscription.updated- Subscription changes - β
subscription.cancelled- Cancellation - β
subscription.trial_will_end- Trial ending - β
refund.created- Refund processed
Payment System Architectureβ
Polar Providerβ
The Polar provider (lib/payment/lib/providers/polar-provider.ts) implements:
- β Customer management
- β Product and pricing management
- β Subscription lifecycle
- β Payment processing
- β Webhook handling
- β Refund support
API Routesβ
The following API routes are available:
| Route | Method | Description |
|---|---|---|
/api/polar/webhook | POST | Handle Polar webhooks |
/api/polar/subscription | POST | Create subscription |
/api/polar/subscription | PUT | Update subscription |
/api/polar/subscription | DELETE | Cancel subscription |
/api/polar/checkout | POST | Create checkout session |
/api/polar/payment | GET | Verify payment status |
UI Componentsβ
The system uses Polar's checkout components:
PolarCheckoutButton- Checkout button componentPolarPaymentForm- Payment form with validation- Responsive design for mobile and desktop
- Support for multiple payment methods
Usage Examplesβ
Create a Subscriptionβ
import { PolarProvider } from '@/lib/payment/providers/polar-provider';
const configs = createProviderConfigs({
apiKey: process.env.POLAR_API_KEY!,
webhookSecret: process.env.POLAR_WEBHOOK_SECRET!,
options: {
appUrl: process.env.POLAR_APP_URL!
}
});
const polarProvider = new PolarProvider(configs.polar);
const subscription = await polarProvider.createSubscription({
customerId: 'customer_id',
productId: 'product_id',
paymentMethodId: 'payment_method_id',
trialPeriodDays: 7
});
Create a Checkout Sessionβ
const checkout = await polarProvider.createCheckout({
productId: 'product_id_here',
customerId: 'customer_id',
successUrl: 'https://yoursite.com/success',
cancelUrl: 'https://yoursite.com/cancel'
});
// Redirect user to checkout.url
Use the Payment Componentβ
import { PolarCheckoutButton } from '@/lib/payment';
function PaymentPage() {
return (
<PolarCheckoutButton
productId="product_id_here"
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 (available in Polar dashboard)
-
Use test payment methods:
- Test cards provided in Polar dashboard
- Test mode for all payment flows
-
Test webhooks locally with a tool like ngrok:
ngrok http 3000Update webhook URL in Polar dashboard to your ngrok URL.
Webhook Testingβ
# Use ngrok to expose your local server
ngrok http 3000
# Update webhook URL in Polar dashboard
https://your-ngrok-url.ngrok.io/api/polar/webhook
# Trigger test events from Polar dashboard
Error Handlingβ
The system automatically handles common errors:
| Error Type | Handling |
|---|---|
| Payment declined | User-friendly error message |
| Network issues | Automatic retry logic |
| Webhook failures | Logged for manual review |
| Validation errors | Form field highlighting |
| Subscription errors | Clear error messages |
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
- Use HTTPS for all webhook endpoints
-
Payment Data:
- Never store payment details
- Use Polar's secure payment processing
- Implement proper authentication
-
User Sessions:
- Verify user authentication
- Validate user permissions
- Log all payment activities
GitHub Integrationβ
Polar offers seamless GitHub integration:
- GitHub Sponsorships: Connect Polar with GitHub Sponsors
- Repository Access: Grant access based on subscriptions
- Organization Support: Manage team subscriptions
- Automated Access: Automatic access management
Setup GitHub Integrationβ
- Go to Settings β Integrations β GitHub
- Connect your GitHub account
- Configure repository access rules
- Set up automated access management
Dependenciesβ
Required packages (already included in Ever Works):
{
"@polar-sh/sdk": "^1.0.0"
}
Troubleshootingβ
Common Issuesβ
Issue: Webhook not receiving events
- Solution: Check webhook URL is publicly accessible
- Use ngrok for local testing
- Verify webhook secret is correct
Issue: Payment fails silently
- Solution: Check browser console for errors
- Verify API keys are correct
- Check Polar dashboard logs
Issue: Subscription not updating
- Solution: Verify webhook events are configured
- Check webhook handler logs
- Ensure database updates are working
Issue: GitHub integration not working
- Solution: Verify GitHub connection in Polar dashboard
- Check repository access settings
- Ensure proper permissions are granted
Comparison: Polar vs Other Providersβ
| Feature | Polar | Stripe | LemonSqueezy |
|---|---|---|---|
| Developer Focus | β Excellent | β οΈ Good | β οΈ Good |
| GitHub Integration | β Native | β No | β No |
| Open Source Friendly | β Yes | β οΈ Limited | β οΈ Limited |
| Setup Complexity | β Simple | β οΈ Moderate | β Simple |
| API Quality | β Excellent | β Excellent | β οΈ Good |
| Tax Compliance | β οΈ Manual | β οΈ Manual | β Automatic |
| Best For | Developers, OSS | High volume | Global sales |
Next Stepsβ
- Stripe Configuration - Alternative payment provider
- LemonSqueezy Configuration - Alternative payment provider
- Payment Overview - Compare payment providers
- Environment Variables - Complete environment setup
- Deployment - Deploy your payment integration
Resourcesβ
Supportβ
Need help with Polar integration? Check our support page or join our community.