<?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: Mahadi Hasan</title>
    <description>The latest articles on DEV Community by Mahadi Hasan (@mhsantonu).</description>
    <link>https://dev.to/mhsantonu</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%2F1914147%2F7db29b45-59ba-4bd2-a53b-589d1197a21a.jpg</url>
      <title>DEV Community: Mahadi Hasan</title>
      <link>https://dev.to/mhsantonu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mhsantonu"/>
    <language>en</language>
    <item>
      <title>4 Hard E-Commerce Engineering Problems We Solved Building a Self-Hosted Laravel Commerce Core</title>
      <dc:creator>Mahadi Hasan</dc:creator>
      <pubDate>Mon, 30 Nov 2026 16:55:45 +0000</pubDate>
      <link>https://dev.to/mhsantonu/4-hard-e-commerce-engineering-problems-we-solved-building-a-self-hosted-laravel-commerce-core-bdb</link>
      <guid>https://dev.to/mhsantonu/4-hard-e-commerce-engineering-problems-we-solved-building-a-self-hosted-laravel-commerce-core-bdb</guid>
      <description>&lt;p&gt;When growing DTC brands decide to leave Shopify or WooCommerce, engineering teams usually hit an uncomfortable choice:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Plugin-heavy stacks&lt;/strong&gt; (like WooCommerce or legacy Magento) where every checkout tweak feels like defusing a bomb.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Barebones headless frameworks&lt;/strong&gt; where you spend four months rebuilding basic commerce plumbing—tax invoices, multi-warehouse stock, returns, store credit, and product feeds—before taking a single order.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;While building &lt;strong&gt;&lt;a href="https://knarra.com" rel="noopener noreferrer"&gt;Knarra&lt;/a&gt;&lt;/strong&gt; (a self-hosted Laravel e-commerce platform designed as a zero-app-tax alternative to Shopify), we ran into the same four engineering traps that quietly break custom e-commerce builds in production.&lt;/p&gt;

&lt;p&gt;Here’s how we solved each one in Laravel—and the architectural patterns you can reuse in your own commerce projects.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. Never Touch Floats for Money (Especially Across 32+ Currencies)
&lt;/h2&gt;

&lt;p&gt;The fastest way to break reconciliation with Stripe, PayPal, or an accounting team is storing prices as &lt;code&gt;float&lt;/code&gt; or doing percentage discount math directly on decimals.&lt;/p&gt;

&lt;p&gt;Even &lt;code&gt;DECIMAL(10,2)&lt;/code&gt; trips teams up once you support international stores:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;USD / GBP / AUD / EUR&lt;/strong&gt; use 2 decimal places (&lt;code&gt;100&lt;/code&gt; minor units).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;JPY / KRW&lt;/strong&gt; use &lt;strong&gt;0&lt;/strong&gt; decimal places.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;KWD / BHD&lt;/strong&gt; use &lt;strong&gt;3&lt;/strong&gt; decimal places.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Instead of hardcoding &lt;code&gt;* 100&lt;/code&gt; everywhere, we store monetary values as &lt;strong&gt;exact integers in minor units&lt;/strong&gt; alongside the ISO-4217 currency code and run every cart subtotal, BOGO rule, tax zone calculation, and partial refund through &lt;a href="https://github.com/brick/money" rel="noopener noreferrer"&gt;&lt;code&gt;brick/money&lt;/code&gt;&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Brick&lt;/span&gt;\&lt;span class="nc"&gt;Money&lt;/span&gt;\&lt;span class="nc"&gt;Money&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Brick&lt;/span&gt;\&lt;span class="nc"&gt;Math&lt;/span&gt;\&lt;span class="nc"&gt;RoundingMode&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// $49.95 stored as integer 4995 in minor units&lt;/span&gt;
&lt;span class="nv"&gt;$unitPrice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Money&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;ofMinor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$variant&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;price_minor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$cart&lt;/span&gt;&lt;span class="o"&gt;-&amp;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="nv"&gt;$lineTotal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$unitPrice&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;multipliedBy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$qty&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Apply a 15% coupon with deterministic half-up rounding&lt;/span&gt;
&lt;span class="nv"&gt;$discount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$lineTotal&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;multipliedBy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'0.15'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;RoundingMode&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;HALF_UP&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$finalTotal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$lineTotal&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;minus&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$discount&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;&lt;strong&gt;Why this matters in production:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Partial refunds + line-item tax calculations always sum back to the exact Stripe payment intent amount—no off-by-one-cent webhook mismatches.&lt;/li&gt;
&lt;li&gt;Store credit ledgers stay immutable and auditable down to the exact minor unit per currency.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  2. Atomic Stock Reservations (So Ghost Checkouts Don’t Freeze Inventory)
&lt;/h2&gt;

&lt;p&gt;A classic e-commerce race condition: you have &lt;strong&gt;1 unit left&lt;/strong&gt; in your main warehouse, and two shoppers hit checkout at the same second.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you only deduct stock &lt;em&gt;after&lt;/em&gt; Stripe’s webhook fires, you oversell.&lt;/li&gt;
&lt;li&gt;If you deduct stock as soon as someone enters checkout without an expiry mechanism, abandoned checkouts silently lock up your inventory until a staff member manually fixes it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Our solution uses &lt;strong&gt;two pieces working together&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Pessimistic row locking (&lt;code&gt;lockForUpdate()&lt;/code&gt;) inside a DB transaction&lt;/strong&gt; when creating a pending checkout reservation across warehouses:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="no"&gt;DB&lt;/span&gt;&lt;span class="o"&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;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$warehouseId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$variantId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$qty&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$order&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$stock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;WarehouseStock&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'warehouse_id'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$warehouseId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'variant_id'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$variantId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;lockForUpdate&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="nf"&gt;firstOrFail&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="nv"&gt;$available&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$stock&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;on_hand&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nv"&gt;$stock&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;reserved&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="nv"&gt;$available&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nv"&gt;$qty&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;InsufficientStockException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Only {$available} left in stock."&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="nv"&gt;$stock&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'reserved'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$qty&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nc"&gt;StockReservation&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="s1"&gt;'order_id'&lt;/span&gt;     &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$order&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s1"&gt;'warehouse_id'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$warehouseId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s1"&gt;'variant_id'&lt;/span&gt;   &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$variantId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s1"&gt;'quantity'&lt;/span&gt;     &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$qty&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s1"&gt;'expires_at'&lt;/span&gt;   &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;now&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="nf"&gt;addMinutes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;20&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="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;ol&gt;
&lt;li&gt;
&lt;strong&gt;An hourly self-healing scheduler (&lt;code&gt;orders:expire-stale&lt;/code&gt;)&lt;/strong&gt; that finds unpaid/abandoned reservations past &lt;code&gt;expires_at&lt;/code&gt;, releases &lt;code&gt;reserved&lt;/code&gt; back to available inventory inside a transaction, and logs the release in the order timeline.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;No overselling during flash sales, and no ghost carts freezing stock overnight.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Keep Private Per-SKU COGS Strictly Out of Public API Payloads
&lt;/h2&gt;

&lt;p&gt;Most DTC founders end up paying $30–$100/month for a third-party profit analytics app just to see real gross margins per SKU—because platforms don't track Cost of Goods Sold (COGS) natively on variants.&lt;/p&gt;

&lt;p&gt;Adding &lt;code&gt;cost_amount&lt;/code&gt; to the variant table is trivial. The subtle bug is &lt;strong&gt;accidentally leaking supplier cost&lt;/strong&gt; through a headless storefront endpoint, JSON-LD block, or &lt;code&gt;toArray()&lt;/code&gt; serialization.&lt;/p&gt;

&lt;p&gt;In Laravel, we enforce a strict boundary:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sensitive fields (&lt;code&gt;cost_amount&lt;/code&gt;, internal staff notes, audit metadata) are never exposed on public &lt;code&gt;/api/v1&lt;/code&gt; API Resources—storefront endpoints use dedicated &lt;code&gt;StorefrontVariantResource&lt;/code&gt; classes with an explicit allowlist of public keys.&lt;/li&gt;
&lt;li&gt;Margin badges (&lt;code&gt;(price - cost) / price&lt;/code&gt;) are computed exclusively in the Admin layer, while ERP/3PL integrations use &lt;strong&gt;scoped, revocable integration API tokens&lt;/strong&gt; isolated from customer authentication.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  4. Migrating Live Stores Without Tanking SEO or Emailing 10,000 Past Customers
&lt;/h2&gt;

&lt;p&gt;When teams migrate off Shopify or WooCommerce, the code is rarely what causes panic—it’s the cutover:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;URL changes break rankings:&lt;/strong&gt; Shopify uses &lt;code&gt;/products/{handle}&lt;/code&gt; and &lt;code&gt;/collections/{handle}&lt;/code&gt;. Changing slugs without automatic &lt;strong&gt;301 redirects&lt;/strong&gt; wipes out organic traffic.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hotlinked images die on cancellation:&lt;/strong&gt; If an importer copies image URLs instead of downloading and archiving media locally, every product photo breaks the day the old Shopify store closes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Historical order imports trigger email storms:&lt;/strong&gt; Firing standard &lt;code&gt;OrderCreated&lt;/code&gt; events while importing 2 years of past orders will email thousands of past customers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cutover gaps:&lt;/strong&gt; Running an initial import on Monday and switching DNS on Thursday misses 3 days of live orders unless your importer supports &lt;strong&gt;idempotent delta re-sync&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Building a proper migration pipeline meant treating it like a state machine: &lt;strong&gt;Pre-flight dry run → silent historical import (events muted, live stock untouched) → local media archiving → automatic 301 redirect registration → delta re-sync right before DNS switch.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Wrapping Up
&lt;/h2&gt;

&lt;p&gt;If you’re evaluating self-hosted e-commerce in the PHP/Laravel ecosystem—whether you're comparing &lt;strong&gt;Bagisto&lt;/strong&gt;, &lt;strong&gt;Lunar&lt;/strong&gt;, &lt;strong&gt;Medusa&lt;/strong&gt;, or building a custom stack—getting money math, stock locking, API data boundaries, and migration tooling right on day one saves months of painful rewrites later.&lt;/p&gt;

&lt;p&gt;We built all of these patterns directly into &lt;strong&gt;&lt;a href="https://knarra.com" rel="noopener noreferrer"&gt;Knarra&lt;/a&gt;&lt;/strong&gt; (alongside native Returns/RMA, Store Credit, Waitlists, Abandoned Cart Recovery, and live Google Shopping/Meta feeds so merchants don't pay $260–$900+/mo in Shopify apps).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Full technical breakdown &amp;amp; comparisons: &lt;a href="https://knarra.com/compare/" rel="noopener noreferrer"&gt;knarra.com/compare&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Core feature inventory: &lt;a href="https://knarra.com/features/" rel="noopener noreferrer"&gt;knarra.com/features&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you've built custom checkout or inventory systems in Laravel, I'd love to hear how you handled stock reservations or multi-currency rounding in the comments.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>ecommerce</category>
      <category>php</category>
      <category>architecture</category>
    </item>
  </channel>
</rss>
