# Refund

## Refund

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

This API is used for refund orders.&#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.                                               |
| amount<mark style="color:red;">\*</mark>     | String | Amount of a transaction.                                                                                            |
| ipnurl                                       | String | <p>Asynchronous callback Url. </p><p>The IPN Url should be secure and reliable.</p>                                 |

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

```javascript
{
    "ret_msg": "refund success ",
    "ret_code": "000100"
}
```

{% endtab %}

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

```javascript
{
   "ret_code": "000000",
   "retMsg": "data error : verifySign",
}
```

{% endtab %}
{% endtabs %}

### Response <a href="#xiang-ying" id="xiang-ying"></a>

| **Parameter** | **Type** | **Description**              |
| ------------- | -------- | ---------------------------- |
| ret\_msg      | String   | Return response message.     |
| ret\_code     | String   | Return response status code. |

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

```shell
curl --location --request POST 'https://api.2pay.co//app-data-search/v1/refund' \
--header 'Content-Type: application/json' \
--data-raw '{
    "merchantNo": "M1659212481867",
    "reference": "86772212-8b1c-4bf7-79a3-4f32afa7e2c6",
    "amount": "9.990000",
    "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: 'M16593129121867',
  reference: '2991223db-4cdc-ba0f-7e15bfc9beb2',
  amount: '1.0000',
}


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/refund',
  form: buildAndSignParams(params)
});
```

{% endtab %}

{% tab title="Php" %}

```php
<?php
    function securepayRefund()
    {
        $url = 'https://api.2pay.co/app-data-search/v1/refund';
        $token = '5cbfb079f15b150122261c8537086d77a';
        $params = [
            'merchantNo' => 'M201210043',
            'amount' => '0.01',
            'reference' => '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);
    }
    securepayRefund();
?>
```

{% endtab %}

{% tab title="Go" %}

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

	t := pay.New("MerchantNo", "Token")
	param := pay.Refund{
		orderNo:      "alipay",
		Amount:      "10",
	}

	resp, err := t.SecurePay(&param)
	fmt.Println(resp, err)
}
```

{% endtab %}

{% tab title="Java" %}

```java
RefundRequest rfd = new RefundRequest();
rfd.setamount("1000000");
rfd.setReference("6ce3653f-5365-4bcd-b596-e9007b683bec");
RefundResponse rfs = client.execute(rfd);
System.out.println(JSONObject.fromObject(rfs));
```

{% endtab %}
{% endtabs %}
