Payment-Service — Payment Methods, Razorpay Orders & Webhooks
Responsibility
payment-service owns tenant-scoped payment configuration and the Razorpay payment-order lifecycle.
- Serves allowed-payment-methods lookup per tenant over gRPC (
paymentinfo.proto) —shop-service’s checkout resolution (GetCheckoutDetails) is the first and, today, only consumer, caching the result into its checkout-session snapshot per SR-012 FR-15. - Creates a Razorpay payment order for a checkout (
paymentorder.proto’sCreatePaymentOrder), wrapping Razorpay’s order-creation API behind a dedicatedrepository/razorpay/provider integration. - Receives Razorpay’s webhook (
POST /payment/webhook/razorpay), verifies its signature, and persists the resultingcaptured/failed/refund.processedevent via aWebhookEventStrategyper event type — the only REST route this service exposes. - Is a gRPC client of
order-service, notifying it of payment outcomes as part of the checkout saga. - Payment history, payment-status tracking, and payment-gateway integration beyond Razorpay order creation are future scope — not built, not scaffolded speculatively.
Tech Stack
| Concern | Choice |
|---|---|
| Language | Rust |
| gRPC framework | tonic |
| REST framework | Axum (Razorpay webhook receiver only) |
| Data store | PostgreSQL, via SeaORM |
| Template | @templates/rust-postgres-microservice |
| Server bootstrap | Both servers run concurrently via tokio::try_join! in one binary |
| Core library | rust-lang-core-lib (typed errors, logging macros, #[tracing::instrument], Pub/Sub publisher) |
| Observability | Tracing + OpenTelemetry |
Ports
| Gateway | HTTP (host → container) | gRPC (host → container) | Service Name |
|---|---|---|---|
development/nginx.conf routes POST /payment/webhook/razorpay only; every other capability is gRPC-only |
8094 |
50060 → 50053 |
payment-service |
Data Model — PostgreSQL
| Table | Purpose |
|---|---|
allowed_payment_methods |
Tenant-scoped list of enabled payment methods, served by PaymentInfoService |
payment_gateway_orders |
One row per Razorpay order created via CreatePaymentOrder |
razorpay_events |
Raw webhook event record, keyed by Razorpay’s own event id for idempotency |
payment_ledger |
Derived ledger entries (capture/failure/refund) written atomically alongside razorpay_events |
PaymentEventRepositoryTrait/PaymentLedgerRepositoryTrait deviate from this service’s other repositories by taking an explicit &DatabaseTransaction parameter, so a webhook’s event record and its ledger entry commit atomically — both writes belong to a single outcome, so a partial commit would leave the ledger and the raw event record disagreeing.
gRPC Contract
paymentinfo.proto— tenant-scoped allowed-payment-methods lookup.paymentorder.proto—CreatePaymentOrder, the only write path intopayment_gateway_orders;RefundPayment, called byorder-service’s checkout saga as its fulfillment-failure compensating action — see SR-017 FR-04.RefundPayment’s own response is not a ledger write — thepayment_ledgerentry is written later, only once Razorpay’srefund.processedwebhook is processed.
CreatePaymentOrder implementation
Follows the repository-per-provider pattern (see the platform’s layered architecture standard “Repository wraps every outbound call” section): repository/razorpay/ (Razorpay’s own wire-format DTOs, one method per Razorpay endpoint — currently just create_order, since only one Razorpay endpoint is integrated today) is a separate repository from repository/paymentorder/ (the Postgres persistence for payment_gateway_orders). service/ never holds a raw reqwest::Client directly — the Razorpay HTTP call is wrapped behind RazorpayRepositoryTrait, composed via a build(cfg) -> Arc<dyn RazorpayRepositoryTrait> factory.
Razorpay webhook implementation
POST /payment/webhook/razorpay — controller → parser → signature-verifier → strategy dispatch:
- Signature verification — the raw request body is HMAC-verified against Razorpay’s webhook secret before any parsing proceeds.
- Parsing — the verified payload is decoded into a typed event.
- Strategy dispatch — a
WebhookEventStrategytrait with one implementation per event type (PaymentCapturedStrategy,PaymentFailedStrategy,RefundProcessedStrategy) persists the event and its ledger entry inside one transaction. Adding a new Razorpay event type adds a new strategy implementation, never a branch inside an existing one.
Document audit trail — message_publisher/documentaudit_publisher.rs
payment-service is the first instrumented publisher into document-service’s audit trail sink, and the reference Rust implementation the pattern is documented against.
- A
DocumentAuditPublisherTraitwith apublish_audit_eventmethod, callingrust_lang_core_lib::pubsub::publish— never a raw Pub/Sub client held inservice/. - Two call sites publish fire-and-forget via
tokio::spawn: Razorpay order creation (transaction_name: "create_payment_order") and the webhook handler (one event per Razorpay webhook delivery). Both send the full, unmasked request/response JSON — masking happens exclusively insidedocument-service, never here. - A publish failure is logged and never propagates into the RPC/webhook response that triggered it, per SR-002 NFR-01.
RefundPaymentis not a third call site. UnlikeCreatePaymentOrder, theRefundPaymentRPC implementation has no audit-publish call of its own — the only audit record of a refund comes later, from the webhook handler picking up Razorpay’srefund.processedevent. Tracked as a gap, not an oversight — see Technical Debt — Payment Transaction Ledger & Refund Audit Gaps, TD-01.
Implementation Notes
- The webhook route is the only REST surface in an otherwise gRPC-only service — everything else (
paymentinfo,paymentorder) is served over gRPC, running alongside the Axum webhook server in the same binary viatokio::try_join!. PaymentEventRepositoryTrait/PaymentLedgerRepositoryTrait’s explicit-transaction parameter is a deliberate deviation from this service’s other repositories (which don’t take a transaction handle) — needed specifically because a webhook’s raw event and its derived ledger entry must commit together or not at all.
Related Documents
- BRD-015 — Payment Capture & Confirmation
- SR-016 — Payment Capture & Confirmation System Requirements
- BRD-016 — Payment Transaction Ledger, Refund Guarantee & Data Security
- SR-017 — Payment Transaction Ledger & Security System Requirements
- ADR-00028 — Append-Only, Webhook-Anchored Ledger vs. Synchronous or Mutable-Balance Alternatives for Payment Transaction Records
- Architecture — Payment Transaction Ledger, Refund Compensation & Gateway Audit Trail
- Technical Debt — Payment Transaction Ledger & Refund Audit Gaps
- BRD-013 — Checkout Completion: Real Payment Capture and Reliable Fulfillment Handoff
- SR-014 — Checkout Completion: Payment Capture & Fulfillment Handoff System Requirements
- SR-012 — Reusable Checkout Orchestration: System Requirements —
shop-service’s checkout consumption of the allowed-payment-methods lookup (FR-15) - order-service
- shop-service — consumer of this service’s allowed-payment-methods lookup during checkout resolution
Full service reference: payment-service/CLAUDE.md. Audit-trail pipeline overview: ADR-00002 — Subscribe-Only Audit Sink, Mask-Once-At-Persistence.