-
Notifications
You must be signed in to change notification settings - Fork 174
/
helper.go
78 lines (72 loc) · 1.86 KB
/
helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package support
import (
"crypto/hmac"
"crypto/md5"
"crypto/sha256"
"encoding/hex"
"fmt"
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel/power"
"sort"
"strings"
)
func PaymentV2ParamsJoinBackup(params *power.HashMap, key string) string {
var arr []string
for k, v := range *params {
if v == "" {
continue
}
arr = append(arr, k)
}
sort.Strings(arr)
//for i, k := range arr {
for i := 0; i < len(arr); i++ {
k := arr[i]
v := (*params)[k]
switch v.(type) {
case string:
arr[i] = fmt.Sprintf("%s=%s", k, v)
break
case int:
case int8:
case int16:
case int32:
case int64:
arr[i] = fmt.Sprintf("%s=%d", k, v)
break
case float32:
case float64:
arr[i] = fmt.Sprintf("%s=%f", k, v)
break
}
//arr[i] = fmt.Sprintf("%s=%x", k, (*params)[k])
}
return fmt.Sprintf("%s&key=%s", strings.Join(arr, "&"), key)
}
func PaymentV2ParamsJoin(params *power.StringMap, key string) string {
var arr []string
for k, v := range *params {
if v == "" {
continue
}
arr = append(arr, k)
}
sort.Strings(arr)
for i, k := range arr {
arr[i] = fmt.Sprintf("%s=%s", k, (*params)[k])
}
return fmt.Sprintf("%s&key=%s", strings.Join(arr, "&"), key)
}
// GenerateSignMD5 适用于微信支付V2 MD5签名算法
// https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=4_3
func GenerateSignMD5(params *power.StringMap, key string) string {
sign := fmt.Sprintf("%x", md5.Sum([]byte(PaymentV2ParamsJoin(params, key))))
return strings.ToUpper(sign)
}
// GenerateSignHmacSHA256 适用于微信支付V2 HMAC-SHA256签名算法
// https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=4_3
func GenerateSignHmacSHA256(params *power.StringMap, key string) string {
h := hmac.New(sha256.New, []byte(key))
h.Write([]byte(PaymentV2ParamsJoin(params, key)))
sign := hex.EncodeToString(h.Sum(nil))
return strings.ToUpper(sign)
}