# CLAUDE.md

This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.

## Commands

```bash
# Install dependencies
composer install

# Run all tests
vendor/bin/phpunit

# Run a single test file
vendor/bin/phpunit tests/Unit/Builder/AcquiringResponseBuilderTest.php

# Run a single test method
vendor/bin/phpunit --filter testMethodName

# Run only unit or functional tests
vendor/bin/phpunit tests/Unit/
vendor/bin/phpunit tests/Functional/

# Symfony console
php bin/console [command]
```

## Architecture

This is a **PHP 8.1 / Symfony 5.4** payment acquiring gateway that proxies and orchestrates calls to multiple external microservices (Card, BIN, Transaction, Gateway, Store, Merchant, Account, 3DS, etc.) to process card payment operations (pre-authorization, purchase, capture, refund, reversal, credit, OCT, AVS).

### Request flow

```
HTTP Request
  → Controller (src/Controller/)        — validates input, delegates to Manager
  → Manager (src/Manager/)              — orchestrates business logic
  → RequestManager (src/RequestManager/) — calls external microservice APIs via Guzzle
  → Builder (src/Builder/)              — transforms raw API responses into DTOs
  → Controller returns JsonResponse
```

### Key layers

- **Controllers** (`src/Controller/`) — thin: deserialize request into DTOs, call the matching Manager, return response.
- **Managers** (`src/Manager/`) — the business logic lives here. `PaymentManager` is the most complex; it coordinates the full lifecycle of a transaction across multiple external services.
- **RequestManagers** (`src/RequestManager/`) — one sub-namespace per external service (Account, Bin, Card, Gateway, Instance, Merchant, Store, Transaction, ThreeDomain, Noto, Payoo, MemberCheck). Each issues Guzzle HTTP calls to the corresponding microservice URL configured in env vars.
- **Builders** (`src/Builder/`) — convert external-service responses to internal DTOs.
- **DTOs** (`src/Dto/Request/`, `src/Dto/Response/`) — typed data contracts; Symfony Serializer handles deserialization.
- **Constants** (`src/Constants/`) — canonical values for operations, transaction statuses, card schemes, processors, SCA exemptions, recurring types, etc. Always prefer these over raw strings.

### Async messaging

Symfony Messenger with AMQP (RabbitMQ) handles async work. Message classes live in `src/Message/`; handlers in `src/MessageHandler/` (if present). Messages: `TopUpMessage`, `TransactionMessage`, `NotificationMessage`, `TransactionServiceStatusMessage`.

### Authentication

Instance/merchant identity is established via HMAC-signed headers. See `src/Security/Instance/` for the security voter/provider. `InstanceProviderMock` is used in tests.

### Deployment

Deployed to AWS Lambda via [Bref](https://bref.sh) with ALB in front (`serverless.yaml`). Lambda writes cache/logs to `/tmp`.

## Testing

Tests live in `tests/Unit/` and `tests/Functional/`. Functional tests extend `BaseWebTestCase` (`tests/Functional/BaseWebTestCase.php`), which:
- Boots the Symfony kernel in `test` environment
- Injects a `GuzzleHttp\Handler\MockHandler` so all outbound HTTP calls to external services can be queued with `$this->httpClientHandler->append(new Response(...))`
- Provides helpers: `generateAuthHeaders()`, `assertResponseSuccess()`, `assertResponseError()`, `getDecodedJsonResponse()`

Mock response payloads are centralised in `tests/Helper/Responses/` (e.g. `CardServiceResponses`, `BinServiceResponses`, `TransactionServiceResponses`). Shared constants for test data are in `tests/Helper/Constants/AcquiringTestConstants.php`.

`dama/doctrine-test-bundle` wraps each test in a rolled-back transaction to keep the database clean between tests.

Behavioral guidelines to reduce common LLM coding mistakes. Merge with project-specific instructions as needed.

**Tradeoff:** These guidelines bias toward caution over speed. For trivial tasks, use judgment.

## 1. Think Before Coding

**Don't assume. Don't hide confusion. Surface tradeoffs.**

Before implementing:
- State your assumptions explicitly. If uncertain, ask.
- If multiple interpretations exist, present them - don't pick silently.
- If a simpler approach exists, say so. Push back when warranted.
- If something is unclear, stop. Name what's confusing. Ask.

## 2. Simplicity First

**Minimum code that solves the problem. Nothing speculative.**

- No features beyond what was asked.
- No abstractions for single-use code.
- No "flexibility" or "configurability" that wasn't requested.
- No error handling for impossible scenarios.
- If you write 200 lines and it could be 50, rewrite it.

Ask yourself: "Would a senior engineer say this is overcomplicated?" If yes, simplify.

## 3. Surgical Changes

**Touch only what you must. Clean up only your own mess.**

When editing existing code:
- Don't "improve" adjacent code, comments, or formatting.
- Don't refactor things that aren't broken.
- Match existing style, even if you'd do it differently.
- If you notice unrelated dead code, mention it - don't delete it.

When your changes create orphans:
- Remove imports/variables/functions that YOUR changes made unused.
- Don't remove pre-existing dead code unless asked.

The test: Every changed line should trace directly to the user's request.

## 4. Goal-Driven Execution

**Define success criteria. Loop until verified.**

Transform tasks into verifiable goals:
- "Add validation" → "Write tests for invalid inputs, then make them pass"
- "Fix the bug" → "Write a test that reproduces it, then make it pass"
- "Refactor X" → "Ensure tests pass before and after"

For multi-step tasks, state a brief plan:
```
1. [Step] → verify: [check]
2. [Step] → verify: [check]
3. [Step] → verify: [check]
```