Appearance
Authentication
Signature Generation Rules
Unified Signature Generation Rule:
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 askey=value&
, forming a concatenated string.After concatenating the string, append the merchant's private key (
key
) from the HiPay backend to the end of the string.Perform MD5 encryption on the concatenated string to obtain the final signature string.
Parameter Example Values
Parameter | Type | Example Value |
---|---|---|
mchNo | String | M1735112701 |
appId | String | 676bb7fefb715596544e2210 |
mchOrderNo | String | PAYIN_TEST_0003 |
amount | Number | 1000 |
subject | String | test |
body | String | test |
notifyUrl | String | http://domain.com |
successUrl | String | http://domain.com |
failUrl | String | http://domain.com |
cancelUrl | String | http://domain.com |
expiredTime | Number | 600 |
userName | String | test |
userEmail | String | test@gmail.com |
userPhone | String | 0899998888 |
userAddress | String | test |
reqTime | Number | 1739413509 |
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¬ifyUrl=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;
}