-
Notifications
You must be signed in to change notification settings - Fork 174
/
client.go
121 lines (96 loc) · 3.65 KB
/
client.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
package redpack
import (
"context"
"github.com/ArtisanCloud/PowerLibs/v3/object"
payment "github.com/ArtisanCloud/PowerWeChat/v3/src/payment/kernel"
"github.com/ArtisanCloud/PowerWeChat/v3/src/payment/redpack/request"
"github.com/ArtisanCloud/PowerWeChat/v3/src/payment/redpack/response"
"net/http"
)
type Client struct {
*payment.BaseClient
}
func NewClient(app *payment.ApplicationPaymentInterface) (*Client, error) {
baseClient, err := payment.NewBaseClient(app)
if err != nil {
return nil, err
}
return &Client{
baseClient,
}, nil
}
// Query Red Pack.
// https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_6&index=5
func (comp *Client) Info(ctx context.Context, mchBillNO string) (*response.ResponseSendNormal, error) {
config := (*comp.App).GetConfig()
result := &response.ResponseSendNormal{}
params := &object.HashMap{
"mch_billno": mchBillNO,
"appid": config.GetString("app_id", ""),
"mch_id": config.GetString("mch_id", ""),
"bill_type": "MCHT",
"nonce_str": object.QuickRandom(32),
}
endpoint := comp.Wrap("mmpaymkttransfers/gethbinfo")
_, err := comp.SafeRequest(ctx, endpoint, params, http.MethodPost, &object.HashMap{}, nil, result)
return result, err
}
// Send Miniprogram Normal redpack.
// https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=18_2&index=2
func (comp *Client) SendMiniProgramNormal(ctx context.Context, data *request.RequestSendMiniProgramNormal) (*response.ResponseSendMiniProgramNormal, error) {
result := &response.ResponseSendMiniProgramNormal{}
config := (*comp.App).GetConfig()
params, err := object.StructToHashMapWithXML(data)
if err != nil {
return nil, err
}
base := &object.HashMap{
"total_num": config.GetString("mch_id", "1"),
"wxappid": config.GetString("app_id", ""),
"notify_way": "MINI_PROGRAM_JSAPI",
"mch_id": config.GetString("mch_id", ""),
}
params = object.MergeHashMap(params, base)
endpoint := comp.Wrap("/mmpaymkttransfers/sendminiprogramhb")
_, err = comp.SafeRequest(ctx, endpoint, params, http.MethodPost, &object.HashMap{}, nil, result)
return result, err
}
// Send Normal redpack.
// https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_4&index=3
func (comp *Client) SendNormal(ctx context.Context, data *request.RequestSendRedPack) (*response.ResponseSendNormal, error) {
result := &response.ResponseSendNormal{}
config := (*comp.App).GetConfig()
params, err := object.StructToHashMapWithXML(data)
if err != nil {
return nil, err
}
base := &object.HashMap{
"total_num": 1,
"wxappid": config.GetString("app_id", ""),
"mch_id": config.GetString("mch_id", ""),
}
params = object.MergeHashMap(params, base)
endpoint := comp.Wrap("/mmpaymkttransfers/sendredpack")
_, err = comp.SafeRequest(ctx, endpoint, params, http.MethodPost, &object.HashMap{}, nil, result)
return result, err
}
// Send Group redpack.
// https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_5&index=4
func (comp *Client) SendGroup(ctx context.Context, data *request.RequestSendGroupRedPack) (*response.ResponseSendGroupRedPack, error) {
config := (*comp.App).GetConfig()
result := &response.ResponseSendGroupRedPack{}
if data.AmtType == "" {
data.AmtType = "ALL_RAND"
}
if data.WXAppID == "" {
data.WXAppID = config.GetString("app_id", "")
}
if data.MchID == "" {
data.MchID = config.GetString("mch_id", "")
}
//params, err := object.StructToHashMapWithTag(data,"json")
params, err := object.StructToHashMap(data)
endpoint := comp.Wrap("/mmpaymkttransfers/sendgroupredpack")
_, err = comp.SafeRequest(ctx, endpoint, params, http.MethodPost, &object.HashMap{}, nil, result)
return result, err
}