Overview
Payment processing issues can cause revenue loss, customer frustration, and compliance problems. Common issues include:- Tax calculation errors due to integer division
- Missing module imports causing runtime crashes
- Payment flow interruptions
- Calculation precision problems
Incidents
INC-004: Test payment flow failing - incorrect tax calculation
INC-004: Test payment flow failing - incorrect tax calculation
Incident Details
- Severity: P2 - High
- Service: python-service
- Environment: CI/Testing
- Reported: 2026-02-28T11:30:00Z
- Status: Resolved
Problem
Thetest_payment_flow test suite started failing in CI after a refactoring to “improve precision” using integer arithmetic. Tax amounts were calculated as zero for order subtotals under $100, resulting in incorrect order totals.Test Failures:Root Cause
The tax calculation was refactored to use integer division (//) instead of float division (/), causing the result to be truncated to zero for any subtotal less than $100 when the tax rate is 8.5%.Math breakdown:- For subtotal = $49.99 and tax rate = 8.5%:
- Expected:
49.99 * 8.5 / 100 = 4.25 - Actual:
int(49.99) // 100 * 8.5 = 49 // 100 * 8.5 = 0 * 8.5 = 0
- Expected:
app/services/payment_service.py:13-23Problematic Code
payment_service.py
Resolution
Use proper float arithmetic for percentage calculations:payment_service.py
Alternative: Use Decimal for Precision
For financial calculations, consider using Python’sdecimal module:payment_service.py
Prevention
- Test edge cases: Include tests with small amounts (< 10, < $100)
- Never use integer division for percentages: Always use float division for percentage calculations
- Consider Decimal: Use
decimal.Decimalfor financial calculations to avoid floating-point precision issues - Code review: Be suspicious of “performance optimizations” in financial code
- Regression tests: Add failing test cases before fixing bugs
Test Coverage
INC-008: Checkout endpoint crashes with NameError
INC-008: Checkout endpoint crashes with NameError
Incident Details
- Severity: P1 - Critical
- Service: python-service
- Environment: Staging
- Reported: 2026-02-28T16:45:00Z
- Status: Resolved
Problem
ThePOST /api/payments/checkout endpoint crashed with a 500 Internal Server Error when processing payments. The order was created successfully, but attempting to process the payment failed. This was introduced when adding logging statements to track payment events.Error Message:Root Cause
Thelogging module was used without being imported. A developer added logging.info() calls to track payment processing but forgot to add the import statement at the top of the file.Location: app/routes/payments.py:62Problematic Code
payments.py
Resolution
Add the missinglogging import:payments.py
Alternative: Use Flask’s Logger
Flask applications have a built-in logger accessible viacurrent_app.logger:payments.py
Prevention
- Linting: Use a linter like
pylintorflake8to catch undefined names - IDE support: Use an IDE that highlights undefined variables
- Type checking: Run
mypyto catch missing imports and type errors - Pre-commit hooks: Add linting to pre-commit hooks
- Test coverage: Ensure tests execute all code paths
Linting Configuration
.flake8
pyproject.toml
Pre-commit Hook
.pre-commit-config.yaml
Common Patterns
Tax Calculation Best Practices
Payment Processing Pattern
Logging Setup
Quick Reference
| Issue Type | Common Cause | Quick Fix |
|---|---|---|
| Tax always zero | Integer division for percentages | Use float(subtotal) * (rate / 100) |
| NameError | Missing import | Add import statement at top of file |
| Rounding errors | Float precision issues | Use decimal.Decimal for money |
| Tax on wrong amount | Pre/post discount confusion | Clarify tax calculation order |