---
title: "Embedding the card payment page"
description: "Embed the CM.com card payment page in an iframe on your own page, and follow the payment through the messages it sends."
canonical: "https://docs.cm.com/payments/online-payments/guides/cards/iframe-integration"
---

# Embedding the card payment page

> Embed the CM.com card payment page in an iframe on your own page, and follow the payment through the messages it sends.

Source: https://docs.cm.com/payments/online-payments/guides/cards/iframe-integration

With the [initialize card transaction (v2)](https://docs.cm.com/payments/online-payments/v1.27.20/api-reference#operation/initializeCreditCardTransactionV2)
endpoint you get a card payment page that is hosted by CM.com. Inside it the consumer enters their card details and
completes 3D Secure authentication if their bank asks for it. Card data never touches your systems.

The page is made to be **embedded in an iframe** on your own checkout page. While the consumer pays, it sends
messages to your page with
[`postMessage`](https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage), so you know when to show
your own success or failure screen.

# How does it work?

1. Call the [initialize card transaction (v2)](https://docs.cm.com/payments/online-payments/v1.27.20/api-reference#operation/initializeCreditCardTransactionV2)
   endpoint. The transaction is created with status `OPEN`.
2. Set `action.iframe.url` from the response as the `src` of an iframe on your page. Use the URL as it is; do not
   build or change it.
3. The consumer enters their card details in the iframe and confirms the payment.
4. If the bank asks for 3D Secure authentication, the consumer completes it inside the same iframe. On our
   acceptance environment you can use [these OTP codes](https://docs.cm.com/payments/online-payments/guides/cards/test-card-numbers#3ds-otp) to test the different
   outcomes.
5. When the payment has a final status, the page sends a `transactionStatus` message to your page.
6. You confirm the final status with a [webhook](https://docs.cm.com/payments/online-payments/guides/webhooks) for the `STATUS_CHANGE` event, or with the
   [get card transaction (v2)](https://docs.cm.com/payments/online-payments/v1.27.20/api-reference#operation/getCreditCardTransactionV2) endpoint. Then
   replace the iframe with your own result screen. The page shows a short result after 3D Secure, but not after
   every outcome, so do not rely on it.

The consumer stays on your page the whole time. They are not redirected to `returnUrl` or `returnUrls`: these
fields are required in the request, but this flow does not use them.

\:::caution The message is not proof of payment

A `postMessage` is a hint for your user interface. It can be lost, for example when the consumer closes the tab.
Always use the webhook, or the get transaction endpoint, to decide whether the order is paid.

\:::

\:::note `action.redirect.url` is deprecated

The response also holds the same URL in `action.redirect.url`. That field is kept for existing integrations and
will be removed in a future version. Use `action.iframe.url`. Opening the page as a top-level page or in a popup is
not supported: your page then receives no messages.

\:::

# Adding the iframe

```html
<iframe
  id="cm-card-payment"
  src="<action.iframe.url>"
  title="Card payment"
  style="width: 100%; height: 500px; border: 0;"
  sandbox="allow-scripts allow-forms allow-same-origin allow-popups"
></iframe>
```

- **`sandbox`**: if you sandbox the iframe, allow at least the four tokens above. The page needs scripts and forms
  for the card form and 3D Secure, and its own origin for its requests. Some banks open help links in a new
  window. Do not add `allow-top-navigation`: the page never navigates your page.
- **Height**: start with a height of about 500 pixels. The page sends `iframeResize` messages with the height it
  needs.
- **Content Security Policy**: if your page has a CSP, allow the CM.com host of your environment in `frame-src`
  (see [Checking where a message comes from](#checking-where-a-message-comes-from)).
- **Keep the iframe on the page** until you have received a final status. Removing the iframe, or the consumer
  leaving your page, while the payment is in progress cancels the payment.

# Messages

Every message is a plain object with these fields:

| Field | Type | Description |
| :- | :- | :- |
| `type` | string | The kind of message. See below. |
| `version` | number | The version of the message format. Currently `1`. |
| `iframeId` | string | Always `creditCardV2Frame`. Use it to tell these messages from others. |

Messages do not contain personal data or card data. Ignore types and fields you don't know: new ones can be added
without a version change. A change that breaks the format comes with a new `version`.

### `transactionStatus`

The payment has a final status. The page sends this message once each time it loads. If the iframe is loaded
again after the payment is final, you receive the message again, so make sure a duplicate does no harm.

```json
{
  "type": "transactionStatus",
  "version": 1,
  "iframeId": "creditCardV2Frame",
  "transaction": {
    "status": "successful"
  }
}
```

`transaction.status` is the status of the card payment page, which differs from the API status. See
[Outcomes](#outcomes) for what each value means.

| `transaction.status` | API status (get transaction, webhook) |
| :- | :- |
| `successful` | `SUCCESS` |
| `failed` | `FAILURE` |
| `cancelled` | `CANCELLED` |
| `expired` | `EXPIRED` |
| `rejected` | `FAILURE` |

### `error`

The page could not determine a final status. The payment may still complete, so get the status from the API. See
[When you receive no final status](#when-you-receive-no-final-status).

```json
{
  "type": "error",
  "version": 1,
  "iframeId": "creditCardV2Frame",
  "code": "status_unavailable"
}
```

| `code` | Meaning |
| :- | :- |
| `status_unavailable` | The page could not get the final status of the payment in time. |
| `initialization_failed` | The card form or the 3D Secure page loaded, but could not start. |

### `iframeResize`

The page needs a different height, for example when the 3D Secure challenge of the bank is shown. Set the height
of the iframe to `height` pixels.

```json
{ "type": "iframeResize", "version": 1, "iframeId": "creditCardV2Frame", "height": 640 }
```

### `resetScroll`

The content of the page changed completely, for example when 3D Secure starts. Scroll your page so the top of the
iframe is visible.

```json
{ "type": "resetScroll", "version": 1, "iframeId": "creditCardV2Frame" }
```

# Listening for messages

## Checking where a message comes from

Any page can send your page a message, so check every message before you act on it:

1. `event.origin` must be the CM.com host of your environment:

   | Environment | Origin |
   | :- | :- |
   | Acceptance | `https://cardprocessing.acc.pay.cm.com` |
   | Production | `https://cardprocessing.pay.cm.com` |

2. `event.source` must be the window of your iframe.

3. `event.data.iframeId` must be `creditCardV2Frame`.

## Example

```js
const CM_ORIGIN = 'https://cardprocessing.pay.cm.com'; // https://cardprocessing.acc.pay.cm.com on acceptance
const iframe = document.getElementById('cm-card-payment');

window.addEventListener('message', (event) => {
  if (event.origin !== CM_ORIGIN || event.source !== iframe.contentWindow) {
    return;
  }
  const message = event.data;
  if (!message || message.iframeId !== 'creditCardV2Frame') {
    return;
  }

  switch (message.type) {
    case 'transactionStatus':
      // A hint: confirm the status on your server (webhook or get transaction) before you show the result.
      showResult(message.transaction.status);
      break;
    case 'error':
      // No final status yet: ask your server to poll the get transaction endpoint.
      waitForFinalStatus();
      break;
    case 'iframeResize':
      iframe.style.height = `${message.height}px`;
      break;
    case 'resetScroll':
      iframe.scrollIntoView({ block: 'start' });
      break;
    // Ignore any other type.
  }
});
```

# Outcomes

The iframe never navigates your page and never closes itself, and it does not show a result for every outcome. So
replace or hide the iframe as soon as you receive a `transactionStatus` or `error` message, and show your own
result screen.

Which message your page receives for each outcome, what the iframe shows, and what we recommend you do.

| Outcome | Message | API status | What the iframe shows | What to do |
| :- | :- | :- | :- | :- |
| Payment authorised, with or without a 3D Secure challenge | `transactionStatus`, `successful` | `SUCCESS` | "Payment successful" after 3D Secure, otherwise the card form | Confirm the status, then show your order confirmation. |
| 3D Secure challenge not passed (for example a wrong code) | `transactionStatus`, usually `failed`, sometimes `cancelled` | `FAILURE` or `CANCELLED` | "Payment not completed" | Let the consumer try again with a new transaction, or offer another payment method. |
| 3D Secure failed without a challenge, the card was declined, or a technical error | `transactionStatus`, `failed` | `FAILURE` | "Payment not completed" after 3D Secure, otherwise the card form | Let the consumer try again with a new transaction, or offer another payment method. |
| Blocked by fraud screening | `transactionStatus`, `rejected` | `FAILURE` | "Payment not completed" after 3D Secure, otherwise the card form | Offer another payment method. Do not retry with the same card. |
| 3D Secure was not finished in time | `transactionStatus`, `expired` | `EXPIRED` | "Payment not completed" | Start a new transaction if the consumer wants to pay. |
| The card details were rejected, or the payment could not be submitted | None | `OPEN`, or the final status if the payment was already finished | The card form with an error message. The consumer can correct their details and confirm again. | Keep the iframe. If the consumer gives up, rely on the webhook or the get transaction endpoint. |
| The consumer did not confirm the card form in time | None | `EXPIRED` | The card form | Rely on the webhook or the get transaction endpoint. |
| The page could not get the final status | `error`, `status_unavailable` | Any | "We could not confirm your payment" after 3D Secure, otherwise the card form with an error message | Poll the get transaction endpoint until the status is final. |
| The card form or the 3D Secure page loaded, but could not start | `error`, `initialization_failed` | `OPEN`, later `EXPIRED` | "Payment not completed" on the 3D Secure page. The card form can stay empty or incomplete. | Check that you use `action.iframe.url` unchanged, then start a new transaction. |
| The URL was changed or is incomplete, or the transaction is unknown | None | `OPEN`, later `EXPIRED` | A plain error page | Use `action.iframe.url` unchanged, then start a new transaction. |
| The consumer closed the tab or left your page | None | `CANCELLED` or `EXPIRED` | Nothing, the iframe is gone | Rely on the webhook or the get transaction endpoint. |

After a wrong 3D Secure code the bank decides the result, and depending on how it reports it you receive `failed`
or `cancelled`. Treat both the same way: the payment is not completed.

A transaction can be paid only once. To let the consumer try again after a final status other than `SUCCESS`,
initialize a new transaction and load its URL in the iframe.

# When you receive no final status

Your page can miss the final message, for example when the consumer's connection drops. Do not wait for a message
forever:

- When you receive an `error` message, get the status from your server.
- When you have received no message about 1 minute after the consumer confirmed the card form, also get the status
  from your server, and keep the iframe on the page. Getting the status does not affect the payment.
- The page itself does not wait forever either. The card form checks for a final status for about 30 seconds
  after the consumer confirms it. During 3D Secure the consumer can take longer at their bank, so the 3D Secure
  page keeps checking until the transaction expires, plus 2 minutes. When the page stops checking without a final
  status, it sends `error` with `status_unavailable`.
- Your server uses the webhook, or polls the
  [get card transaction (v2)](https://docs.cm.com/payments/online-payments/v1.27.20/api-reference#operation/getCreditCardTransactionV2) endpoint with a
  growing interval, until the status is final (`SUCCESS`, `FAILURE`, `CANCELLED` or `EXPIRED`).
- A transaction that is never finished expires. See [transaction expiration](https://docs.cm.com/payments/online-payments/guides/transaction-expiration).

# Sequence

The payment without problems:

```text
Consumer          Your page / server           CM.com API          CM.com card payment page (iframe)
   |                     |                          |                          |
   |  go to checkout     |                          |                          |
   |-------------------->|  POST .../initialize     |                          |
   |                     |------------------------->|                          |
   |                     |  201, action.iframe.url  |                          |
   |                     |<-------------------------|                          |
   |                     |  <iframe src=action.iframe.url>                     |
   |                     |---------------------------------------------------->|
   |  card details, 3D Secure (inside the iframe)                              |
   |-------------------------------------------------------------------------->|
   |                     |  postMessage transactionStatus "successful"         |
   |                     |<----------------------------------------------------|
   |                     |  webhook                 |                          |
   |                     |<-------------------------|                          |
   |                     |  GET transaction         |                          |
   |                     |------------------------->|                          |
   |                     |  status SUCCESS          |                          |
   |                     |<-------------------------|                          |
   |  order confirmation |                          |                          |
   |<--------------------|                          |                          |
```

When the payment is not completed (`failed`, `cancelled`, `rejected` or `expired`):

```text
Consumer          Your page / server           CM.com API          CM.com card payment page (iframe)
   |                     |                          |                          |
   |  card details, 3D Secure (inside the iframe)                              |
   |-------------------------------------------------------------------------->|
   |                     |  postMessage transactionStatus "failed"             |
   |                     |<----------------------------------------------------|
   |                     |  webhook                 |                          |
   |                     |<-------------------------|                          |
   |                     |  GET transaction         |                          |
   |                     |------------------------->|                          |
   |                     |  status FAILURE          |                          |
   |                     |<-------------------------|                          |
   |  try again, or another payment method          |                          |
   |<--------------------|                          |                          |
   |  try again          |                          |                          |
   |-------------------->|  POST .../initialize (new transaction)              |
   |                     |------------------------->|                          |
   |                     |  201, new action.iframe.url                         |
   |                     |<-------------------------|                          |
```

When the page cannot confirm the status:

```text
Consumer          Your page / server           CM.com API          CM.com card payment page (iframe)
   |                     |                          |                          |
   |  card details, 3D Secure (inside the iframe)                              |
   |-------------------------------------------------------------------------->|
   |                     |  postMessage error "status_unavailable"             |
   |                     |<----------------------------------------------------|
   |                     |  GET transaction (repeat until final)               |
   |                     |------------------------->|                          |
   |                     |  status SUCCESS / FAILURE / CANCELLED / EXPIRED     |
   |                     |<-------------------------|                          |
   |  result             |                          |                          |
   |<--------------------|                          |                          |
```
