## Discover the next action

This endpoint guide you through the payment flow by telling you what action to take next.
You should poll this endpoint from your frontend to track the payment progress. We recommend polling
every 1-2 seconds.

The possible responses are:

- `WAIT`: The payment is not ready yet, continue polling
- `DISPLAY_QR`: Display a QR code to the customer
- `REDIRECT`: Redirect the customer to complete the payment
- `CHECK_STATUS`: The payment flow is complete, check the final status

General implementation flow:

1. Start polling this endpoint after selecting a payment method
2. When you receive an action (`DISPLAY_QR` or `REDIRECT`) take the appropriate action for your customer and continue polling in the background.
3. When you receive `CHECK_STATUS` stop polling this endpoint and call `check-status` to see if payment succeeded.

Implementation tips:

- Use `setInterval()` or `setTimeout()` for polling from your frontend
- Add exponential backoff if you get multiple `WAIT` responses
- Handle network errors gracefully and retry failed requests
- Stop polling once you receive `CHECK_STATUS`

## Endpoint signature

```http
POST https://api.reservepay.com/sdk/discover-next-action HTTP/1.1
Content-Type: application/json
Accept: application/json

{
  merchant_id: string,
  installation_id: string,
  payment_session_id: string,
}
```

Returns: string

## Request arguments

### `merchant_id` (string)

**Required**. Your merchant ID that uniquely identifies your account.

### `installation_id` (string)

**Required**. The installation ID for this platform (e.g. your iOS app, Android app, website, or terminal).

### `payment_session_id` (string)

**Required**. The ID of the payment session returned from `select-payment-method`.

## Errors specific to this endpoint

### `NOT_FOUND`

This error occurs when we cannot find the merchant account, installation, or payment session with the IDs provided. Double check that you are using the correct IDs.

## Errors common to all endpoints

### `UNHANDLED_ERROR`

This error occurs when the server encounters an unexpected internal error that it cannot handle gracefully. This typically happens due to bugs, infrastructure issues, or edge cases that weren't anticipated during development.

### `INVALID_ARGUMENTS`

This error occurs when the request contains invalid or missing parameters. Common cases include missing required fields, or values that don't match the expected format or type.

### `BAD_VERSION`

This error occurs when making requests to an API version that is either deprecated or not yet released. This commonly happens when using an outdated SDK or when the API version specified in the request URL is incorrect.
