> 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.

# Payment Links

A **payment link** is a shareable URL that opens a hosted pay page where a
customer can complete a one-off payment without any integration on the
merchant's side. MVS Pay does not build its own hosted checkout for this — the
URL is minted by **Hyperswitch's native payment-link feature**, and MVS keeps a
thin sidecar row (`PaymentLinkExtension`) for the dashboard-only fields that
Hyperswitch doesn't model (nickname, amount type, lifecycle state, expiry,
tipping, tags).

This split is a direct application of
[ADR-007 · Native-Hyperswitch bias](/mvs-pay/guides/concepts/adrs/adr-007-native-hyperswitch-bias):
let Hyperswitch own anything it already does well (here, the hosted pay page and
the payment intent), and have MVS own only the gaps.

> **Note**
>
> This page documents how the feature is wired and where the truth lives in code.
> For the exact request/response contract of each endpoint, see the
> **[API Reference](/mvs-pay/api)** tab — it is generated from the OpenAPI
> fragments and is authoritative for payloads.

## How it fits together

There are three layers in front of every payment-link operation, and they all
appear in the source listed at the bottom of this section:

| Layer                | Path                                                   | Role                                                                                                                                                                                                |
| -------------------- | ------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Dashboard API routes | `apps/mvs-pay/app/api/payment-links/`                  | Next.js route handlers. Authenticate, scope-check, then delegate to the SDK. Reshape the `{ data }` envelope into `{ paymentLink }` / `{ paymentLinks }` for the UI. Owns the **email send** route. |
| Typed SDK namespace  | `packages/mvs-pay-sdk/src/namespaces/payment-links.ts` | `client.paymentLinks.{create,list,get,update,cancel}`. Maps each method to an HTTP call against the extensions service.                                                                             |
| Extensions service   | `apps/mvs-pay-extensions/src/routes/payment-links.ts`  | The only layer that talks to Hyperswitch and to the `PaymentLinkExtension` table. Mints the link, persists the sidecar, enforces org tenancy.                                                       |

The extensions service is mounted at `/v1/payment-links` (see
`apps/mvs-pay-extensions/src/app.ts`, where `api.route("/payment-links",
paymentLinkRoutes)` is registered and an `idempotency()` middleware is attached
to the same prefix). It is **not** a Next.js API route — orchestration lives in
the standalone EKS service per
[ADR-006](/mvs-pay/guides/concepts/adrs/adr-006-extensions-separate-app). See the
[Extensions Service](/mvs-pay/guides/surfaces/extensions-service) and [SDK](/mvs-pay/guides/surfaces/sdk)
surface pages for the broader picture.

```mermaid
flowchart LR
  UI["Dashboard / API consumer"] -->|POST /api/payment-links| Next["apps/mvs-pay<br />Next route handler"]
  Next -->|client.paymentLinks.create| SDK["@mvs/mvs-pay-sdk<br />PaymentLinksNamespace"]
  SDK -->|POST /v1/payment-links| Ext["apps/mvs-pay-extensions<br />payment-links.ts"]
  Ext -->|payments.create with payment_link: true| HS["Hyperswitch router"]
  HS -->|payment_link.link URL| Ext
  Ext -->|persist sidecar row| DB[("PaymentLinkExtension<br />(MVS Pay DB)")]
  Ext -->|merged PaymentLinkView| SDK
```

## The `PaymentLinkExtension` model

The sidecar is a Prisma model in
`packages/mvs-pay/prisma/schema.prisma` (mapped to table
`payment_link_extension`). Per the schema's own comment, the extension sidecars
are **keyed by an opaque Hyperswitch ID string — there is no foreign key into
Hyperswitch's database**, which is separate. The "join" back to Hyperswitch
happens at the application layer through the SDK.

| Field                     | Type                          | Notes                                                                                                                        |
| ------------------------- | ----------------------------- | ---------------------------------------------------------------------------------------------------------------------------- |
| `id`                      | `String` (cuid)               | MVS-owned primary key. This is the `:id` used in every route.                                                                |
| `organizationId`          | `String`                      | Tenant key. FK to `PaymentOrganization`, `onDelete: Cascade`. The only tenant boundary on the shared DB client.              |
| `hyperswitchPaymentId`    | `String` `@unique`            | The Hyperswitch payment intent that backs the link.                                                                          |
| `hyperswitchMerchantId`   | `String?`                     | Hyperswitch merchant the link belongs to. Used as the list filter.                                                           |
| `linkUrl`                 | `String?`                     | The hosted pay-page URL Hyperswitch returned (`payment_link.link`). Null if Hyperswitch did not return one.                  |
| `nickname`                | `String?`                     | MVS-only display label.                                                                                                      |
| `description`             | `String?`                     | Mirrored onto the Hyperswitch intent at create time, then also stored here.                                                  |
| `amountType`              | `String` `@default("FIXED")`  | `FIXED` or `CUSTOMER_CHOOSES`.                                                                                               |
| `amount`                  | `Int?`                        | Cents. **Null when `amountType = CUSTOMER_CHOOSES`** — the create path explicitly drops `amount` unless the type is `FIXED`. |
| `currency`                | `String` `@default("USD")`    | ISO currency code.                                                                                                           |
| `state`                   | `String` `@default("ACTIVE")` | `ACTIVE \| EXPIRED \| PAID \| CANCELED`.                                                                                     |
| `expiresAt`               | `DateTime?`                   | MVS-tracked expiry.                                                                                                          |
| `allowTipping`            | `Boolean` `@default(false)`   | MVS display flag.                                                                                                            |
| `tags`                    | `String[]`                    | MVS-only tags. Settable on create.                                                                                           |
| `metadata`                | `Json?`                       | Free-form. Not surfaced in the view shape today.                                                                             |
| `createdAt` / `updatedAt` | `DateTime`                    | Standard timestamps. Indexed `(organizationId, createdAt desc)` for the list query's order-by.                               |

> **Warning**
>
> `state`, `amountType`, `expiresAt`, and `tags` are **MVS bookkeeping, not
> Hyperswitch enforcement**. Marking a link `EXPIRED` or `CANCELED` in the sidecar
> updates the dashboard's view but does **not** currently call Hyperswitch to
> deactivate the underlying intent or pay page (see
> [Known gaps](#known-gaps-and-honest-caveats)). Treat lifecycle state as advisory
> until that linkage is closed.

### The view shape vs. the row

Routes never return the raw Prisma row. `toView()` in `payment-links.ts` maps a
`PaymentLinkExtension` to a merged `PaymentLinkView` — Hyperswitch holds
amount/currency/description on the intent, MVS holds everything else — and the
SDK commits to that view (`PaymentLinkRow` in
`packages/mvs-pay-sdk/src/types.ts`) rather than the database row. Notable
renames the new owner should know:

* `hyperswitchMerchantId` is surfaced as `merchantId`.
* `expiresAt` and `createdAt` are serialized to ISO strings (`null` when unset).
* `metadata`, `organizationId`, and `hyperswitchPaymentId` are **not** in the
  view — they never leave the extensions service.

## Create / list / send flow

### Creating a link

`POST /api/payment-links` (dashboard) → `client.paymentLinks.create` →
`POST /v1/payment-links` (extensions). The extensions handler does two writes in
sequence:

#### Mint the Hyperswitch payment + hosted link

It calls `hyperswitch.payments.create` with `payment_link: true`,
`capture_method: "automatic"`, the amount/currency/description, the optional
`payment_link_config`, and metadata stamping the org id (`mvs_org_id`) plus
`mvs_payment_link: "true"`. Hyperswitch returns a `payment_id`, optional
`merchant_id`, and `payment_link.link` (the URL).

`amount` defaults to `0` and `currency` defaults to `"USD"` if the caller omits
them.

#### Persist the MVS sidecar

It writes a `PaymentLinkExtension` row keyed by the returned `payment_id`, copies
`payment_link.link` into `linkUrl`, sets `state: "ACTIVE"`, and stores the
MVS-only fields. `amount` is persisted **only when `amountType === "FIXED"`**;
otherwise it is `null`. `hyperswitchMerchantId` falls back to the body's
`merchantId`, then the Hyperswitch-returned `merchant_id`, then `null`.

#### Return the merged view

The new row is mapped through `toView()` and returned. The dashboard route wraps
it as `{ paymentLink }`.

The create body (`CreatePaymentLinkInput`):

```ts
interface CreatePaymentLinkInput {
  merchantId?: string;
  amountType?: string;        // defaults to "FIXED"
  amount?: number;            // cents; defaults to 0 on the intent
  currency?: string;          // defaults to "USD"
  nickname?: string;
  description?: string;
  expiresAt?: string;
  allowTipping?: boolean;
  tags?: string[];
  customerEmail?: string;          // forwarded to the Hyperswitch intent
  payment_link_config?: Record<string, unknown>; // passthrough to Hyperswitch
}
```

> **Tip**
>
> Pass an `idempotency-key` header on create. It is forwarded all the way to
> `hyperswitch.payments.create`, and the `/payment-links` prefix is wrapped by the
> extensions service's `idempotency()` middleware. The same header flows through
> the dashboard route and the SDK's `RequestMetadata.idempotencyKey`.

### Listing and reading

| Operation | Dashboard route               | SDK                   | Extensions endpoint                 |
| --------- | ----------------------------- | --------------------- | ----------------------------------- |
| List      | `GET /api/payment-links`      | `paymentLinks.list`   | `GET /v1/payment-links`             |
| Get one   | `GET /api/payment-links/[id]` | `paymentLinks.get`    | `GET /v1/payment-links/:id`         |
| Update    | `PUT /api/payment-links/[id]` | `paymentLinks.update` | `PATCH /v1/payment-links/:id`       |
| Cancel    | (via update with `state`)     | `paymentLinks.cancel` | `POST /v1/payment-links/:id/cancel` |

A few specifics worth knowing:

* **List filter.** The dashboard forwards an optional `merchant_id` query param
  straight through; the extensions handler adds `hyperswitchMerchantId` to the
  where-clause when present. Results are ordered `createdAt desc` and returned
  as `{ data: [...] }`, which the dashboard reshapes to `{ paymentLinks }`.
* **Org scoping is the security boundary.** Every read/write is filtered by
  `organizationId` from the request context. A link another org owns surfaces as
  a plain `404`, not a `403`. There is no cross-org read path.
* **Update is a whitelist.** `PATCH` strips `organizationId`,
  `hyperswitchPaymentId`, and `id` so a caller cannot mass-assign tenancy or
  identity columns, then applies only `nickname`, `description`, `expiresAt`,
  `allowTipping`, and `state`. The dashboard uses the `PUT` verb; it maps to the
  SDK's `update` method (and the extensions service's `PATCH`).
* **Cancel.** Both the dedicated SDK `cancel()` (`POST .../:id/cancel`) and an
  update with `state: "CANCELED"` set the sidecar state to `CANCELED`. As noted
  above, this is a DB-only state change today.

### Sending a link by email

This is the one piece of payment-link behaviour that lives **entirely in the
Next.js app**, not the extensions service or the SDK namespace. The route is
`POST /api/payment-links/[id]/send`
(`apps/mvs-pay/app/api/payment-links/[id]/send/route.ts`).

```mermaid
sequenceDiagram
  participant UI as Dashboard
  participant API as POST /api/payment-links/[id]/send
  participant SDK as paymentLinks.get
  participant Mail as @mvs/mail sendEmail
  participant Resend as Resend API

  UI->>API: { email, message? }
  API->>API: authenticateRequest (payment_links:write)
  API->>API: guard: RESEND_API_KEY set?
  alt key unset
    API-->>UI: 503 "Email is not configured…"
  else key present
    API->>SDK: get(id) (org-scoped)
    API->>API: format amount, format expiresAt
    API->>Mail: sendEmail(templateId: "paymentLinkRequest", context)
    Mail->>Resend: POST https://api.resend.com/emails
    Mail-->>API: true / false
    API-->>UI: 200 { sent: true, email } | 502
  end
```

What the route does, in order:

1. **Auth + scope.** Requires `payment_links:write`.
2. **Config guard.** If `process.env.RESEND_API_KEY` is unset it returns a
   **503** with an honest message —
   `"Email is not configured (RESEND_API_KEY is unset); use Copy Link / Open
   Link to share this payment link"`. It does **not** fake a delivery.
3. **Validate input.** `email` is required (`400` if missing/blank). `message`
   is optional and becomes the template's `personalMessage`.
4. **Fetch the link** via `client.paymentLinks.get(id)` (org-scoped — a
   not-owned link 404s here).
5. **Require a URL.** If `link.linkUrl` is null it returns **422** ("This
   payment link has no shareable URL").
6. **Format the amount.** `link.amount` is in cents; it is divided by 100 and
   formatted with `Intl.NumberFormat` in the link's currency. When `amount` is
   null (e.g. `CUSTOMER_CHOOSES`) it renders the literal string
   `"Amount at checkout"`.
7. **Send** via `@mvs/mail`'s `sendEmail` with `templateId:
   "paymentLinkRequest"`. The `merchantName` falls back through
   `link.nickname → auth.context.orgSlug → "MVS Pay"`. `expiresAt` is formatted
   as a long US date. `tenantBrand` is `defaultTenantBrand`.
8. **Map the result.** `sendEmail` returns a boolean; `false` → **502**, `true`
   → **200** with `{ sent: true, email }`.

> **Warning**
>
> `tenantBrand` is hard-coded to `defaultTenantBrand` — **the send route does not
> yet resolve per-organization branding** (logo, primary color, support email),
> even though the template (`PaymentLinkRequest.tsx`) and the underlying layout
> fully support it. Per-tenant brand wiring for this email is an open gap.

#### The `@mvs/mail` delivery path

`sendEmail` (`packages/mail/src/util/send.ts`) is shared platform mail
infrastructure, not payment-link-specific. For a payment-link send the relevant
behaviours are:

* **Idempotency.** It builds a SHA-256 idempotency key from
  `to | templateId | subject | dayBucket` and short-circuits (`return true`,
  logged as a skip) if the same email was already sent **that calendar day**.
  This means a second send of the same link to the same recipient on the same
  day is silently de-duplicated and still reports success.
* **Retries.** Up to two retries with exponential backoff, but **only for
  network-shaped errors** (`ECONNREFUSED`, `ENOTFOUND`, `EAI_AGAIN`, DNS,
  network). Other failures fail fast.
* **Clinical DLQ does not apply here.** `paymentLinkRequest` is not in the
  clinical template set, so failed sends are logged and return `false` (no
  dead-letter enqueue).
* **Provider.** `packages/mail/src/provider/index.ts` re-exports only
  `./resend`, so the active provider is **Resend**. `send()` resolves config
  with precedence **cached DB settings → env (`RESEND_API_KEY`,
  `config.mails.from`, `RESEND_REPLY_TO_EMAIL`) → defaults**, then `POST`s to
  `https://api.resend.com/emails`. Mailgun/Postmark/Paubox/Plunk/Nodemailer/
  console providers exist in the directory but are not wired through the index.

> **Note**
>
> There are **two** Resend config layers and they are not the same gate. The
> send route guards on `process.env.RESEND_API_KEY` directly (its 503 path),
> while the mail package's Resend provider resolves the key through
> `getResendConfig()`. In practice both read the same env var, but the route's
> guard runs first and is what produces the user-facing "not configured" 503.

### The email template

`packages/mail/src/templates/billing/PaymentLinkRequest.tsx` is a
`react-email` template rendered through `PatientEmailLayout`. The route passes
this context (`PaymentLinkRequestProps`):

| Prop              | Source in the send route                                                   |   |         |   |             |
| ----------------- | -------------------------------------------------------------------------- | - | ------- | - | ----------- |
| `merchantName`    | \`link.nickname                                                            |   | orgSlug |   | "MVS Pay"\` |
| `amount`          | formatted from `link.amount` (cents → currency), or `"Amount at checkout"` |   |         |   |             |
| `description`     | `link.description ?? undefined`                                            |   |         |   |             |
| `paymentUrl`      | `link.linkUrl` (button target — "Pay Now")                                 |   |         |   |             |
| `expiresAt`       | `link.expiresAt` formatted as a long US date, else omitted                 |   |         |   |             |
| `personalMessage` | request body `message` (rendered as an info Alert)                         |   |         |   |             |
| `tenantBrand`     | `defaultTenantBrand` (see warning above)                                   |   |         |   |             |

The rendered email shows a centered "Payment Request from \{merchantName}", a
prominent amount card, the optional personal message, a "Pay Now" button, and a
footer noting the link is secure and (if set) when it expires.

## Endpoint reference (summary)

| Method | Path (dashboard)               | Scope                 | SDK call                  | Extensions endpoint                 |
| ------ | ------------------------------ | --------------------- | ------------------------- | ----------------------------------- |
| `POST` | `/api/payment-links`           | `payment_links:write` | `paymentLinks.create`     | `POST /v1/payment-links`            |
| `GET`  | `/api/payment-links`           | `payment_links:read`  | `paymentLinks.list`       | `GET /v1/payment-links`             |
| `GET`  | `/api/payment-links/[id]`      | `payment_links:read`  | `paymentLinks.get`        | `GET /v1/payment-links/:id`         |
| `PUT`  | `/api/payment-links/[id]`      | `payment_links:write` | `paymentLinks.update`     | `PATCH /v1/payment-links/:id`       |
| —      | (cancel)                       | `payment_links:write` | `paymentLinks.cancel`     | `POST /v1/payment-links/:id/cancel` |
| `POST` | `/api/payment-links/[id]/send` | `payment_links:write` | (none — uses `@mvs/mail`) | n/a                                 |

Scopes are defined in `apps/mvs-pay/lib/api-auth.ts`
(`payment_links:read` = "View payment links",
`payment_links:write` = "Create payment links"). See
[Authentication & Tenancy](/mvs-pay/guides/concepts/authentication) for how scopes are
resolved from API keys vs. session.

> **Note**
>
> There is **no dedicated dashboard route for cancel today** — the
> `POST /v1/payment-links/:id/cancel` extensions endpoint and the SDK's `cancel()`
> exist, but the Next.js app reaches cancel through the generic `PUT` update with
> `state: "CANCELED"`. Both paths converge on the same DB state change.

## Known gaps and honest caveats

#### Lifecycle state is MVS-only and not pushed to Hyperswitch

Setting `state` to `EXPIRED`/`CANCELED` (via update or cancel) only mutates the
`PaymentLinkExtension` row. The extensions service does **not** call Hyperswitch
to deactivate the underlying payment intent or hosted pay page. A "canceled"
link's URL may still be payable on the Hyperswitch side. Closing this requires a
Hyperswitch call in the cancel/expire path. There is also no scheduled job that
flips `ACTIVE → EXPIRED` when `expiresAt` passes, nor a webhook that flips
`ACTIVE → PAID` on payment success — `state` is set to `ACTIVE` at creation and
only changes through explicit dashboard edits.

#### Email branding is not per-tenant yet

The send route hard-codes `tenantBrand: defaultTenantBrand`. The template and
layout support full tenant branding (`organizationName`, `primaryColor`,
`supportEmail`), but the route does not resolve the caller's organization brand.
Until wired, every payment-link email uses MVS default branding regardless of
org.

#### merchantName fallback can be unintuitive

`merchantName` falls back to `link.nickname`. The nickname is an internal
display label, not necessarily a customer-facing merchant name, so the email
header may show an internal label. If nickname is unset it falls back to the org
slug, then `"MVS Pay"`.

#### Same-day email de-duplication is silent

`@mvs/mail`'s idempotency key buckets by calendar day. Re-sending the same link
to the same recipient on the same day returns `{ sent: true }` without actually
dispatching a second email. This is correct-by-design for most flows but can
surprise an operator expecting a re-send.

#### Mail provider is Resend-only as wired

Although the `provider/` directory contains Mailgun, Postmark, Paubox, Plunk,
Nodemailer, and console implementations, `provider/index.ts` re-exports only
`./resend`. Switching providers is a code change to that index, not a config
toggle.

## Related pages

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

Why the link is minted by Hyperswitch and MVS only owns the sidecar.

#### [Extensions Service](/mvs-pay/guides/surfaces/extensions-service)

The standalone EKS service that holds the routes and the DB writes.

#### [The SDK](/mvs-pay/guides/surfaces/sdk)

The typed `paymentLinks` namespace consumers call.

#### [Payments & Refunds](/mvs-pay/guides/features/payments-and-refunds)

The underlying Hyperswitch payment intent a link is built on.

#### [Data Model](/mvs-pay/guides/concepts/data-model)

Where the extension-sidecar pattern is documented in full.

#### [API Reference](/mvs-pay/api)

Authoritative payload contracts for every endpoint above.