---
metadata:
  - name: generator
    content: Diplodoc Platform v5.52.0
alternate:
  - https://pay.yandex.ru/docs/en/custom/web-sdk/order-update.md
  - https://pay.yandex.ru/docs/ru/custom/web-sdk/order-update.md
  - href: en/custom/web-sdk/order-update.md
    type: text/markdown
    title: Markdown version
  - href: ../../llms.txt
    type: text/markdown
    title: llms.txt
---
> **Documentation Index:** Fetch the complete configuration index at https://pay.yandex.ru/docs/en/llms.txt

# Update order value and cart items

You may sometimes need to update the cart when the payment session already exists.
Updating the cart is an asynchronous action that includes the following steps:

1. The user changes the items in the cart or the final order amount.
2. The merchant updates the data on their server.
3. The merchant updates the data in the payment session Yandex Pay.

The problem is that the user might start the payment process somewhere between Step 1 and Step 2, before the updated data has arrived. As a result, the user might click the button and open the Yandex Pay form with the old cart.

To avoid such a situation, updating the cart must lock the Yandex Pay button for the update period.

As an argument, the `paymentSession.update` method accepts a function (which is immediately executed). As a result of this function, a `Promise` is expected. The button is locked until the `Promise` resolves.

## Example {#example}

<!-- source: en/custom/web-sdk/_snippets/cart-update--checkout.mdx -->
<!-- markdownlint-disable -->

<div class="ypd-preview ypd-preview_with-source">
    <a href="https://yastatic.net/s3/pay-static/docs/v16.0.0/custom/cart-update--checkout/index.html" target="_blank">
        <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24"><path fill="currentColor" d="M18 19H6c-.55 0-1-.45-1-1V6c0-.55.45-1 1-1h5c.55 0 1-.45 1-1s-.45-1-1-1H5c-1.11 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2v-6c0-.55-.45-1-1-1s-1 .45-1 1v5c0 .55-.45 1-1 1zM14 4c0 .55.45 1 1 1h2.59l-9.13 9.13c-.39.39-.39 1.02 0 1.41.39.39 1.02.39 1.41 0L19 6.41V9c0 .55.45 1 1 1s1-.45 1-1V3h-6c-.55 0-1 .45-1 1z"></path></svg>
    </a>
    <iframe src="https://yastatic.net/s3/pay-static/docs/v16.0.0/custom/cart-update--checkout/index.html" style="height: 200px"></iframe>
</div>

```javascript
function onYaPayLoad() {
    const YaPay = window.YaPay;
    let activeSession = null;

    // Payment details
    const paymentData = {
        env: YaPay.PaymentEnv.Sandbox,
        version: 3,
        currencyCode: YaPay.CurrencyCode.Rub,
        merchantId: '<YOUR_MERCHANT_ID>',
        cart: getNewCart(),
    };

    // Handler to obtain a payment token
    function onPaymentSuccess(event) {
        console.log(`OrderId — ${event.orderId}`);
    }

    // Payment error handler
    function onPaymentError(event) {
        console.log(`Payment error — ${event.reason}`);
    }

    // Payment cancellation handler
    function onPaymentAbort(event) {}

    // Create a payment session.
    YaPay.createSession(paymentData, {
        onSuccess: onPaymentSuccess,
        onAbort: onPaymentAbort,
        onError: onPaymentError,
    })
        .then(function (paymentSession) {
            activeSession = paymentSession;
            paymentSession.mountButton(document.querySelector('#button_container'), {
                type: YaPay.ButtonType.Checkout,
                theme: YaPay.ButtonTheme.Black,
                width: YaPay.ButtonWidth.Auto,
            });
        })
        .catch(function (err) {
            // Couldn't create a payment session.
        });

    // Cart update emulation
    //   apiRequestPromise — API request promise to update the cart on the service
    onOrderUpdate(function (apiRequestPromise) {
        if (!activeSession) {
            return console.log('Payment session is not created');
        }

        const newCart = getNewCart();

        // Update the cart, locking the button
        activeSession.update(async function () {
            await apiRequestPromise;

            return {
                cart: newCart,
            };
        });
    });
}

/**
 * Collateral features for illustrative purposes
 */

function getNewCart() {
    const randomInt = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;

    const price = 7990;
    const count = randomInt(1, 20);
    const total = (count * price).toFixed(2);

    document.querySelector('#order_amount').innerHTML = Intl.NumberFormat('ru-RU', {
        style: 'currency',
        currency: 'RUB',
    }).format(total);

    return {
        items: [{ productId: '3', total, quantity: { count } }],
    };
}

function onOrderUpdate(cb) {
    const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

    const updateButton = document.querySelector('#update_order_button');

    updateButton.addEventListener('click', function () {
        // Emulate the request's Promise to update the cart on the server
        const apiRequestPromise = wait(2000);

        cb(apiRequestPromise);
    });
}

```
<!-- endsource: en/custom/web-sdk/_snippets/cart-update--checkout.mdx -->
