-
Notifications
You must be signed in to change notification settings - Fork 158
/
handler.go
170 lines (139 loc) · 3.76 KB
/
handler.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package notify
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/ArtisanCloud/PowerLibs/v3/object"
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel/support"
base2 "github.com/ArtisanCloud/PowerWeChat/v3/src/payment/base"
"github.com/ArtisanCloud/PowerWeChat/v3/src/payment/kernel"
"github.com/ArtisanCloud/PowerWeChat/v3/src/payment/notify/request"
"io/ioutil"
"net/http"
)
type Handler struct {
App *kernel.ApplicationPaymentInterface
Message *request.RequestNotify
fail string
Attributes *object.StringMap
Check bool
Sign bool
ExternalRequest *http.Request
//Handle func(closure func(message *request.RequestNotify, transaction *models.Transaction, fail func(groupWelcomeTemplate string)) interface{}) *http.Response
}
const SUCCESS = "SUCCESS"
const FAIL = "FAIL"
func NewHandler(app *kernel.ApplicationPaymentInterface, r *http.Request) *Handler {
//-------------- external request --------------
request := &http.Request{}
if r != nil {
request = r
}
return &Handler{
App: app,
Check: true,
Sign: false,
ExternalRequest: request,
}
}
func (handler *Handler) Fail(message string) {
handler.fail = message
}
func (handler *Handler) RespondWith(attributes *object.StringMap, sign bool) *Handler {
handler.Attributes = attributes
handler.Sign = sign
return handler
}
func (handler *Handler) ToResponse() (response *http.Response, err error) {
returnCode := SUCCESS
returnMsg := "成功"
if handler.fail != "" {
returnCode = FAIL
returnMsg = handler.fail
err = errors.New(handler.fail)
}
base := &object.StringMap{
"code": returnCode,
"uniformMessage": returnMsg,
}
attributes := object.MergeStringMap(base, handler.Attributes)
baseClient := (*handler.App).GetComponent("Base").(*base2.Client)
if handler.Sign {
(*attributes)["sign"], err = baseClient.BaseClient.Signer.GenerateSign("")
if err != nil {
return nil, err
}
}
bodyBuffer, _ := json.Marshal(attributes)
rs := &http.Response{
StatusCode: http.StatusOK,
}
rs.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBuffer))
return rs, err
}
func (handler *Handler) GetMessage() (notify *request.RequestNotify, err error) {
if handler.Message != nil {
return handler.Message, nil
}
externalRequest := handler.ExternalRequest
requestBody, err := ioutil.ReadAll(externalRequest.Body)
if err != nil {
return nil, err
}
handler.Message = &request.RequestNotify{}
err = object.JsonDecode(requestBody, handler.Message)
if err != nil {
return nil, err
}
handler.Message.RawRequest = externalRequest
return handler.Message, nil
}
func (handler *Handler) DecryptMessage() (string, error) {
message, err := handler.GetMessage()
if err != nil {
return "", err
}
if message.Resource == nil {
return "", errors.New("uniformMessage doesn't have the key value")
}
config := (*handler.App).GetConfig()
wxKey := config.GetString("mch_api_v3_key", "")
nonce := message.Resource.Nonce
associatedData := message.Resource.AssociatedData
cipherText := message.Resource.Ciphertext
return support.DecryptAES256GCM(
wxKey,
associatedData,
nonce,
cipherText,
)
}
func (handler *Handler) Strict(result interface{}) {
bResult := true
strResult := ""
switch result.(type) {
case bool:
bResult = result.(bool)
strResult = fmt.Sprintf("%t", result)
case string:
strResult = result.(string)
if strResult != "" {
bResult = false
}
default:
return
}
if bResult != true && handler.fail == "" {
handler.Fail(strResult)
}
}
func (handler *Handler) reqInfo() (content string, err error) {
content, err = handler.DecryptMessage()
if err != nil {
return "", err
}
// save the decoded content to message resource
handler.Message.Resource.Plaintext = content
return content, nil
}