# Bet placement

## Ticket reserve:

<mark style="color:green;">`POST`</mark> `{clientApi}/ticket/reserve`\
\
When placing a bet, the CmsWager system will send this request to the client system to check if the user has enough balance to play the bet

#### Example Request

```json
{
    "clientId": "xyzk",
    "userId": 123456,
    "transactionType": 2,
    "amount": 10.56,
    "ticket": {
        "code": "abcd1234",
        "isBonus": false,
        "amount": 10.56
    }
}
```

#### Example Success Response

<mark style="color:green;">`Response code`</mark> 200

If the user has sufficient balance, the withdraw amount in the request must be deducted from the user, and a record should be created, with the specific 'code' under ticket.

After the withdraw amount is deducted and a record is successfully created, a 200 success response should be returned.

```json
{    
    "status": "success",
    "balance": 140.85,
    "error_code": 0,
    "error_message": ""
}
```

#### Example Failure Response

<mark style="color:red;">`Response code`</mark> 406

If the user has insufficient balance, no actions should be taken, and a 406 response with 2001 error\_code should be returned.

```json
{
    "status": "error",
    "balance": 140.85,
    "error_code": 406,
    "error_message": "Not enough balance"
}
```

## Ticket rollback

<mark style="color:green;">`POST`</mark> `{clientApi}/ticket/rollback`

Sometimes, a server error may occur on each side during \`ticket/reserve\` request, due to a gateway timeout, or a technical issue. Also, the ticket may just get rejected by the user's risk limits and fail to successfully create. In this case, the CmsWager system will send a rollback request with the same ticket code of "reserve" request to prevent discrepancies.

{% hint style="info" %}
If this request encounters a server error, it will be retried for 24 hours until it gets a meaningful response
{% endhint %}

{% hint style="warning" %}
If the rollback request is received more than one time and it was just processed, the client system does not need to change the user balance and a success response must be returned
{% endhint %}

#### Example Request

```json
{
    "clientId": "xyzk",
    "userId": 123456,
    "transactionType": 1,
    "amount": 10.56,
    "ticket": {
        "code": "abcd1234",       
        "amount": 10.56
    },
    "code": 2020,
    "reason":"ticket_rejected"
}
```

#### Example Success response

<mark style="color:green;">`Response code`</mark> 200

Success response must be returned in these cases:\
if a record is found and the status is not setted to "rollbacked". \
If a record is not found: it mean that the reserve request was not processed and client system does not need to modify the user balance

```json
{    
    "status": "success",
    "balance": 140.85,
    "error_code": 0,
    "error_message": ""
}
```

#### Example Failure response

<mark style="color:red;">`Response code`</mark> 406

```json
{    
    "status": "error",
    "balance": 140.85,
    "error_code": 406,
    "error_message": "generic error"
}
```

## Ticket Confirm

<mark style="color:green;">`POST`</mark> `{clientApi}/ticket/confirm`

If the ticket/reserve request returns a success response, and the bet is successfully created with no problems, a \`ticket/confirm\` request will be sent by the CmsWager system, to the client system.

This request may bring a lower stake amount compared to 'reserve' request. This is caused by risk limits and counter offers. If that is the case, the difference (amount indicated by "deposit" key inside the request) should be refunded to the user.

{% hint style="info" %}
If this request encounters a server error, it will be retried for 24 hours until it gets a meaningful response.
{% endhint %}

{% hint style="warning" %}
If the "ticket confirm" request is received more than one time and it was just processed, a success response must be returned
{% endhint %}

#### Example Request with same amount of reserve

```json
{
    "clientId": "xyzk",
    "userId": 123456,
    "transactionType": 0,
    "amount": 0,
    "ticket": {
        "id": 123456,
        "code": "abcd1234",
        "isBonus": false,
        "isFreebet": false,
        "freebetId": 0,
        "ticketType": 3,
        "amount": 10.56,
        "minAccumulator": 2.26,
        "maxAccumulator": 11.60,
        "minBonus": 2.60,
        "maxBonus": 5.20,
        "minWin": 10.56,
        "maxWin": 10.56,
        "combinations": 7,
        "playerIp" : "192.0.0.0" 
        "odds": [
            {
                "banker": false,
                "isLive": false,
                "status": 0,
                "match": {
                    "id": 56963,
                    "home": "Italy",
                    "away": "England",
                    "matchDate": "2024-01-10T18:30:00.000Z"
                },
                "odd": {
                    "id": "56s2685d",
                    "name": "home",
                    "oddValue": 2.56
                },
                "market": {
                    "id": 123,
                    "name": "1x2"
                },
                "sport": {
                    "id": 1,
                    "name": "Soccer"
                },
                "category": {
                    "id": 20,
                    "name": "International"
                },
                "tournament": {
                    "id": 22,
                    "name": "Euro 2020"
                }
            },
            {
                "banker": false,
                "isLive": false,
                "status": 0,
                "match": {
                    "id": 56964,
                    "home": "Spain",
                    "away": "France",
                    "matchDate": "2024-01-10T18:30:00.000Z"
                },
                "odd": {
                    "id": "df8522s",
                    "name": "home",
                    "oddValue": 2.56
                },
                "market": {
                    "id": 123,
                    "name": "1x2"
                },
                "sport": {
                    "id": 1,
                    "name": "Soccer"
                },
                "category": {
                    "id": 20,
                    "name": "International"
                },
                "tournament": {
                    "id": 22,
                    "name": "Euro 2020"
                }
            }
        ],
        "ticketSystem": [
            {
                "code": 1,
                "combinations": 3,
                "TotalStake": 3.0,
                "minWin": 1.39,
                "maxWin": 4.97
            },
            {
                "code": 2,
                "combinations": 3,
                "TotalStake": 3.0,
                "minWin": 1.9738,
                "maxWin": 8.0434
            },
            {
                "code": 3,
                "combinations": 1,
                "TotalStake": 1.0,
                "minWin": 4.263408,
                "maxWin": 4.263408
            }
        ]
    }
}
```

#### Example Request with different amount of reserve

in this case the "transactionType" value is "1" and the amount value in root is different from "0".  The amount should be refunded to user.

```json
{
    "clientId": "xyzk",
    "userId": 123456,
    "transactionType": 1,
    "amount": 5.60,
    "ticket": {
        "id": 123456,
        "code": "abcd1234",
        "isBonus": false,
        "isFreebet": false,
        "freebetId": 0,
        "ticketType": 3,
        "amount": 10.56,
        "minAccumulator": 2.26,
        "maxAccumulator": 11.60,
        "minBonus": 2.60,
        "maxBonus": 5.20,
        "minWin": 10.56,
        "maxWin": 10.56,
        "combinations": 7
        "odds": [
            {
                "banker": false,
                "isLive": false,
                "status": 0,
                "match": {
                    "id": 56963,
                    "home": "Italy",
                    "away": "England",
                    "matchDate": "2024-01-10T18:30:00.000Z"
                },
                "odd": {
                    "id": "56s2685d",
                    "name": "home",
                    "oddValue": 2.56
                },
                "market": {
                    "id": 123,
                    "name": "1x2"
                },
                "sport": {
                    "id": 1,
                    "name": "Soccer"
                },
                "category": {
                    "id": 20,
                    "name": "International"
                },
                "tournament": {
                    "id": 22,
                    "name": "Euro 2020"
                }
            },
            {
                "banker": false,
                "isLive": false,
                "status": 0,
                "match": {
                    "id": 56964,
                    "home": "Spain",
                    "away": "France",
                    "matchDate": "2024-01-10T18:30:00.000Z"
                },
                "odd": {
                    "id": "df8522s",
                    "name": "home",
                    "oddValue": 2.56
                },
                "market": {
                    "id": 123,
                    "name": "1x2"
                },
                "sport": {
                    "id": 1,
                    "name": "Soccer"
                },
                "category": {
                    "id": 20,
                    "name": "International"
                },
                "tournament": {
                    "id": 22,
                    "name": "Euro 2020"
                }
            }
        ],
        "ticketSystem": [
            {
                "code": 1,
                "combinations": 3,
                "TotalStake": 3.0,
                "minWin": 1.39,
                "maxWin": 4.97
            },
            {
                "code": 2,
                "combinations": 3,
                "TotalStake": 3.0,
                "minWin": 1.9738,
                "maxWin": 8.0434
            },
            {
                "code": 3,
                "combinations": 1,
                "TotalStake": 1.0,
                "minWin": 4.263408,
                "maxWin": 4.263408
            }
        ]
    }
}
```

#### Example Success Response

<mark style="color:green;">`Response code`</mark> 200

If the ticket is saved successfully in client system, the bet placement operation can be considered concluded. A 200 response with "success" status should be returned.

```json
{    
    "status": "success",
    "balance": 140.85,
    "error_code": 0,
    "error_message": ""
}
```

#### Example Failure Response

<mark style="color:red;">`Response code`</mark> 406

```
{    
    "status": "error",
    "balance": 140.85,
    "error_code": 2040,
    "error_message": "generic error"
}
```

{% hint style="danger" %}
Important: No further action should be taken on the ticket until the "ticket confirm" message has been processed.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.cmswager.com/cmswager-integration-document/client-api-reference/api-reference/bet-placement.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
