Skip to content

Authentication

Signature Generation Rules

Unified Signature Generation Rule:

  1. Sort all the key-value pairs in the Map in lexicographical order (case-insensitive), and concatenate them into a query string format. Specifically, each key-value pair will be concatenated as key=value&, forming a concatenated string.

  2. After concatenating the string, append the merchant's private key (key) from the HiPay backend to the end of the string.

  3. Perform MD5 encryption on the concatenated string to obtain the final signature string.

Parameter Example Values

ParameterTypeExample Value
mchNoStringM1735112701
appIdString676bb7fefb715596544e2210
mchOrderNoStringPAYIN_TEST_0003
amountNumber1000
subjectStringtest
bodyStringtest
notifyUrlStringhttp://domain.com
successUrlStringhttp://domain.com
failUrlStringhttp://domain.com
cancelUrlStringhttp://domain.com
expiredTimeNumber600
userNameStringtest
userEmailStringtest@gmail.com
userPhoneString0899998888
userAddressStringtest
reqTimeNumber1739413509

Concatenated String Example

Concatenate the above parameters in lexicographical order, then append the key.

Example of the concatenated string: mchNo=M1735112701&appId=676bb7fefb715596544e2210&mchOrderNo=PAYIN_TEST_0003&amount=1000&subject=test&body=test&notifyUrl=http://domain.com&successUrl=http://domain.com&failUrl=http://domain.com&cancelUrl=http://domain.com&expiredTime=600&userName=test&userEmail=test@gmail.com&userPhone=0899998888&userAddress=test&reqTime=1739413509&key=your_private_key

Signature Calculation

Using the merchant's private key, perform MD5 encryption on the concatenated string to obtain the final signature value.

The final signature is: sign = MD5(concatenated string).toUpperCase()

Code Example

1. Get Concatenated String

java
public static String getStrSort(Map<String, String> map) {
    ArrayList<String> list = new ArrayList<>();
    Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
    while (iterator.hasNext()) {
        Map.Entry<String, String> entry = iterator.next();
        if (entry.getValue() != null && !entry.getValue().isEmpty()) {
            list.add(entry.getKey() + "=" + entry.getValue() + "&");
        }
    }

    String[] arrayToSort = list.toArray(new String[0]);
    Arrays.sort(arrayToSort, String.CASE_INSENSITIVE_ORDER);

    StringBuilder sb = new StringBuilder();
    for (String s : arrayToSort) {
        sb.append(s);
    }
    return sb.toString();
}

2. Calculate Signature Using Map Parameters and Private Key

java
public static String getSign(Map<String, String> map, String key) {
    String result = getStrSort(map);
    result = result + "key=" + key;
    if (_log.isDebugEnabled()) {
        _log.debug("signStr: {}", result);
    }
    result = md5(result, encodingCharset).toUpperCase();
    if (_log.isDebugEnabled()) {
        _log.debug("signValue: {}", result);
    }
    return result;
}

3. MD5 Encryption Method

java
public static String md5(String value, String charset) {
    MessageDigest md = null;
    try {
        byte data[] = value.getBytes(charset);
        md = MessageDigest.getInstance("MD5");
        byte digestData[] = md.digest(data);
        return toHex(digestData);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return null;
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return null;
}