> The index of all pages is at [llms.txt](https://developers.qliro.com/llms.txt). Add `.md` to any page URL for its Markdown.

# Listeners (/qliro-checkout/frontend-features/listeners)

Your frontend application can subscribe to these listeners to get notifications from the checkout frontend.

## Define q1Ready

After the checkout renders, the variable q1 is defined automatically in the global scope of the browser. q1 is a frontend communication endpoint. It gives your frontend application functions to communicate with the checkout iframe and to set optional listeners.

Define a function q1Ready in the global scope of the browser. This makes sure the checkout is rendered before the listeners are registered. Add all the listeners inside the body of the function. The checkout calls q1Ready after it has rendered.

### Implementation example

```javascript
window.q1Ready = function(q1) {
  q1.onPaymentDeclined(paymentDeclinedListener);
  // ...
}
```

## onCheckoutLoaded()

Called when Qliro Checkout is fully loaded. This is when the interface has loaded and the order has been fetched from the server. The spinner might still be visible, and the customer cannot yet interact with the checkout.

### Arguments

| Parameter | Type | Description | Required |
| :- | :- | :- | :- |
| callback | function | A function without parameters | <Required /> |

### Implementation example

```javascript
q1.onCheckoutLoaded(function callback() {
  /the checkout has been loaded successfully
})
```

## onCustomerInfoChanged()

Called when the customer updates their contact information: email, mobile number, address or personal/organization number. Use it to save customer information in case of an abandoned cart.

### Arguments

| Parameter | Type | Description | Required |
| :- | :- | :- | :- |
| callback | function | A function that takes a **customer** (object). If the address is masked the object will only contain **isMasked** set to true and if there is no address yet the address object will be null. | <Required /> |

### Implementation example

```javascript
q1.onCustomerInfoChanged(function callback(customer) {
  // Save customer info
})
```

### Customer example

```json
{
  "email": "test@qliro.com",
  "mobileNumber": "0123456789",
  "personalNumber": "0987654321",
  "address": {
    "firstName": "FirstName",
    "lastName": "LastName",
    "street": "Street",
    "city": "City",
    "careOf": "",
    "postalCode": "12345",
    "isMasked": false
  }
}
```

## onOrderUpdated()

Starts the order synchronization towards the checkout frontend. Call q1.lock() first. Call q1.unlock() as soon as the orders match.

### Arguments

| Parameter | Type | Description | Required |
| :- | :- | :- | :- |
| callback | function | A function that takes an order (object) argument and calls unlock if the order is updated. | <Required /> |

### Implementation example

```javascript
q1.onOrderUpdated(function (order) {
  // if order reflects the local cart, use q1.unlock()
  if (order.totalPrice === localCart.totalPrice) {
    q1.unlock()
  }
  // else, don't do anything
})
```

### Order example

```json
{
  "totalPrice": 350,
  "orderItems": [
    {
      "merchantReference": "RedHat",
      "pricePerItemIncVat": 100,
      "quantity": 3
    },
    {
      "merchantReference": "BlackHat",
      "pricePerItemIncVat": 50,
      "quantity": 1
    }
  ]
}
```

## onCustomerDeauthenticating()

Called when the customer clicks "Not you?" or chooses to re-authenticate with their personal number during checkout.

### Arguments

| Parameter | Type | Description | Required |
| :- | :- | :- | :- |
| callback | function | A function without parameters. | <Required /> |

### Implementation example

```javascript
q1.onCustomerDeauthenticating(function callback() {
  // The customer has chosen to deauthenticate
})
```

## onPaymentMethodChanged()

Called when the customer changes payment method or subtype. Use it to apply discounts based on payment method.

### Arguments

| Parameters | Type | Description | Required |
| :- | :- | :- | :- |
| callback | function | A function that takes a paymentMethod (object) argument. The paymentMethod object will contain method (string), subtype (string), price (number) and priceExVat (number). | <Required /> |

### Implementation example

```javascript
q1.onPaymentMethodChanged(function callback(paymentMethod) {
  // For example: Apply payment based discounts
})
```

## onPaymentDeclined()

Called when the customer tries to complete the purchase and the payment is declined. The decline comes from a negative response to MerchantOrderValidationUrl, set when the order was created. The checkout calls **callback** with the DeclineReason as an argument.

### Arguments

| Parameter | Type | Description | Required |
| :- | :- | :- | :- |
| callback | function | A function that will be executed if the payment is declined by the merchant backend. A declineReason (string) and possibly declineReasonMessage (string) will be passed as an argument. | <Required /> |

### Implementation example

```javascript
q1.onPaymentDeclined(function callback(declineReason, declineReasonMessage) {
  // Depending on the declineReason, inform the customer what to do next
})
```

### Available declineReasons

| declineReason | Description |
| :- | :- |
| OutOfStock | One or more of the items in the cart is now out of stock. |
| PostalCodeIsNotSupported | The merchant does not deliver to the shipping address provided by the customer. |
| ShippingIsNotSupportedForPostalCode | The selected shipping method does not support the shipping address provided by the customer. |

## onPaymentProcess()

Called when the payment process starts and ends. While the checkout processes the payment, any change to the order might lead to unexpected behaviour for the customer.

### Arguments

| Parameter | Type | Description |
| :- | :- | :- |
| onStart | function | A callback that will be executed when the payment process starts. |
| onEnd | function | A callback that will be executed when the payment process ends. |

### Implementation example

```javascript
q1.onPaymentProcess(function onStart() {
  // Lock the interface to prevent the customer from changing the order
}, function onEnd() {
  // Unlock the interface to open up for order changes again
})
```

## onSessionExpired()

If the authorization token expires during a session, Qliro Checkout informs the customer. **onSessionExpired** is optional. It replaces this behavior with a call to **updateToken**.

A checkout session is valid for 90 minutes. After that, update the order to create a new session. An order is active for at most 48 hours. After that, create a new order.

### Arguments

| Parameter | Type | Description | Required |
| :- | :- | :- | :- |
| updateToken | function | A function that will be executed when the customer acknowledges the session expiration. | <Required /> |

### Implementation example

```javascript
q1.onSessionExpired(function updateToken() {
  // Implement a way to update the token
})
```

## onShippingMethodChanged()

Active when you provide shipping options in the createOrder request and use the shipping functionality in Qliro Checkout. Called when the customer changes shipping method, secondary option or additional shipping services.

### Arguments

| Parameter | Type | Description | Required |
| :- | :- | :- | :- |
| callback | function | A function that takes a **shipping** (object) argument. The shipping object will contain **method** (string), **secondaryOption** (string), **additionalShippingServices** (array of strings), **price** (number), **priceExVat** (number), **totalShippingPrice** (number) and **totalShippingPriceExVat** (number). | <Required /> |

<Callout type="info">
**price** represents the price of the shipping method selected and **totalShippingPrice** represents the price of the shipping method selected plus any additionalServices that might cost extra.
</Callout>

### Implementation example

```javascript
q1.onShippingMethodChanged(function callback(shipping) {
  // For example: Apply discounts based on shipping method and options
})
```

## onShippingPriceChanged()

Active when you provide shipping options in the createOrder request and use the shipping functionality in Qliro Checkout. When the customer changes a shipping option that affects the shipping fee, the checkout calls **callback**. The arguments are the new shipping price and the new total shipping price.

### Arguments

| Parameter | Type | Description | Required |
| :- | :- | :- | :- |
| callback | function | A function that takes a **newShippingPrice** (number) and **newTotalShippingPrice** (number) arguments. | <Required /> |

### Implementation example

```javascript
q1.onShippingPriceChanged(function callback(newShippingPrice, newTotalShippingPrice) {
  // For example: Apply discounts based on amount including shipping price
  // newTotalShippingPrice includes costs for both shipping and selected additional shipping services
})
```

<Cards>
<Card title="Enable scrolling" href="/qliro-checkout/frontend-features/enable-scrolling">
Continue to Enable scrolling
</Card>
</Cards>