Security

How to Verify Razorpay Payments in ASP.NET Core

DEV Community: #csharp Naheel Muhammed
Security

A .NET 8 store's payment service decides whether a Razorpay order is actually paid: it recomputes the HMAC-SHA256 signature on the server, converts totals to integer paise and calls Razorpay's API directly when no signature exists.

Key points

  • The verify endpoint accepts RazorpayOrderId, RazorpayPaymentId, RazorpaySignature and an optional Amount, and treats all of them as untrusted input.
  • Razorpay's HMAC-SHA256 signature covers only orderId|paymentId, so a valid signature never proves how much money moved.
  • Amounts are converted once to integer paise with Math.Round and MidpointRounding.AwayFromZero; truncation could turn 4398.90m into 439889.
  • The amount comparison uses >=, so underpayment fails hard while overpayment does not leave an order stuck in pending.
  • Payment-link flow has no signature, so TryFetchPaymentLinkAsync returns null on timeout, 500 or DNS failure and the order stays unpaid.
  • A guard throws PaymentAlreadyVerified when a paid payment row has an unconfirmed order, blocking a second link from charging twice.

Why it matters

Anyone wiring a payment gateway into ASP.NET Core meets the same fork: trust the callback, or re-derive the truth on the server. This article takes the second path and shows that two separate proofs are needed — one for the identifier, one for the money — rather than a single 'verified' flag.

Before copying it, read your own gateway's signature payload: if the amount is excluded, as with Razorpay, the amount check is mandatory. Test the paise conversion with values like 4398.90m, keep the key secret off the client, and plan a webhook so orders don't wait for the customer to come back.

Real-world use

  • An e-commerce checkout where the browser posts a success callback and the server must decide on its own whether the order is paid.
  • A Razorpay payment-link flow that returns no signature, so payment status has to be fetched server-to-server.
  • Customers who retry a failed payment, where the cart must be restored by merging lines instead of duplicating them.
  • A partially failed flow where money was recorded but the order was never confirmed, so a second link must be refused.

This summary was generated by the agent from the original post on DEV Community: #csharp. The full text and all rights belong to its author and publisher. Go to original

Report this story

Is something wrong with the summary, or do you own this content? Your message comes straight to my inbox.

Type of message

Related stories

Feedback

Found a bug or have an idea? Your message lands straight in my inbox.

Type of message