Overview
The Payment service processes payments for orders. It validates order state, checks reservations, simulates payment gateway interactions, and publishes payment events to Kafka for downstream processing. Port: 5004Database Schema:
bc_paymentDependencies: PostgreSQL, Redis, Kafka, Ordering Service, Inventory Service
Responsibilities
- Process payment transactions for orders
- Validate order state before payment
- Verify reservation validity
- Simulate payment gateway integration (development mode)
- Track payment status (pending, succeeded, failed)
- Publish
payment-succeededandpayment-failedevents to Kafka - Store payment records with metadata
API Endpoints
Process Payment
Processes a payment for an order. Validates order state, checks reservation, simulates payment, and publishes events.
The ID of the order to pay for
The customer making the payment
The associated reservation ID
Payment amount (must be > 0)
Currency code
Payment method (e.g., “credit_card”, “debit_card”)
~/workspace/source/services/payment/src/Api/Controllers/PaymentsController.cs
Whether the payment succeeded
The payment object with details
Error message if payment failed
- 400 Bad Request: Validation errors (order state, reservation, etc.)
- 401 Unauthorized: Customer mismatch
- 404 Not Found: Order not found
- 422 Unprocessable Entity: Payment gateway failure
- 500 Internal Server Error: Other failures
Domain Models
Payment
Represents a payment transaction.Unique payment identifier
Reference to the order being paid
Customer making the payment
Associated reservation ID
Payment amount
Currency code (default: “USD”)
Payment method used
Payment status:
pending, succeeded, or failedError code if payment failed
Error message if payment failed
Detailed failure reason
When the payment was created
When the payment was processed
Whether this is a simulated payment (development mode)
Simulated gateway response data
~/workspace/source/services/payment/src/Domain/Entities/Payment.cs
Configuration
Database Connections
appsettings.json
Kafka Configuration
External Service URLs
Redis Configuration
Payment Processing Flow
- Validate request: Check OrderId, CustomerId, Amount, PaymentMethod
- Fetch order: Query Ordering service for order details
- Validate order state: Ensure order is in
pendingstate - Check reservation: Verify reservation is still active (if provided)
- Process payment: Simulate payment gateway call (or real gateway in production)
- Update payment record: Mark as
succeededorfailed - Publish event: Send
payment-succeededorpayment-failedto Kafka - Return response: Return payment result to client
Kafka Integration
Published Events:- payment-succeeded: Published when payment completes successfully
- Consumed by Ordering service (updates order to
paid) - Consumed by Fulfillment service (triggers ticket generation)
- Consumed by Ordering service (updates order to
- payment-failed: Published when payment fails
- Consumed by Ordering service (may cancel order or allow retry)
Payment Gateway Simulation
In development mode, the service simulates payment processing:- All payments are marked as simulated (
IsSimulated = true) - Success rate can be configured for testing failure scenarios
- Simulated response data is stored in
SimulatedResponsefield - Production mode would integrate with real payment gateways (Stripe, PayPal, etc.)
Use Cases
- ProcessPayment: Validates order, processes payment, publishes result events
Architecture Notes
- Uses MediatR for CQRS-style command/query handling
- Infrastructure services registered via
AddInfrastructure()extension method - Calls external services (Ordering, Inventory) via HTTP clients
- Uses Kafka for event-driven communication with downstream services
- Supports simulated payment gateway for development/testing
