# Make a payment

<mark style="color:green;">`POST`</mark> <https://agwsapi.tarlanpayments.kz/showcase-gateway/api/v1/action/cash-in>

**Headers**

| Name         | Value                                                                              |
| ------------ | ---------------------------------------------------------------------------------- |
| Content-Type | `application/json`                                                                 |
| X-Signature  | [Authorization hash](https://docs.tarlanpayments.kz/eng/agws/creating-a-signature) |

**Body**

| Name                                            | Type   | Description                                                                            |
| ----------------------------------------------- | ------ | -------------------------------------------------------------------------------------- |
| username<mark style="color:red;">\*</mark>      | String | User ID                                                                                |
| amount<mark style="color:red;">\*</mark>        | Float  | Transaction amount                                                                     |
| agent<mark style="color:red;">\*</mark>         | String | Showcase code in Tarlan system                                                         |
| project<mark style="color:red;">\*</mark>       | String | Project Code assigned by Tarlan                                                        |
| service\_code<mark style="color:red;">\*</mark> | String | Service ID on the showcase side                                                        |
| external\_id<mark style="color:red;">\*</mark>  | String | Unique payment identifier on the side of the storefront                                |
| callback\_url                                   | String | The URL to which the request about the operation status will be sent.                  |
| datetime<mark style="color:red;">\*</mark>      | String | Time of payment initiation in the storefront system. ISO 8601 Current Timestamp format |

**Response**

| Name              | Type   | Description                                                                                                   |
| ----------------- | ------ | ------------------------------------------------------------------------------------------------------------- |
| status            | bool   | Request processing status                                                                                     |
| status\_code      | uint   | Error code                                                                                                    |
| message           | string | Description of error                                                                                          |
| result            | Object | The result of a query that contains information                                                               |
| -error\_code      | uint   | Error code                                                                                                    |
| -message          | String | Error description                                                                                             |
| -data             | Object | Information about data                                                                                        |
| --status\_code    | String | Transaction status code                                                                                       |
| --status\_message | String | Description of transaction status                                                                             |
| --username        | String | User ID                                                                                                       |
| --amount          | Float  | Amount credited                                                                                               |
| --datetime        | String | Time of payment initiation in the storefront system. ISO 8601 Current Timestamp format                        |
| --project         | String | Project Code assigned by Tarlan                                                                               |
| --service\_code   | String | Service ID on the showcase side                                                                               |
| --external\_id    | String | Payment ID on the showcase side                                                                               |
| --fail\_reason    | Object | Field Containing the [Failure Reason](https://docs.tarlanpayments.kz/eng/agws/reason-for-operation-rejection) |
| ---code           | Int    | Rejection Reason Code                                                                                         |
| ---massage        | String | Description of the Operation Rejection Reason                                                                 |

{% tabs %}
{% tab title="200: OK Example of a successful response" %}

```json
{
    "status": true,
    "status_code": 0,
    "message": "Success",
    "result": {
        "error_code": 0,
        "message": "",
        "data": {
            "status_code": "2",
            "status_message": "Transaction successfully processed",
            "username": "12345",
            "amount": 1000,
            "datetime": "2025-01-01T01:01:01+05:00",
            "project": "project",
            "service_code": "service",
            "external_id": "200001"
        }
    }
}
```

{% endtab %}

{% tab title="200: OK Example of a fail response" %}

```json
{
    "status": true,
    "status_code": 0,
    "message": "Success",
    "result": {
        "error_code": 0,
        "message": "",
        "data": {
           "status_code": "4",
            "status_message": "Transaction was failed",
            "username": "12345",
            "amount": 1000,
            "datetime": "2024-01-01T00:00:00",
            "project": "project",
            "service_code": "service",
            "external_id": "200001",
            "fail_reason": {
                "code": 100,
                "message": "Unknown reason, clarification required"
            }
        }
    }
}
```

{% endtab %}

{% tab title="200: OK Example of a temporary status" %}

```json
{
    "status": true,
    "status_code": 0,
    "message": "Success",
    "result": {
        "error_code": 0,
        "message": "",
        "data": {
            "status_code": "1",
            "status_message": "Transaction created",
            "username": "12345",
            "amount": 1000,
            "datetime": "2024-01-01T00:00:00",
            "project": "project",
            "service_code": "service",
            "external_id": "200001"
        }
    }
}
```

{% endtab %}

{% tab title="200 OK: Expected error" %}

```json
{
    "status": true,
    "status_code": 0,
    "message": "Success",
    "result" : {
        "error_code" : 1042,
        "message": "duplicate external id",
        "data": null
    }
}
```

{% endtab %}

{% tab title="400 Bad Request: Unexpected error" %}

```json
{
    "status": false,
    "status_code": 1014,
    "message": "Invalid signature",
    "result": {}
}
```

{% endtab %}
{% endtabs %}

Make Payment

{% tabs %}
{% tab title="Go" %}

```go
package main

import (
	"bytes"
	"crypto/sha256"
	"encoding/base64"
	"encoding/hex"
	"encoding/json"
	"io/ioutil"
	"log"
	"net/http"
	"sort"
	"time"
)

type Body struct {
	UserName    string  `json:"username"`
	Agent       string  `json:"agent"`
	Project     string  `json:"project"`
	ServiseCode string  `json:"service_code"`
	Amount      float64 `json:"amount"`
	ExternalID  string  `json:"external_id"`
	DateTime    string  `json:"datetime"`
}

func MakeSign(body Body, secretKey string) (string, error) {
	dataMap := make(map[string]interface{})
	jsonData, _ := json.Marshal(body)
	json.Unmarshal(jsonData, &dataMap)

	keys := make([]string, 0, len(dataMap))
	for key := range dataMap {
		keys = append(keys, key)
	}
	sort.Strings(keys)

	sortedData := make(map[string]interface{})
	for _, key := range keys {
		sortedData[key] = dataMap[key]
	}

	sortedJson, err := json.Marshal(sortedData)
	if err != nil {
		return "", err
	}

	base64EncodedData := base64.StdEncoding.EncodeToString(sortedJson)
	
	dataToSign := base64EncodedData + secretKey

	sha256Hash := sha256.Sum256([]byte(dataToSign))
	sign := hex.EncodeToString(sha256Hash[:])

	return sign, nil
}

func CheckLogin(body Body, url, signature string) (string, error) {
	jsonData, err := json.Marshal(body)
	if err != nil {
		return "", err
	}

	req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
	if err != nil {
		return "", err
	}

	req.Header.Set("X-Signature", signature)
	req.Header.Set("Content-Type", "application/json")

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		return "", err
	}
	defer resp.Body.Close()

	bodyBytes, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return "", err
	}

	response := string(bodyBytes)


	return response, nil
}

func main() {
	RequestURL := "https://agwsapi.tarlanpayments.kz/showcase-gateway/api/v1/action/cash-in"

	SecretKey := "122112"

	requestBody := Body{
		UserName:    "login",
		Agent:       "agent",
		Project:     "project",
		ServiseCode: "xbetTest",
		Amount:      10.01,
		ExternalID:  "reference_id",
		DateTime:    time.Now().Format(time.RFC3339),
	}

	sign, err := MakeSign(requestBody, SecretKey)
	if err != nil {
		log.Println("Error generating signature:", err)
		return
	}

	Response, err := CheckLogin(requestBody, RequestURL, sign)
	if err != nil {
		log.Panic("Error sending request", err)
		return
	}

	if Response != "" {
		log.Println(Response)
	}
}

```

{% endtab %}
{% endtabs %}
