<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Alase’s Engineering Notes]]></title><description><![CDATA[Alase’s Engineering Notes]]></description><link>https://alase.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Sat, 21 Nov 2026 16:46:24 GMT</lastBuildDate><atom:link href="https://alase.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[The race condition that prints free money, and how I stopped it in my Laravel payment gateway]]></title><description><![CDATA[How I stopped race conditions, double charges and fake webhooks in a Laravel payment gateway with row locks, idempotency keys and HMAC.]]></description><link>https://alase.hashnode.dev/the-race-condition-that-prints-free-money-and-how-i-stopped-it-in-my-laravel-payment-gateway</link><guid isPermaLink="true">https://alase.hashnode.dev/the-race-condition-that-prints-free-money-and-how-i-stopped-it-in-my-laravel-payment-gateway</guid><category><![CDATA[Laravel]]></category><category><![CDATA[fintech]]></category><category><![CDATA[PHP]]></category><category><![CDATA[software development]]></category><category><![CDATA[Software Engineering]]></category><category><![CDATA[software architecture]]></category><category><![CDATA[React]]></category><category><![CDATA[Next.js]]></category><category><![CDATA[webdevelopment]]></category><dc:creator><![CDATA[Gbolahan Olanrewaju]]></dc:creator><pubDate>Thu, 19 Nov 2026 11:38:34 GMT</pubDate><content:encoded><![CDATA[<p><a class="embed-card" href="https://youtu.be/8jZALDUuLeM">https://youtu.be/8jZALDUuLeM</a></p>

<p>I built PayMesh, a payment gateway with a merchant dashboard, an admin console, and a hosted checkout page. The frontend is Next.js. The Laravel backend is where I learned the most. Here are the design choices that mattered, starting with the bug that scares me most in payments.</p>
<h2>The bug that prints free money</h2>
<p>A merchant has 10,000 in their wallet. Two withdrawal requests for 8,000 arrive at the same moment.</p>
<p>Both requests read the balance: 10,000. Both pass the check. Both get paid out. The wallet ends at -6,000, and 16,000 left the system when only 10,000 existed.</p>
<p>Nothing crashes. No error shows up. Tests that send one request at a time will never catch it. This is a race condition, and it is why payment code needs more care than normal CRUD.</p>
<p>The fix is a row lock. I lock the wallet row inside a database transaction (<code>lockForUpdate()</code> in Laravel) before reading the balance. The second request waits. When it gets its turn, it sees 2,000 and fails cleanly.</p>
<p>That one idea shaped the rest of the backend.</p>
<h2>1. A ledger, not a balance column</h2>
<p>Money never changes through a plain balance update. Every movement writes a ledger entry, and each entry stores a balance snapshot. The ledger is also the audit trail. You can always answer "why is this balance what it is?"</p>
<h2>2. Idempotency that checks the payload</h2>
<p>Every payment request takes an <code>Idempotency-Key</code> header. I store the key with a fingerprint of the request body.</p>
<ul>
<li>Same key, same body: return the original result.</li>
<li>Same key, different body: reject the request.</li>
</ul>
<p>This stops double charges when a client retries. It also catches clients that reuse keys by mistake.</p>
<h2>3. Provider adapters behind a factory</h2>
<p>Paystack and Cray both sit behind one interface. The rest of the app never talks to a provider directly. A factory picks the right adapter. Adding a new provider means writing one class, not changing the payment flow.</p>
<h2>4. Webhooks in both directions</h2>
<p>Inbound provider webhooks are checked with an HMAC signature before I trust them. Outbound webhooks to merchants are signed the same way, so they can verify me.</p>
<p>Merchants register their own webhook URLs, so I block unsafe targets such as internal addresses. This prevents SSRF.</p>
<h2>5. API keys and access control</h2>
<p>API keys are stored hashed, like passwords. Each key has scopes, so a key can be limited to what it needs.</p>
<p>Teams have roles: owner, admin, developer, and finance. A finance user should not manage API keys, and a developer should not approve withdrawals.</p>
<h2>6. Withdrawals</h2>
<p>Withdrawals use a verification token that the server issues. The client cannot decide for itself that a withdrawal is approved.</p>
<h2>What I would improve next</h2>
<p>Refunds, fraud checks, and a load test that fires many concurrent requests at one wallet to prove the lock holds.</p>
<h2>Stack</h2>
<ul>
<li>Backend: Laravel 13, Postgres, Sanctum, queue worker, Docker</li>
<li>Frontend: Next.js, React, TypeScript, Tailwind</li>
</ul>
<p>Demo video: <a href="https://youtu.be/8jZALDUuLeM">https://youtu.be/8jZALDUuLeM</a></p>
<p>Source code: <a href="https://github.com/gbollybambam/mini-payment-gateway">https://github.com/gbollybambam/mini-payment-gateway</a></p>
<p>How do you handle concurrent writes in your money code? I'd like to hear it in the comments.</p>
]]></content:encoded></item></channel></rss>