<!--
   Copyright (c) 2026 Vipps MobilePay AS (or its affiliates). All rights reserved.

   Vipps UCP Payment Handler specification. Use of this specification
   is subject to the Specification Use License in the repository root (LICENSE).
   You may read, reference, and use it to implement integrations with Vipps
   and the Universal Commerce Protocol. Redistribution or modification of the
   specification is not permitted. See LICENSE for full terms.
-->

# Vipps Payment Handler

- **Handler Name:** `no.vipps.ucp.payment_handler`
- **Version:** `2026-04-08`

## Introduction

This payment handler supports payment creation through the
[Vipps ePayment API](https://developer.vippsmobilepay.com/api/epayment/#tag/CreatePayments/operation/createPayment).
Two payment methods are supported:

- **WALLET** - For customers with a Vipps wallet. Uses push notification flow
  (`userFlow: PUSH_MESSAGE`).
- **CARD** - For customers without a wallet. Uses redirect flow (`userFlow: WEB_REDIRECT`) where
  users enter card details on the Vipps payment page.

### Key Benefits

- Efficient integration with Vipps payment services.
- Seamless checkout experience for customers with or without a Vipps wallet.
- Secure handling of payment credentials and transactions.

### Integration Guide

| Participant  | Integration Section                           |
| :----------- | :-------------------------------------------- |
| **Business** | [Business Integration](#business-integration) |
| **Platform** | [Platform Integration](#platform-integration) |

---

## Participants

This payment handler covers two participants: the **business** for which a _customer_ is shopping
from and the **platform** that facilitates the checkout process (such as an agent).

- The **business** is responsible for processing payments using this handler,
- The **platform** offers the supported _payment instrument_ to the customer in the checkout flow.
- The **business** registers this handler and configures/implements it, and the **platform**
  discovers and utilizes the handler during checkout.
- The **business** needs to have an active merchant agreement and an ecommerce sales unit registered
  with Vipps
- The **platform** needs to validate that a user has a valid Vipps identity before offering this
  payment option. Suggestion is to utilize the
  [Vipps Login Product](https://developer.vippsmobilepay.com/docs/APIs/login-api/) to do so

> **Note on Terminology:** While this specification refers to the participant as the **"business,"**
> technical schema fields may retain the standard industry nomenclature **`merchant_*`** (e.g.,
> `merchant_id`). Mappings are documented below.

| Participant  | Role                                                                                             | Prerequisites                                                             |
| :----------- | :----------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------ |
| **Business** | Creates a payment for the defined customer in the Vipps ecosystem                                | Needs to have an active merchant agreement and ecom sales unit with Vipps |
| **Platform** | Collects the customer information, along with validating that the customer has the Vipps wallet. | Needs to validate that the user has the Vipps wallet                      |

### Payment Flow Diagram

The following diagram shows the async payment flow for this handler. The credential can be either
MSISDN (phone number) or TOKEN (customer token):

```text
Platform                    Business                    Vipps                    User
   │                           │                          │                        │
   │ POST /complete            │                          │                        │
   │ (MSISDN or TOKEN)         │                          │                        │
   │──────────────────────────>│                          │                        │
   │                           │ CreatePayment            │                        │
   │                           │ (phoneNumber or          │                        │
   │                           │  customerToken)          │                        │
   │                           │─────────────────────────>│                        │
   │                           │ state: CREATED           │  Push Notification     │
   │                           │<─────────────────────────│───────────────────────>│
   │ status: complete_in_progress                         │                        │
   │<──────────────────────────│                          │                        │
   │                           │                          │                        │
   │ (tells user to check Vipps)                          │       User Approves    │
   │                           │                          │<───────────────────────│
   │                           │ Callback: AUTHORIZED     │                        │
   │                           │<─────────────────────────│                        │
   │                           │ (updates session)        │                        │
   │                           │                          │                        │
   │ GET /checkout-sessions/id │                          │                        │
   │──────────────────────────>│                          │                        │
   │ status: completed + order │                          │                        │
   │<──────────────────────────│                          │                        │
```

---

> **Note:** To create a payment, the business **MUST** create a
> [Vipps Access Token](https://developer.vippsmobilepay.com/docs/APIs/access-token-api/) using their
> credentials and then call the
> [Vipps Epayment API CreatePayments endpoint](https://developer.vippsmobilepay.com/docs/APIs/epayment-api/)
> to create a payment. The suggested payment `userFlow` is `PUSH_MESSAGE`, which triggers a push
> notification to the user's Vipps app to approve the payment.

## Business Integration

### Business Prerequisites

Before advertising this handler, businesses **MUST** complete:

1. Create a merchant agreement and register an Ecommerce sales unit with Vipps
2. Implement server-side logic to generate access tokens and create payments using the Vipps
   [AccessToken](https://developer.vippsmobilepay.com/docs/APIs/access-token-api/) and
   [ePayment](https://developer.vippsmobilepay.com/docs/APIs/epayment-api/) APIs.

**Prerequisites Output:**

| Field                           | Description                                       |
| :------------------------------ | :------------------------------------------------ |
| `identity.clientId`             | The client id to call the Vipps APIs with         |
| `identity.clientSecret`         | The secret to call the Vipps APIs with            |
| `identity.subscriptionKey`      | The subscription key to call the Vipps APIs with. |
| `merchant.merchantSerialNumber` | The merchant identifier in the Vipps systems      |

### Handler Configuration

Businesses advertise support for this handler in their `/.well-known/ucp` profile under the
`payment_handlers` map, keyed by the handler name `no.vipps.ucp.payment_handler`.

#### Handler Schema

**Schema URL:** `https://ucp.vipps.no/ucp/2026-04-08/schemas/schema.json`

The root schema defines the supported payment instrument and three configuration variants for the
different participants and contexts:

| Variant           | Config Type                                                                                            | Description                                               |
| :---------------- | :----------------------------------------------------------------------------------------------------- | :-------------------------------------------------------- |
| `business_schema` | [`types/business_config.json`](https://ucp.vipps.no/ucp/2026-04-08/schemas/types/business_config.json) | Merchant-specific settings declared in `/.well-known/ucp` |
| `platform_schema` | [`types/platform_config.json`](https://ucp.vipps.no/ucp/2026-04-08/schemas/types/platform_config.json) | Platform-level capabilities declared by the platform      |
| `response_schema` | [`types/response_config.json`](https://ucp.vipps.no/ucp/2026-04-08/schemas/types/response_config.json) | Resolved runtime state returned in checkout responses     |

Each variant extends the corresponding base
[`payment_handler.json`](https://ucp.dev/2026-04-08/schemas/payment_handler.json) variant and
overlays the Vipps `config`.

#### Business Config Fields

These fields go in the handler declaration's `config` object.

| Field                    | Type   | Required | Description                                                                                                                                            |
| :----------------------- | :----- | :------- | :----------------------------------------------------------------------------------------------------------------------------------------------------- |
| `merchant_serial_number` | string | **Yes**  | The merchant identifier (MSN) in the Vipps systems. This is obtained when registering an ecommerce sales unit with Vipps. 4 to 20 digits.              |
| `environment`            | enum   | No       | The Vipps environment. One of `TEST` or `PRODUCTION`. Defaults to `PRODUCTION` if not specified. Use `TEST` for sandbox testing with test credentials. |

Instrument narrowing is expressed at the handler-declaration level via the `available_instruments`
field (a sibling of `config`), not inside `config`. When omitted, all instrument types (`wallet` and
`card`) are available.

#### Platform Config Fields

Platforms can declare these fields in the handler `config` of their own profile.

| Field                        | Type  | Required | Description                                                                                                                                                                                                     |
| :--------------------------- | :---- | :------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `supported_credential_types` | array | No       | The wallet credential types the platform can supply: `MSISDN`, `TOKEN`, or both. If not specified, all credential types are assumed supported. TOKEN support is coming soon; until then, only MSISDN is usable. |

#### Example Handler Declaration

```json
{
  "ucp": {
    "version": "2026-04-08",
    "payment_handlers": {
      "no.vipps.ucp.payment_handler": [
        {
          "id": "vipps",
          "version": "2026-04-08",
          "spec": "https://ucp.vipps.no/ucp/2026-04-08/vipps_payment_handler",
          "schema": "https://ucp.vipps.no/ucp/2026-04-08/schemas/schema.json",
          "available_instruments": [
            { "type": "wallet" },
            { "type": "card" }
          ],
          "config": {
            "merchant_serial_number": "123456",
            "environment": "PRODUCTION"
          }
        }
      ]
    }
  }
}
```

### Instrument Schema

**Schema URL:** `https://ucp.vipps.no/ucp/2026-04-08/schemas/payment_instrument.json`

The Vipps payment instrument defines the structure for payments submitted by platforms.

| Field        | Type   | Required              | Description                                                                                                  |
| :----------- | :----- | :-------------------- | :----------------------------------------------------------------------------------------------------------- |
| `type`       | enum   | **Yes**               | The payment instrument type. One of `wallet` or `card`.                                                      |
| `credential` | object | **Yes** (WALLET only) | The credential information required to process WALLET payments. See [Credential Schema](#credential-schema). |
| `return_url` | string | **Yes** (CARD only)   | The URL to redirect the user back to after CARD payment. **MUST** be an `https://` URL.                      |

#### Instrument Types

| Type     | Vipps `paymentMethod.type` | Vipps `userFlow` | Description                                                                            |
| :------- | :------------------------- | :--------------- | :------------------------------------------------------------------------------------- |
| `wallet` | `WALLET`                   | `PUSH_MESSAGE`   | Push notification to user's Vipps app. Requires customer credential (MSISDN or TOKEN). |
| `card`   | `CARD`                     | `WEB_REDIRECT`   | Redirect user to Vipps payment page to enter card details. No wallet required.         |

#### Credential Schema

**Schema URL:** `https://ucp.vipps.no/ucp/2026-04-08/schemas/wallet_payment_credential.json`

The credential object contains the buyer's Vipps wallet identifier. Two credential types are
supported:

| Field   | Type   | Required | Description                                                              |
| :------ | :----- | :------- | :----------------------------------------------------------------------- |
| `type`  | enum   | **Yes**  | The credential type. One of `MSISDN` or `TOKEN`.                         |
| `value` | string | **Yes**  | The credential value. Format depends on the credential type (see below). |

##### MSISDN Credential (Public)

The MSISDN credential uses the customer's phone number. This maps to the `customer.phoneNumber`
field in the
[Vipps ePayment API](https://developer.vippsmobilepay.com/api/epayment/#tag/CreatePayments/operation/createPayment).

| Field   | Description                                                                                                                                                                                        |
| :------ | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `value` | The phone number in MSISDN format: digits only with country code and subscriber number, no leading zeros or plus sign. 9 to 15 digits. Example: `4712345678` for Norwegian number +47 12 34 56 78. |

**Example MSISDN Instrument:**

```json
{
  "type": "wallet",
  "credential": {
    "type": "MSISDN",
    "value": "4712345678"
  }
}
```

##### TOKEN Credential

The TOKEN credential uses an encoded customer token obtained from authenticated user sessions. This
maps to the `customer.customerToken` field in the
[Vipps ePayment API](https://developer.vippsmobilepay.com/api/epayment/#tag/CreatePayments/operation/createPayment).

> **Note:** TOKEN is declared in the schema, but support is coming soon. Use MSISDN until TOKEN is
> supported.

| Field   | Description                                                            |
| :------ | :--------------------------------------------------------------------- |
| `value` | An encoded customer token string obtained from authenticated sessions. |

**Example TOKEN Instrument:**

```json
{
  "type": "wallet",
  "credential": {
    "type": "TOKEN",
    "value": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
  }
}
```

#### CARD Instrument (No Wallet Required)

The CARD instrument allows customers who do not have a Vipps wallet to pay using their card. This
uses the redirect flow where the user is sent to the Vipps payment page to enter their card details.

| Field        | Type   | Required | Description                                                                                                                                                                      |
| :----------- | :----- | :------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `type`       | enum   | **Yes**  | Must be `card`.                                                                                                                                                                  |
| `return_url` | string | **Yes**  | The URL to redirect the user back to after payment. **MUST** be an `https://` URL: a universal link or app link (e.g., `https://example.com/payment-complete`) or a website URL. |

**Example CARD Instrument:**

```json
{
  "type": "card",
  "return_url": "https://chat.vipps.no/payment-complete?session_id=cs-ABC123XYZ"
}
```

---

### Processing Payments

Upon receiving a call for completing a checkout with this handler, businesses **MUST**:

1. **Validate Handler:** Confirm `instrument.handler_id` matches an advertised handler.
2. **Ensure Idempotency:** If the request is a retry (matches a previous `checkout_id` or
   idempotency key), return the previous result immediately without re-processing funds.
3. **Determine Payment Method:** Based on the instrument type:
   - `wallet` → Use `paymentMethod: { type: "WALLET" }` and `userFlow: "PUSH_MESSAGE"`
   - `card` → Use `paymentMethod: { type: "CARD" }` and `userFlow: "WEB_REDIRECT"`
4. **Build Vipps API Request:** See [WALLET Payment Flow](#wallet-payment-flow) or
   [CARD Payment Flow](#card-payment-flow-redirect) below.
5. Call the
   [Vipps ePayment API CreatePayment](https://developer.vippsmobilepay.com/api/epayment/#tag/CreatePayments/operation/createPayment)
   endpoint.
6. **Return Response:** Based on instrument type:
   - `wallet` → Return `status: complete_in_progress` with message to check Vipps app
   - `card` → Return `status: complete_in_progress` with `continue_url` for platform to redirect
     user
7. **Handle Vipps Callback and Poll:** When the user approves/rejects, Vipps sends a callback.
   Update the checkout session accordingly. As callbacks are not guaranteed, implement polling as a
   backup to check payment status. It is also possible to base this on webhooks if the platform
   supports that.
8. **Return Completed Order:** When the platform polls and payment is authorized, return
   `status: completed` with the order.

#### Vipps API `paymentMethod` Mapping

The `paymentMethod.type` field in the Vipps ePayment API **MUST** match the instrument type:

| Instrument Type | Vipps `paymentMethod.type` | Vipps `userFlow` | Description                                   |
| :-------------- | :------------------------- | :--------------- | :-------------------------------------------- |
| `wallet`        | `WALLET`                   | `PUSH_MESSAGE`   | Push notification to user's Vipps app         |
| `card`          | `CARD`                     | `WEB_REDIRECT`   | Redirect to Vipps payment page for card entry |

See the
[Vipps ePayment API documentation](https://developer.vippsmobilepay.com/api/epayment/#tag/CreatePayments/operation/createPayment)
for full details on the `paymentMethod` and `userFlow` fields.

---

### WALLET Payment Flow

The Vipps `PUSH_MESSAGE` flow is **asynchronous** - the user must approve the payment in their Vipps
app. This can take seconds to minutes. Businesses **MUST NOT** block the HTTP response waiting for
user approval.

#### Why Async?

| Approach             | Problem                                        |
| -------------------- | ---------------------------------------------- |
| Block HTTP request   | Timeouts after 30s, user often needs more time |
| Short timeout (5s)   | User can't approve that fast                   |
| Keep connection open | Unreliable, poor scalability                   |

#### Recommended Flow

1. **Business creates payment** → Vipps returns `state: CREATED`
2. **Business returns immediately** with `status: complete_in_progress`
3. **Vipps sends push notification** to user's phone
4. **User approves** in Vipps app
5. **Vipps sends callback** to business with `state: AUTHORIZED`
6. **Business updates checkout** to `status: completed`
7. **Platform polls** and receives completed order

#### Complete Checkout Response (Pending Approval)

When the payment is created but awaiting user approval:

```json
{
  "id": "cs-ABC123XYZ",
  "status": "complete_in_progress",
  "messages": [
    {
      "type": "info",
      "code": "payment_pending_user_approval",
      "content": "A payment request has been sent to your Vipps app. Please open Vipps and approve the payment to complete your order."
    }
  ],
  "payment": {
    "state": "pending_approval",
    "expires_at": "2026-01-26T12:35:00Z"
  }
}
```

#### Message Codes

| Code                            | Type    | Instrument | Description                                                         |
| ------------------------------- | ------- | ---------- | ------------------------------------------------------------------- |
| `payment_pending_user_approval` | `info`  | `wallet`   | Payment request sent, awaiting user approval in Vipps app           |
| `payment_redirect_required`     | `info`  | `card`     | User must be redirected to Vipps payment page to enter card details |
| `payment_approved`              | `info`  | Both       | User approved/completed the payment                                 |
| `payment_rejected`              | `error` | Both       | User rejected or cancelled the payment                              |
| `payment_expired`               | `error` | Both       | Payment request expired before user responded                       |

#### Timeout Handling

Businesses **SHOULD** implement timeout handling:

- **Default timeout:** 5 minutes from payment creation
- **On timeout:** Cancel the Vipps payment and set checkout to error state
- **Expose expiry:** Include `payment.expires_at` in responses

---

### Vipps Callback Handling

Businesses **MUST** implement a callback endpoint to receive payment status updates from Vipps.

> **Important:** Vipps does not guarantee callback delivery. Businesses **MUST** also implement
> polling as a backup. See
> [Vipps Polling Guidelines](https://developer.vippsmobilepay.com/docs/knowledge-base/polling-guidelines/).

**Callback URL:** Configured in `VippsMerchantInfo.callbackUrl`

**Callback Payload:**

```json
{
  "reference": "cs-ABC123XYZ",
  "state": "AUTHORIZED",
  "pspReference": "1234567890"
}
```

**Callback States:**

| Vipps State  | Action                                                              |
| ------------ | ------------------------------------------------------------------- |
| `AUTHORIZED` | Update checkout to `completed`, create order                        |
| `ABORTED`    | Set checkout `status` to `incomplete` with error `payment_rejected` |
| `EXPIRED`    | Update checkout with error: payment expired                         |
| `TERMINATED` | Update checkout with error: payment cancelled                       |

---

### Business-Side Polling (Backup)

Since Vipps callbacks are not guaranteed, businesses **MUST** implement polling as a backup
mechanism to check payment status.

**Vipps Polling Guidelines:**

- **Start polling:** 5 seconds after payment creation
- **Poll interval:** Every 2 seconds
- **Endpoint:** `GET /epayment/v1/payments/{reference}`

**Example Polling Logic:**

```javascript
async function pollVippsPaymentStatus(reference: string, maxAttempts = 150) {
  const POLL_INTERVAL_MS = 2000; // 2 seconds
  const INITIAL_DELAY_MS = 5000; // 5 seconds

  // Wait 5 seconds before starting
  await new Promise(resolve => setTimeout(resolve, INITIAL_DELAY_MS));

  for (let attempt = 0; attempt < maxAttempts; attempt++) {
    const payment = await getVippsPayment(reference);

    if (payment.state === "AUTHORIZED") {
      // Payment approved - complete the order
      return { success: true, state: "AUTHORIZED" };
    }

    if (["ABORTED", "EXPIRED", "TERMINATED"].includes(payment.state)) {
      // Payment failed - update checkout with error
      return { success: false, state: payment.state };
    }

    // Still CREATED - wait and poll again
    await new Promise(resolve => setTimeout(resolve, POLL_INTERVAL_MS));
  }

  // Timeout - treat as expired
  return { success: false, state: "TIMEOUT" };
}
```

> **Note:** The callback and polling mechanisms are complementary. Whichever receives the status
> update first should process it, and the other should recognize that processing has already
> occurred (idempotency).

---

### CARD Payment Flow (Redirect)

The CARD payment flow uses `WEB_REDIRECT` user flow. The user is redirected to the Vipps payment
page to enter their card details. This flow is for customers who do not have a Vipps wallet.

#### CARD Flow Diagram

```text
Platform                    Business                    Vipps                    User
   │                           │                          │                        │
   │ POST /complete            │                          │                        │
   │ (type: card, return_url)  │                          │                        │
   │──────────────────────────>│                          │                        │
   │                           │ CreatePayment            │                        │
   │                           │ (paymentMethod: CARD,    │                        │
   │                           │  userFlow: WEB_REDIRECT, │                        │
   │                           │  returnUrl)              │                        │
   │                           │─────────────────────────>│                        │
   │                           │ state: CREATED           │                        │
   │                           │ + redirectUrl            │                        │
   │                           │<─────────────────────────│                        │
   │ status: complete_in_progress                         │                        │
   │ + continue_url            │                          │                        │
   │<──────────────────────────│                          │                        │
   │                           │                          │                        │
   │ (redirects user)          │                          │                        │
   │───────────────────────────────────────────────────────────────────────────────>│
   │                           │                          │   User enters card     │
   │                           │                          │<───────────────────────│
   │                           │ Callback: AUTHORIZED     │                        │
   │                           │<─────────────────────────│                        │
   │                           │                          │  Redirect to returnUrl │
   │<───────────────────────────────────────────────────────────────────────────────│
   │                           │                          │                        │
   │ GET /checkout-sessions/id │                          │                        │
   │──────────────────────────>│                          │                        │
   │ status: completed + order │                          │                        │
   │<──────────────────────────│                          │                        │
```

#### CARD-Specific Request Fields

When creating a payment with `paymentMethod: { type: "CARD" }`, the business **MUST** include:

| Field                | Description                                                                                   |
| :------------------- | :-------------------------------------------------------------------------------------------- |
| `paymentMethod.type` | **MUST** be `CARD`                                                                            |
| `userFlow`           | **MUST** be `WEB_REDIRECT`                                                                    |
| `returnUrl`          | The URL to redirect the user back to after payment. Use the `return_url` from the instrument. |

#### CARD Business Response

When the payment is created, Vipps returns a `redirectUrl`. The business **MUST** return this to the
platform as `continue_url` per the
[UCP Continue URL specification](https://ucp.dev/latest/specification/checkout/#continue-url).

The `continue_url` enables checkout handoff from platform to business UI, allowing the buyer to
complete the payment on the Vipps payment page. Per UCP, when `status` is `complete_in_progress`,
the business is processing the Complete Checkout request and may provide a `continue_url` for user
interaction.

```json
{
  "id": "cs-ABC123XYZ",
  "status": "complete_in_progress",
  "continue_url": "https://api.vipps.no/checkout/v3/session/...",
  "messages": [
    {
      "type": "info",
      "code": "payment_redirect_required",
      "content": "Please complete your payment on the Vipps payment page."
    }
  ],
  "payment": {
    "state": "pending_redirect",
    "expires_at": "2026-01-26T12:35:00Z"
  }
}
```

#### Platform Redirect Handling

When the platform receives a `complete_in_progress` response with a `continue_url` and message code
`payment_redirect_required`:

1. **Redirect the user:** Open the `continue_url` in a browser or webview to hand off to the Vipps
   payment page
2. **User completes payment:** User enters card details on the Vipps payment page
3. **Return redirect:** After payment, Vipps redirects user to the `returnUrl` provided in the
   instrument
4. **Poll for completion:** After redirect, poll `GET /checkout-sessions/{id}` to get the final
   status (`completed` or error)

**Return URL Formats:**

| Type                                    | Example                                                          | Use Case                                                                 |
| :-------------------------------------- | :--------------------------------------------------------------- | :----------------------------------------------------------------------- |
| Universal link, app link or website URL | `https://chat.vipps.no/payment-complete?session_id=cs-ABC123XYZ` | Native mobile app (opens the app when it is installed) or web-based chat |

> **Note:** The `returnUrl` should include sufficient context (e.g., `session_id`) for the platform
> to resume the checkout flow after the user returns.

---

### Example Business Implementation

```javascript
const instrument: PaymentInstrument; // The payment instrument from the request
const checkout: Checkout; // The checkout with lineItems
const vippsCredentials: VippsCredentials; // Vipps Credentials
const accessToken: string; // The accessToken you received from Vipps
const idempotencyKey: string; // Your idempotency key

const url = "https://apitest.vipps.no/epayment/v1/payments"; // Replace for production

/**
 * Build the Vipps API request body based on instrument type.
 * See: https://developer.vippsmobilepay.com/api/epayment/#tag/CreatePayments/operation/createPayment
 */
function buildVippsPaymentRequest(instrument: PaymentInstrument, checkout: Checkout) {
  const baseRequest = {
    amount: {
      currency: checkout.currency,
      value: checkout.totals.amount
    },
    reference: checkout.id,
    paymentDescription: `Order ${checkout.id}`
  };

  if (instrument.type === "wallet") {
    // WALLET: Use PUSH_MESSAGE flow with customer credential
    return {
      ...baseRequest,
      customer: buildCustomerObject(instrument.credential),
      paymentMethod: {
        type: "WALLET"  // REQUIRED for WALLET instruments
      },
      userFlow: "PUSH_MESSAGE"
    };
  } else if (instrument.type === "card") {
    // CARD: Use WEB_REDIRECT flow, no customer credential needed
    return {
      ...baseRequest,
      paymentMethod: {
        type: "CARD"  // REQUIRED for CARD instruments
      },
      userFlow: "WEB_REDIRECT",
      returnUrl: instrument.return_url  // Platform provides this for redirect back
    };
  }

  throw new Error(`Unsupported instrument type: ${instrument.type}`);
}

/**
 * Build the customer object for WALLET payments based on credential type.
 */
function buildCustomerObject(credential: WalletCredential): { phoneNumber?: string; customerToken?: string } {
  switch (credential.type) {
    case "MSISDN":
      return { phoneNumber: credential.value };
    case "TOKEN":
      return { customerToken: credential.value };
    default:
      throw new Error(`Unsupported credential type: ${credential.type}`);
  }
}

const body = buildVippsPaymentRequest(instrument, checkout);

const response = await fetch(url, {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "Authorization": `Bearer ${accessToken}`,
    "Ocp-Apim-Subscription-Key": vippsCredentials.subscriptionKey,
    "Merchant-Serial-Number": vippsCredentials.merchantSerialNumber,
    "Idempotency-Key": idempotencyKey
  },
  body: JSON.stringify(body)
});

const payment = await response.json();

if (!response.ok) {
  return mapVippsErrorToUCPResponse(response.status, payment);
}

// Build response based on instrument type
checkout.status = "complete_in_progress";
checkout.payment = {
  vipps_reference: payment.reference,
  expires_at: new Date(Date.now() + 5 * 60 * 1000).toISOString()
};

if (instrument.type === "wallet") {
  // WALLET: Return message to check Vipps app
  checkout.payment.state = "pending_approval";
  checkout.messages = [{
    type: "info",
    code: "payment_pending_user_approval",
    content: "A payment request has been sent to your Vipps app. Please open Vipps and approve the payment."
  }];
} else if (instrument.type === "card") {
  // CARD: Return continue_url for platform to redirect user (per UCP spec)
  checkout.payment.state = "pending_redirect";
  checkout.continue_url = payment.redirectUrl;  // From Vipps response, returned as continue_url
  checkout.messages = [{
    type: "info",
    code: "payment_redirect_required",
    content: "Please complete your payment on the Vipps payment page."
  }];
}

return checkout;
```

**TypeScript Types:**

```typescript
type InstrumentType = "wallet" | "card";
type CredentialType = "MSISDN" | "TOKEN";

interface WalletCredential {
  type: CredentialType;
  value: string;
}

interface WalletInstrument {
  type: "wallet";
  credential: WalletCredential;
}

interface CardInstrument {
  type: "card";
  return_url: string; // https:// URL (universal link, app link or website)
}

type PaymentInstrument = WalletInstrument | CardInstrument;

// Example Vipps API payloads:

// WALLET with MSISDN -> paymentMethod.type: "WALLET", customer.phoneNumber
const walletMsisdnPayload = {
  paymentMethod: { type: "WALLET" },
  userFlow: "PUSH_MESSAGE",
  customer: { phoneNumber: "4712345678" },
  // ... other fields
};

// WALLET with TOKEN -> paymentMethod.type: "WALLET", customer.customerToken
const walletTokenPayload = {
  paymentMethod: { type: "WALLET" },
  userFlow: "PUSH_MESSAGE",
  customer: { customerToken: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..." },
  // ... other fields
};

// CARD -> paymentMethod.type: "CARD", no customer field needed
const cardPayload = {
  paymentMethod: { type: "CARD" },
  userFlow: "WEB_REDIRECT",
  returnUrl: "https://chat.vipps.no/payment-complete?session_id=cs-ABC123XYZ",
  // ... other fields
};
```

> NOTE: For the `CardInstrument`, the `returnUrl` must be exactly the same as provided by the
> platform, to ensure the customer is successfully redirected back to the platform after payment
> approval.

### Error Handling

Businesses **MUST** follow
[UCP Checkout error handling](https://ucp.dev/latest/specification/checkout/#error-handling): return
error messages in the checkout `messages` array only when the **platform or buyer can act** on them.
The `severity` field declares who resolves the error:

- **`recoverable`** — The platform can fix via API (e.g. correct payment data and retry). Use for
  invalid instrument data the platform can correct.
- **`requires_buyer_input`** — The business requires input that cannot be provided via API (e.g.
  buyer must install or link a wallet). Contributes to `status: requires_escalation`; platform
  should hand off via `continue_url`.

Do **not** return UCP error messages for **implementation or backend errors on the business side**
(e.g. merchant misconfiguration, auth failure, Vipps internal errors). The platform cannot resolve
these; the checkout should be discarded or set to a terminal state. The business may handle such
errors server-side (retries, alerts, logging) at their discretion.

Map
[Vipps ePayment API errors](https://developer.vippsmobilepay.com/docs/APIs/epayment-api/api-guide/errors/#epayment-api-errors)
(RFC 7807, `extraDetails` error code when present) using the table below. **If the error does not
appear in the mapping**, treat it as a business-side/implementation error: do not surface it as a
UCP message to the platform; handle server-side and treat the checkout as non-recoverable.

> **Note:** This mapping covers only **payment creation** and UCP payment-handling flows.

| HTTP Status | Vipps Error Code | Context (Vipps title / description)     | UCP Error Code           | UCP Severity  | User-Facing Message                                                                         |
| :---------- | :--------------- | :-------------------------------------- | :----------------------- | :------------ | :------------------------------------------------------------------------------------------ |
| 400         | —                | Request body validation (no error code) | `invalid_payment_data`   | `recoverable` | The payment request contains invalid data. Please verify and try again.                     |
| 400         | 4050             | Invalid CustomerToken                   | `invalid_customer_token` | `recoverable` | The provided customer information is invalid. Please validate and try again.                |
| 400         | 4070             | Invalid phone number                    | `invalid_phone`          | `recoverable` | The provided phone number is invalid.                                                       |
| 400         | 7010             | Customer not found                      | `wallet_not_found`       | `recoverable` | No wallet account found for the provided identifier. Please try a different payment method. |

For **wallet_not_found** and **invalid_phone**, the business has no `continue_url` to redirect to
(the buyer cannot “fix” the wallet link in business UI). The platform should treat these as
**recoverable**—e.g. offer another payment method such as CARD, or prompt the user to try a
different identifier.

**Important:** Error messages returned to platforms should be opaque and user-friendly. Do not
expose internal Vipps API error details. Log detailed errors server-side for debugging.

**Example UCP Error Response** (e.g. for 7010 Customer not found):

```json
{
  "messages": [
    {
      "type": "error",
      "code": "wallet_not_found",
      "severity": "recoverable",
      "content": "No wallet account found for this phone number. Please try a different payment method.",
      "path": "$.payment.instruments[0]"
    }
  ]
}
```

---

## Platform Integration

### Platform Prerequisites

Before using this handler, Platforms **MUST** complete:

1. If applicable: Register with merchant to allow to call their `checkout_sessions` endpoint. This
   could involve getting an access token
2. Obtain a valid customer identifier based on the credential type:
   - **MSISDN:** Capture the user's MSISDN registered with Vipps. A suggestion is to implement the
     [Vipps Login Product](https://developer.vippsmobilepay.com/docs/APIs/login-api/) in your
     application.
   - **TOKEN:** Obtain a customer token from an authenticated session.

**Prerequisites Output:**

| Field                     | Description                                                              |
| :------------------------ | :----------------------------------------------------------------------- |
| `merchant.access_token`   | Access token to call merchant (if applicable)                            |
| `vippsUser.msisdn`        | Vipps user MSISDN registered on their wallet (for MSISDN credential)     |
| `vippsUser.customerToken` | Encoded customer token from authenticated session (for TOKEN credential) |

### Payment Protocol

Platforms **MUST** follow this flow to acquire a payment instrument:

#### Step 1: Discover Handler

The Platform identifies the `no.vipps.ucp.payment_handler` key in the business's `payment_handlers`
map.

```json
{
  "no.vipps.ucp.payment_handler": [
    {
      "id": "vipps",
      "version": "2026-04-08",
      "spec": "https://ucp.vipps.no/ucp/2026-04-08/vipps_payment_handler",
      "schema": "https://ucp.vipps.no/ucp/2026-04-08/schemas/schema.json",
      "available_instruments": [
        { "type": "wallet" },
        { "type": "card" }
      ],
      "config": {
        "merchant_serial_number": "123456",
        "environment": "PRODUCTION"
      }
    }
  ]
}
```

#### Step 2: Choose Payment Method and Gather Required Data

The Platform **MUST** determine which payment method to use based on whether the customer has a
Vipps wallet:

##### Option A: WALLET (User has Vipps wallet)

For users with a Vipps wallet, use the WALLET instrument. The Platform **MUST** obtain a customer
identifier:

- **MSISDN:** Prompt the user for their MSISDN registered with Vipps. Use the
  [Vipps Login Product](https://developer.vippsmobilepay.com/docs/APIs/login-api/) to authenticate
  and retrieve the MSISDN.
- **TOKEN:** Use an encoded customer token obtained from an authenticated session.

##### Option B: CARD (User does not have Vipps wallet)

For users without a Vipps wallet, use the CARD instrument. The Platform **MUST** provide:

- **return_url:** The `https://` URL to redirect the user back to after payment, e.g.,
  `https://chat.vipps.no/payment-complete?session_id=...`. For native mobile apps, use a universal
  link or app link, so the URL opens the app when it is installed.

> **Tip:** Include the checkout session ID in the return URL so the platform can resume the checkout
> flow after redirect.

#### Step 3: Complete Checkout

The Platform submits the checkout with the constructed payment instrument.

**Example with MSISDN credential:**

```json
POST /checkout-sessions/{checkout_id}/complete
Content-Type: application/json

{
  "payment_data": {
    "id": "instr_vipps_1",
    "handler_id": "vipps",
    "type": "wallet",
    "credential": {
      "type": "MSISDN",
      "value": "4712345678"
    }
  },
  "risk_signals": {
    "device_id": "device_abc123",
    "session_id": "session_xyz789",
    "ip_address": "192.168.1.1",
    "user_agent": "Mozilla/5.0 ...",
    "accept_language": "nb-NO"
  }
}
```

**Example with TOKEN credential:**

```json
POST /checkout-sessions/{checkout_id}/complete
Content-Type: application/json

{
  "payment_data": {
    "id": "instr_vipps_1",
    "handler_id": "vipps",
    "type": "wallet",
    "credential": {
      "type": "TOKEN",
      "value": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
    }
  }
}
```

**Example with CARD instrument (No wallet required):**

```json
POST /checkout-sessions/{checkout_id}/complete
Content-Type: application/json

{
  "payment_data": {
    "id": "instr_card_1",
    "handler_id": "vipps",
    "type": "card",
    "return_url": "https://chat.vipps.no/payment-complete?session_id=cs-ABC123XYZ"
  }
}
```

#### Step 4: Handle Async Response

Both WALLET and CARD flows return `status: complete_in_progress`. The Platform **MUST** handle the
response based on the message code:

**WALLET Response (pending user approval in Vipps app):**

```json
{
  "id": "cs-ABC123XYZ",
  "status": "complete_in_progress",
  "messages": [
    {
      "type": "info",
      "code": "payment_pending_user_approval",
      "content": "A payment request has been sent to your Vipps app. Please open Vipps and approve the payment to complete your order."
    }
  ],
  "payment": {
    "state": "pending_approval",
    "expires_at": "2026-01-26T12:35:00Z"
  }
}
```

**CARD Response (redirect required):**

```json
{
  "id": "cs-ABC123XYZ",
  "status": "complete_in_progress",
  "continue_url": "https://api.vipps.no/checkout/v3/session/...",
  "messages": [
    {
      "type": "info",
      "code": "payment_redirect_required",
      "content": "Please complete your payment on the Vipps payment page."
    }
  ],
  "payment": {
    "state": "pending_redirect",
    "expires_at": "2026-01-26T12:35:00Z"
  }
}
```

**Platform behavior for WALLET (`payment_pending_user_approval`):**

1. **Inform the user:** Display the message content (e.g., "Please check your Vipps app to approve
   the payment")
2. **Poll for completion:** Call `GET /checkout-sessions/{checkout_id}` every 2-5 seconds
3. **Check status:** Continue polling until `status` is `completed`, or until the checkout leaves
   `complete_in_progress` with an error message (e.g. `incomplete` with `payment_rejected`)
4. **Handle timeout:** If `payment.expires_at` passes, inform user the payment expired

**Platform behavior for CARD (`payment_redirect_required`):**

Per the
[UCP Continue URL specification](https://ucp.dev/latest/specification/checkout/#continue-url), the
`continue_url` enables checkout handoff from platform to business UI:

1. **Redirect the user:** Open the `continue_url` in a browser or webview to hand off to the Vipps
   payment page
2. **User completes payment:** User enters card details on the Vipps payment page
3. **Handle return:** After payment, Vipps redirects user back to the `return_url` provided in the
   instrument
4. **Resume checkout:** Extract session ID from URL and poll `GET /checkout-sessions/{id}` for final
   status (`completed` or error)

#### Step 5: Poll for Completion

```json
GET /checkout-sessions/{checkout_id}
```

**Response when payment is approved:**

```json
{
  "id": "cs-ABC123XYZ",
  "status": "completed",
  "order": {
    "id": "order-789",
    "reference": "ORD-2026-001234",
    "created_at": "2026-01-26T12:32:15Z"
  },
  "messages": [
    {
      "type": "info",
      "code": "payment_approved",
      "content": "Payment approved. Your order has been placed."
    }
  ]
}
```

**Response when user rejects:**

```json
{
  "id": "cs-ABC123XYZ",
  "status": "incomplete",
  "messages": [
    {
      "type": "error",
      "code": "payment_rejected",
      "severity": "recoverable",
      "content": "Payment was declined. Please try again or choose a different payment method."
    }
  ]
}
```

#### Polling Best Practices

| Recommendation          | Details                                         |
| ----------------------- | ----------------------------------------------- |
| **Poll interval**       | 2-5 seconds initially, increase if many retries |
| **Max duration**        | Stop after `payment.expires_at` + small buffer  |
| **Exponential backoff** | Consider increasing interval after 30 seconds   |
| **User feedback**       | Show progress indicator while polling           |

---

## Security Considerations

| Requirement           | Description                                                                                                                                                                      |
| :-------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **TLS Required**      | All communication with the Vipps API **MUST** use TLS 1.2 or higher. Connections using older TLS versions will be rejected.                                                      |
| **MSISDN Validation** | For MSISDN credentials, the value **MUST** be validated to ensure it is a valid phone number format (digits only, 9-15 digits, no leading zero) before sending to the Vipps API. |
| **TOKEN Validation**  | For TOKEN credentials, the value **MUST** be validated to ensure it is a non-empty string. Token validity is verified by the Vipps API.                                          |
| **Data Residency**    | All payment data is processed within the EEA (European Economic Area). PII is handled according to GDPR requirements.                                                            |

---

## References

- **Handler Spec:** `https://ucp.vipps.no/ucp/2026-04-08/vipps_payment_handler`
- **Handler Schema:** `https://ucp.vipps.no/ucp/2026-04-08/schemas/schema.json`
- **Business Config Schema:**
  `https://ucp.vipps.no/ucp/2026-04-08/schemas/types/business_config.json`
- **Platform Config Schema:**
  `https://ucp.vipps.no/ucp/2026-04-08/schemas/types/platform_config.json`
- **Response Config Schema:**
  `https://ucp.vipps.no/ucp/2026-04-08/schemas/types/response_config.json`
- **Instrument Schema:** `https://ucp.vipps.no/ucp/2026-04-08/schemas/payment_instrument.json`
- **Credential Schema:**
  `https://ucp.vipps.no/ucp/2026-04-08/schemas/wallet_payment_credential.json`
