-
Notifications
You must be signed in to change notification settings - Fork 171
/
application.go
332 lines (281 loc) · 8.35 KB
/
application.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package payment
import (
"errors"
"fmt"
"github.com/ArtisanCloud/PowerLibs/v3/logger"
"github.com/ArtisanCloud/PowerLibs/v3/object"
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel"
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel/models"
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel/providers"
"github.com/ArtisanCloud/PowerWeChat/v3/src/payment/base"
"github.com/ArtisanCloud/PowerWeChat/v3/src/payment/bill"
"github.com/ArtisanCloud/PowerWeChat/v3/src/payment/jssdk"
kernel2 "github.com/ArtisanCloud/PowerWeChat/v3/src/payment/kernel"
"github.com/ArtisanCloud/PowerWeChat/v3/src/payment/notify"
"github.com/ArtisanCloud/PowerWeChat/v3/src/payment/notify/request"
"github.com/ArtisanCloud/PowerWeChat/v3/src/payment/order"
"github.com/ArtisanCloud/PowerWeChat/v3/src/payment/profitSharing"
"github.com/ArtisanCloud/PowerWeChat/v3/src/payment/redpack"
"github.com/ArtisanCloud/PowerWeChat/v3/src/payment/refund"
"github.com/ArtisanCloud/PowerWeChat/v3/src/payment/reverse"
"github.com/ArtisanCloud/PowerWeChat/v3/src/payment/sandbox"
"github.com/ArtisanCloud/PowerWeChat/v3/src/payment/transfer"
"net/http"
"time"
)
type Payment struct {
kernel2.ApplicationPaymentInterface
*kernel.ServiceContainer
Config *kernel.Config
Order *order.Client
JSSDK *jssdk.Client
Sandbox *sandbox.Client
Refund *refund.Client
Bill *bill.Client
RedPack *redpack.Client
Transfer *transfer.Client
TransferBatch *transfer.BatchClient
Reverse *reverse.Client
ProfitSharing *profitSharing.Client
Base *base.Client
Logger *logger.Logger
}
type UserConfig struct {
AppID string
MchID string
MchApiV3Key string
Key string
CertPath string
KeyPath string
RSAPublicKeyPath string
SerialNo string
Http Http
ResponseType string
Log Log
OAuth OAuth
Cache kernel.CacheInterface
HttpDebug bool
Debug bool
NotifyURL string
Sandbox bool
}
type Log struct {
Level string
File string
ENV string
}
type OAuth struct {
Callback string
Scopes []string
}
type Http struct {
Timeout float64
BaseURI string
}
func NewPayment(config *UserConfig) (*Payment, error) {
var err error
userConfig, err := MapUserConfig(config)
if err != nil {
return nil, err
}
// init an app container
container := &kernel.ServiceContainer{
UserConfig: userConfig,
DefaultConfig: &object.HashMap{
"http": &object.HashMap{
"base_uri": "https://api.mch.weixin.qq.com",
},
},
}
container.GetConfig()
// init app
app := &Payment{
ServiceContainer: container,
}
//-------------- global app config --------------
// global app config
app.Config = providers.RegisterConfigProvider(app)
app.Logger, err = logger.NewLogger("", &object.HashMap{
"env": app.Config.GetString("env", "develop"),
"outputPath": app.Config.GetString("log.file", "./wechat.log"),
"errorPath": app.Config.GetString("log.file", "./wechat.log"),
})
if err != nil {
return nil, err
}
if err != nil {
return nil, err
}
//-------------- register Base --------------
app.Base, err = base.RegisterProvider(app)
if err != nil {
return nil, err
}
//-------------- Order --------------
app.Order, err = order.RegisterProvider(app)
if err != nil {
return nil, err
}
//-------------- JSSDK --------------
app.JSSDK, err = jssdk.RegisterProvider(app)
if err != nil {
return nil, err
}
//-------------- Sandbox --------------
app.Sandbox, err = sandbox.RegisterProvider(app)
if err != nil {
return nil, err
}
//-------------- Refund --------------
app.Refund, err = refund.RegisterProvider(app)
if err != nil {
return nil, err
}
//-------------- Bill --------------
app.Bill, err = bill.RegisterProvider(app)
if err != nil {
return nil, err
}
//-------------- Red Pack --------------
app.RedPack, err = redpack.RegisterProvider(app)
if err != nil {
return nil, err
}
//-------------- Transfer --------------
app.Transfer, app.TransferBatch, err = transfer.RegisterProvider(app)
if err != nil {
return nil, err
}
//-------------- Reverse --------------
app.Reverse, err = reverse.RegisterProvider(app)
if err != nil {
return nil, err
}
//-------------- ProfitSharing --------------
app.ProfitSharing, err = profitSharing.RegisterProvider(app)
if err != nil {
return nil, err
}
return app, err
}
func (app *Payment) GetContainer() *kernel.ServiceContainer {
return app.ServiceContainer
}
func (app *Payment) GetAccessToken() *kernel.AccessToken {
//return app.AccessToken.AccessToken
return nil
}
func (app *Payment) GetConfig() *kernel.Config {
return app.Config
}
func (app *Payment) GetComponent(name string) interface{} {
switch name {
case "Base":
return app.Base
case "JSSDK":
return app.JSSDK
case "Sandbox":
return app.Sandbox
case "Config":
return app.Config
case "Order":
return app.Order
case "Refund":
return app.Refund
case "Bill":
return app.Bill
case "RedPack":
return app.RedPack
case "Transfer":
return app.Transfer
case "Reverse":
return app.Reverse
case "ProfitSharing":
return app.ProfitSharing
case "Logger":
return app.Logger
default:
return nil
}
}
func (app *Payment) Scheme(productID string) string {
appID := app.Config.GetString("app_id", "")
mchID := app.Config.GetString("mch_id", "")
//key := app.Config.GetString("key", "")
params := &object.StringMap{
"appid": appID,
"mch_id": mchID,
"time_stamp": fmt.Sprintf("%d", time.Now().Nanosecond()),
"nonce_str": object.UniqueID(""),
"product_id": productID,
}
//var err error
//(*params)["sign"], err = support.GenerateSign(params, key, "md5")
return "weixin://wxpay/bizpayurl?" + object.ConvertStringMapToString(params, "&")
}
func (app *Payment) CodeUrlScheme(codeUrl string) string {
return fmt.Sprintf("weixin://wxpay/bizpayurl?sr=%s", codeUrl)
}
func (app *Payment) SetSubMerchant(mchID string, appID string) kernel2.ApplicationPaymentInterface {
app.Config.Set("sub_mch_id", mchID)
app.Config.Set("sub_appid", appID)
return app
}
func (app *Payment) HandlePaidNotify(request *http.Request, closure func(message *request.RequestNotify, transaction *models.Transaction, fail func(message string)) interface{}) (*http.Response, error) {
return notify.NewPaidNotify(app, request).Handle(closure)
}
func (app *Payment) HandleRefundedNotify(request *http.Request, closure func(message *request.RequestNotify, transaction *models.Refund, fail func(message string)) interface{}) (*http.Response, error) {
return notify.NewRefundNotify(app, request).Handle(closure)
}
func (app *Payment) HandleScannedNotify(request *http.Request, closure func(message *request.RequestNotify, fail func(message string), alert func(message string)) interface{}) (*http.Response, error) {
return notify.NewScannedNotify(app, request).Handle(closure)
}
func (app *Payment) InSandbox() bool {
return app.Config.GetBool("sandbox", false)
}
func (app *Payment) GetKey(endpoint string) (string, error) {
if "sandboxnew/pay/getsignkey" == endpoint {
return app.Config.GetString("key", ""), nil
}
key := app.Config.GetString("key", "")
if app.InSandbox() {
key, _ = app.Sandbox.GetKey()
}
if key == "" {
return key, errors.New("config key should not empty. ")
}
if len(key) != 32 {
return key, errors.New(fmt.Sprintf("'%s' should be 32 chars length.", key))
}
return key, nil
}
func MapUserConfig(userConfig *UserConfig) (*object.HashMap, error) {
config := &object.HashMap{
"app_id": userConfig.AppID,
"mch_id": userConfig.MchID,
"mch_api_v3_key": userConfig.MchApiV3Key,
"key": userConfig.Key,
"cert_path": userConfig.CertPath,
"key_path": userConfig.KeyPath,
"rsa_public_key_path": userConfig.RSAPublicKeyPath,
"serial_no": userConfig.SerialNo,
"response_type": userConfig.ResponseType,
"log": &object.HashMap{
"level": userConfig.Log.Level,
"file": userConfig.Log.File,
"env": userConfig.Log.ENV,
},
"http": &object.HashMap{
"timeout": userConfig.Http.Timeout,
"base_uri": userConfig.Http.BaseURI,
},
"oauth.callbacks": userConfig.OAuth.Callback,
"oauth.scopes": userConfig.OAuth.Scopes,
"notify_url": userConfig.NotifyURL,
"cache": userConfig.Cache,
"http_debug": userConfig.HttpDebug,
"debug": userConfig.Debug,
"sandbox": userConfig.Sandbox,
}
return config, nil
}