<?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: Admin Fast Value</title>
    <description>The latest articles on DEV Community by Admin Fast Value (@astvalue).</description>
    <link>https://dev.to/astvalue</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%2F4281958%2F7680aff5-ef60-4b56-8cd2-3779175b12b7.png</url>
      <title>DEV Community: Admin Fast Value</title>
      <link>https://dev.to/astvalue</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/astvalue"/>
    <language>en</language>
    <item>
      <title>Designing Reliable Payment Tracking for SaaS Applications That Support Partial Payments</title>
      <dc:creator>Admin Fast Value</dc:creator>
      <pubDate>Fri, 04 Dec 2026 06:16:50 +0000</pubDate>
      <link>https://dev.to/astvalue/designing-reliable-payment-tracking-for-saas-applications-that-support-partial-payments-5223</link>
      <guid>https://dev.to/astvalue/designing-reliable-payment-tracking-for-saas-applications-that-support-partial-payments-5223</guid>
      <description>&lt;p&gt;When building a SaaS application that handles invoices and payments, one of the easiest mistakes is treating an invoice as a single payment transaction.&lt;/p&gt;

&lt;p&gt;In reality, customers frequently pay invoices in multiple installments. They might pay an advance, settle part of the outstanding balance later, and complete the remaining amount after a deliverable is approved.&lt;/p&gt;

&lt;p&gt;This introduces several interesting engineering challenges:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How do you maintain an accurate outstanding balance?&lt;/li&gt;
&lt;li&gt;How should partial payments be recorded?&lt;/li&gt;
&lt;li&gt;How do you prevent duplicate transactions?&lt;/li&gt;
&lt;li&gt;How should taxes and payment fees be accounted for?&lt;/li&gt;
&lt;li&gt;What happens when a payment gateway sends the same webhook multiple times?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These are not merely accounting questions. They are database consistency and application architecture problems.&lt;/p&gt;

&lt;p&gt;Let's look at how to design a reliable payment tracking system for a SaaS application.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Separate invoices from payments
&lt;/h2&gt;

&lt;p&gt;The first architectural decision should be to treat invoices and payments as separate entities.&lt;/p&gt;

&lt;p&gt;An invoice represents an amount owed by a customer. A payment represents money actually received.&lt;/p&gt;

&lt;p&gt;They have different lifecycles.&lt;/p&gt;

&lt;p&gt;For example, consider an invoice with the following details:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;th&gt;Amount&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Service amount&lt;/td&gt;
&lt;td&gt;₹20,000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;GST (18%)&lt;/td&gt;
&lt;td&gt;₹3,600&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Total invoice&lt;/td&gt;
&lt;td&gt;₹23,600&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The customer doesn't necessarily need to pay the entire amount immediately.&lt;/p&gt;

&lt;p&gt;They might pay:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First payment: ₹10,000&lt;/li&gt;
&lt;li&gt;Second payment: ₹8,000&lt;/li&gt;
&lt;li&gt;Final payment: ₹5,600&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The invoice should remain partially paid until the total successful payments equal the invoice amount.&lt;/p&gt;

&lt;p&gt;A suitable relational structure is:&lt;/p&gt;

&lt;h3&gt;
  
  
  Invoices
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Field&lt;/th&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;id&lt;/td&gt;
&lt;td&gt;Unique invoice identifier&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;customer_id&lt;/td&gt;
&lt;td&gt;Customer reference&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;invoice_number&lt;/td&gt;
&lt;td&gt;Human-readable invoice number&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;subtotal&lt;/td&gt;
&lt;td&gt;Amount before tax&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;tax_amount&lt;/td&gt;
&lt;td&gt;Applicable tax&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;total_amount&lt;/td&gt;
&lt;td&gt;Total payable&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;currency&lt;/td&gt;
&lt;td&gt;Invoice currency&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;status&lt;/td&gt;
&lt;td&gt;Draft, issued, partially paid, paid, etc.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;created_at&lt;/td&gt;
&lt;td&gt;Creation timestamp&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Payments
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Field&lt;/th&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;id&lt;/td&gt;
&lt;td&gt;Unique payment identifier&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;invoice_id&lt;/td&gt;
&lt;td&gt;Associated invoice&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;transaction_id&lt;/td&gt;
&lt;td&gt;Gateway transaction reference&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;amount&lt;/td&gt;
&lt;td&gt;Actual payment amount&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;currency&lt;/td&gt;
&lt;td&gt;Payment currency&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;status&lt;/td&gt;
&lt;td&gt;Pending, successful, failed, refunded&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;payment_method&lt;/td&gt;
&lt;td&gt;Payment method&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;received_at&lt;/td&gt;
&lt;td&gt;Successful payment timestamp&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The relationship should be one invoice to many payments.&lt;/p&gt;

&lt;p&gt;This design makes it possible to track multiple transactions against a single invoice without modifying the original invoice amount.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Calculate outstanding balances using successful payments
&lt;/h2&gt;

&lt;p&gt;One common mistake is updating the invoice's outstanding balance using payment attempts rather than successful payments.&lt;/p&gt;

&lt;p&gt;Only successfully received payments should reduce the outstanding amount.&lt;/p&gt;

&lt;p&gt;The calculation is straightforward:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;totalPaid&lt;/span&gt; &lt;span class="o"&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;filter&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;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;payment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"successful"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;payment&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;payment&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;0&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;outstanding&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;invoice&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;totalAmount&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;totalPaid&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For financial applications, use integer amounts in the smallest currency unit (paise for INR) or an appropriate decimal representation. Avoid JavaScript floating-point arithmetic for financial calculations.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Transaction&lt;/th&gt;
&lt;th&gt;Amount&lt;/th&gt;
&lt;th&gt;Status&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Payment 1&lt;/td&gt;
&lt;td&gt;₹10,000&lt;/td&gt;
&lt;td&gt;Successful&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Payment 2&lt;/td&gt;
&lt;td&gt;₹8,000&lt;/td&gt;
&lt;td&gt;Successful&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Payment 3&lt;/td&gt;
&lt;td&gt;₹5,600&lt;/td&gt;
&lt;td&gt;Pending&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The calculation should be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Invoice total: ₹23,600&lt;/li&gt;
&lt;li&gt;Successfully received: ₹18,000&lt;/li&gt;
&lt;li&gt;Outstanding: ₹5,600&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The pending payment must not reduce the outstanding balance.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Handle payment gateway webhooks carefully
&lt;/h2&gt;

&lt;p&gt;Payment gateways typically communicate payment status changes through webhooks.&lt;/p&gt;

&lt;p&gt;A successful checkout redirect does not necessarily mean that money has been received.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A customer initiates payment.&lt;/li&gt;
&lt;li&gt;The gateway processes the transaction.&lt;/li&gt;
&lt;li&gt;The customer is redirected to the application.&lt;/li&gt;
&lt;li&gt;The application receives a payment confirmation webhook.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The backend should validate the webhook signature and verify the transaction according to the gateway's documented process.&lt;/p&gt;

&lt;p&gt;Do not rely exclusively on frontend success-page navigation.&lt;/p&gt;

&lt;p&gt;A simplified Express implementation might look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&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="s2"&gt;"/webhooks/payment"&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;event&lt;/span&gt; &lt;span class="o"&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;// Verify the gateway signature before processing.&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;verified&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;verifyGatewaySignature&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="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;verified&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="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;401&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Invalid signature"&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="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"successful"&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;recordSuccessfulPayment&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="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="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&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="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Received"&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;The signature verification and event parsing must follow the specific payment gateway's documentation.&lt;/p&gt;

&lt;p&gt;The important principle is that the backend, not the browser, determines whether a payment is successful.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Prevent duplicate payments using idempotency
&lt;/h2&gt;

&lt;p&gt;This is particularly important.&lt;/p&gt;

&lt;p&gt;Payment gateways may retry webhook notifications if they do not receive an acknowledgement.&lt;/p&gt;

&lt;p&gt;The same successful transaction could therefore reach your application more than once.&lt;/p&gt;

&lt;p&gt;Without appropriate protection, you might accidentally record the same payment twice.&lt;/p&gt;

&lt;p&gt;Use the gateway transaction ID as an idempotency reference.&lt;/p&gt;

&lt;p&gt;For example, create a unique database constraint:&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;CREATE&lt;/span&gt; &lt;span class="k"&gt;UNIQUE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt;
  &lt;span class="n"&gt;unique_payment_transaction&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="nf"&gt;payments&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;transaction_id&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;Before creating a payment record, check whether that transaction has already been processed.&lt;/p&gt;

&lt;p&gt;Ideally, perform the operation within a database transaction.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&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="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;existing&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;findPaymentByTransactionId&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="n"&gt;gatewayTransactionId&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="n"&gt;existing&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="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;createSuccessfulPayment&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="n"&gt;paymentData&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;updateInvoiceStatus&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="n"&gt;invoiceId&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;The database constraint remains essential because two webhook requests could arrive simultaneously.&lt;/p&gt;

&lt;p&gt;Idempotency should be enforced at the database level, not only through application-level checks.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Treat payment fees and taxes separately
&lt;/h2&gt;

&lt;p&gt;Another important consideration is distinguishing invoice value from the amount available to the business.&lt;/p&gt;

&lt;p&gt;Suppose an invoice contains:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Service value: ₹20,000&lt;/li&gt;
&lt;li&gt;GST: ₹3,600&lt;/li&gt;
&lt;li&gt;Payment fee: ₹300&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The customer pays ₹10,000 initially.&lt;/p&gt;

&lt;p&gt;You need to distinguish between:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Amount invoiced.&lt;/li&gt;
&lt;li&gt;Amount received from the customer.&lt;/li&gt;
&lt;li&gt;Tax liability.&lt;/li&gt;
&lt;li&gt;Payment gateway charges.&lt;/li&gt;
&lt;li&gt;Net amount available to the business.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Do not assume that all these values are interchangeable.&lt;/p&gt;

&lt;p&gt;A useful financial model maintains separate records for invoice items, tax components, payments and transaction fees.&lt;/p&gt;

&lt;p&gt;If fees or tax deductions are allocated progressively across installments, the allocation must be deterministic and auditable.&lt;/p&gt;

&lt;p&gt;For example, the application should not deduct the entire fee from every installment or deduct the same tax liability twice.&lt;/p&gt;

&lt;p&gt;Maintain a clear allocation record for each payment.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Maintain an auditable payment history
&lt;/h2&gt;

&lt;p&gt;Financial records should not be treated like ordinary application preferences.&lt;/p&gt;

&lt;p&gt;Avoid simply overwriting a payment amount or deleting a transaction when something changes.&lt;/p&gt;

&lt;p&gt;Instead, maintain a traceable history of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Payment initiation.&lt;/li&gt;
&lt;li&gt;Payment confirmation.&lt;/li&gt;
&lt;li&gt;Failed payment attempts.&lt;/li&gt;
&lt;li&gt;Refunds.&lt;/li&gt;
&lt;li&gt;Payment reversals.&lt;/li&gt;
&lt;li&gt;Invoice adjustments.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A useful approach is to keep the original transaction record and record subsequent financial adjustments separately.&lt;/p&gt;

&lt;p&gt;This makes reconciliation easier and provides a reliable history when customers raise payment disputes.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Design payment status transitions carefully
&lt;/h2&gt;

&lt;p&gt;An invoice and its payments should have independent statuses.&lt;/p&gt;

&lt;h3&gt;
  
  
  Invoice statuses
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Draft&lt;/li&gt;
&lt;li&gt;Issued&lt;/li&gt;
&lt;li&gt;Partially Paid&lt;/li&gt;
&lt;li&gt;Paid&lt;/li&gt;
&lt;li&gt;Overdue&lt;/li&gt;
&lt;li&gt;Cancelled&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Payment statuses
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Pending&lt;/li&gt;
&lt;li&gt;Successful&lt;/li&gt;
&lt;li&gt;Failed&lt;/li&gt;
&lt;li&gt;Refunded&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Do not mark an invoice as paid simply because a payment was initiated.&lt;/p&gt;

&lt;p&gt;Likewise, do not mark an invoice as partially paid unless the application has recorded an actual successful payment.&lt;/p&gt;

&lt;p&gt;The invoice status should be derived from the verified payment records and any applicable credit notes or adjustments.&lt;/p&gt;

&lt;h2&gt;
  
  
  8. Don't forget subscription and revenue tracking
&lt;/h2&gt;

&lt;p&gt;For SaaS products, payment tracking has another dimension: subscription lifecycle management.&lt;/p&gt;

&lt;p&gt;A subscription may have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An activation date.&lt;/li&gt;
&lt;li&gt;A billing period.&lt;/li&gt;
&lt;li&gt;A renewal date.&lt;/li&gt;
&lt;li&gt;A payment status.&lt;/li&gt;
&lt;li&gt;A cancellation date.&lt;/li&gt;
&lt;li&gt;A payment history.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A subscription being active does not necessarily mean that a new payment has been received.&lt;/p&gt;

&lt;p&gt;Similarly, a successful payment does not automatically mean that a subscription should be extended by an entire billing period without considering the existing subscription terms.&lt;/p&gt;

&lt;p&gt;Keep subscription state and financial transactions separate, with explicit rules governing how successful payments affect subscription entitlements.&lt;/p&gt;

&lt;p&gt;This distinction becomes especially important when supporting upgrades, renewals, refunds and partial payments.&lt;/p&gt;

&lt;h2&gt;
  
  
  9. Final architecture recommendations
&lt;/h2&gt;

&lt;p&gt;For a SaaS application handling invoices and payments, I would recommend the following principles:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Maintain a one-to-many relationship between invoices and payments.&lt;/li&gt;
&lt;li&gt;Record every successful payment independently.&lt;/li&gt;
&lt;li&gt;Use gateway transaction IDs for idempotency.&lt;/li&gt;
&lt;li&gt;Verify payment notifications on the backend.&lt;/li&gt;
&lt;li&gt;Use database transactions for financial updates.&lt;/li&gt;
&lt;li&gt;Maintain separate tax, fee and payment records.&lt;/li&gt;
&lt;li&gt;Never overwrite financial history without an audit trail.&lt;/li&gt;
&lt;li&gt;Derive invoice balances from verified payment records.&lt;/li&gt;
&lt;li&gt;Separate subscription status from payment status.&lt;/li&gt;
&lt;li&gt;Make financial calculations deterministic and testable.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These principles apply to a wide range of SaaS applications, including creator management, agency management, invoicing and subscription platforms.&lt;/p&gt;

&lt;p&gt;For example, creator collaboration management systems such as &lt;a href="https://collabcy.com/" rel="noopener noreferrer"&gt;Collabcy&lt;/a&gt; need to account for the relationship between brand deals, invoices, deliverables and multiple payments.&lt;/p&gt;

&lt;p&gt;The underlying challenge is the same: financial records must accurately represent what was invoiced, what was received and what remains outstanding.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Partial payments are not an edge case that can be addressed with a simple invoice status field.&lt;/p&gt;

&lt;p&gt;They should influence the way a SaaS application's database, payment processing, reconciliation and subscription logic are designed.&lt;/p&gt;

&lt;p&gt;Separating invoices from payments, validating gateway notifications, enforcing idempotency and maintaining an auditable transaction history provides a much stronger foundation for financial functionality.&lt;/p&gt;

&lt;p&gt;Getting these fundamentals right early can prevent significant reconciliation and data-integrity problems as an application grows.&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>backend</category>
      <category>saas</category>
      <category>systemdesign</category>
    </item>
  </channel>
</rss>
