A RESTful API for small businesses to accept payments from customers using PayPal Sandbox.
- Process payments with minimal customer information (name, email, amount)
- Integration with PayPal Sandbox for testing
- RESTful API with versioning
- Automated testing and deployment through CI/CD
POST /api/v1/payments/
Request Body:
{
"customer_name": "John Doe",
"customer_email": "[email protected]",
"amount": 50.00,
"currency": "USD"
}Response:
{
"payment": {
"id": "PAY-12345678",
"customer_name": "John Doe",
"customer_email": "[email protected]",
"amount": 50.00,
"currency": "USD",
"status": "processing"
},
"redirect_url": "https://www.sandbox.paypal.com/checkoutnow?token=ABC123XYZ",
"status": "success",
"message": "Payment initiated successfully. Redirect the customer to complete payment."
}GET api/v1/payments/all/
Response:
{
"payments": [
{
"id": "bc8797f0-a847-4d96-88a2-fc3a32e1a39a",
"customer_name": "John Doe",
"customer_email": "[email protected]",
"amount": "50.00",
"currency": "USD",
"status": "failed",
"created_at": "2025-03-24T15:59:18.913491Z"
},
{
"id": "444bcdc4-0340-47eb-ac4b-cffd16f27592",
"customer_name": "John Doe",
"customer_email": "[email protected]",
"amount": "50.00",
"currency": "USD",
"status": "failed",
"created_at": "2025-03-24T16:02:46.925681Z"
},
{
"id": "03d280e2-1704-426a-a3c8-3610b7964a15",
"customer_name": "John Doe",
"customer_email": "[email protected]",
"amount": "50.00",
"currency": "USD",
"status": "failed",
"created_at": "2025-03-24T16:07:45.091864Z"
},
{
"id": "38db523e-b739-4387-972f-d1efd2317b58",
"customer_name": "John Doe",
"customer_email": "[email protected]",
"amount": "50.00",
"currency": "USD",
"status": "processing",
"created_at": "2025-03-24T16:12:20.356695Z"
}
],
"status": "success",
"message": "All payments retrieved successfully."
}- Customer submits payment information
- Your server initiates a PayPal payment and receives a redirect URL
- Redirect the customer to the PayPal page to complete payment
- PayPal redirects back to your success/cancel endpoints
- Your server captures the payment and updates the status
- Python 3.8+
- PostgreSQL
- PayPal Developer Account with Sandbox credentials
- Create a PayPal Developer account at developer.paypal.com
- Create a Sandbox app to get your CLIENT_ID and CLIENT_SECRET
- Set up a Sandbox account for testing
-
Clone the repository:
git clone https://github.com/yourusername/payment-gateway-api.git cd payment-gateway-api -
Create a virtual environment and activate it:
python -m venv venv source venv/bin/activate # On Windows, use: venv\Scripts\activate -
Install dependencies:
pip install -r requirements.txt -
Set up environment variables: Create a
.envfile in the project root with the following variables:DEBUG=True SECRET_KEY=your_secret_key DATABASE_URL=postgres://user:password@localhost:5432/payment_gateway_db BASE_URL=http://localhost:8000 # PayPal Sandbox API Credentials PAYPAL_CLIENT_ID=your_paypal_sandbox_client_id PAYPAL_CLIENT_SECRET=your_paypal_sandbox_client_secret PAYPAL_API_URL=https://api-m.sandbox.paypal.com -
Apply migrations:
python manage.py migrate -
Run the development server:
python manage.py runserver
- Use the
/api/v1/paymentsendpoint to create a payment - Use the returned
redirect_urlto simulate a customer payment - Log in with your PayPal Sandbox buyer account
- Complete the payment
- PayPal will redirect to your success URL
- Check the payment status using the payment ID
Run the tests with:
python manage.py test
This project uses GitHub Actions for continuous integration and deployment:
- Test: Runs the Django test suite on each push and pull request
- Deploy: Automatically deploys to Render when changes are pushed to the main branch
When ready to move to production:
- Create a PayPal live account and obtain production credentials
- Update your environment variables with the production credentials
- Change the PayPal API URL to the production endpoint
- Ensure your server has HTTPS enabled for secure payments
- Thoroughly test the payment flow before going live
This project is licensed under the MIT License - see the LICENSE file for details.