> ## Documentation Index
> Fetch the complete documentation index at: https://docs.rinne.com.br/llms.txt
> Use this file to discover all available pages before exploring further.

# 3D Secure Element

> Mount the element that runs 3DS authentication — frictionless and challenge — and handle success, failure, and retry flows.

The 3D Secure Element runs 3DS authentication for a session created on your backend. Mounting it with a `tds_session_id` is what starts authentication: frictionless flows complete invisibly, and when the issuer requires a challenge the element renders the issuer's verification UI (OTP, approve-in-app instructions, biometric) and detects completion.

You create the element once, mount it with a `tds_session_id`, and react to success, failure, or technical errors through callbacks.

> [!WARNING]
>
> The element is not optional challenge UI — it is the component that executes authentication. Until it is mounted, nothing happens: the cardholder's bank is not notified and the session stays `ACTION_REQUIRED` until it expires. Never replace it with your own "confirm with your bank" screen — when an in-app approval is needed, the element shows that instruction and detects the approval.

## Create a 3DS Element

Create the element once with callbacks, then call `mount(sessionId)` whenever you need to authenticate.

```typescript theme={null}
const threeDSecure = await rinne.elements.threeDSecure({
  target: '#three-ds-container',        // requires <div id="three-ds-container"></div> in your page
  size: { width: '100%', height: '450px' },
  colorScheme: 'dark',
  locale: 'pt',
  onReady: () => hideLoadingIndicator(),
  onSuccess: () => finalizePayment(),   // authenticated — create/authenticate the transaction on your backend
  onFailure: (error) => offerRetry(error), // authentication failed — a new session is required to retry
  onError: (error) => showFallback(error)  // technical error — log it and offer another payment method
})

// tds_session_id from the 3DS session your backend created — mounting runs the authentication
await threeDSecure.mount('tds_visa_e7c2yfa3867c')
```

## Options

```typescript theme={null}
interface ThreeDSecureElementOptions {
  target?: string | HTMLElement   // default: modal overlay
  size?: { width: string; height: string }
  colorScheme?: 'light' | 'dark'  // iframe root color-scheme; match your page theme
  theme?: ThemeOption
  locale?: 'pt' | 'en' | 'es'     // default: 'pt'
  onReady?: () => void
  onSuccess?: () => void
  onFailure?: (error?: { code?: string; message?: string }) => void
  onError?: (error: { code: string; message: string }) => void
}
```

## Callback Semantics

| Callback | Meaning | Typical action |
| - | - | - |
| `onReady` | Challenge UI is rendered and ready | Hide loading placeholders |
| `onSuccess` | Authentication completed successfully | Call backend to confirm/finalize payment |
| `onFailure` | Authentication was attempted but failed | Show retry CTA and create a new session |
| `onError` | Technical error (component/network/runtime) | Show fallback path and log diagnostics |

### Awaiting the Outcome

Checkout flows usually need to `await` the authentication before continuing. Wrap creation and mount in a promise that the callbacks settle — and forward creation/mount rejections with `.catch(reject)` so the promise can never hang:

```typescript theme={null}
function runThreeDSAuthentication(tdsSessionId: string): Promise<void> {
  return new Promise((resolve, reject) => {
    rinne.elements
      .threeDSecure({
        target: '#three-ds-container',
        onSuccess: () => resolve(),   // authenticated — the awaiting checkout flow continues below
        onFailure: (error) => reject(new Error(error?.message ?? 'Authentication failed')),
        onError: (error) => reject(new Error(error.message))
      })
      .then((threeDS) => threeDS.mount(tdsSessionId))
      .catch(reject)                  // element creation or mount failed — authentication never started
  })
}

// In your checkout flow:
if (session.auth_status === 'ACTION_REQUIRED') {
  await runThreeDSAuthentication(session.tds_session_id)
}

// Authenticated — create or authenticate the transaction on your backend, linked by session `id`
const payment = await processTransaction({ three_d_secure_session_id: session.id })
```

See [Guide: Card + 3DS Checkout](https://docs.rinne.com.br/rinne-js/guides/card-3ds-checkout.md) for this pattern inside a complete session-first and transaction-first checkout.

## colorScheme

Use `colorScheme` to align the 3DS iframe root with your page theme.

```typescript theme={null}
const colorScheme =
  document.documentElement.getAttribute('data-theme') === 'dark' ? 'dark' : 'light'

const threeDSecure = await rinne.elements.threeDSecure({
  target: '#three-ds-container',
  colorScheme,
  onSuccess: () => confirmPayment()
})
```

## Theme

`theme` supports preset strings (`clean`, `minimal`, `material`) and function themes.

```typescript theme={null}
const threeDSecure = await rinne.elements.threeDSecure({
  theme: 'material'
})
```

> [!NOTE]
>
> If you pass `theme` as an object, the 3DS component currently uses the `preset` value.

### Full Theme Example

```typescript theme={null}
const isDark = document.documentElement.classList.contains('dark')

const threeDSTheme = (utils) => ({
  styles: {
    ':root': {
      colorScheme: isDark ? 'dark' : 'light',
      backgroundColor: isDark ? '#020617' : '#ffffff'
    },
    body: {
      margin: 0,
      fontFamily: 'Inter, ui-sans-serif, system-ui, sans-serif'
    },
    ...utils.media('(max-width: 640px)', {
      body: {
        fontSize: 14
      }
    })
  }
})

const threeDSecure = await rinne.elements.threeDSecure({
  target: '#three-ds-container',
  size: { width: '100%', height: '450px' },
  colorScheme: isDark ? 'dark' : 'light',
  theme: threeDSTheme
})
```

> [!NOTE]
>
> 3DS challenge copy and issuer-controlled challenge UI cannot be fully restyled. Use `theme`, `size`, and `colorScheme` to style the container experience around it.

## Mount Behavior

* `mount(sessionId)` requires a non-empty session ID.
* If you mount again with a new session ID, the SDK cleans up the previous instance automatically.
* Callbacks from options are automatically wired for each mount.

> [!TIP]
>
> 3DS sessions expire 1 hour after creation. When a session expires, request a new session ID from your backend and call `mount(newSessionId)`. The SDK automatically attempts to unmount the previous instance, but you should unmount manually for safety.

## Backend Contract

Use the frontend 3DS element as an authentication step between your backend session creation and transaction confirmation.

**1. Create a 3DS session on your backend**

Return both session IDs from your backend:

* `tds_session_id` for `threeDSecure.mount(...)` in the frontend
* `id` for `three_d_secure_session_id` in backend transaction create/authenticate calls, and to read the session

Use one of the supported create-session endpoints based on your context:

* `POST /v1/3ds-sessions` (self)
* `POST /v1/merchants/{merchantId}/3ds-sessions` (organization on behalf of merchant)

**2. Mount the 3DS element**

In practice, sessions come back as `ACTION_REQUIRED` or `FAILED` at creation — even frictionless authentication completes through the SDK element. The API contract allows `AUTHENTICATED` as a creation-time response for forward compatibility, so always branch on all three statuses.

* **`FAILED`**: the card was rejected before the flow could start — surface the error and let the user retry.
* **`AUTHENTICATED`**: authentication already completed — skip mounting and proceed to payment confirmation.
* **`ACTION_REQUIRED`**: mount with `tds_session_id`. The SDK runs the full 3DS flow internally.

When mounted, the issuer decides the path:

* **Frictionless**: the issuer authenticates the transaction silently in the background. No challenge UI is shown. `onSuccess` fires automatically.
* **Challenge**: the issuer presents a verification step inside the element. `onSuccess` fires after the user completes it.

Your code handles both identically.

```typescript theme={null}
if (session.auth_status === 'FAILED') {
  showError(session.failure_reason ?? 'Authentication failed')
  return
}

if (session.auth_status === 'AUTHENTICATED') {
  confirmPayment()
  return
}

await threeDSecure.mount(session.tds_session_id)
// onSuccess/onFailure/onError callbacks drive the next step
```

**3. Handle success/failure callbacks**

On `onSuccess`, create/authenticate the transaction on your backend using the internal session `id`. On `onFailure`, create a new session before retrying.

To confirm the outcome on your backend, subscribe to the `three-d-secure-session.status-changed` webhook or read the session by `id`. See [Read a session from your backend](https://docs.rinne.com.br/guides/three-d-secure-authentication.md#read-a-session-from-your-backend).

## Modal vs Embedded

Without `target`, the element renders as a modal overlay on top of your page. With `target`, it renders inline inside your container — use this to keep the challenge within your checkout layout.

> [tab] Modal (Default)
>
> ```typescript theme={null}
> // No target — the element renders its own overlay; no container markup needed
> const threeDSecure = await rinne.elements.threeDSecure({
>   onSuccess: () => confirmPayment()
> })
>
> await threeDSecure.mount(sessionId)
> ```

> [tab] Embedded
>
> ```html theme={null}
> <div id="three-ds-container"></div>
> ```
>
> ```typescript theme={null}
> const threeDSecure = await rinne.elements.threeDSecure({
>   target: '#three-ds-container',
>   size: { width: '100%', height: '450px' },
>   onSuccess: () => confirmPayment()
> })
>
> await threeDSecure.mount(sessionId)
> ```

## Mount Errors

`mount()` rejects when the element cannot start — for example, an empty or invalid session ID. Authentication outcomes never reject the mount promise: they arrive through `onSuccess`/`onFailure`/`onError` after mounting succeeds.

```typescript theme={null}
try {
  await threeDSecure.mount(session.tds_session_id)
} catch (error) {
  // The element could not start — authentication never ran
  showError('Could not start authentication. Please try again.')
}
```

## Unmount

```typescript theme={null}
const mounted = await threeDSecure.mount(sessionId)

// Later — for example, the customer abandons checkout mid-challenge
mounted.unmount()
```

## Retrying After Failure

3DS sessions are single-use: after `onFailure`, an expired session, or a failed `/authenticate` call, request a **new** session from your backend and mount again — remounting cleans up the previous instance and rewires the callbacks.

```typescript theme={null}
async function retryAuthentication() {
  const session = await create3DSSession() // your backend — never reuse a failed or consumed session
  if (session.auth_status !== 'ACTION_REQUIRED') return handleStatus(session)

  await threeDSecure.mount(session.tds_session_id)
}
```

> [!NOTE]
>
> `locale` applies to SDK/component messages. Issuer-hosted challenge copy is controlled by the card issuer.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.