-
Notifications
You must be signed in to change notification settings - Fork 173
/
application.go
416 lines (356 loc) · 10.7 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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
package payment
import (
"errors"
"fmt"
"github.com/ArtisanCloud/PowerLibs/v3/logger"
"github.com/ArtisanCloud/PowerLibs/v3/logger/contract"
"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/apply4Sub"
"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/merchant"
"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/partner"
"github.com/ArtisanCloud/PowerWeChat/v3/src/payment/payScore"
"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/security"
"github.com/ArtisanCloud/PowerWeChat/v3/src/payment/tax"
"github.com/ArtisanCloud/PowerWeChat/v3/src/payment/transfer"
"net/http"
"time"
)
type Payment struct {
kernel2.ApplicationPaymentInterface
*kernel.ServiceContainer
Config *kernel.Config
Order *order.Client
Partner *partner.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
Apply4Sub *apply4Sub.Client
Merchant *merchant.Client
PayScore *payScore.Client
Tax *tax.Client
Base *base.Client
Security *security.Client
Logger *logger.Logger
}
type UserConfig struct {
AppID string
MchID string
MchApiV3Key string
Key string
CertPath string
KeyPath string
SerialNo string
CertificateKeyPath string
WechatPaySerial string
RSAPublicKeyPath string
SpAppID string
SpMchID string
SubAppID string
SubMchID string
Http Http
ResponseType string
Log Log
OAuth OAuth
Cache kernel.CacheInterface
HttpDebug bool
Debug bool
NotifyURL string
Sandbox bool
}
type Log struct {
Driver contract.LoggerInterface
Level string
File string
Error 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(app.Config.Get("log.driver", nil), &object.HashMap{
"level": app.Config.GetString("log.level", "info"),
"env": app.Config.GetString("log.env", "develop"),
"outputPath": app.Config.GetString("log.file", "./wechat/info.log"),
"errorPath": app.Config.GetString("log.error", "./wechat/error.log"),
})
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
}
//-------------- Partner --------------
app.Partner, err = partner.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
}
//-------------- Apply4Sub --------------
app.Apply4Sub, err = apply4Sub.RegisterProvider(app)
if err != nil {
return nil, err
}
//-------------- Merchant --------------
app.Merchant, err = merchant.RegisterProvider(app)
if err != nil {
return nil, err
}
//-------------- Pay Score --------------
app.PayScore, err = payScore.RegisterProvider(app)
if err != nil {
return nil, err
}
//-------------- Tax --------------
app.Tax, err = tax.RegisterProvider(app)
if err != nil {
return nil, err
}
//-------------- Security --------------
app.Security, err = security.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 "Partner":
return app.Partner
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 "Apply4Sub":
return app.Apply4Sub
case "Tax":
return app.Tax
case "Merchant":
return app.Merchant
case "PayScore":
return app.PayScore
case "Security":
return app.Security
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) {
baseURI := "https://api.mch.weixin.qq.com"
if userConfig.Http.BaseURI != "" {
baseURI = userConfig.Http.BaseURI
}
timeout := 5.0
if userConfig.Http.Timeout > 0 {
timeout = userConfig.Http.Timeout
}
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,
"certificate_key_path": userConfig.CertificateKeyPath,
"serial_no": userConfig.SerialNo,
"rsa_public_key_path": userConfig.RSAPublicKeyPath,
"wechat_pay_serial": userConfig.WechatPaySerial,
"sub_appid": userConfig.SubAppID,
"sub_mch_id": userConfig.SubMchID,
"response_type": userConfig.ResponseType,
"log": &object.HashMap{
"driver": userConfig.Log.Driver,
"level": userConfig.Log.Level,
"file": userConfig.Log.File,
"error": userConfig.Log.Error,
"env": userConfig.Log.ENV,
},
"http": &object.HashMap{
"timeout": timeout,
"base_uri": 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
}