A simplified payment switch built in Rust that demonstrates the core concepts of payment processing, connector routing, and database persistence.
- Web Server: Built with Axum framework
- Payment Processing: Mock connectors for Stripe and Adyen
- Database: PostgreSQL integration with SQLx
- Connector Routing: Automatic connector selection based on currency
- Async Processing: Full async/await support
- Logging: Comprehensive logging with tracing
┌─────────────┐ ┌─────────────┐ ┌─────────────┐
│ Client │───▶│ Axum │───▶│ Payment │
│ │ │ Server │ │ Handler │
└─────────────┘ └─────────────┘ └─────────────┘
│ │
▼ ▼
┌─────────────┐ ┌─────────────┐
│ PostgreSQL │ │ Connector │
│ Database │ │ Router │
└─────────────┘ └─────────────┘
│
▼
┌─────────────────┐
│ Mock Connectors │
│ │
│ Stripe Adyen │
└─────────────────┘
- Rust: Install Rust from rustup.rs
- jq (optional): For formatted JSON output in demo scripts
- macOS:
brew install jq - Ubuntu:
sudo apt-get install jq
- macOS:
-
Clone and build:
cargo build
-
Set up environment variables (optional): Create a
.envfile in the project root for custom logging:RUST_LOG=info
-
Run the application:
cargo run
The server will start on http://127.0.0.1:3000
GET /health
Returns 200 OK if the server is running.
POST /payments
Request Body:
{
"amount": 1000,
"currency": "USD",
"payment_method": "card"
}Response:
{
"payment_id": "550e8400-e29b-41d4-a716-446655440000",
"status": "succeeded",
"amount": 1000,
"currency": "USD",
"connector_used": "stripe",
"created_at": "2024-01-01T12:00:00Z"
}The system automatically selects payment connectors based on currency:
- USD, EUR, GBP: Routes to Mock Stripe (80% success rate)
- Other currencies: Routes to Mock Adyen (75% success rate)
This Project uses in-memory storage for simplicity. In a production environment, you would integrate with PostgreSQL or another database.
pub struct Payment {
pub id: Uuid,
pub amount: i64,
pub currency: String,
pub payment_method: String,
pub status: PaymentStatus,
pub connector_used: String,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}Run the demo script to see the API in action:
./demo.sh# Test health endpoint
curl http://localhost:3000/health
# Test payment processing
curl -X POST http://localhost:3000/payments \
-H "Content-Type: application/json" \
-d '{
"amount": 2500,
"currency": "USD",
"payment_method": "card"
}'# Test health endpoint
http GET localhost:3000/health
# Test payment processing
http POST localhost:3000/payments \
amount:=2500 \
currency=USD \
payment_method=cardmicro-hyperswitch/
├── src/
│ ├── main.rs # Application entry point
│ ├── models.rs # Data structures
│ ├── handlers.rs # HTTP request handlers
│ ├── database.rs # Database operations
│ └── connectors/
│ ├── mock_stripe.rs # Mock Stripe connector
│ └── mock_adyen.rs # Mock Adyen connector
├── migrations/ # Database migrations (for future use)
│ └── 20240101000000_create_payments_table.sql
├── Cargo.toml
├── README.md
├── demo.sh # Interactive demo script
└── test_api.sh # API testing script
- Payment Switch Logic: Automatic routing to different payment processors
- Async Processing: Non-blocking payment processing with delays
- Database Persistence: Storing payment records and status updates
- Error Handling: Graceful handling of connector failures
- Logging: Comprehensive logging for debugging and monitoring
- RESTful API: Clean HTTP API design
- Add more mock connectors (PayPal, Square, etc.)
- Implement webhook notifications
- Add payment capture functionality
- Implement retry logic for failed payments
- Add authentication and authorization
- Implement payment refunds
- Add metrics and monitoring
This is a learning project to understand payment switch architecture. Feel free to experiment and extend the functionality!