> This page is for MVS Pay.

> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.mvscloud.com/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.mvscloud.com/_mcp/server.

# Disputes & Mandates

This page covers two distinct but adjacent surfaces in mvs-pay-extensions:

* **Disputes** — the chargeback lifecycle: reading disputes Hyperswitch surfaces
  from connectors, attaching and finalizing evidence, and the MVS-only sidecar
  metadata (alerts, assignment, deadlines) layered on top.
* **Mandates** — the authorization underneath recurring payments: listing,
  reading, revoking, and the **amend** orchestration MVS synthesizes because
  Hyperswitch has no native amend endpoint.

Both are thin Hono routers in `apps/mvs-pay-extensions/src/routes/` that delegate
to the typed Hyperswitch client (`apps/mvs-pay-extensions/src/services/hyperswitch-client.ts`)
and, where MVS adds value, to per-org extension tables in
`packages/mvs-pay/prisma/schema.prisma`. Hyperswitch is the system of record for
the money-moving state; MVS owns only the workflow metadata around it. This is
the [native-Hyperswitch bias](/mvs-pay/guides/concepts/adrs/adr-007-native-hyperswitch-bias) at
work — we wrap, we do not reimplement.

> **Note**
>
> Every route on this page is mounted under `/v1` behind the gateway middleware
> (`app.use("*", authMiddleware())`), so a resolved `tenant.organizationId` is
> present on the request context. The order is **auth → rate-limit → idempotency →
> audit-log → handler** (see `apps/mvs-pay-extensions/src/app.ts`). See
> [Authentication](/mvs-pay/guides/concepts/authentication) for how the internal-secret +
> `x-mvs-org-id` contract resolves the tenant.

## Disputes (chargebacks)

### Routes

All dispute routes live in `apps/mvs-pay-extensions/src/routes/disputes.ts`,
mounted at `/v1/disputes` (`api.route("/disputes", disputeRoutes)`).

| Method & path                      | Purpose                                 | Hyperswitch call                                             | MVS sidecar effect                                        |
| ---------------------------------- | --------------------------------------- | ------------------------------------------------------------ | --------------------------------------------------------- |
| `GET /v1/disputes`                 | List disputes (optional `limit`)        | `disputes.list` → `/disputes/list`                           | none                                                      |
| `GET /v1/disputes/:id`             | Read one dispute, merged with sidecar   | `disputes.get` → `/disputes/:id`                             | reads `disputeExtension` (org-scoped)                     |
| `POST /v1/disputes/:id/evidence`   | Attach an evidence file/field           | `disputes.submitEvidence` → `/disputes/evidence`             | none                                                      |
| `POST /v1/disputes/:id/submit`     | Finalize & submit evidence to connector | `disputes.submitEvidenceFinal` → `/disputes/evidence/submit` | upserts `disputeExtension`, stamps `lastPlatformActionAt` |
| `PATCH /v1/disputes/:id/extension` | Edit MVS-only workflow metadata         | none                                                         | upserts `disputeExtension`                                |

The SDK mirrors these as `client.disputes.list / get / submitEvidence / submit /
updateExtension` (`packages/mvs-pay-sdk/src/namespaces/disputes.ts`). Full
request/response shapes are in the [API Reference](/mvs-pay/guides/reference) under the disputes
resource.

> **Warning**
>
> There are **two distinct submit steps**, and they are not interchangeable.
> `POST /:id/evidence` (`/disputes/evidence`) *attaches* a piece of evidence.
> `POST /:id/submit` (`/disputes/evidence/submit`) *finalizes* the attached bundle
> and hands it to the connector for review — this is the irreversible one. Attach
> all evidence first, then submit exactly once.

### Evidence submission flow

```mermaid
sequenceDiagram
    participant Op as Operator / app
    participant SDK as mvs-pay-sdk
    participant Ext as mvs-pay-extensions
    participant HS as Hyperswitch
    participant DB as DisputeExtension (Neon)

    Op->>SDK: disputes.submitEvidence(id, {...})
    SDK->>Ext: POST /v1/disputes/:id/evidence
    Ext->>HS: POST /disputes/evidence {dispute_id, ...}
    HS-->>Ext: evidence attached
    Note over Op,HS: repeat for each evidence field/file

    Op->>SDK: disputes.submit(id)
    SDK->>Ext: POST /v1/disputes/:id/submit (idempotency-key)
    Ext->>DB: ownership pre-check (hyperswitchDisputeId unique)
    Ext->>HS: POST /disputes/evidence/submit {dispute_id}
    HS-->>Ext: submitted to connector
    Ext->>DB: upsert sidecar, set lastPlatformActionAt = now()
    Ext-->>Op: result
```

The attach step (`submitEvidence`) merges the caller's JSON body into the
Hyperswitch request as `{ dispute_id, ...body }` — the client method injects
`dispute_id`, so callers do not pass it. Hyperswitch's evidence schema (document
types, field names) is owned upstream; mvs-pay-extensions passes the body through
untouched.

### The dispute extension sidecar

`GET /v1/disputes/:id` augments Hyperswitch's payload with the MVS-only row, when
one exists, under a `mvs_extension` key:

```ts
return c.json({ ...(dispute as object), mvs_extension: ext });
```

The sidecar is `DisputeExtension` in `packages/mvs-pay/prisma/schema.prisma`,
keyed by a **globally unique** `hyperswitchDisputeId` and owned by exactly one
`organizationId`:

| Column group | Fields                                                                                          | Notes                                                  |
| ------------ | ----------------------------------------------------------------------------------------------- | ------------------------------------------------------ |
| Identity     | `hyperswitchDisputeId` (unique), `hyperswitchPaymentIntentId`, `hyperswitchMerchantId`          | dispute id is server-controlled, never mass-assignable |
| Alerting     | `alertPriority`, `alertAcknowledgedAt`, `alertAcknowledgedBy`, `emailSentAt`                    | populated by the alert/notification path               |
| Workflow     | `platformNotes`, `assignedToUserId`, `lastPlatformActionAt`, `autoAcceptAt`, `platformPriority` | edited via `PATCH /:id/extension`                      |
| Soft delete  | `isDeleted`, `deletedAt`                                                                        | rows are soft-deleted, never hard-deleted              |

> **Warning**
>
> Because `hyperswitchDisputeId` is globally unique across all tenants, both
> `POST /:id/submit` and `PATCH /:id/extension` run an **ownership pre-check**: if a
> sidecar row already exists for a *different* org, the route returns `404 not_found`
> rather than `403`. This is deliberate — a `403` would leak the existence of
> another tenant's dispute. Do not "fix" the 404 into a 403.

`PATCH /:id/extension` also strips `organizationId` and `hyperswitchDisputeId`
from the request body before writing, so the caller can never reassign tenancy or
identity columns via mass assignment:

```ts
const { organizationId: _o, hyperswitchDisputeId: _h, ...data } = body;
```

`autoAcceptAt` and `platformPriority` exist as columns for an
operator-driven auto-accept workflow, but **there is no scheduler today that acts
on `autoAcceptAt`** — it is metadata only. If you build the auto-accept job,
that's a net-new worker, not an existing one.

### PHI audit logging

`GET /:id`, `POST /:id/evidence`, and `POST /:id/submit` all emit a `phiAudit.logAccess`
record (`@mvs/logs`) with `resourceType: "dispute"` and a `subAction` of
`dispute.read`, `dispute.submit_evidence`, or `dispute.submit_evidence_final`.
This is the HIPAA access trail — dispute evidence can reference patient billing,
so reads and writes are logged even though the `patientId` field is passed empty
here. Do not strip these calls when refactoring.

### Webhook-driven dispute events

Hyperswitch fires `WEBHOOK_DISPUTE_*` lifecycle events to
`apps/mvs-pay-extensions/src/routes/webhooks-hyperswitch.ts`. Per the handler's
own docstring the intended fan-out is to "create a Notification on
`WEBHOOK_DISPUTE_OPENED`, etc." — routing of dispute events into the notification
/ alert tables is owned by the webhook dispatcher
(`apps/mvs-pay-extensions/src/services/webhook-dispatcher.ts`), not by the dispute
routes themselves. Verified webhooks are persisted as `WebhookDeliveryAttempt`
rows (idempotent on `hyperswitchEventId`) and re-driven by the Temporal
`webhookDlqDrainerWorkflow` on failure. See
[Authentication](/mvs-pay/guides/concepts/authentication) for webhook signature verification
(HMAC-SHA512 of the raw body using the merchant's `payment_response_hash_key`,
exposed as `HYPERSWITCH_WEBHOOK_SECRET`).

> **Note**
>
> Rate limiting: dispute routes use the `disputes` bucket
> (`MVS_PAY_RATE_LIMIT_DISPUTES_*`, default 600 requests / 60s), configured in
> `apps/mvs-pay-extensions/src/middleware/rate-limit.ts` and applied in `app.ts` to
> both `/disputes` and `/disputes/*`.

### Honest gaps (disputes)

* **No first-class evidence-bundle model in MVS.** Evidence lives entirely in
  Hyperswitch; MVS does not persist what was submitted. The sidecar records only
  *that* a platform action happened (`lastPlatformActionAt`), not the contents.
* **No auto-accept scheduler.** `autoAcceptAt` is a column with no worker.
* **No dedicated dispute-event handler in the route layer.** Dispute notification
  fan-out is the dispatcher's responsibility and should be verified there before
  relying on it.

## Mandates (recurring authorization)

A mandate is the standing authorization a customer grants for off-session
recurring charges. In MVS, the mandate is *only* the payment authorization — the
subscription-of-record is `BillingSubscription` in mvs-pay. The mandate id is
stored on the subscription and is what recurring captures ride.

### Routes

All mandate routes live in `apps/mvs-pay-extensions/src/routes/mandates.ts`,
mounted at `/v1/mandates`.

| Method & path                  | Purpose                                | Hyperswitch call                           |
| ------------------------------ | -------------------------------------- | ------------------------------------------ |
| `GET /v1/mandates`             | List mandates (optional `customer_id`) | `mandates.list` → `/mandates/list`         |
| `GET /v1/mandates/:id`         | Read one mandate                       | `mandates.get` → `/mandates/:id`           |
| `POST /v1/mandates/:id/revoke` | Revoke (terminal) a mandate            | `mandates.revoke` → `/mandates/revoke/:id` |
| `POST /v1/mandates/:id/amend`  | Change a mandate's amount cap          | **synthesized** — revoke + setup-mandate   |

SDK equivalents: `client.mandates.list / get / revoke / amend`
(`packages/mvs-pay-sdk/src/namespaces/mandates.ts`).

> **Note**
>
> **Creation is not its own endpoint here.** A mandate is minted as a side effect
> of a payment intent that carries `setup_future_usage: "off_session"` (or
> `on_session`) plus a `mandate_data.customer_acceptance` block — handled on the
> payments surface. There is no `POST /v1/mandates`. The amend route below uses
> exactly this setup-mandate flow to mint a replacement.

### Amend: why it is an orchestration, not a passthrough

> **Warning**
>
> Hyperswitch has **no mandate-amend endpoint**. A mandate's amount cap is fixed at
> creation. To change it, mvs-pay-extensions revokes the old mandate and creates a
> fresh one. This is the single most surprising thing on this page.

`POST /v1/mandates/:id/amend` runs a three-step server-side orchestration. The
caller must supply a positive `new_amount` (minor units) and a fresh
`customer_acceptance` object — most card schemes and bank rails (SEPA, BACS, ACH)
require renewed consent for an amount increase, which is why the route rejects a
request without it rather than silently re-using the old consent.

#### Fetch the old mandate

`hyperswitch.mandates.get(mandateId)` — used to carry `customer_id` and
`payment_method` forward when the caller omits them, and to return the pre-revoke
shape.

#### Revoke the old mandate

`hyperswitch.mandates.revoke(mandateId)`. **After this point the old mandate is
terminal** and no further captures ride it.

#### Mint the replacement via a \$0 setup intent

`hyperswitch.payments.create({ amount: 0, confirm: true, setup_future_usage:
"off_session", mandate_data: { customer_acceptance, mandate_type: { multi_use: {
amount: new_amount, currency } } }, metadata: { mvs_amend_of_mandate, mvs_org_id }
})`. The `mandate_id` returned by this intent is re-fetched and returned as
`newMandate`.

```mermaid
sequenceDiagram
    participant App as Caller (billing platform)
    participant Ext as mvs-pay-extensions
    participant HS as Hyperswitch

    App->>Ext: POST /v1/mandates/:id/amend {new_amount, customer_acceptance}
    Ext->>Ext: validate new_amount > 0, customer_acceptance present
    Ext->>HS: 1. GET /mandates/:id  (hydrate customer_id + payment_method)
    Ext->>HS: 2. POST /mandates/revoke/:id  (old mandate now TERMINAL)
    Ext->>HS: 3. POST /payments {amount:0, off_session, mandate_data.multi_use}
    HS-->>Ext: payment intent with new mandate_id
    Ext->>HS: GET /mandates/:newId  (re-fetch the minted mandate)
    Ext-->>App: { oldMandate: status "revoked", newMandate }
```

The route returns **both records** so the caller can persist the transition: mark
`oldMandate` revoked and point the subscription at `newMandate.mandate_id`.
`newMandate.status` may be `pending` until the connector confirms the acceptance
block — poll `client.mandates.get(newMandateId)` until `active` and surface a
"confirming" state in the UI.

> **Warning**
>
> **`multi_use` is mandatory for recurring.** The replacement mandate is created
> with `mandate_type.multi_use`, carrying the new per-transaction amount cap. A
> `single_use` replacement would authorize exactly one off-session payment and
> *silently* break the subscription's future automatic captures. Do not change this
> to `single_use`.

### Idempotency and the dangerous middle state

The amend is multi-step and **not atomic**. The most important failure mode: the
revoke succeeds but the setup-intent fails (e.g. the connector rejects the new
acceptance). The subscription is now left with **no live mandate**.

> **Warning**
>
> Always pass a stable `idempotency-key` header (the SDK forwards it; derive it from
> the subscription + amendment version, e.g. `amend_${mandateId}_${version}`).
> Retrying *without* an idempotency key after a partial failure risks re-revoking an
> already-revoked mandate. The idempotency key is forwarded only to the
> setup-intent `payments.create` call — the revoke is not idempotency-keyed — so a
> keyless retry is genuinely unsafe.

Validation and recovery, lifted directly from the runbook:

| Symptom                                    | Cause                                   | Recovery                                                                                                                                  |
| ------------------------------------------ | --------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------- |
| `400 new_amount must be a positive number` | missing/zero `new_amount`               | supply a positive minor-unit amount                                                                                                       |
| `400 customer_acceptance is required`      | omitted consent block                   | collect fresh acceptance (IP + UA + timestamp) and resend                                                                                 |
| `400 invalid_json`                         | unparseable request body                | send valid JSON                                                                                                                           |
| `400 tenant_required`                      | no resolved `organizationId` on context | fix the auth/org header (see Authentication)                                                                                              |
| revoke succeeded, setup-intent failed      | connector rejected the new acceptance   | old mandate is revoked — subscription has **no live mandate**; create a brand-new mandate (not an amend) and update `BillingSubscription` |
| `newMandate.status === "pending"`          | connector hasn't confirmed acceptance   | poll `client.mandates.get(newMandateId)` until `active`                                                                                   |

The setup intent stamps `metadata.mvs_amend_of_mandate = <oldMandateId>` and
`metadata.mvs_org_id`, so an amendment is traceable in Hyperswitch's event stream.
A successful amend also logs `[mandates] amend complete` with the old/new mandate
ids and new amount.

### SDK usage

```ts
import { MvsPayClient } from "@mvs/mvs-pay-sdk";

const mvsPay = new MvsPayClient({
  baseUrl: process.env.MVS_PAY_EXTENSIONS_URL!,
  internalSecret: process.env.MVS_PAY_EXTENSIONS_INTERNAL_SECRET!,
  orgId: tenant.mvsOrgId,
});

const { oldMandate, newMandate } = await mvsPay.mandates.amend(
  {
    mandateId: "man_existing",
    newAmount: 4900, // new cap, minor units
    customerAcceptance: {
      acceptance_type: "online",
      accepted_at: new Date().toISOString(),
      online: { ip_address: req.ip, user_agent: req.headers["user-agent"] },
    },
    // optional — hydrated from the old mandate if omitted:
    // currency, customerId, paymentMethod, paymentMethodType, profileId
  },
  { idempotencyKey: `amend_${mandateId}_${version}` },
);
```

### Honest gaps (mandates)

* **No native amend.** The entire amend behavior is synthesized client-side over
  revoke + setup-mandate. There is no transaction wrapping the two; the
  middle-state window is real and must be handled by the caller.
* **No `POST /v1/mandates`.** Mandate creation is implicit via the payments
  surface; there is no standalone create route or SDK method.
* **The revoke step is not idempotency-keyed**, so retries lean entirely on the
  caller's stable key and the runbook's recovery table.

## Where MVS ends and Hyperswitch begins

```mermaid
flowchart LR
    subgraph MVS["mvs-pay-extensions + mvs-pay"]
      D["disputes.ts<br />(workflow + sidecar)"]
      M["mandates.ts<br />(amend orchestration)"]
      DB[("DisputeExtension<br />BillingSubscription")]
    end
    subgraph HS["Hyperswitch (system of record)"]
      HD["disputes + evidence"]
      HM["mandates + setup intents"]
    end
    D --> HD
    D --> DB
    M --> HM
    M -. "subscription.mandateId" .- DB
```

* **Hyperswitch owns:** dispute state and evidence contents; mandate state, amount
  caps, and acceptance validation.
* **MVS owns:** dispute workflow metadata (alerts, assignment, deadlines, notes),
  the subscription-of-record, and the amend orchestration that papers over the
  missing Hyperswitch endpoint.

## Related

#### [Runbook: Mandate amendment](/mvs-pay/guides/operations/runbooks/mandate-amendment)

Step-by-step amend procedure, failure modes, and billing-platform coupling.

#### [Runbook: Smart Retries](/mvs-pay/guides/operations/runbooks/smart-retries)

Decline recovery for the recurring charges that ride an active mandate.

#### [ADR-011: Revenue Recovery](/mvs-pay/guides/concepts/adrs/adr-011-revenue-recovery)

Dunning for failed recurring charges is handed to Hyperswitch Revenue Recovery.

#### [ADR-007: Native Hyperswitch bias](/mvs-pay/guides/concepts/adrs/adr-007-native-hyperswitch-bias)

Why disputes and mandates wrap rather than reimplement Hyperswitch.

#### [Authentication](/mvs-pay/guides/concepts/authentication)

Tenant resolution, internal-secret auth, and webhook signature verification.

#### [API Reference](/mvs-pay/guides/reference)

Full request/response shapes for the disputes and mandates resources.

#### Source files referenced on this page

* `apps/mvs-pay-extensions/src/routes/disputes.ts`
* `apps/mvs-pay-extensions/src/routes/mandates.ts`
* `apps/mvs-pay-extensions/src/services/hyperswitch-client.ts`
* `apps/mvs-pay-extensions/src/routes/webhooks-hyperswitch.ts`
* `apps/mvs-pay-extensions/src/middleware/rate-limit.ts`
* `apps/mvs-pay-extensions/src/app.ts`
* `packages/mvs-pay/prisma/schema.prisma` (`DisputeExtension`)
* `packages/mvs-pay-sdk/src/namespaces/disputes.ts`
* `packages/mvs-pay-sdk/src/namespaces/mandates.ts`
* `docs/runbooks/mandate-amendment.md`