feat: implement circuit breaker pattern for external payment provider calls#304
Merged
Nathydre merged 3 commits intoJun 24, 2026
Conversation
Closes nathydre21#293 - Add CircuitBreaker utility class (backend/src/utils/circuitBreaker.ts) with CLOSED/OPEN/HALF_OPEN states, configurable failure threshold and recovery time, CircuitOpenError typed error, and manual reset support - Integrate circuit breaker in PaymentService: wraps executePayment so failures above threshold stop hitting the provider; also adds input validation for meter_id, amount and userId - Integrate per-provider circuit breakers in MultiProviderPaymentService: one independent breaker per provider so a degraded provider does not block healthy ones; lazy creation for providers added after construction - Add circuit-breaker.test.ts covering all state transitions (CLOSED, OPEN, HALF_OPEN), error propagation, manual reset, and custom config
Fix 10 TypeScript compilation errors caught by the Comprehensive Tests CI job: - NotificationPermission.tsx: split mixed import into value + 'import type' for NotificationPreferences (verbatimModuleSyntax) - AccountMigration.tsx: use 'import type' for MigrationStatus, MigrationConfig, EmergencyRecovery, EncryptedMigrationData - AdminRoutes.tsx: fix wrong module path '../components/Admin/ContractManager' -> '../hooks/ContractManager' (file lives in hooks/) - components/AdvancedSearch.tsx: correct '../../hooks/usePaymentSearch', '../../services/searchService', '../../utils/sanitize' paths to '../...' (file is already in src/components, not a subdirectory); fix implicit 'any' on setFilters callback param - services/AdvancedSearch.tsx: same path and implicit-any fixes as above; add SearchFilters to import
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #293
This PR implements a circuit breaker pattern for all external payment provider calls in �ackend/src/services/payment-service.ts and �ackend/src/services/multiProviderPaymentService.ts, improving system resilience against provider outages and repeated failures.
Problem
External payment provider calls had no protection against cascading failures. A provider outage or repeated errors would cause every request to hit the failing service and return errors with no fast-fail mechanism.
Solution
A lightweight, zero-dependency CircuitBreaker class was added and integrated at the call sites for both payment services.
Changes
New file: �ackend/src/utils/circuitBreaker.ts
ecoveryTimeMs (default 60 s)
eset() method for admin/maintenance use
Modified: �ackend/src/payment-service.ts
Modified: �ackend/src/services/multiProviderPaymentService.ts
New file: �ackend/src/test/circuit-breaker.test.ts
Acceptance Criteria
Testing
The CI workflow (.github/workflows/test.yml) runs the Rust contract tests. The Node.js backend tests (Jest) are in �ackend/ and cover all new and modified code.
New test file: �ackend/src/test/circuit-breaker.test.ts
Existing test suites unaffected: