This project demonstrates a PayPal subscription system using a NextJS frontend and Rails API backend.
Reference: https://blog.ademartutor.com/p/implementing-paypal-subscriptions
subscription-frontend/
: NextJS frontend applicationsubscription-api/
: Rails API backend application
- Node.js (v18+)
- Ruby (v3.0+)
- Rails (v7.0+)
- PostgreSQL
- PayPal Developer Account (for API credentials and subscription plans)
-
Navigate to the backend directory:
cd subscription-api
-
Install dependencies:
bundle install
-
Set up the database:
rails db:create db:migrate
-
Start the Rails server:
rails s -p 3001
-
Navigate to the frontend directory:
cd subscription-frontend
-
Install dependencies:
npm install
-
Create a
.env.local
file with your PayPal credentials (as described in Environment Variables section below) -
Start the development server:
npm run dev
Create a .env.local
file in the root of the subscription-frontend
directory with the following content:
NEXT_PUBLIC_PAYPAL_CLIENT_ID=your_paypal_client_id
NEXT_PUBLIC_PAYPAL_BASIC_PLAN_ID=your_basic_plan_id
NEXT_PUBLIC_PAYPAL_PREMIUM_PLAN_ID=your_premium_plan_id
NEXT_PUBLIC_API_URL=http://localhost:3001/api
For production, create a .env.production
file with your production values:
NEXT_PUBLIC_PAYPAL_CLIENT_ID=your_production_paypal_client_id
NEXT_PUBLIC_PAYPAL_BASIC_PLAN_ID=your_production_basic_plan_id
NEXT_PUBLIC_PAYPAL_PREMIUM_PLAN_ID=your_production_premium_plan_id
NEXT_PUBLIC_API_URL=https://your-production-api.com/api
The Rails API application uses encrypted credentials to store sensitive information:
-
Check if
config/master.key
exists. If not, generate new credentials:cd subscription-api rails credentials:edit
-
Add your PayPal credentials in this format:
paypal_client_id: your_paypal_client_id paypal_client_secret: your_paypal_client_secret api_key: your_api_key_for_receipt_downloads
-
Save and close the editor. This will encrypt your credentials.
For additional environment configuration, create a .env
file in the subscription-api
root:
# Database configuration (if needed)
DATABASE_URL=postgres://username:password@localhost:5432/subscription_development
# Rails environment
RAILS_ENV=development
- Create a PayPal Developer account at developer.paypal.com
- Create an app in the PayPal Developer Dashboard to get your client ID and secret
- Set up subscription plans in the PayPal dashboard and note their IDs for your environment variables
- Never commit environment files (
.env*
) orconfig/master.key
to your repository - For production deployment, set environment variables on your hosting platform rather than using files
- For the Rails app, ensure
RAILS_MASTER_KEY
is properly set in your production environment
- Subscription Plans: Basic Plan ($139/month) and Premium Plan ($199/month)
- PayPal Integration: Seamless subscription creation via PayPal
- Billing Page: View subscription details and payment history
- Receipt Generation: Background job for receipt generation
GET /api/health
: Health check endpointPOST /api/company_subscription
: Create a new subscriptionGET /api/payments
: Get payment historyGET /api/receipts/:id
: Get receipt details for a payment
- Configure your production database in
config/database.yml
- Add your PayPal credentials to Rails credentials
- Deploy to your preferred hosting platform (Heroku, AWS, etc.)
- Run database migrations on the production server
- Set up a job scheduler for background jobs (such as Sidekiq)
- Ensure
RAILS_MASTER_KEY
is set in your production environment
-
Build the NextJS app:
npm run build
-
Deploy the built files to your preferred hosting platform
-
Set the environment variables for your production environment
- Set up SSL for secure communication
- Configure CORS properly for the API
- Implement proper error monitoring and logging
- Consider implementing a centralized logging system
- Set up automated backups for the database
cd subscription-api
rails test
cd subscription-frontend
npm test
This is a demonstration project and not intended for production use without additional security and feature enhancements.
The PayPal integration is using the sandbox mode for testing purposes.