forked from go-pay/gopay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
red.go
186 lines (167 loc) · 5.68 KB
/
red.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
/*
微信现金红包
文档:https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_1
*/
package wechat
import (
"encoding/xml"
"fmt"
"github.com/iGoogle-ink/gotil"
"github.com/ideadawn/gopay"
)
// SendCashRed 发放现金红包
// 注意:此处参数中的 wxappid 需要单独传参,不复用 NewClient 时的 appid,total_num = 1
// 微信文档:https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_4&index=3
func (w *Client) SendCashRed(bm gopay.BodyMap, certFilePath, keyFilePath, pkcs12FilePath interface{}) (wxRsp *SendCashRedResponse, err error) {
if err = checkCertFilePath(certFilePath, keyFilePath, pkcs12FilePath); err != nil {
return nil, err
}
err = bm.CheckEmptyError("nonce_str", "mch_billno", "wxappid", "send_name", "re_openid", "total_amount", "total_num", "wishing", "client_ip", "act_name", "remark")
if err != nil {
return nil, err
}
func() {
w.mu.RLock()
defer w.mu.RUnlock()
if bm.Get("wxappid") == gotil.NULL {
bm.Set("wxappid", w.AppId)
}
if bm.Get("mch_id") == gotil.NULL {
bm.Set("mch_id", w.MchId)
}
if bm.Get("sign") == gotil.NULL {
sign := getReleaseSign(w.ApiKey, SignType_MD5, bm)
bm.Set("sign", sign)
}
}()
tlsConfig, err := w.addCertConfig(certFilePath, keyFilePath, pkcs12FilePath)
if err != nil {
return nil, err
}
bs, err := w.doProdPostPure(bm, sendCashRed, tlsConfig)
if err != nil {
return nil, err
}
wxRsp = new(SendCashRedResponse)
if err = xml.Unmarshal(bs, wxRsp); err != nil {
return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err)
}
return wxRsp, nil
}
// SendGroupCashRed 发放现金裂变红包
// 注意:此处参数中的 wxappid 需要单独传参,不复用 NewClient 时的 appid,amt_type = ALL_RAND
// 微信文档:https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_5&index=4
func (w *Client) SendGroupCashRed(bm gopay.BodyMap, certFilePath, keyFilePath, pkcs12FilePath interface{}) (wxRsp *SendCashRedResponse, err error) {
if err = checkCertFilePath(certFilePath, keyFilePath, pkcs12FilePath); err != nil {
return nil, err
}
err = bm.CheckEmptyError("nonce_str", "mch_billno", "wxappid", "send_name", "re_openid", "total_amount", "total_num", "amt_type", "wishing", "act_name", "remark")
if err != nil {
return nil, err
}
func() {
w.mu.RLock()
defer w.mu.RUnlock()
if bm.Get("wxappid") == gotil.NULL {
bm.Set("wxappid", w.AppId)
}
if bm.Get("mch_id") == gotil.NULL {
bm.Set("mch_id", w.MchId)
}
if bm.Get("sign") == gotil.NULL {
sign := getReleaseSign(w.ApiKey, SignType_MD5, bm)
bm.Set("sign", sign)
}
}()
tlsConfig, err := w.addCertConfig(certFilePath, keyFilePath, pkcs12FilePath)
if err != nil {
return nil, err
}
bs, err := w.doProdPostPure(bm, sendGroupCashRed, tlsConfig)
if err != nil {
return nil, err
}
wxRsp = new(SendCashRedResponse)
if err = xml.Unmarshal(bs, wxRsp); err != nil {
return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err)
}
return wxRsp, nil
}
// SendAppletRed 发放小程序红包
// 注意:此处参数中的 wxappid 需要单独传参,不复用 NewClient 时的 appid,total_num = 1,notify_way = MINI_PROGRAM_JSAPI
// 微信文档:https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=18_2&index=3
func (w *Client) SendAppletRed(bm gopay.BodyMap, certFilePath, keyFilePath, pkcs12FilePath interface{}) (wxRsp *SendAppletRedResponse, err error) {
if err = checkCertFilePath(certFilePath, keyFilePath, pkcs12FilePath); err != nil {
return nil, err
}
err = bm.CheckEmptyError("nonce_str", "mch_billno", "wxappid", "send_name", "re_openid", "total_amount", "total_num", "wishing", "act_name", "remark", "notify_way")
if err != nil {
return nil, err
}
func() {
w.mu.RLock()
defer w.mu.RUnlock()
if bm.Get("wxappid") == gotil.NULL {
bm.Set("wxappid", w.AppId)
}
if bm.Get("mch_id") == gotil.NULL {
bm.Set("mch_id", w.MchId)
}
if bm.Get("sign") == gotil.NULL {
sign := getReleaseSign(w.ApiKey, SignType_MD5, bm)
bm.Set("sign", sign)
}
}()
tlsConfig, err := w.addCertConfig(certFilePath, keyFilePath, pkcs12FilePath)
if err != nil {
return nil, err
}
bs, err := w.doProdPostPure(bm, sendAppletRed, tlsConfig)
if err != nil {
return nil, err
}
wxRsp = new(SendAppletRedResponse)
if err = xml.Unmarshal(bs, wxRsp); err != nil {
return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err)
}
return wxRsp, nil
}
// QueryRedRecord 查询红包记录
// 注意:此处参数中的 appid 需要单独传参,不复用 NewClient 时的 appid,bill_type = MCHT
// 微信文档:https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_6&index=5
func (w *Client) QueryRedRecord(bm gopay.BodyMap, certFilePath, keyFilePath, pkcs12FilePath interface{}) (wxRsp *QueryRedRecordResponse, err error) {
if err = checkCertFilePath(certFilePath, keyFilePath, pkcs12FilePath); err != nil {
return nil, err
}
err = bm.CheckEmptyError("nonce_str", "mch_billno", "appid", "bill_type")
if err != nil {
return nil, err
}
func() {
w.mu.RLock()
defer w.mu.RUnlock()
if bm.Get("appid") == gotil.NULL {
bm.Set("appid", w.AppId)
}
if bm.Get("mch_id") == gotil.NULL {
bm.Set("mch_id", w.MchId)
}
if bm.Get("sign") == gotil.NULL {
sign := getReleaseSign(w.ApiKey, SignType_MD5, bm)
bm.Set("sign", sign)
}
}()
tlsConfig, err := w.addCertConfig(certFilePath, keyFilePath, pkcs12FilePath)
if err != nil {
return nil, err
}
bs, err := w.doProdPostPure(bm, getRedRecord, tlsConfig)
if err != nil {
return nil, err
}
wxRsp = new(QueryRedRecordResponse)
if err = xml.Unmarshal(bs, wxRsp); err != nil {
return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err)
}
return wxRsp, nil
}