> ## Documentation Index
> Fetch the complete documentation index at: https://docs.m4psp.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Payout creation

### Process of shop payouts generation is as follows:

```mermaid placement="bottom-left" theme={null}
sequenceDiagram
    autonumber

    participant S as Shop
    participant PG as Payment Gateway API

    rect rgb(245,245,245)
        Note over S,PG: Payout validation

        S->>PG: POST /withdraw/try
        PG-->>S: result=true
    end

    rect rgb(245,245,245)
        Note over S,PG: Account validation

        S->>PG: POST /check_account
        PG-->>S: accountAvailable=true
    end

    rect rgb(245,245,245)
        Note over S,PG: Create payout

        S->>PG: POST /withdraw/create
        activate PG

        PG-->>S: payoutId

        deactivate PG
    end

		rect rgb(245,245,245)
        Note over S,PG: Getting payout status
    end
        
    loop Until final status received

        alt Search by payoutId

            S->>PG: POST /withdraw/status
            PG-->>S: payout status

        else Search by shop payment number

            S->>PG: POST /withdraw/shop_payment_status
            PG-->>S: payout status

        end

    end

    Note over S,PG: Final status = SUCCESS or FAILED
```

1. Shop executes a request for a preliminary calculation and receiving additional parameters for further creation of the payout using the method [withdraw/try](/api-reference/withdraw-create/payout-preliminary-calculation);
2. If `result: true`, the shop sends request for a preliminary check of the existence and possibility of adding funds to the account [check\_account](/api-reference/withdraw-create/checking-the-account-for-the-possibility-of-replenishment), to the specified payment way (if necessary, specify additional parameters obtained from the previous request from the `account_info_config` field);
3. If `result: true`, verify availability of additional information in the `account_info` field, if available, it is necessary to pass additional parameters in the payout creation request. When getting `result: false`, analyse the error code in the `error_code` field (see [description of error codes](/errors-list)). If the `error code` is not fatal, then you need to start requesting the status of the transaction after a certain time (for 2 hours at 30-second intervals). If the `error code` is fatal the communication with our API is over.
4. Shop submits a request for payout creation [withdraw/create](/api-reference/withdraw-create/payout-creation), specifying the required parameters and additional information from `account_info` (if it is required for the specified payment way);
5. The response returns information with the generated payout request and indicating the amounts and the payout ID. In this case, the part of the balance of the shop from which the payout is made is frozen until the final payout status.
6. After payout creation, it is necessary to make a request to get the status of the created payout [withdraw/status](/api-reference/withdraw-create/payout-status-request-by-id-of-the-payout-created-in-m4-system) or [withdraw/shop\_payment\_status](/api-reference/withdraw-create/payout-status-request-by-the-shop-payout-number), before receiving the final status.

> **Attention:** Status requests are recommended to be made at 30-second intervals.

7. After final status is received, the payout is considered completed, and the funds are debited from the shop's balance (or returned to the balance if the payout was not completed).

### Guidelines for creation and verification of payout requests

If you received `result: true` after creating the payout, you should immediately check the `data.status` field, perhaps there will already be a final [status](/withdraw-statuses), for example `6 (Rejected)` and in this case, you do not need to make a request for the payout status.

If you receive the `Read timeout` error at the payout creation request, then you need to make a status request by the shop's payout number [withdraw/shop\_payment\_status](/api-reference/withdraw-create/payout-status-request-by-the-shop-payout-number) and if the response returns an error stating the absence of such a payout, you can repeat the request to create a payout. If the payout status is returned in the response, then it means that it was created and you need to focus on this status.

Reason for payout rejection ([getting status 6](/withdraw-statuses)) can be clarified through the support service [support@m4psp.com](mailto:support@m4psp.com).

### Securing payouts idempotency

> **Warning:** System has duplication protection for payouts, `shop_payment_id` must be unique within a single shop.
