A comprehensive real estate management system with integrated M-Pesa payment processing, built with Spring Boot.
-
Property Management: Complete property and unit management
-
Buyer Management: Customer profiles and purchase tracking
-
Invoice System: Automated invoice generation and management
-
M-Pesa Integration: STK Push payments with real-time status tracking
-
Payment Processing: Multi-method payment support
-
RESTful API: Comprehensive REST API with OpenAPI documentation
-
Security: Spring Security integration with CORS support
-
Monitoring: Health checks and metrics with Actuator
-
Database: MySQL database integration with Spring Data JPA
-
Email Notifications: Email notifications for important events
-
Logging: Logging with Logback and SLF4J
-
Testing: Unit and integration testing with JUnit, Mockito, and TestContainers
During the development of this project, I gained significant insights into building backend applications with Spring Boot. Here are the key resources and references that guided my learning process:
-
Spring Boot Tutorial: A Comprehensive Guide for Beginners
This guide offered a clear and practical introduction to Spring Boot, helping me understand the foundational concepts and best practices. -
Books
- Spring Boot in Action
This book provided in-depth coverage of Spring Boot features and real-world usage scenarios. - Baeldung (baeldung.com)
Baeldung articles were invaluable for quick tips, best practices, and deep dives into specific Spring Boot topics.
- Spring Boot in Action
-
Official Documentation
- Spring Boot Reference Documentation
The official docs were crucial for understanding advanced configurations and troubleshooting.
- Spring Boot Reference Documentation
- Setting up and structuring a Spring Boot application.
- Working with RESTful APIs using Spring MVC.
- Implementing data persistence with Spring Data JPA.
- Understanding dependency injection and application context.
- Managing application properties and external configurations.
- Securing endpoints and handling authentication.
- Testing Spring Boot applications efficiently.
These resources and experiences have significantly strengthened my backend development skills and deepened my understanding of the Spring Boot ecosystem.
- Java 17+
- Maven 3.6+
- MySql 12+
- Docker & Docker Compose (optional)
-
Clone the repository
git clone https://github.com/Amarsalim30/real-estate-app-backend.git cd real-estate-app-backend
-
Run setup script
chmod +x scripts/setup-dev.sh ./scripts/setup-dev.sh
-
Configure M-Pesa credentials
Update
.env.dev
with your M-Pesa sandbox credentials:MPESA_CONSUMER_KEY=your_consumer_key MPESA_CONSUMER_SECRET=your_consumer_secret MPESA_SHORTCODE=174379 MPESA_PASSKEY=your_passkey MPESA_CALLBACK_URL=https://your-domain.com/api/payments/mpesa/callback
-
Start the application
source .env.dev ./mvnw spring-boot:run
-
Access the application
- API Documentation: http://localhost:8080/swagger-ui.html
- Health Check: http://localhost:8080/actuator/health
- Models: JPA entities for data persistence
- Repositories: Data access layer with Spring Data JPA
- Services: Business logic layer with transaction management
- Controllers: REST API endpoints with validation
- DTOs: Data transfer objects for API communication
The application provides comprehensive M-Pesa STK Push integration:
- STK Push Initiation: Trigger payment requests to customer phones
- Callback Handling: Process M-Pesa payment confirmations
- **Status Que
- Status Queries: Real-time payment status checking
- Payment Processing: Automatic invoice updates and payment records
- Error Handling: Comprehensive error management and retry logic
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ BuyerProfile │ │ Invoice │ │ Payment │
├─────────────────┤ ├─────────────────┤ ├─────────────────┤
│ id (PK) │◄──┤ buyer_id (FK) │◄──┤ invoice_id (FK) │
│ email │ │ unit_id (FK) │ │ buyer_id (FK) │
│ phone │ │ total_amount │ │ amount │
│ address │ │ status │ │ status │
│ national_id │ │ issued_date │ │ method │
│ kra_pin │ └─────────────────┘ │ payment_date │
└─────────────────┘ └─────────────────┘
│
┌─────────────────┐
│ PaymentDetail │
├─────────────────┤
│ payment_id (FK) │
│ transaction_id │
│ card_details │
│ bank_details │
└─────────────────┘
│
┌─────────────────┐
│ MpesaPayment │
├─────────────────┤
│ checkout_req_id │
│ phone_number │
│ mpesa_receipt │
│ callback_data │
└─────────────────┘
# Database Configuration
spring:
datasource:
url: jdbc:MySql://localhost:5432/real_estate_db
username: ${DATABASE_USERNAME}
password: ${DATABASE_PASSWORD}
# M-Pesa Configuration
mpesa:
consumer-key: ${MPESA_CONSUMER_KEY}
consumer-secret: ${MPESA_CONSUMER_SECRET}
shortcode: ${MPESA_SHORTCODE}
passkey: ${MPESA_PASSKEY}
base-url: https://sandbox.safaricom.co.ke # Use https://api.safaricom.co.ke for production
callback-url: ${MPESA_CALLBACK_URL}
Variable | Description | Required |
---|---|---|
DATABASE_URL |
MySql connection URL | Yes |
DATABASE_USERNAME |
Database username | Yes |
DATABASE_PASSWORD |
Database password | Yes |
MPESA_CONSUMER_KEY |
M-Pesa API consumer key | Yes |
MPESA_CONSUMER_SECRET |
M-Pesa API consumer secret | Yes |
MPESA_SHORTCODE |
M-Pesa business shortcode | Yes |
MPESA_PASSKEY |
M-Pesa passkey | Yes |
MPESA_CALLBACK_URL |
M-Pesa callback URL | Yes |
Method | Endpoint | Description |
---|---|---|
POST |
/api/payments/mpesa/stk-push |
Initiate STK Push payment |
POST |
/api/payments/mpesa/callback |
M-Pesa callback webhook |
GET |
/api/payments/mpesa/status/{checkoutRequestId} |
Get payment status |
GET |
/api/payments/mpesa/buyer/{buyerId} |
Get buyer's payment history |
GET |
/api/payments/mpesa/invoice/{invoiceId}/summary |
Get invoice payment summary |
curl -X POST http://localhost:8080/api/payments/mpesa/stk-push \
-H "Content-Type: application/json" \
-d '{
"phoneNumber": "254708374149",
"amount": 1000.00,
"invoiceId": 1,
"accountReference": "INV-001",
"transactionDesc": "Payment for property unit"
}'
{
"success": true,
"message": "STK Push initiated successfully",
"data": {
"checkoutRequestId": "ws_CO_123456789012_1234567890",
"merchantRequestId": "12345-67890-12345",
"responseCode": "0",
"responseDescription": "Success. Request accepted for processing",
"customerMessage": "Success. Request accepted for processing"
},
"timestamp": "2023-12-01T10:30:00"
}
# Run all tests
./mvnw test
# Run specific test class
./mvnw test -Dtest=MpesaStkServiceTest
# Run tests with coverage
./mvnw test jacoco:report
- Unit Tests: Service layer business logic
- Integration Tests: Database and external API interactions
- Controller Tests: REST endpoint validation
- Security Tests: Authentication and authorization
For M-Pesa integration testing:
- Use Sandbox Environment: Configure sandbox URLs and credentials
- Test Phone Numbers: Use Safaricom provided test numbers
- Mock Responses: Use WireMock for isolated testing
- Callback Testing: Use ngrok for local callback testing
# Install ngrok
npm install -g ngrok
# Expose local server
ngrok http 8080
# Update callback URL in configuration
MPESA_CALLBACK_URL=https://your-ngrok-id.ngrok.io/api/payments/mpesa/callback
# Build and deploy with Docker Compose
chmod +x scripts/deploy.sh
./scripts/deploy.sh
# Build application
./mvnw clean package -DskipTests
# Run database migrations
./mvnw flyway:migrate
# Start application
java -jar target/real-estate-app-0.0.1-SNAPSHOT.jar
- Database: Use managed MySql service (AWS RDS, Google Cloud SQL)
- Security: Enable HTTPS and configure proper CORS policies
- Monitoring: Set up application monitoring (Prometheus, Grafana)
- Logging: Configure centralized logging (ELK Stack)
- Backup: Implement database backup strategies
- Scaling: Use load balancers and multiple application instances
- Spring Security integration
- JWT token support (configurable)
- Role-based access control
- CORS configuration for cross-origin requests
- Secure credential management
- Request signing and validation
- Callback URL verification
- Rate limiting and fraud prevention
- Environment-based configuration
- Secure password storage
- API rate limiting
- Input validation and sanitization
- SQL injection prevention
# Application health
curl http://localhost:8080/actuator/health
# Database connectivity
curl http://localhost:8080/actuator/health/db
# Custom health indicators
curl http://localhost:8080/actuator/health/mpesa
- Application metrics via Micrometer
- Prometheus integration
- Custom business metrics
- Payment processing metrics
- Structured logging with Logback
- Request/response logging
- M-Pesa transaction logging
- Error tracking and alerting
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature
- Commit changes:
git commit -m 'Add amazing feature'
- Push to branch:
git push origin feature/amazing-feature
- Open a Pull Request
- Follow Spring Boot best practices
- Write comprehensive tests
- Update documentation
- Use conventional commit messages
- Ensure code coverage > 80%
This project is licensed under the MIT License - see the LICENSE file for details.
M-Pesa Integration Issues:
- Verify sandbox/production URLs
- Check credential configuration
- Validate callback URL accessibility
- Review M-Pesa API documentation
Database Issues:
- Ensure MySql is running
- Verify connection parameters
- Check database permissions
- Run Flyway migrations
Application Startup Issues:
- Verify Java 17 installation
- Check port availability
- Review application logs
- Validate environment variables
- 📧 Email: [email protected]
- 🐛 Issues: GitHub Issues
- 📖 Documentation: Wiki
- ✅ Core real estate management
- ✅ M-Pesa STK Push integration
- ✅ Payment processing
- ✅ API documentation
- ✅ Email notifications
- 🔄 Advanced reporting and analytics
- 🔄 Email Attachments Download
- 🔄 SMS integration
- 🔄 Mobile app API enhancements
- 📋 Multi-tenant support
- 📋 Advanced security features
- 📋 Integration with other payment providers
- 📋 Real-time notifications