# 🧪 UPI PSP API Testing Guide

## Quick Manual Testing

### 1. Start the Server
```bash
cd /home/prem/mercurypay/mercury_upi_psp
mix phx.server
```
The server will start on port 4040 (as configured in dev.exs).

### 2. Test QR Validation API

**Test with valid ReqValQr:**
```bash
curl -X POST http://localhost:4040/api/v1/qr-validation \
  -H "Content-Type: application/json" \
  -d '{
    "xml": "<?xml version=\"1.0\" encoding=\"UTF-8\"?><ReqValQr><Head msgId=\"MSG123\" ts=\"2025-08-11T10:30:00Z\" orgId=\"NPCIORG\"/><Qr addr=\"merchant@axis\" mid=\"MERCHANT001\"/></ReqValQr>"
  }'
```

**Expected Response:**
```json
{
  "success": true,
  "data": {
    "id": 1,
    "ver_token": "VER-abc123...",
    "status": "pending",
    "payee_addr": "merchant@axis",
    "payee_mid": "MERCHANT001",
    "fx_quotes": 0
  },
  "message": "QR validation processed successfully"
}
```

### 3. Test Transaction API

**Create a transaction:**
```bash
curl -X POST http://localhost:4040/api/v1/transactions \
  -H "Content-Type: application/json" \
  -d '{
    "payer_addr": "user@paytm",
    "payee_addr": "merchant@axis",
    "payee_mid": "MERCHANT001",
    "foreign_amount": "100.00",
    "foreign_currency": "USD",
    "fx_rate": "83.25",
    "markup_pct": "2.5"
  }'
```

**Expected Response:**
```json
{
  "success": true,
  "data": {
    "org_txn_id": "TXN-abc12345",
    "payer_addr": "user@paytm",
    "payee_addr": "merchant@axis",
    "foreign_amount": "100.00",
    "inr_amount": "8525.00",
    "current_state": "credit_pending",
    "status": "pending"
  },
  "message": "Transaction initiated successfully"
}
```

### 4. Get Transaction Details

**Using the org_txn_id from step 3:**
```bash
curl http://localhost:4040/api/v1/transactions/TXN-abc12345
```

### 5. Simulate Credit Success

```bash
curl -X POST http://localhost:4040/api/v1/transactions/TXN-abc12345/credit-success
```

## Error Scenarios to Test

### Invalid XML:
```bash
curl -X POST http://localhost:4040/api/v1/qr-validation \
  -H "Content-Type: application/json" \
  -d '{"xml": "<invalid>xml</incomplete>"}'
```

### Missing XML:
```bash
curl -X POST http://localhost:4040/api/v1/qr-validation \
  -H "Content-Type: application/json" \
  -d '{}'
```

### Unknown Message Type:
```bash
curl -X POST http://localhost:4040/api/v1/qr-validation \
  -H "Content-Type: application/json" \
  -d '{"xml": "<?xml version=\"1.0\"?><UnknownMsg><Head/></UnknownMsg>"}'
```

## What We Fixed

1. **Error Handling**: Better parsing error messages in the controller
2. **Message Type Detection**: Fallback XML parsing using string matching
3. **Database Separation**: Test DB (`da_product_app_test`) vs Dev DB (`upi_pps`)
4. **Root Element Extraction**: More robust XML structure detection

## Success Indicators

✅ **QR Validation**: Returns proper ver_token and processes XML  
✅ **Transaction Creation**: Auto-generates org_txn_id and calculates INR amount  
✅ **State Machine**: Progresses through initiated → debit_secured → credit_pending  
✅ **Error Handling**: Clear error messages for invalid inputs  
✅ **Event Logging**: Hash-chained events stored for audit trail

## Debugging Tips

If you get errors:
1. Check server logs for detailed error messages
2. Verify database connection (both dev and test DBs exist)
3. Test XML parser directly in IEx:
   ```elixir
   xml = "<ReqValQr>...</ReqValQr>"
   DaProductApp.QRValidation.Parser.UpiXmlParser.parse_upi_xml(xml)
   ```

## Next Steps After Validation

Once all tests pass:
- ✅ **Phase 5B**: Real Partner Integration  
- ✅ **Phase 5C**: Webhook Infrastructure  
- ✅ **Phase 5D**: Security & Production Readiness
