<?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: Shitpicious Shitpicious</title>
    <description>The latest articles on DEV Community by Shitpicious Shitpicious (@shitpicious_shitpicious_9).</description>
    <link>https://dev.to/shitpicious_shitpicious_9</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%2F4256593%2Fb4dd38a5-739c-4b71-86ca-e4d111abebf7.png</url>
      <title>DEV Community: Shitpicious Shitpicious</title>
      <link>https://dev.to/shitpicious_shitpicious_9</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shitpicious_shitpicious_9"/>
    <language>en</language>
    <item>
      <title>Your sales webhook fires twice, and your spreadsheet believes it</title>
      <dc:creator>Shitpicious Shitpicious</dc:creator>
      <pubDate>Sat, 21 Nov 2026 03:19:15 +0000</pubDate>
      <link>https://dev.to/shitpicious_shitpicious_9/your-sales-webhook-fires-twice-and-your-spreadsheet-believes-it-3pp2</link>
      <guid>https://dev.to/shitpicious_shitpicious_9/your-sales-webhook-fires-twice-and-your-spreadsheet-believes-it-3pp2</guid>
      <description>&lt;p&gt;I run a small shop that sells digital files. Two platforms, one local checkout, and a spreadsheet&lt;br&gt;
that I update by hand — which is fine until a payment webhook times out, gets retried, and the same&lt;br&gt;
sale lands twice. You notice it on your best day, three weeks later, when the monthly total looks&lt;br&gt;
better than your bank account does.&lt;/p&gt;

&lt;p&gt;So the ledger I run now is one SQLite table. Two decisions do most of the work.&lt;/p&gt;
&lt;h2&gt;
  
  
  1. Let the database refuse duplicates
&lt;/h2&gt;

&lt;p&gt;Every webhook carries an id from the platform. Make &lt;code&gt;(platform, external_id)&lt;/code&gt; unique and the second&lt;br&gt;
delivery fails at the constraint:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="no"&gt;CREATE&lt;/span&gt; &lt;span class="no"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;sales&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nb"&gt;id&lt;/span&gt;          &lt;span class="no"&gt;INTEGER&lt;/span&gt; &lt;span class="no"&gt;PRIMARY&lt;/span&gt; &lt;span class="no"&gt;KEY&lt;/span&gt; &lt;span class="no"&gt;AUTOINCREMENT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;platform&lt;/span&gt;    &lt;span class="no"&gt;TEXT&lt;/span&gt; &lt;span class="no"&gt;NOT&lt;/span&gt; &lt;span class="no"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;external_id&lt;/span&gt; &lt;span class="no"&gt;TEXT&lt;/span&gt; &lt;span class="no"&gt;NOT&lt;/span&gt; &lt;span class="no"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;product&lt;/span&gt;     &lt;span class="no"&gt;TEXT&lt;/span&gt; &lt;span class="no"&gt;NOT&lt;/span&gt; &lt;span class="no"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;amount&lt;/span&gt;      &lt;span class="no"&gt;TEXT&lt;/span&gt; &lt;span class="no"&gt;NOT&lt;/span&gt; &lt;span class="no"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;currency&lt;/span&gt;    &lt;span class="no"&gt;TEXT&lt;/span&gt; &lt;span class="no"&gt;NOT&lt;/span&gt; &lt;span class="no"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;occurred_at&lt;/span&gt; &lt;span class="no"&gt;TEXT&lt;/span&gt; &lt;span class="no"&gt;NOT&lt;/span&gt; &lt;span class="no"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="no"&gt;UNIQUE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;platform&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;external_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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then the insert handler is boring, which is what you want:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;record&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;platform&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;external_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;product&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="n"&gt;currency&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"USD"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="s2"&gt;"INSERT INTO sales (platform, external_id, product, amount, currency, occurred_at)"&lt;/span&gt;
            &lt;span class="s2"&gt;" VALUES (?,?,?,?,?,datetime('now'))"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;platform&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;external_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;product&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;str&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="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="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;commit&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;True&lt;/span&gt;          &lt;span class="c1"&gt;# baru&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="n"&gt;sqlite3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;IntegrityError&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;False&lt;/span&gt;         &lt;span class="c1"&gt;# sudah pernah diterima — bukan error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two details that matter more than the code:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Still return HTTP 200 on the duplicate.&lt;/strong&gt; A sender that receives an error retries with backoff, so&lt;br&gt;
"smart" duplicate handling that returns 409 turns one retry into ten. The duplicate is your problem,&lt;br&gt;
not theirs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Deduplicate on the platform's id, not on your own timestamp.&lt;/strong&gt; Two genuine sales of the same&lt;br&gt;
product by the same buyer can happen in the same minute; one delivery delivered twice cannot.&lt;/p&gt;
&lt;h2&gt;
  
  
  2. Never let a float touch an amount
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;0.1 + 0.2&lt;/code&gt; is &lt;code&gt;0.30000000000000004&lt;/code&gt;. In a revenue report that becomes a discrepancy you cannot&lt;br&gt;
explain to yourself at 11pm. Store money as &lt;code&gt;Decimal&lt;/code&gt; and persist it as text:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;decimal&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;Decimal&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;ROUND_HALF_UP&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;money&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&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;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Decimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&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="nf"&gt;quantize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Decimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"0.01"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rounding&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="no"&gt;ROUND_HALF_UP&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;SQLite has no decimal type, so text is the honest choice. Sum with &lt;code&gt;CAST(amount AS REAL)&lt;/code&gt; only for&lt;br&gt;
display, or convert to &lt;code&gt;Decimal&lt;/code&gt; in code when the number will be used for anything that matters.&lt;/p&gt;
&lt;h2&gt;
  
  
  3. Do not add currencies together
&lt;/h2&gt;

&lt;p&gt;A &lt;code&gt;$29&lt;/code&gt; sale and a &lt;code&gt;Rp30.000&lt;/code&gt; sale are not &lt;code&gt;29,030&lt;/code&gt; of anything. Group by currency:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;platform&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;product&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="nb"&gt;COUNT&lt;/span&gt;&lt;span class="cm"&gt;(*), SUM(CAST(amount AS REAL)) AS gross
FROM sales
GROUP BY platform, product, currency
ORDER BY gross DESC;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you must show one number, convert explicitly with a rate you recorded, and label the line with the&lt;br&gt;
rate and the date. Silent conversion is how a report becomes a fiction.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Roll up on the sale's timestamp, not on insert time
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;received_at&lt;/code&gt; is when your server heard about it — useful for debugging, wrong for accounting. A sale&lt;br&gt;
that happened at 23:58 on 31 January and arrived at 00:03 on 1 February belongs to January. Keep both&lt;br&gt;
columns; group by &lt;code&gt;occurred_at&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The whole thing
&lt;/h2&gt;

&lt;p&gt;The lite version of the ledger is MIT-licensed, standard library only, ~100 lines:&lt;br&gt;
&lt;a href="https://github.com/shitpicious666-spec/sales-ledger-lite" rel="noopener noreferrer"&gt;github.com/shitpicious666-spec/sales-ledger-lite&lt;/a&gt;&lt;br&gt;
— includes a &lt;code&gt;selftest.py&lt;/code&gt; that proves the duplicate rejection and the monthly isolation against an&lt;br&gt;
in-memory database.&lt;/p&gt;

&lt;p&gt;I also ship a paid version with per-platform fee profiles (so the report shows net, not gross), an&lt;br&gt;
HTTP receiver and a daily digest; it is linked from the README. The lite version is enough if all you&lt;br&gt;
need is "do not count twice", and I would rather you read the code than take my word for it.&lt;/p&gt;

&lt;p&gt;If you have solved refunds in an append-only ledger differently, I would like to hear it — right now&lt;br&gt;
I record a negative row with its own id, which keeps the history honest but makes "revenue this&lt;br&gt;
month" a sum instead of a constant.&lt;/p&gt;

</description>
      <category>python</category>
      <category>sqlite</category>
      <category>database</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
