# Cancel a session with campaign budgets

> One of the things we can expect when cancelling a session is that the impact of the session's effects on campaign budgets will be reverted, except for coupon creations.

> For the complete documentation index, see [llms.txt](https://docs.talon.one/llms.txt).

Let's cancel a customer session that involves a [_discount total_ budget limit](/docs/product/campaigns/settings/manage-campaign-budgets.md#discount-limits)
and a [_coupon creation_ budget limit](/docs/product/campaigns/settings/manage-campaign-budgets.md#coupon-limits).

Before following this tutorial, we recommend that you read the generic explanation about
the following:

- [Campaign budget limits](/docs/product/campaigns/settings/manage-campaign-budgets.md#limits).
- [Customer session states](/docs/dev/concepts/entities/customer-sessions.md#customer-session-states).

Let's imagine that our customer purchased one pair of shoes and one shirt.

The customer session is part of a campaign that applies a 20% discount [on the session total](/docs/dev/integration-api/api-effects.md#setdiscount)
and [creates a new coupon](/docs/dev/integration-api/api-effects.md#couponcreated) for the customer
when they [redeem a valid coupon](/docs/dev/integration-api/api-effects.md#acceptcoupon).
The value of the discount applied to this session is $20.

## Close the session during checkout

Let's use the [Update customer session](/integration-api#tag/Customer-sessions/operation/updateCustomerSessionV2)
endpoint to change the session state from `open` to `closed` during checkout. The request
contains the following body:

```json title="Session update request"
{
  "customerSession": {
    "profileId": "ExampleUser",
    "couponCodes": [
      "SPRING-20-ALL-34001" // The coupon code that the customer used
    ],
    "state": "closed",
    "cartItems": [
      {
        "name": "shoes1",
        "sku": "9696",
        "quantity": 1,
        "price": 60,
        "category": "shoes"
        },
      {
        "name": "shirt35",
        "sku": "6078",
        "quantity": 1,
        "price": 40,
        "category": "shirts"
        }
    ]
  },
  "responseContent": [
    "customerSession",
    "customerProfile"
  ]
}
```

This request returns the following discount effects:

```javascript title="Applied discount effects"
{
  ...
  "effectType": "setDiscount",
  "props": {
    "name": "20% Discount promo",
    "value": 20 // The monetary value of the applied discount
  }
}
```

```javascript title="Applied coupon effects"
{
  ...
  "effectType": "acceptCoupon",
  "props": {
    "value": "SPRING-20-ALL-34001" // The coupon code that was accepted
  }
},
{
  ...
  "effectType": "couponCreated",
  "props": {
    "value": "SUMMER-20-ALL-35465", // The coupon code that was created
    "profileId": "ExampleUser"
  }
}
```

Our customer receives a 20% discount on their order and a new coupon code (SUMMER-20-ALL-35465)
that they can use in a new order.

Let's imagine that this campaign has a $20,000 discount total budget limit and that the coupon
creation budget limit is 8,000. After the session is closed, the situation of the budgets
is as follows:

| Limit type      | Budget limit  | Variation in session | Remaining amount until budget limit |
| --------------- | ------------- | -------------------- | ----------------------------------- |
| Discount total  | $20,000       | $20                  | $19,980                             |
| Coupon creation | 8,000 coupons | 1 coupon             | 7,999 coupons                       |

After the purchase, our customer wants to return both items.

## Cancel the session

Let's use the [Update customer session](/integration-api#tag/Customer-sessions/operation/updateCustomerSessionV2)
endpoint to change the session state from `closed` to `cancelled`.

Because we are cancelling the session with this request, we receive a [`rollbackDiscount`](/docs/dev/integration-api/api-effects.md#rollbackdiscount)
effect to undo the discount effect applied during the closing of the session, and a
[`rollbackCoupon`](/docs/dev/integration-api/api-effects.md#rollbackcoupon) effect to undo the
coupon redemption.

We get an `effects` array in the response with two rollback effects:

- A [discount rollback effect](#roll-back-the-discount-effect)
- A [coupon rollback effect](#roll-back-the-coupon-effect)

### Roll back the discount effect

```javascript title="Rollback discount effect"
{
  ...
  "effectType": "rollbackDiscount",
  "props": {
    "name": "20% Discount promo",
    "value": 20 // The monetary value of the discount that was rolled back
  }
}
```

The impact of the discount effect on the [discount total budget limit](/docs/product/campaigns/settings/manage-campaign-budgets.md#discount-limits) is automatically reverted. The total amount of discount that can be given in our campaign
is $20,000 again.

Let's use the [Return cart items](/integration-api#tag/Customer-sessions/operation/returnCartItems) endpoint to
return the items in the session.

At this point, the rest of the process takes place in our e-commerce app, where we apply
the necessary steps to refund the correct amount.

### Roll back the coupon effect

```javascript title="Rollback coupon redemption effect"
{
  ...
  "effectType": "rollbackCoupon",
  "props": {
    "value": "SPRING-20-ALL-34001" // The coupon code whose usage has been rolled back
  }
}
```

Although a rollback effect is triggered for the coupon that was redeemed by our customer,
no rollback effect is triggered for the new coupon. The number of [coupons that can be
created](/docs/product/campaigns/settings/manage-campaign-budgets/#coupon-limits) is still
7,999.

:::note
The coupon created in this session (SUMMER-20-ALL-35465) is still redeemable, and the coupon that
our customer redeemed (SPRING-20-ALL-34001) becomes redeemable again.
:::

At this point, no further action is required on the integration layer.

## Related pages

- [Display customer sessions](/docs/product/applications/display-customer-sessions.md)
- [API discount effects](/docs/dev/integration-api/api-effects.md#discounts)
- [Return individual items](/docs/dev/tutorials/partially-return-a-session.md)
- [Manage campaign budgets](/docs/product/campaigns/settings/manage-campaign-budgets.md)
