A starter template for building Cloudflare Workers with Go. This template uses the workers
package to create HTTP servers that run on Cloudflare's edge network.
This is a Cloudflare Worker built with Go that integrates with Dodo Payments for handling subscription payments and webhooks. The system processes payment events, manages customer subscriptions, and provides API endpoints for checkout flows.
Cloudflare Workers let you run serverless code at the edge, closer to your users. This template helps you build Workers using Go instead of JavaScript, giving you:
- Type safety with Go's strong typing system
- Performance with compiled Go code running as WebAssembly
- Familiar syntax if you're already a Go developer
- Edge deployment with automatic global distribution
- Payment processing with Dodo Payments integration
- Node.js (for Cloudflare tooling)
- Go 1.24.0 or later
- Dodo Payments account - Register here to get your API credentials
git clone https://github.com/rasadov/PaymentService.git
cd PaymentService
npm run dev
This project integrates with Dodo Payments for subscription billing and payment processing. You'll need to set up a Dodo Payments account and configure the required environment variables.
- Go to dodopayments.com and create an account
- Complete your account setup and verification
- Navigate to your dashboard to get your API credentials
From your Dodo Payments dashboard, you'll need:
- API Key - For authenticating API requests
- Webhook Secret - For verifying webhook signatures
- Checkout URL - Your Dodo Payments checkout endpoint
Set up the following environment variables in your Cloudflare Worker:
# Dodo Payments Configuration
DODO_API_KEY=your_dodo_api_key_here
DODO_WEBHOOK_SECRET=your_webhook_secret_here
DODO_CHECKOUT_URL=https://checkout.dodopayments.com/your_checkout_id
DODO_CHECKOUT_REDIRECT_URL=https://yourapp.com/success
# Application Configuration
PAYMENT_CALLBACK_URL=https://your-worker.your-subdomain.workers.dev/webhook
KV_NAMESPACE=your_kv_namespace_name
ENVIRONMENT=production
- In your Dodo Payments dashboard, go to Webhooks
- Add a new webhook endpoint:
https://your-worker.your-subdomain.workers.dev/webhook
- Select the events you want to receive (subscription created, payment succeeded, etc.)
- Save your webhook configuration
main.go
- Entry point of the applicationinternal/app
- Application instanceinternal/config
- Configuration for the applicationinternal/db
- Database instanceinternal/dto
- Data transfer objectsinternal/handler
- HTTP handlersinternal/service
- Business logicinternal/payments
- Dodo Payments integrationpkg
- Helper functionswrangler.toml
- Cloudflare Worker configuration- Config struct - Environment variables for Dodo Payments integration:
DODO_API_KEY
- API key for Dodo PaymentsDODO_WEBHOOK_SECRET
- Secret for webhook signature verificationDODO_CHECKOUT_URL
- Dodo Payments checkout page URLDODO_CHECKOUT_REDIRECT_URL
- Where to redirect after successful paymentPAYMENT_CALLBACK_URL
- Your webhook endpoint URLKV_NAMESPACE
- Cloudflare KV storage namespace for data persistence
Command | Description |
---|---|
npm start |
Run development server with hot reload |
go run . |
Run server locally (without Cloudflare features) |
npm run build |
Build Go code to WebAssembly |
npm run deploy |
Deploy to Cloudflare Workers |
- Free plan: 3MB maximum
- Paid plan: 10MB maximum
If your Go application with dependencies exceeds these limits, consider using the TinyGo template instead, which produces smaller binaries.
The main.go
file contains an HTTP server with Dodo Payments integration. You can modify it to:
- Add new API endpoints for your business logic
- Customize webhook event handling
- Implement different subscription tiers or products
- Add user authentication and authorization
- Connect to databases or external APIs
- Process different payment events (subscriptions, one-time payments, etc.)
- Customer initiates checkout - Your app sends customer data to
/checkout
- Redirect to Dodo Payments - Customer completes payment on Dodo's secure checkout page
- Webhook notification - Dodo Payments sends payment events to
/webhook
- Process and store - Your Worker processes the event and updates customer data in Cloudflare KV
- Customer redirect - Customer is redirected back to your success page
- Set up Dodo Payments - Register and get your API credentials
- Configure environment variables - Add all required Dodo Payments settings
- Edit
main.go
- Implement your business logic and payment handling - Test locally - Use
npm start
and test with webhook tools like ngrok - Deploy - Use
npm run deploy
to publish your Worker - Monitor - Check your Worker logs and Dodo Payments dashboard
For more advanced usage, check out:
workers
package documentation- Dodo Payments API documentation
- Cloudflare Workers documentation