<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Moksh Upadhyay</title>
    <description>The latest articles on DEV Community by Moksh Upadhyay (@mokshupadhyay).</description>
    <link>https://dev.to/mokshupadhyay</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4267530%2F500ce315-b7cd-4299-9626-bdf70e63eb16.jpg</url>
      <title>DEV Community: Moksh Upadhyay</title>
      <link>https://dev.to/mokshupadhyay</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mokshupadhyay"/>
    <language>en</language>
    <item>
      <title>Verifying payment webhooks from Indian gateways</title>
      <dc:creator>Moksh Upadhyay</dc:creator>
      <pubDate>Thu, 26 Nov 2026 18:41:30 +0000</pubDate>
      <link>https://dev.to/mokshupadhyay/verifying-payment-webhooks-from-indian-gateways-98e</link>
      <guid>https://dev.to/mokshupadhyay/verifying-payment-webhooks-from-indian-gateways-98e</guid>
      <description>&lt;h2&gt;
  
  
  In short
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Six gateways use five different ways to prove a webhook is real. What matters is what each proof covers.&lt;/li&gt;
&lt;li&gt;Razorpay and Cashfree sign the whole body. Only Cashfree also signs a timestamp, which stops old requests being replayed.&lt;/li&gt;
&lt;li&gt;PayU and Easebuzz hash only some fields. PhonePe and Juspay sign nothing in the body, so check the order with the gateway before you ship.&lt;/li&gt;
&lt;li&gt;Verify the raw bytes, de-duplicate on signed fields, and handle amounts as integer paise.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Written for: Backend developers taking payments in India.&lt;/p&gt;

&lt;p&gt;A payment gateway tells your server that a customer paid by sending it a webhook: an HTTP request with the payment in the body. Your server reads it and marks the order paid. Anyone on the internet can send that same request to that same URL. The only thing that separates the real one from a forged one is the proof the gateway attaches to it.&lt;/p&gt;

&lt;p&gt;I build payment software for a living, and I assumed that proof worked the same way everywhere. It does not. Six widely used Indian gateways, Razorpay, Cashfree, PhonePe, PayU, Juspay and Easebuzz, use five different schemes. Some sign every byte. Some sign a few fields. Two sign nothing in the body at all.&lt;/p&gt;

&lt;p&gt;I read each gateway's docs and official SDK and wrote &lt;a href="https://github.com/mokshupadhyay/india-webhooks" rel="noopener noreferrer"&gt;india-webhooks&lt;/a&gt;, a small open-source package that checks all six and returns one event shape. This post is what I learned doing it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Six gateways, five schemes
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Gateway&lt;/th&gt;
&lt;th&gt;Where the proof is&lt;/th&gt;
&lt;th&gt;How it is made&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Razorpay&lt;/td&gt;
&lt;td&gt;&lt;code&gt;X-Razorpay-Signature&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;HMAC-SHA256 of the raw body, hex&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Cashfree&lt;/td&gt;
&lt;td&gt;&lt;code&gt;x-webhook-signature&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;HMAC-SHA256 of &lt;code&gt;x-webhook-timestamp&lt;/code&gt; followed by the raw body, base64&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;PayU&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;hash&lt;/code&gt; form field&lt;/td&gt;
&lt;td&gt;SHA-512 of the salt and some form fields, joined with `\&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Easebuzz&lt;/td&gt;
&lt;td&gt;{% raw %}&lt;code&gt;hash&lt;/code&gt; form field&lt;/td&gt;
&lt;td&gt;The same idea as PayU, with a different field order&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;PhonePe&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Authorization&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;SHA-256 of &lt;code&gt;username:password&lt;/code&gt; from your dashboard&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Juspay&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Authorization&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;HTTP Basic auth with the username and password from your dashboard&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The difference that matters is not the hash function. It is what the proof covers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Razorpay and Cashfree: the body is signed
&lt;/h2&gt;

&lt;p&gt;Razorpay computes an HMAC of the request body with your webhook secret and puts it in a header. You compute the same HMAC over the bytes you received and compare. If one byte of the body changed, the values will not match.&lt;/p&gt;

&lt;p&gt;Cashfree does the same, but puts a timestamp in front of the body before signing. That one change matters a lot. Cashfree's docs say to reject anything older than five minutes, so a request someone captured yesterday cannot be sent again today. Of the six, it is the only one with replay protection built into the signature.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;src/providers/cashfree.ts&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;cashfreeSignature&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;timestamp&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Uint8Array&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;secret&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;toBase64&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;hmacSha256&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;utf8&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;secret&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;concatBytes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;utf8&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;timestamp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both are simple to verify. Both are also easy to get wrong in one specific way.&lt;/p&gt;

&lt;h3&gt;
  
  
  The raw body problem
&lt;/h3&gt;

&lt;p&gt;The signature covers the exact bytes the gateway sent. Most web frameworks parse the body as JSON before your handler sees it. If you take that parsed object and turn it back into text, you get different bytes. Cashfree's own docs give the example: &lt;code&gt;"payment_amount": 170.00&lt;/code&gt; comes back as &lt;code&gt;170&lt;/code&gt;, and the signature no longer matches.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Express&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Wrong: the JSON parser has already thrown the original bytes away&lt;/span&gt;
&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'/webhooks/razorpay'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;express&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// not what Razorpay signed&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Right: keep the bytes for this route&lt;/span&gt;
&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'/webhooks/razorpay'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;express&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'*/*'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;verifyWebhook&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'razorpay'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;rawBody&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// a Buffer&lt;/span&gt;
    &lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;secret&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;RAZORPAY_WEBHOOK_SECRET&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sendStatus&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ok&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It usually goes wrong in a quiet way. Every test payload happens to re-serialise to the same text, so verification passes in development. Then a real payment with &lt;code&gt;170.00&lt;/code&gt; fails in production. Or a developer sees the failures, decides the check is flaky, and turns it off.&lt;/p&gt;

&lt;h2&gt;
  
  
  PayU and Easebuzz: only some fields are signed
&lt;/h2&gt;

&lt;p&gt;PayU and Easebuzz send webhooks as form posts, not JSON, with a &lt;code&gt;hash&lt;/code&gt; field. The hash is a SHA-512 of your salt and a fixed list of fields joined with pipes. For PayU it is:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PayU reverse hash: the fields, in order&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SALT|status||||||udf5|udf4|udf3|udf2|udf1|email|firstname|productinfo|amount|txnid|key
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The order runs backwards compared with the request hash you send when starting a payment, which is why PayU calls it a reverse hash. The five empty slots after &lt;code&gt;status&lt;/code&gt; are always empty. PayU's official SDK hard-codes them.&lt;/p&gt;

&lt;p&gt;The important part is what is missing from that list. &lt;code&gt;status&lt;/code&gt;, &lt;code&gt;amount&lt;/code&gt; and &lt;code&gt;txnid&lt;/code&gt; are covered. PayU's own payment id &lt;code&gt;mihpayid&lt;/code&gt;, the payment &lt;code&gt;mode&lt;/code&gt;, &lt;code&gt;bank_ref_num&lt;/code&gt; and &lt;code&gt;unmappedstatus&lt;/code&gt; are not. Anyone who has one real webhook can change those fields and the hash still matches.&lt;/p&gt;

&lt;p&gt;india-webhooks does exactly that, and puts every field the hash does not cover into a separate &lt;code&gt;event.unverified&lt;/code&gt; object. Your code then shows clearly which values were checked and which were not.&lt;/p&gt;

&lt;p&gt;There is one more trap in form bodies. A field can appear twice: &lt;code&gt;status=failure&amp;amp;status=success&lt;/code&gt;. Different parsers disagree on which copy wins. If the verifier hashes the first and your app reads the last, the check passes on a value your app never uses. The package rejects any body with a repeated field name, even when the copies are equal.&lt;/p&gt;

&lt;h2&gt;
  
  
  PhonePe and Juspay: nothing in the body is signed
&lt;/h2&gt;

&lt;p&gt;This is the part that surprised me most. PhonePe's &lt;code&gt;Authorization&lt;/code&gt; header is a SHA-256 of the username and password you typed into its dashboard. Juspay sends the same username and password as HTTP Basic auth. Neither one depends on the body.&lt;/p&gt;

&lt;p&gt;That has two consequences. A valid header proves the sender knows your credentials, but says nothing about whether the body was changed. And because the PhonePe header is the same on every request, one leaked request is enough. Anyone who has it can attach it to any body they like, forever.&lt;/p&gt;

&lt;p&gt;PhonePe's dashboard does offer a newer HMAC option, but the docs do not say which hash it uses, how it is encoded, or exactly which bytes are signed, and the official SDK has no code for it. I left it out rather than guess.&lt;/p&gt;

&lt;h2&gt;
  
  
  Try it yourself
&lt;/h2&gt;

&lt;p&gt;Here are both kinds of check, running for real in your browser. On the Razorpay tab, change the amount and watch the signature fail. Then try “Parse and re-serialise”: the data is the same, but the bytes are not, so it fails too. On the PhonePe tab, make the same change and see the check still pass.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Run the live demo (real hashing, nothing is sent anywhere):&lt;/strong&gt; &lt;a href="https://mokshupadhyay.com/writing/indian-payment-webhooks#try-it-yourself" rel="noopener noreferrer"&gt;https://mokshupadhyay.com/writing/indian-payment-webhooks#try-it-yourself&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Webhooks arrive twice, late and out of order
&lt;/h2&gt;

&lt;p&gt;Gateways send a webhook again when your server does not answer with a success in time, and Razorpay's and Juspay's docs say plainly that the same event can arrive more than once. Razorpay's also say events can arrive out of order. So a handler has to de-duplicate. The obvious key is the delivery id the gateway sends: &lt;code&gt;x-razorpay-event-id&lt;/code&gt; or Cashfree's &lt;code&gt;x-idempotency-key&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Both are headers, and neither signature covers them. Someone replaying a captured Razorpay request can put any value there and pass your de-duplication check. That is why india-webhooks builds &lt;code&gt;event.id&lt;/code&gt; only from fields inside the signature, such as the event name, the payment id and the signed timestamp, and keeps the delivery id separately for logging.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A handler that holds up&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;verifyWebhook&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;provider&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;rawBody&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;secret&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;400&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s1"&gt;'payment.captured'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;orderId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amountMinor&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;payment&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// PhonePe and Juspay sign nothing in the body: ask the gateway first.&lt;/span&gt;
  &lt;span class="c1"&gt;// fetchOrderStatus is your own call to the gateway's status API.&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;provider&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s1"&gt;'phonepe'&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;provider&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s1"&gt;'juspay'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetchOrderStatus&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;provider&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;orderId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="s1"&gt;'paid'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// One transaction: store event.id in a unique column, then update the order.&lt;/span&gt;
  &lt;span class="c1"&gt;// A duplicate delivery finds the id already stored and changes nothing.&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;transaction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;tx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;insertEventIdIfNew&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;tx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;markPaid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;orderId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amountMinor&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// checks amountMinor against the order&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Out-of-order delivery needs the same care. If a customer's first attempt fails and the second succeeds, the &lt;code&gt;payment.failed&lt;/code&gt; for the first can reach you after the &lt;code&gt;payment.captured&lt;/code&gt; for the second. Move an order forward from a webhook, never backwards: a paid order should not become unpaid because an older event showed up late.&lt;/p&gt;

&lt;h2&gt;
  
  
  Amounts: four gateways send rupees
&lt;/h2&gt;

&lt;p&gt;Razorpay and PhonePe send amounts as integer paise: &lt;code&gt;50000&lt;/code&gt; means ₹500. Cashfree and Juspay send rupees as JSON numbers like &lt;code&gt;1.8&lt;/code&gt;. PayU and Easebuzz send rupees as text like &lt;code&gt;"40.00"&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The tempting fix is &lt;code&gt;Math.round(amount * 100)&lt;/code&gt;. It usually works, but it multiplies a binary float: in JavaScript &lt;code&gt;1.005 * 100&lt;/code&gt; is &lt;code&gt;100.49999999999999&lt;/code&gt;. It also rounds amounts that should never have been accepted: &lt;code&gt;12.345&lt;/code&gt; becomes &lt;code&gt;1235&lt;/code&gt; paise without complaint. A mismatch between the amount a webhook reports and the amount you charged is exactly what you want to catch, not hide.&lt;/p&gt;

&lt;p&gt;The package converts amounts as decimal text, never as floats, and rejects anything that is not an exact number of paise:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;src/core/amount.ts&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;match&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;DECIMAL&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;       &lt;span class="c1"&gt;// /^(\d+)(?:\.(\d+))?$/&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;match&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;whole&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;match&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;fraction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;match&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fraction&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;!/^&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;$&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fraction&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// "12.345" is not paise&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;stripLeadingZeros&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;whole&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;fraction&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;padEnd&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'0'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// "1.8" → "180"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The result, &lt;code&gt;amountMinor&lt;/code&gt;, is a string. It stays exact at any size, and you compare it with the amount in paise you stored when you created the order.&lt;/p&gt;

&lt;h2&gt;
  
  
  How I tested it
&lt;/h2&gt;

&lt;p&gt;Where a gateway publishes a test vector, a known secret with its expected signature, the tests use it. Razorpay, PhonePe and PayU publish one in their official SDKs, and I recomputed each with &lt;code&gt;openssl&lt;/code&gt; or &lt;code&gt;shasum&lt;/code&gt; before trusting it. Cashfree, Juspay and Easebuzz publish none, so those rely on sample payloads and round trips.&lt;/p&gt;

&lt;p&gt;Most of the confidence comes from property-based tests with &lt;a href="https://github.com/dubzzz/fast-check" rel="noopener noreferrer"&gt;fast-check&lt;/a&gt;. For random payloads and secrets they check that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;anything the package signs, it verifies;&lt;/li&gt;
&lt;li&gt;changing any single byte of a signed body makes verification fail;&lt;/li&gt;
&lt;li&gt;a request without valid authentication only ever gets &lt;code&gt;missing_header&lt;/code&gt; or &lt;code&gt;bad_signature&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;amountMinor&lt;/code&gt; is always an integer string that matches the amount sent;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;verifyWebhook&lt;/code&gt; never throws, whatever it is given.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To check the tests themselves, I broke the source on purpose once per property, for example by making Cashfree's signature stop covering the body, and confirmed that the right property failed. That run is &lt;a href="https://github.com/mokshupadhyay/india-webhooks/blob/main/docs/MUTATION_EVIDENCE.md" rel="noopener noreferrer"&gt;written up in the repo&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The package also ships the same tools for your own tests. &lt;code&gt;india-webhooks/testing&lt;/code&gt; signs payloads exactly the way each gateway does and generates duplicated, out-of-order, late, tampered and stale deliveries, so you can check that your handler updates an order once and ends in the right state.&lt;/p&gt;

&lt;h2&gt;
  
  
  The short version
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Verify against the raw bytes, before any body parser runs.&lt;/li&gt;
&lt;li&gt;Know what your gateway's proof covers. For PayU and Easebuzz, trust only the hashed fields. For PhonePe and Juspay, trust nothing in the body until you have checked the order status with the gateway.&lt;/li&gt;
&lt;li&gt;De-duplicate on values inside the signature, not on delivery id headers.&lt;/li&gt;
&lt;li&gt;Move order state forward only.&lt;/li&gt;
&lt;li&gt;Handle amounts as integer paise, converted from text, and compare them with what you charged.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Every rule above comes from the gateway's own docs or SDK, and the &lt;a href="https://github.com/mokshupadhyay/india-webhooks/blob/main/docs/SOURCES.md" rel="noopener noreferrer"&gt;sources page&lt;/a&gt; lists each one with the date it was read. india-webhooks is MIT licensed and has no runtime dependencies. If a real webhook verifies with the gateway but fails with the package, or the reverse, please &lt;a href="https://github.com/mokshupadhyay/india-webhooks/issues" rel="noopener noreferrer"&gt;open an issue&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://mokshupadhyay.com/writing/indian-payment-webhooks" rel="noopener noreferrer"&gt;mokshupadhyay.com&lt;/a&gt;. I'm Moksh, a full-stack engineer building stablecoin settlement and cross-border payments, and I maintain the open-source payment libraries these posts are about.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>payments</category>
      <category>webdev</category>
      <category>security</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Making a payment request safe to retry</title>
      <dc:creator>Moksh Upadhyay</dc:creator>
      <pubDate>Thu, 26 Nov 2026 18:41:03 +0000</pubDate>
      <link>https://dev.to/mokshupadhyay/making-a-payment-request-safe-to-retry-4jli</link>
      <guid>https://dev.to/mokshupadhyay/making-a-payment-request-safe-to-retry-4jli</guid>
      <description>&lt;h2&gt;
  
  
  In short
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;After a network error, an app cannot know whether the payment went through, so retrying blindly can charge twice.&lt;/li&gt;
&lt;li&gt;An idempotency key lets the server recognise a retry and return the first result instead of charging again.&lt;/li&gt;
&lt;li&gt;The client has to create the key once, save it before sending, and let only one sender use it.&lt;/li&gt;
&lt;li&gt;idempotent-queue does this in the browser, Node and React Native, and is tested with random crashes and lost responses.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Written for: Web and mobile developers who send payments from an app.&lt;/p&gt;

&lt;p&gt;A user taps Pay. The spinner turns for ten seconds, and then the app shows a network error. Did the payment go through?&lt;/p&gt;

&lt;p&gt;The honest answer is that the app does not know. There are three things that could have happened:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The request never reached the server. Nothing was charged.&lt;/li&gt;
&lt;li&gt;The request reached the server, the card was charged, and the response was lost on the way back.&lt;/li&gt;
&lt;li&gt;The request reached the server and is still being processed.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;From the phone, all three look the same: no response. If the app retries, case 2 charges the customer twice. If it does not retry, case 1 loses a payment the user meant to make. Showing an error and asking the user to tap again is the same retry, just slower.&lt;/p&gt;

&lt;p&gt;I build payment software for a living, and this is the problem I kept running into. So I wrote &lt;a href="https://github.com/mokshupadhyay/idempotent-queue" rel="noopener noreferrer"&gt;idempotent-queue&lt;/a&gt;, a small open-source library for the browser and Node, and &lt;a href="https://github.com/mokshupadhyay/react-native-idempotent-queue" rel="noopener noreferrer"&gt;react-native-idempotent-queue&lt;/a&gt; for mobile apps. This post is about the ideas behind them, which matter more than the code.&lt;/p&gt;

&lt;h2&gt;
  
  
  The fix lives on the server: idempotency keys
&lt;/h2&gt;

&lt;p&gt;The standard answer, used by Stripe, Adyen and many payment APIs, is an &lt;strong&gt;idempotency key&lt;/strong&gt;. The client makes up a unique key for each payment, once, and sends it in a header with every attempt:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Every attempt carries the same key&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="no"&gt;POST&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;payments&lt;/span&gt;
&lt;span class="nc"&gt;Idempotency&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;6f1c2a&lt;/span&gt;&lt;span class="mf"&gt;4e-8d&lt;/span&gt;&lt;span class="mb"&gt;0b&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;4c&lt;/span&gt;&lt;span class="mf"&gt;1e-9a&lt;/span&gt;&lt;span class="mi"&gt;57&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;3b2f0d&lt;/span&gt;&lt;span class="mf"&gt;9e7c&lt;/span&gt;&lt;span class="mi"&gt;41&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The server stores the result of the first request it handles with a given key. When the same key comes in again, it does not do the work again. It returns the stored result. A retry stops being dangerous: it either does the work for the first time, or gets back the answer to the first time.&lt;/p&gt;

&lt;p&gt;Try both ways yourself. Lose the first response, then retry with the same key, and then with a new key each time:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Run the live demo on the original post:&lt;/strong&gt; &lt;a href="https://mokshupadhyay.com/writing/safe-to-retry" rel="noopener noreferrer"&gt;https://mokshupadhyay.com/writing/safe-to-retry&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The client can still get it wrong
&lt;/h2&gt;

&lt;p&gt;The server side is well understood. The client side is where I kept seeing mistakes, and each one quietly undoes the key:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;A new key on every attempt.&lt;/strong&gt; If the key is generated inside the function that sends the request, every retry looks like a new payment.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A key that only lives in memory.&lt;/strong&gt; The user reloads the page, or the OS kills the app, and the key is gone. The next attempt gets a fresh key.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Two senders.&lt;/strong&gt; The user has two tabs open, or taps twice. Two copies of the same payment go out at the same time, and if they have different keys, the server has no way to know.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Retries that make things worse.&lt;/strong&gt; Every client retrying on a fixed timer, at the same moment, right after an outage.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How the queue works
&lt;/h2&gt;

&lt;p&gt;You give it a &lt;code&gt;send&lt;/code&gt; function and enqueue payloads. With the built-in &lt;code&gt;fetch&lt;/code&gt; helper it looks like this:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;payments.ts&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;createIdempotentQueue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fetchSender&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'idempotent-queue'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;Payment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;currency&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;payments&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;createIdempotentQueue&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Payment&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;send&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;fetchSender&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'/api/payments'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;payments&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// In the click handler&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;payments&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;enqueue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2500&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;currency&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'EUR'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'acct_42'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;group&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'payments'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Behind that small API are a few rules.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Save first, then send
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;enqueue&lt;/code&gt; only resolves after the item and its key are written to storage. Each attempt is also recorded in storage before the request goes out. If the tab closes one millisecond later, the payment and its key are still on disk. In the browser the default storage is IndexedDB.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. One key for the whole life of the payment
&lt;/h3&gt;

&lt;p&gt;The key is created when the item is enqueued, as a UUID v4 from &lt;code&gt;crypto.randomUUID()&lt;/code&gt; where it exists, and it never changes. &lt;code&gt;fetchSender&lt;/code&gt; sets the &lt;code&gt;Idempotency-Key&lt;/code&gt; header from it on every attempt, and ignores a header with the same name in your own options, so nothing can override it by accident. If your payment already has a natural unique id, like an order id, you can pass that as the key instead.&lt;/p&gt;

&lt;p&gt;Enqueuing a key that is already stored returns the existing item and creates nothing. Succeeded items are kept for 24 hours by default, so a double tap returns the finished payment instead of sending a second one.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. One sender at a time
&lt;/h3&gt;

&lt;p&gt;Every tab of the site shares the queue, but only one tab sends. At &lt;code&gt;start()&lt;/code&gt;, each tab asks for a Web Lock (&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Locks_API" rel="noopener noreferrer"&gt;MDN&lt;/a&gt;). The browser gives it to one tab and makes the others wait. The others still see every item, because a tab that changes the queue tells the rest over a &lt;code&gt;BroadcastChannel&lt;/code&gt;. Inside the sending tab, a key that is in flight is never started a second time, even after its attempt has timed out. Items in the same &lt;code&gt;group&lt;/code&gt; go one at a time, in order, and a failed item holds back the rest of its group. A transfer should not be sent before the payment it depends on.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Pick up where a closed tab stopped
&lt;/h3&gt;

&lt;p&gt;This is the case I cared most about. The sending tab closes in the middle of a request. The browser releases its lock, and the next tab in line gets it. That tab finds an item marked &lt;code&gt;inflight&lt;/code&gt; that it is not sending, so it knows the attempt was cut off. It puts the item back to &lt;code&gt;pending&lt;/code&gt; and sends it again, with the same key:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;src/queue.ts&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;recovered&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;QueueItem&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;QueueItem&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;attempts&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;policy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;maxAttempts&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'failed'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;updatedAt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lastError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'interrupted'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="cm"&gt;/* … */&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'pending'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;updatedAt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nextAttemptAt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The queue cannot know whether the cut-off request reached the server. It does not need to. If it did, the server replays the stored result. If it did not, this attempt is the real one.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Retry without making things worse
&lt;/h3&gt;

&lt;p&gt;Not every error should be retried. The default rules: a network failure, a timeout, &lt;code&gt;408&lt;/code&gt;, &lt;code&gt;409&lt;/code&gt;, &lt;code&gt;425&lt;/code&gt;, &lt;code&gt;429&lt;/code&gt; and any &lt;code&gt;5xx&lt;/code&gt; are retried. Any other &lt;code&gt;4xx&lt;/code&gt; is permanent, because sending the same bad request again will not fix it. &lt;code&gt;409&lt;/code&gt; is on the retry list on purpose. It is what a server answers when the first request with this key is still running, which is exactly what happens after a tab closes mid-request. The retry later gets the stored result.&lt;/p&gt;

&lt;p&gt;Retries wait using exponential backoff with full jitter: after the n-th failure, a random delay between zero and &lt;code&gt;min(maxDelay, base × 2^(n−1))&lt;/code&gt;. The randomness spreads clients out so they do not all hit the server at the same moment. A server's &lt;code&gt;Retry-After&lt;/code&gt; header takes priority. While the device is offline, items wait and do not use up attempts.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;src/backoff.ts&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;backoffDelay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;failures&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;policy&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ResolvedRetryPolicy&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;ceiling&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;backoffCeiling&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;failures&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;policy&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;random&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;EPSILON&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;ceiling&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  6. Storage that does not half-write
&lt;/h3&gt;

&lt;p&gt;With several tabs, two of them can change the queue at the same moment. If both read the state, change it and write it back, one change is lost. So every change is one read-modify-write step. On IndexedDB that is a single &lt;code&gt;readwrite&lt;/code&gt; transaction, which the browser runs one at a time across tabs and commits completely or not at all. The state is also written as a versioned, checksummed envelope to two alternating slots. If one copy is ever unreadable, the queue loads the other, keeps a copy of the bad data for inspection, and carries on instead of crashing.&lt;/p&gt;

&lt;h2&gt;
  
  
  What it does not promise
&lt;/h2&gt;

&lt;p&gt;I put this section near the top of the README, because it matters most when money is involved.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The client gives at-least-once delivery.&lt;/strong&gt; The request keeps going out, with the same key, until it succeeds, fails permanently, or runs out of attempts. An exactly-once effect only happens if the server honours the key. Without that, a retry can still charge twice.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Payloads are stored in plain text.&lt;/strong&gt; IndexedDB is not encrypted. Never put card numbers or tokens in a payload. Send a reference, like a server-side payment intent id, or use a storage adapter that encrypts.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Something has to be running.&lt;/strong&gt; The queue sends while a page is open or the app is running. Pending items go out the next time &lt;code&gt;start()&lt;/code&gt; is called.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How I tested it
&lt;/h2&gt;

&lt;p&gt;Unit tests are not enough for this kind of code. The bugs live in the gaps between events: a crash between a write and a send, two tabs starting at once, a response lost after the server already did the work. So most of the confidence comes from property-based tests with &lt;a href="https://github.com/dubzzz/fast-check" rel="noopener noreferrer"&gt;fast-check&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The tests generate random sequences of enqueues, crashes, reloads, offline periods, lost requests and lost responses, against a fake server that deduplicates by key. A crash throws away a tab's memory, its unfinished writes and its locks. Then the tests check properties such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;every enqueued key ends up applied exactly once on the server, and its item ends &lt;code&gt;succeeded&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;the same key is never in flight twice at the same time, even across two tabs that crash and reload;&lt;/li&gt;
&lt;li&gt;a permanent error is never retried, and a retryable one is tried at most &lt;code&gt;maxAttempts&lt;/code&gt; times.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To check the tests themselves, a script breaks the source in nine small, deliberate ways, such as skipping the recovery of a closed tab's item or releasing the sender lock too early. Each break made its property fail. The results are written up in the repo. Outside the test suite, version 0.1.0 was also checked with real tabs in Chromium, Firefox and WebKit, including closing the sending tab mid-request and seeing another tab finish it with the same key, applied once.&lt;/p&gt;

&lt;h2&gt;
  
  
  If you build the server side
&lt;/h2&gt;

&lt;p&gt;The client is only half of it. A server that takes payment retries safely needs to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Require an &lt;code&gt;Idempotency-Key&lt;/code&gt; on every request that moves money.&lt;/li&gt;
&lt;li&gt;Store the key with the response of the first request, before or together with doing the work.&lt;/li&gt;
&lt;li&gt;Return the stored response for any repeat of the key, without doing the work again.&lt;/li&gt;
&lt;li&gt;Answer &lt;code&gt;409&lt;/code&gt; while the first request with a key is still running, and &lt;code&gt;422&lt;/code&gt; if the key comes back with a different body.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;With both halves in place, a flaky network turns into a slow payment instead of a lost or doubled one. Both libraries are MIT licensed and on npm: &lt;a href="https://github.com/mokshupadhyay/idempotent-queue" rel="noopener noreferrer"&gt;idempotent-queue&lt;/a&gt; for the web and Node, and &lt;a href="https://github.com/mokshupadhyay/react-native-idempotent-queue" rel="noopener noreferrer"&gt;react-native-idempotent-queue&lt;/a&gt; for React Native. Issues and questions are welcome on GitHub.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://mokshupadhyay.com/writing/safe-to-retry" rel="noopener noreferrer"&gt;mokshupadhyay.com&lt;/a&gt;. I'm Moksh, a full-stack engineer building stablecoin settlement and cross-border payments, and I maintain the open-source payment libraries these posts are about.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>payments</category>
      <category>webdev</category>
      <category>javascript</category>
      <category>api</category>
    </item>
  </channel>
</rss>
