-
Notifications
You must be signed in to change notification settings - Fork 8
/
actionCb.go
45 lines (37 loc) · 1.01 KB
/
actionCb.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
package ocpc
import (
"crypto/md5"
"encoding/hex"
"fmt"
"strings"
)
type ActionCbRequest struct {
Akey string `json:"akey,omitempty"`
CallbackUrl string `json:"callbackUrl,omitempty"`
AType string `json:"a_type,omitempty"`
AValue string `json:"a_value,omitempty"`
}
func (r ActionCbRequest) unsignedUrl() string {
link := strings.Replace(r.CallbackUrl, "{{ATYPE}}", r.AType, -1)
link = strings.Replace(link, "{{AVALUE}}", r.AValue, -1)
return link
}
func (r ActionCbRequest) Sign() string {
signUrl := fmt.Sprintf("%s%s", r.unsignedUrl(), r.Akey)
h := md5.New()
h.Write([]byte(signUrl))
return hex.EncodeToString(h.Sum(nil))
}
func (r ActionCbRequest) Url() string {
return fmt.Sprintf("%s&sign=%s", r.unsignedUrl(), r.Sign())
}
type ActionCbResponse struct {
ErrorCode int `json:"error_code,omitempty"`
ErrorMsg string `json:"error_msg,omitempty"`
}
func (r ActionCbResponse) IsError() bool {
return r.ErrorCode != 0
}
func (r ActionCbResponse) Error() string {
return r.ErrorMsg
}