Payment bugs Python developers fix in a FastAPI service
Payments code has to be right to the cent, every time, including when a request arrives twice. Most of the bugs are small: a float where a Decimal belonged, a check in the wrong place. This guide covers the ones that come up most in a Python payments API, how to spot each one, and how to fix it. The examples come from Ledgerly, Kadra's FastAPI payments practice codebase.
Convert money with Decimal, never float
A float can't hold most decimal amounts exactly. 19.99 is stored as 19.989999..., so int(float("19.99") * 100) gives 1998 cents and a customer is a cent short. Over thousands of transfers, balances stop matching the ledger.
Ledgerly's LEDG-208 ticket is this bug. The fix keeps the amount as a Decimal until it becomes whole cents, rounds once with a stated rule, and refuses floats outright:
from decimal import Decimal, ROUND_HALF_UP
def to_cents(amount: str | Decimal) -> int:
if isinstance(amount, float):
raise TypeError("pass a str or Decimal, not a float")
value = Decimal(amount) if isinstance(amount, str) else amount
return int((value * 100).quantize(Decimal("1"), rounding=ROUND_HALF_UP))
to_cents("19.99") # 1999“going through Decimal instead of float() nails the root cause here”
In a FastAPI request model, declare amounts as Decimal or as integer cents so Pydantic never hands your code a float. Store integer cents in the database.
Check the balance before any money moves
A transfer larger than the balance should be rejected, and the account should look exactly as it did before. In LEDG-201 the check is missing, so the source account goes negative.
Put the check before the first write, so a rejected transfer changes nothing. Return a clear error to the client, such as a 409 or 422 with a message saying the funds are insufficient, rather than a 500.
if accounts[from_id] < amount_cents:
raise InsufficientFundsError(from_id, amount_cents)
accounts[from_id] -= amount_cents
accounts[to_id] += amount_centsTest the boundary too. A transfer of exactly the balance should succeed and leave zero, and one cent more should fail.
Make retried transfers return the first result
Mobile apps and API clients retry when a response is slow. If the server treats the retry as a new request, the money moves twice. The client sends an idempotency key with each transfer, and the server returns the recorded result when it sees that key again. That's LEDG-215, the senior ticket in Ledgerly.
Order matters. The key lookup has to come before the balance check, or a retry after a transfer that emptied the account fails with insufficient funds even though the first request succeeded.
if transfer_id in processed:
return processed[transfer_id] # a retry: same answer, no new transfer
if accounts[from_id] < amount_cents:
raise InsufficientFundsError(from_id, amount_cents)In production, two copies of a request can arrive at the same moment, before either is recorded. A unique constraint on the key in the database makes sure only one of them inserts.
Practice on a payments API
Ledgerly is a FastAPI payments service with the LEDG tickets above planted in it. You get your own GitHub repo, fix each ticket on a branch and open a pull request. An AI teammate reviews the diff, and a hidden pytest suite decides whether the fix passes.
Every Ledgerly repo opens in GitHub Codespaces with Python 3.13 and pip installed, so there's nothing to set up on your own machine.
Questions people ask
Should I use Decimal or integer cents for money in Python?
Store integer cents, and use Decimal for any arithmetic that can produce fractions, such as percentages or currency conversion. Round once, at the end, with a named rounding mode. Never use float.
What is an idempotency key?
A unique id the client sends with a request that changes something, such as a payment. If the same id arrives again, the server returns the first result instead of doing the work twice.
What Python skills do fintech companies look for in junior developers?
Comfort with an existing FastAPI or Django codebase, careful handling of money and dates, and tests for the edge cases. A reviewed pull request that fixed a real money bug gives you something specific to talk about.
Is Kadra free?
Yes, Kadra is free during beta with no credit card. After beta, Pro is planned at $12 a month or $79 a year, and the demo and your first full sprint stay free.
Try it on a real ticket
Pick a stack, fix a bug in a working codebase and get your pull request reviewed. Free during beta.
More guides
- How to get software engineering experience before your first job
- What your first code review at a software job looks like
- How to fix a bug in a codebase you didn't write
- What is a software engineering job simulator?
- Money bugs junior Java developers fix in banking code
- Common bugs in a Node.js CMS backend, and how to fix them
- Python ETL bugs junior data engineers fix first
- Go REST API bugs junior developers fix first
- Booking bugs C# developers fix in ASP.NET Core
- Checkout bugs developers fix in a Next.js store
- Realtime chat bugs developers fix in TypeScript