# Transaction Query

## TranQuery

<mark style="color:green;">`POST`</mark> `https://api.2pay.co/online/v1/tran-query`

This API is used to query transaction details via transaction ID in the merchant services system. &#x20;

#### Request Body

| Name                                         | Type   | Description                                                                                                         |
| -------------------------------------------- | ------ | ------------------------------------------------------------------------------------------------------------------- |
| merchantNo<mark style="color:red;">\*</mark> | String | ID of a merchant.                                                                                                   |
| verifySign<mark style="color:red;">\*</mark> | String | The merchants should verify the signature to ensure the response is from the server and not modified by any others. |
| reference<mark style="color:red;">\*</mark>  | String | The reference number of a trasaction in the merchant services system.                                               |

{% tabs %}
{% tab title="200: OK " %}

```javascript
{
    "ret_code": "000100",
    "ret_msg": "success",
    "result": {
        "transactionNo": "0f72c718-4a33-47ca-81fe-c228p3a8d08d",
        "reference": "8678528c-8c1c-4ff7-79a3-4f32afa6e2c6",
        "amount": 1.99,
        "status": "pending",
        "currency": "USD",
        "settleCurrency": "USD"
    }
}
```

{% endtab %}

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

```javascript
{
    "ret_code": "000022",
    "ret_msg": "order not found"
}
```

{% endtab %}
{% endtabs %}

### Response

| **Parameter** | **Type** | **Description**                    |
| ------------- | -------- | ---------------------------------- |
| result        | Object   | [#undefined](#undefined "mention") |
| ret\_msg      | String   | Return response message.           |
| ret\_code     | String   | Return response status code.       |

### Result Object <a href="#undefined" id="undefined"></a>

| **Parameter**  | **Type** | **Description**                                                                                                                     |
| -------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------- |
| amount         | String   | <p>The amount of a transaction. </p><p>This parameter will be returned when the transaction currency is <strong>"USD"</strong>.</p> |
| currency       | String   | Currency supported by 2Pay is **"USD"**.                                                                                            |
| transactionNo  | String   | The transaction ID of 2Pay system.                                                                                                  |
| reference      | String   | The reference number of a trasaction in the merchant services system.                                                               |
| status         | String   | Transaction status.                                                                                                                 |
| settleCurrency | String   | <p>Currency code from ISO 4217. </p><p>Potential value：<strong>"USD"</strong>.</p>                                                  |

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

```shell
curl --location --request POST 'https://api.2pay.co//app-data-search/v1/tran-query' \
--header 'Content-Type: application/json' \
--data-raw '{
    "merchantNo": "M1659370901281867",
    "reference": "8677528c-8b1c-4bf7-79a3-9291jwil",
    "verifySign": "{{verifySign}}"
}'
```

{% endtab %}

{% tab title="NodeJs" %}

```javascript
import request from 'request'
import CryptoJS from 'crypto-js'
const token = 'yjmsy9vwjkajs21qqt4psc2o8y3rr5q'
const baseUrl = 'https://api.2pay.co'
const params = {
  merchantNo: 'M1653001221867',
  reference: '2991221b-4cdc-ba0f-7e121fc2beb2',
}


function buildAndSignParams(params) {
  let sortArray = [];
  Object.keys(params).sort().forEach(k => {
      if (k == 'verifySign') {
          return
      }
    if(params[k] || params[k] === false) {
      sortArray.push(`${k}=${params[k]}`)
    }
  });
  sortArray.push(CryptoJS.MD5(token).toString());
  console.log('MD5 Token', CryptoJS.MD5(token).toString())
  const tempStr = sortArray.join('&');
  console.log('temStr:', tempStr)
  const verifySign = CryptoJS.MD5(tempStr).toString();
  console.log('verifySign', verifySign)
  return {
    ...params,
    verifySign
  }

}

const json = await request.post({
  url: BaseUrl + 'app-data-search/v1/tran-query',
  form: buildAndSignParams(params)
});
```

{% endtab %}

{% tab title="Php" %}

```php
 <?php
    function securepayReferenceQuery()
    {
        $url = 'https://api.2pay.co/app-data-search/v1/tran-query';
        $token = '5cbfb079f15b150122221017086d77a';
        $params = [
            'merchantNo' => 'M2091210043',
            'reference' => 'kasd-test201218070101'
        ];
        ksort($params, SORT_STRING);
        $str = '';
        foreach ($params as $k => $v) {
            $str .= $k . '=' . $v . '&';
        }
        $params['verifySign'] = md5($str . md5($token));
        echo 'verifySign:', $params['verifySign'];
        echo "\n";
        $ch = curl_init($url);
        curl_setopt_array($ch, array(
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_HEADER => false,
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => http_build_query($params),
        ));
        $result = curl_exec($ch);
        curl_exec($ch);
        echo $result;
        echo "\n";
        return json_decode($result, true);
    }
    securepayReferenceQuery();
?>
```

{% endtab %}

{% tab title="Go" %}

```go
package main

import (
	"fmt"
	pay "github.com/2pay-co/golang-sdk"
	"github.com/gogf/gf/v2/os/gtime"
)

func main() {
	pay := NewPay("MerchantNo", "Token")
	resp, err := pay.Query("2022-08-09 18:39:03")
	t.Logf("resp: %#v, err: %v", resp, err)
}
```

{% endtab %}

{% tab title="Java" %}

```java
TranQueryRequest trq = new TranQueryRequest();
trq.setReference("f194b314-298b-4023-970e-a8701f77316d");
TranQueryResponse tfs = client.execute(trq);
System.out.println(JSONObject.fromObject(tfs)); 
```

{% endtab %}
{% endtabs %}
