服务器需要接入小米支付, 但是官网只有java和php版本,因此签名解析需要自己写
实行步骤:
1. 用eclipse打开java版本支付用例,加入mainClass文件,代码如下:
package com.xiaomi.migc.server.sdk;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
public class mainClass {
private static String getSign(Map
params) throws Exception{
String signString = ParamUtil.getSortQueryString(params);
return HmacSHA1Encryption.hmacSHA1Encrypt(signString, "QFehjb0X8qEhbkpi380GTQ=1");
}
public static void main(String args[]){
Map
mapInfo = new HashMap
();
mapInfo.put("appId", "2882303761517239138");
mapInfo.put("cpOrderId", "9786bffc-996d-4553-aa33-f7e92c0b29d5");
mapInfo.put("orderConsumeType", "10");
mapInfo.put("orderId", "21140990160359583390");
mapInfo.put("orderStatus", "TRADE_SUCCESS");
mapInfo.put("payFee", "1");
mapInfo.put("payTime", "2014-09-05%2015:20:27");
mapInfo.put("productCode", "com.demo_1");
mapInfo.put("productCount", "1");
mapInfo.put("productName", "银子1两");
mapInfo.put("uid", "100010");
try {
System.out.println(mainClass.getSign(mapInfo));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
生成测试用例:121f401e6e92fa8d8bb45392b8102891a058c1be
2. 编写golang代码,并测试,代码如下:
package main
import (
"crypto/hmac"
"crypto/sha1"
"fmt"
"net/url"
"sort"
)
func CheckXiaoMiPay(urlInfo url.Values) bool {
mapKeyInfo := map[string]string{}
var slKeys sort.StringSlice
for key, _ := range urlInfo {
if key == `signature` {
continue
}
mapKeyInfo[key] = urlInfo.Get(key)
slKeys = append(slKeys, key)
}
//check success
slKeys.Sort()
var sText string
for _, key := range slKeys {
sText = sText + key + `=` + mapKeyInfo[key] + `&`
}
if len(sText) != 0 {
sText = sText[:len(sText)-1]
}
sEncrypt := getEncryptKey(sText, "QFehjb0X8qEhbkpi380GTQ=1")
fmt.Println(sEncrypt)
return sEncrypt == urlInfo.Get(`signature`)
}
func getEncryptKey(sText string, appSecret string) string {
mac := hmac.New(sha1.New, []byte(appSecret))
mac.Write([]byte(sText))
slBytes := mac.Sum(nil)
if slBytes == nil {
return ``
}
var strRtn string
for _, aByte := range slBytes {
strRtn = strRtn + fmt.Sprintf("%02x", aByte)
}
return strRtn
}
func main() {
urlData := url.Values{
"appId": []string{"2882303761517239138"},
"cpOrderId": []string{"9786bffc-996d-4553-aa33-f7e92c0b29d5"},
"orderConsumeType": []string{"10"},
"orderId": []string{"21140990160359583390"},
"orderStatus": []string{"TRADE_SUCCESS"},
"payFee": []string{"1"},
"payTime": []string{"2014-09-05%2015:20:27"},
"productCode": []string{"com.demo_1"},
"productCount": []string{"1"},
"productName": []string{"银子1两"},
"uid": []string{"100010"},
"signature": []string{"121f401e6e92fa8d8bb45392b8102891a058c1be"},
}
fmt.Println(CheckXiaoMiPay(urlData))
}
经过测试打印为true,服务器程序只需要复制上面俩个函数,即可对签名进行确认。