PaymentAdapter interface.
Supported Architecture
ShipFree uses an adapter pattern that makes it easy to add new payment providers. All providers implement the same interface, ensuring consistency across your application.Adding a New Provider
Step 1: Implement the PaymentAdapter Interface
Create a new file insrc/lib/payments/providers/ for your provider (e.g., paddle.ts, paypal.ts, razorpay.ts).
Implement the PaymentAdapter interface:
Step 2: Add Provider Type
Update thePaymentProvider type in src/config/payments.ts:
Step 3: Register the Adapter
Add your adapter to the payment service insrc/lib/payments/service.ts:
Step 4: Add Environment Variables
Updatesrc/config/env.ts to include your provider’s environment variables:
.env.example:
Step 5: Add Price Configuration
Updatesrc/config/payments.ts to include your provider’s prices:
Step 6: Update Webhook Handler
Updatesrc/app/api/webhooks/payments/route.ts to handle your provider’s webhook signature:
Example Providers You Could Add
Paddle
Paddle is a Merchant of Record similar to Lemon Squeezy. Key Features:- Handles global tax compliance
- Merchant of Record
- Good for SaaS and digital products
- Built-in fraud prevention
@paddle/paddle-node-sdk
PayPal
PayPal is widely recognized and trusted globally. Key Features:- High customer trust
- Global payment methods
- Subscription support
- PayPal and credit card payments
@paypal/checkout-server-sdk
Razorpay
Razorpay is popular in India and Asia. Key Features:- Strong presence in India
- UPI, wallets, and local payment methods
- Subscription and recurring billing
- Lower fees for Indian businesses
razorpay
Chargebee
Chargebee is a subscription management platform. Key Features:- Advanced subscription management
- Revenue recognition
- Dunning management
- Multiple payment gateway support
chargebee-typescript
Braintree
Braintree (owned by PayPal) offers flexible payment options. Key Features:- PayPal, Venmo, Apple Pay, Google Pay
- Owned by PayPal (stable)
- Good international support
- Recurring billing
braintree
Implementation Reference
Use the existing adapters as reference:Stripe Adapter
src/lib/payments/providers/stripe.ts
- Most complete implementation
- Shows advanced features (trials, seat-based billing)
- Comprehensive webhook handling
Polar Adapter
src/lib/payments/providers/polar.ts
- Simpler implementation
- Modern SDK usage
- Good example of developer-focused provider
Lemon Squeezy Adapter
src/lib/payments/providers/lemonsqueezy.ts
- Merchant of Record pattern
- Custom data handling in checkouts
- HMAC webhook validation
Testing Your Adapter
- Unit Tests: Test each method independently
- Integration Tests: Test full checkout flow
- Webhook Tests: Use provider’s webhook testing tools
- Local Testing: Use ngrok or similar for webhook delivery
- End-to-End: Test complete subscription lifecycle
Example Test
Common Patterns
Status Mapping
Most providers have different status values. Map them to the unified format:Error Handling
Wrap provider API calls with proper error handling:ID Prefixing
Prefix provider IDs to avoid collisions:Best Practices
- Follow the Interface: Strictly implement all methods in
PaymentAdapter - Type Safety: Use TypeScript types from
src/lib/payments/types.ts - Error Handling: Wrap all API calls with try-catch
- Logging: Log important events and errors
- Documentation: Document provider-specific quirks
- Testing: Write tests for all adapter methods
- Environment Variables: Validate required env vars in constructor
- Webhook Security: Always validate webhook signatures
Getting Help
If you’re implementing a new provider and need help:- Study the existing adapters (especially Stripe)
- Check the provider’s official documentation
- Review the
PaymentAdapterinterface insrc/lib/payments/types.ts - Test each method independently
- Ask for help in the ShipFree community
Contributing Your Adapter
If you’ve implemented a new payment provider, consider contributing it back to ShipFree:- Ensure it follows the existing patterns
- Add comprehensive documentation
- Include tests
- Update
.env.example - Submit a pull request to the ShipFree repository