Skip to main content

Payment Service ๐Ÿ’ณ

The Payment Service is responsible for handling payment processing and payment lifecycle management in the ShopVerse platform.

It integrates with external payment gateways (Razorpay), maintains payment state, and publishes events for downstream services such as Notification and Analytics.


๐ŸŽฏ Responsibilitiesโ€‹

The Payment Service handles:

  • Payment order creation
  • Integration with Razorpay
  • Payment status tracking
  • Handling payment callbacks/webhooks
  • Publishing payment-related events
  • Providing payment history to users

๐Ÿง  Why a Separate Payment Service?โ€‹

Payments are:

  • Security-sensitive
  • External-system dependent
  • Failure-prone

Separating them into a dedicated service ensures:

  • Clear isolation of payment logic
  • Safer handling of third-party integrations
  • Independent scaling
  • Easier compliance and auditing

๐Ÿ—๏ธ High-Level Architectureโ€‹


๐Ÿ” Security Modelโ€‹

  • All APIs are accessed via API Gateway

  • Gateway injects:

    • X-User-Email
    • X-User-Role
  • Only authenticated users can initiate payments

  • Admin access is restricted to reporting APIs

  • No payment secrets exposed to clients


๐Ÿ” Payment Flow (Order โ†’ Payment)โ€‹


๐Ÿ—„๏ธ Data Modelโ€‹

Payment Entityโ€‹

  • id
  • orderId
  • customerId (email)
  • amount
  • razorpayOrderId
  • razorpayPaymentId
  • status (CREATED, SUCCESS, FAILED)
  • createdAt

Payment records are immutable after success.


๐Ÿ“ก Kafka Events Publishedโ€‹

Event TypeTopicDescription
PAYMENT_CREATEDpayment-eventsPayment initiated
PAYMENT_SUCCESSpayment-eventsPayment completed
PAYMENT_FAILEDpayment-eventsPayment failure

Consumers:

  • Notification Service
  • Analytics Service
  • Order Service (optional status sync)

๐ŸŒ APIsโ€‹

Customer APIsโ€‹

MethodEndpointDescription
POST/api/payments/createCreate payment order
GET/api/payments/myGet user payment history

Callback API (Internal)โ€‹

MethodEndpointDescription
POST/api/payments/callbackRazorpay webhook

Webhook endpoints are not exposed publicly.


๐Ÿ”„ Payment Status Lifecycleโ€‹

  • CREATED โ†’ Payment order generated
  • SUCCESS โ†’ Payment confirmed via callback
  • FAILED โ†’ Payment failed or cancelled

๐Ÿ›ก๏ธ Reliability & Safety Measuresโ€‹

  • Razorpay signature verification
  • Idempotent callback handling
  • Payment state checks before updates
  • No duplicate payment confirmations
  • Secure secret storage (env variables)

โš ๏ธ Failure Scenariosโ€‹

โŒ Razorpay API Failureโ€‹

  • Payment creation fails
  • User receives retry message

โŒ Callback Delayโ€‹

  • Payment remains in CREATED
  • Callback reconciles later

โŒ Duplicate Callbackโ€‹

  • Idempotency prevents duplicate updates

โš™๏ธ Key Componentsโ€‹

  • PaymentController โ€“ Payment APIs
  • PaymentService โ€“ Core payment logic
  • RazorpayClientConfig โ€“ Gateway integration
  • PaymentRepository โ€“ JPA persistence
  • KafkaEventPublisher โ€“ Event publishing
  • GlobalExceptionHandler โ€“ Error handling

๐Ÿ“ˆ Scalability Considerationsโ€‹

  • Stateless service
  • Async callbacks via Razorpay
  • Kafka decouples post-payment workflows
  • Can handle high payment concurrency

๐Ÿงช Testing Strategyโ€‹

  • Unit tests for payment creation
  • Mock Razorpay responses
  • Callback signature validation tests
  • Failure and retry test cases

๐Ÿ“Œ Summaryโ€‹

The Payment Service ensures:

  • Secure and reliable payment processing
  • Clean integration with third-party gateways
  • Event-driven extensibility
  • Accurate payment state tracking

It plays a critical role in completing the order lifecycle in ShopVerse.