> **Can't find what you're looking for?** Use `search_docs` on the docs MCP server at `https://mpp.dev/api/mcp` to find what you need.

import { MermaidDiagram } from '../../components/MermaidDiagram'

# Refunds \[Return funds to clients]

## Overview

MPP does not define a dedicated refund protocol. How refunds work depends on the payment flow your service uses.

## Charge flow

In the charge flow, funds transfer to the server immediately when the client pays. Refunds are **out-of-protocol**—the server sends funds back to the client's address directly.

```mermaid
sequenceDiagram
  participant Client
  participant Server
  Client->>Server: Request + Credential
  Note over Server: Funds transfer
  Server-->>Client: 200 OK + Receipt
  Note over Server: Refund triggered
  Server->>Client: Direct transfer back

```

The server knows the client's address from the Credential's `source` field and can return funds at any time using a standard on-chain transfer.

### Implementation

To refund a charge, send the refund amount back to the client's public key:

```ts twoslash [server.ts]
// @noEmit
declare const credential: { source: string }
declare function sendTransfer(params: { amount: string; to: string }): Promise<void>
// ---cut---
async function refundCharge(credential: { source: string }, amount: string) {
  const clientAddress = credential.source
  await sendTransfer({ amount, to: clientAddress })
}
```

Refund decisions are up to your service. Common triggers include failed processing, service errors, or customer support requests.

## Session flow

In the v2 session flow, funds are reserved in a Tempo channel but not immediately claimed. If the server never claims the reserved funds, they return to the client after the session expires or closes.

This gives sessions a **built-in refund mechanism**—unclaimed funds are refunded by default.

```mermaid
sequenceDiagram
  participant Client
  participant Server
  participant Tempo
  Client->>Tempo: Deposit tokens
  Tempo-->>Client: Channel created
  Client->>Server: Open Credential
  Server-->>Client: 200 OK (session established)
  loop Per request
      Client->>Server: Request + voucher
      Server-->>Client: 200 OK + Receipt
  end
  Note over Server: Close channel with last voucher
  Server->>Tempo: close(channelId, voucher)
  Note over Tempo: Settle claimed amount to server
  Tempo-->>Client: Refund unclaimed deposit

```

| Scenario | Outcome |
|----------|---------|
| Server claims funds | Payment completes, no refund |
| Server never claims | Funds return to client after session expiry |
| Server claims partial amount | Remaining funds return to client |

### Refunding via channel close

To refund a session, close the channel with the last accepted voucher. Any unclaimed deposit returns to the client automatically.

#### Accounts SDK

```ts [client.ts]
import { tempo } from 'mppx/client'
import { Provider } from 'accounts'

const provider = Provider.create({ mpp: false }) // Avoid double 402 handling; mppx is configured below.
await provider.request({ method: 'wallet_connect' })

const session = tempo.session.manager({
  account: provider.getAccount({ signable: true }),
  getClient: provider.getClient,
  maxDeposit: '10',
})

await session.fetch('https://api.example.com/resource')

const receipt = await session.close()
console.log(receipt?.txHash)
// @log: 0x...
```

#### viem

```ts [client.ts]
import { tempo } from 'mppx/client'
import { privateKeyToAccount } from 'viem/accounts'

const session = tempo.session.manager({
  account: privateKeyToAccount('0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'),
  maxDeposit: '10',
})

await session.fetch('https://api.example.com/resource')

const receipt = await session.close()
console.log(receipt?.txHash)
// @log: 0x...
```

## Best practices

* **Track refunds by Credential** — Use the Challenge `id` and Credential `source` to associate refunds with the original payment.
* **Refund to the same address** — Always return funds to the `source` address from the Credential. Don't ask the client for a separate refund address.
* **Treat refunds as a feedback mechanism** — Like traditional payment systems, refunds avoid costly disputes. Handle them promptly through your service's support process.
* **Log refund transactions** — Keep a record of the original payment and the refund transaction for reconciliation.
