Skip to content

Commit a6d5373

Browse files
authored
Fix naming convention (#107)
* docs: 修复文档中的API链接,使其与示例代码匹配 * docs: 统一文档中的缩写用法 `cert` --> `certificate` `serialNo` --> `serialNumber` * format: 调整代码/注释格式问题 * ci(staticcheck): 修复最新版 staticcheck 安装失败的问题 * ci(staticcheck): 更新 revive disble 注释的书写位置以符合revivei@v1.0.8
1 parent 4460612 commit a6d5373

11 files changed

Lines changed: 77 additions & 60 deletions

File tree

.github/workflows/go.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ jobs:
3939
go-version: "1.16"
4040
- name: staticcheck
4141
run: |
42-
go get -u honnef.co/go/tools/cmd/staticcheck@latest &&
42+
go get -u honnef.co/go/tools/cmd/staticcheck@v0.2.2 &&
4343
$HOME/go/bin/staticcheck ./...
4444
- name: Revive Action
4545
uses: morphy2k/revive-action@v2.1.1

FAQ.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ go get -u github.com/wechatpay-apiv3/wechatpay-go/cmd/wechatpay_download_certs
1818
```
1919
然后执行 `wechatpay_download_certs` 即可下载微信支付平台证书到当前目录
2020
```shell
21-
wechatpay_download_certs -m <mchID> -p <mchPrivateKeyPath> -s <mchSerialNo> -k <mchAPIv3Key>
21+
wechatpay_download_certs -m <mchID> -p <mchPrivateKeyPath> -s <mchSerialNumber> -k <mchAPIv3Key>
2222
```
2323
完整参数列表可运行 `wechatpay_download_certs -h` 查看。
2424

@@ -28,7 +28,7 @@ wechatpay_download_certs -m <mchID> -p <mchPrivateKeyPath> -s <mchSerialNo> -k <
2828

2929
```go
3030
// GetCertificate 获取商户的某个平台证书
31-
func (mgr *CertificateDownloaderMgr) GetCertificate(ctx context.Context, mchID, serialNo string) (*x509.Certificate, bool)
31+
func (mgr *CertificateDownloaderMgr) GetCertificate(ctx context.Context, mchID, serialNumber string) (*x509.Certificate, bool)
3232

3333
// GetCertificateVisitor 获取某个商户的平台证书访问器
3434
func (mgr *CertificateDownloaderMgr) GetCertificateVisitor(mchID string) core.CertificateVisitor

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func main() {
9797

9898
## 更多示例
9999

100-
### [JSAPI下单](https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter4_1_1.shtml) 为例
100+
### [JSAPI下单](https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_1.shtml) 为例
101101

102102
```go
103103
import (
@@ -131,7 +131,7 @@ if err == nil {
131131
}
132132
```
133133

134-
### [查询订单](https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter4_1_2.shtml) 为例
134+
### [查询订单](https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_2.shtml) 为例
135135

136136
```go
137137
import (
@@ -297,9 +297,9 @@ ctx := context.Background()
297297
// 1. 使用 `RegisterDownloaderWithPrivateKey` 注册下载器
298298
err := downloader.MgrInstance().RegisterDownloaderWithPrivateKey(ctx, mchPrivateKey, mchCertificateSerialNumber, mchID, mchAPIV3Key)
299299
// 2. 获取商户号对应的微信支付平台证书访问器
300-
certVisitor := downloader.MgrInstance().GetCertificateVisitor(mchID)
300+
certificateVisitor := downloader.MgrInstance().GetCertificateVisitor(mchID)
301301
// 3. 使用证书访问器初始化 `notify.Handler`
302-
handler := notify.NewNotifyHandler(mchAPIv3Key, verifiers.NewSHA256WithRSAVerifier(certVisitor))
302+
handler := notify.NewNotifyHandler(mchAPIv3Key, verifiers.NewSHA256WithRSAVerifier(certificateVisitor))
303303
```
304304

305305
+ 方法二:像 [发送请求](#发送请求) 那样使用 `WithWechatPayAutoAuthCipher` 初始化 `core.Client`,然后再用client进行接口调用。
@@ -314,9 +314,9 @@ opts := []core.ClientOption{
314314
}
315315
client, err := core.NewClient(ctx, opts...)
316316
// 2. 获取商户号对应的微信支付平台证书访问器
317-
certVisitor := downloader.MgrInstance().GetCertificateVisitor(mchID)
317+
certificateVisitor := downloader.MgrInstance().GetCertificateVisitor(mchID)
318318
// 3. 使用证书访问器初始化 `notify.Handler`
319-
handler := notify.NewNotifyHandler(mchAPIv3Key, verifiers.NewSHA256WithRSAVerifier(certVisitor))
319+
handler := notify.NewNotifyHandler(mchAPIv3Key, verifiers.NewSHA256WithRSAVerifier(certificateVisitor))
320320
// 4. 使用client进行接口调用
321321
// ...
322322
```
@@ -330,9 +330,9 @@ handler := notify.NewNotifyHandler(mchAPIv3Key, verifiers.NewSHA256WithRSAVerifi
330330
mchAPIv3Key := "<your apiv3 key>"
331331
wechatPayCert, err := utils.LoadCertificate("<your wechat pay certificate>")
332332
// 2. 使用本地管理的微信支付平台证书获取微信支付平台证书访问器
333-
certVisitor := core.NewCertificateMapWithList([]*x509.Certificate{wechatPayCert})
333+
certificateVisitor := core.NewCertificateMapWithList([]*x509.Certificate{wechatPayCert})
334334
// 3. 使用apiv3 key、证书访问器初始化 `notify.Handler`
335-
handler := notify.NewNotifyHandler(mchAPIv3Key, verifiers.NewSHA256WithRSAVerifier(certVisitor))
335+
handler := notify.NewNotifyHandler(mchAPIv3Key, verifiers.NewSHA256WithRSAVerifier(certificateVisitor))
336336
```
337337

338338
建议:为了正确使用平台证书下载管理器,你应阅读并理解 [如何使用平台证书下载管理器](FAQ.md#如何使用平台证书下载管理器)

core/certificate_map.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ func (m *CertificateMap) Reset(newCertificates map[string]*x509.Certificate) {
3636
}
3737

3838
// Get 获取证书序列号对应的平台证书
39-
func (m *CertificateMap) Get(_ context.Context, serialNo string) (*x509.Certificate, bool) {
40-
cert, ok := m.m[serialNo]
39+
func (m *CertificateMap) Get(_ context.Context, serialNumber string) (*x509.Certificate, bool) {
40+
cert, ok := m.m[serialNumber]
4141
return cert, ok
4242
}
4343

core/certificate_visitor.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
// CertificateGetter 平台证书提供器
1111
type CertificateGetter interface {
1212
// Get 获取证书序列号对应的平台证书
13-
Get(ctx context.Context, serialNo string) (*x509.Certificate, bool)
13+
Get(ctx context.Context, serialNumber string) (*x509.Certificate, bool)
1414
// GetAll 获取平台证书Map
1515
GetAll(ctx context.Context) map[string]*x509.Certificate
1616
// GetNewestSerial 获取最新的平台证书的证书序列号
@@ -20,7 +20,7 @@ type CertificateGetter interface {
2020
// CertificateExporter 平台证书导出器,可获取平台证书内容,
2121
type CertificateExporter interface {
2222
// Export 获取证书序列号对应的平台证书内容
23-
Export(ctx context.Context, serialNo string) (string, bool)
23+
Export(ctx context.Context, serialNumber string) (string, bool)
2424
// ExportAll 获取平台证书内容Map
2525
ExportAll(ctx context.Context) map[string]string
2626
}

core/client.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,8 @@ func CreateFormFile(w *multipart.Writer, filename, contentType string, file []by
409409
return err
410410
}
411411

412-
// Set Request body from an interface
412+
//revive:disable-next-line:cyclomatic 本函数实现需要考虑多种情况,但理解起来并不复杂,进行圈复杂度豁免
413+
// setBody Set Request body from an interface
413414
func setBody(body interface{}, contentType string) (bodyBuf *bytes.Buffer, err error) {
414415
bodyBuf = &bytes.Buffer{}
415416

@@ -442,7 +443,7 @@ func setBody(body interface{}, contentType string) (bodyBuf *bytes.Buffer, err e
442443
return bodyBuf, nil
443444
}
444445

445-
// contains is a case insensitive match, finding needle in a haystack
446+
// contains is a case-insensitive match, finding needle in a haystack
446447
func contains(haystack []string, needle string) bool {
447448
for _, a := range haystack {
448449
if strings.EqualFold(a, needle) {

core/client_example_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ func ExampleNewClient_default() {
2424
mchID string
2525
mchCertificateSerialNumber string
2626
mchPrivateKey *rsa.PrivateKey
27-
wechatPayCertList []*x509.Certificate
27+
wechatPayCertificateList []*x509.Certificate
2828
customHTTPClient *http.Client
2929
)
3030

3131
client, err := core.NewClient(
3232
context.Background(),
3333
// 一次性设置 签名/验签/敏感字段加解密,并注册 平台证书下载器,自动定时获取最新的平台证书
34-
option.WithWechatPayAuthCipher(mchID, mchCertificateSerialNumber, mchPrivateKey, wechatPayCertList),
34+
option.WithWechatPayAuthCipher(mchID, mchCertificateSerialNumber, mchPrivateKey, wechatPayCertificateList),
3535
// 设置自定义 HTTPClient 实例,不设置时默认使用 http.Client{},并设置超时时间为 30s
3636
option.WithHTTPClient(customHTTPClient),
3737
)

core/downloader/downloader.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ func isSameCertificateMap(l, r map[string]*x509.Certificate) bool {
2828
return false
2929
}
3030

31-
for serialNo := range l {
32-
if _, ok := r[serialNo]; !ok {
31+
for serialNumber := range l {
32+
if _, ok := r[serialNumber]; !ok {
3333
return false
3434
}
3535
}
@@ -47,11 +47,11 @@ type CertificateDownloader struct {
4747
}
4848

4949
// Get 获取证书序列号对应的平台证书
50-
func (d *CertificateDownloader) Get(ctx context.Context, serialNo string) (*x509.Certificate, bool) {
50+
func (d *CertificateDownloader) Get(ctx context.Context, serialNumber string) (*x509.Certificate, bool) {
5151
d.lock.RLock()
5252
defer d.lock.RUnlock()
5353

54-
return d.certificates.Get(ctx, serialNo)
54+
return d.certificates.Get(ctx, serialNumber)
5555
}
5656

5757
// GetAll 获取平台证书Map
@@ -71,11 +71,11 @@ func (d *CertificateDownloader) GetNewestSerial(ctx context.Context) string {
7171
}
7272

7373
// Export 获取证书序列号对应的平台证书内容
74-
func (d *CertificateDownloader) Export(_ context.Context, serialNo string) (string, bool) {
74+
func (d *CertificateDownloader) Export(_ context.Context, serialNumber string) (string, bool) {
7575
d.lock.RLock()
7676
defer d.lock.RUnlock()
7777

78-
content, ok := d.certContents[serialNo]
78+
content, ok := d.certContents[serialNumber]
7979
return content, ok
8080
}
8181

@@ -85,8 +85,8 @@ func (d *CertificateDownloader) ExportAll(_ context.Context) map[string]string {
8585
defer d.lock.RUnlock()
8686

8787
ret := make(map[string]string)
88-
for serialNo, content := range d.certContents {
89-
ret[serialNo] = content
88+
for serialNumber, content := range d.certContents {
89+
ret[serialNumber] = content
9090
}
9191

9292
return ret
@@ -156,10 +156,10 @@ func (d *CertificateDownloader) DownloadCertificates(ctx context.Context) error
156156
return fmt.Errorf("parse downlaoded certificate failed: %v, certcontent:%v", err, certContent)
157157
}
158158

159-
serialNo := *rawCertificate.SerialNo
159+
serialNumber := *rawCertificate.SerialNo
160160

161-
rawCertContentMap[serialNo] = certContent
162-
certificateMap[serialNo] = certificate
161+
rawCertContentMap[serialNumber] = certContent
162+
certificateMap[serialNumber] = certificate
163163
}
164164

165165
if len(certificateMap) == 0 {

core/downloader/downloader_mgr.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ func (d *pseudoCertificateDownloader) GetAll(ctx context.Context) map[string]*x5
2929
}
3030

3131
// Get 获取证书序列号对应的平台证书
32-
func (d *pseudoCertificateDownloader) Get(ctx context.Context, serialNo string) (*x509.Certificate, bool) {
33-
return d.mgr.GetCertificate(ctx, d.mchID, serialNo)
32+
func (d *pseudoCertificateDownloader) Get(ctx context.Context, serialNumber string) (*x509.Certificate, bool) {
33+
return d.mgr.GetCertificate(ctx, d.mchID, serialNumber)
3434
}
3535

3636
// GetNewestSerial 获取最新的平台证书的证书序列号
@@ -44,8 +44,8 @@ func (d *pseudoCertificateDownloader) ExportAll(ctx context.Context) map[string]
4444
}
4545

4646
// Export 获取证书序列号对应的平台证书内容
47-
func (d *pseudoCertificateDownloader) Export(ctx context.Context, serialNo string) (string, bool) {
48-
return d.mgr.ExportCertificate(ctx, d.mchID, serialNo)
47+
func (d *pseudoCertificateDownloader) Export(ctx context.Context, serialNumber string) (string, bool) {
48+
return d.mgr.ExportCertificate(ctx, d.mchID, serialNumber)
4949
}
5050

5151
// CertificateDownloaderMgr 证书下载器管理器
@@ -70,7 +70,7 @@ func (mgr *CertificateDownloaderMgr) Stop() {
7070
}
7171

7272
// GetCertificate 获取商户的某个平台证书
73-
func (mgr *CertificateDownloaderMgr) GetCertificate(ctx context.Context, mchID, serialNo string) (
73+
func (mgr *CertificateDownloaderMgr) GetCertificate(ctx context.Context, mchID, serialNumber string) (
7474
*x509.Certificate, bool,
7575
) {
7676
mgr.lock.RLock()
@@ -81,7 +81,7 @@ func (mgr *CertificateDownloaderMgr) GetCertificate(ctx context.Context, mchID,
8181
return nil, false
8282
}
8383

84-
return downloader.Get(ctx, serialNo)
84+
return downloader.Get(ctx, serialNumber)
8585
}
8686

8787
// GetCertificateMap 获取商户的平台证书Map
@@ -109,7 +109,7 @@ func (mgr *CertificateDownloaderMgr) GetNewestCertificateSerial(ctx context.Cont
109109
}
110110

111111
// ExportCertificate 获取商户的某个平台证书内容
112-
func (mgr *CertificateDownloaderMgr) ExportCertificate(ctx context.Context, mchID, serialNo string) (string, bool) {
112+
func (mgr *CertificateDownloaderMgr) ExportCertificate(ctx context.Context, mchID, serialNumber string) (string, bool) {
113113
mgr.lock.RLock()
114114
downloader, ok := mgr.downloaderMap[mchID]
115115
mgr.lock.RUnlock()
@@ -118,7 +118,7 @@ func (mgr *CertificateDownloaderMgr) ExportCertificate(ctx context.Context, mchI
118118
return "", false
119119
}
120120

121-
return downloader.Export(ctx, serialNo)
121+
return downloader.Export(ctx, serialNumber)
122122
}
123123

124124
// ExportCertificateMap 导出商户的平台证书内容Map

core/downloader/example_test.go

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,22 @@ func ExampleNewCertificateDownloader_saveCert() {
1717
ctx := context.Background()
1818

1919
var (
20-
mchID string
21-
mchCertSerialNo string
22-
mchPrivateKey *rsa.PrivateKey
23-
mchAPIv3Key string
20+
mchID string
21+
mchCertificateSerialNumber string
22+
mchPrivateKey *rsa.PrivateKey
23+
mchAPIv3Key string
2424
)
2525
// 假设以上参数已初始化完成
2626

27-
d, err := downloader.NewCertificateDownloader(ctx, mchID, mchPrivateKey, mchCertSerialNo, mchAPIv3Key)
27+
d, err := downloader.NewCertificateDownloader(ctx, mchID, mchPrivateKey, mchCertificateSerialNumber, mchAPIv3Key)
2828
if err != nil {
2929
fmt.Println(err)
3030
return
3131
}
3232

33-
for serialNo, certContent := range d.ExportAll(ctx) {
34-
// 将 certContent 写入文件 *.pem
35-
_, _ = serialNo, certContent
33+
for serialNumber, certificateContent := range d.ExportAll(ctx) {
34+
// 将 certificateContent 写入文件 *.pem
35+
_, _ = serialNumber, certificateContent
3636
}
3737
}
3838

@@ -42,43 +42,45 @@ func ExampleNewCertificateDownloaderMgr() {
4242
// CertificateDownloaderMgr 初始化完成,尚未注册任何 Downloader,不会进行任何证书下载
4343

4444
var (
45-
mchID string
46-
mchCertSerialNo string
47-
mchPrivateKey *rsa.PrivateKey
48-
mchAPIv3Key string
45+
mchID string
46+
mchCertificateSerialNumber string
47+
mchPrivateKey *rsa.PrivateKey
48+
mchAPIv3Key string
4949
)
5050
// 假设以上参数已初始化完成
5151

5252
// 注册证书下载器
5353
if err := mgr.RegisterDownloaderWithPrivateKey(
54-
ctx, mchPrivateKey, mchCertSerialNo, mchID, mchAPIv3Key,
54+
ctx, mchPrivateKey, mchCertificateSerialNumber, mchID, mchAPIv3Key,
5555
); err == nil {
5656
fmt.Println(err)
5757
return
5858
}
5959
// 可以注册多个商户的证书下载器...
6060

6161
// 获取证书访问器
62-
certVisitor := mgr.GetCertificateVisitor(mchID)
62+
certificateVisitor := mgr.GetCertificateVisitor(mchID)
6363

64-
// 使用 certVisitor 初始化 Validator 进行验签
65-
option.WithVerifier(verifiers.NewSHA256WithRSAVerifier(certVisitor))
64+
// 使用 certificateVisitor 初始化 Validator 进行验签
65+
option.WithVerifier(verifiers.NewSHA256WithRSAVerifier(certificateVisitor))
6666
}
6767

6868
func ExampleNewCertificateDownloaderMgr_useMgr() {
69-
var certDownloadMgr *downloader.CertificateDownloaderMgr
70-
// certDownloadMgr 已经初始化完成且注册了需要的 Downloader
69+
var certificateDownloaderMgr *downloader.CertificateDownloaderMgr
70+
// certificateDownloaderMgr 已经初始化完成且注册了需要的 Downloader
7171

7272
var (
73-
mchID string
74-
mchCertSerialNo string
75-
mchPrivateKey *rsa.PrivateKey
73+
mchID string
74+
mchCertificateSerialNumber string
75+
mchPrivateKey *rsa.PrivateKey
7676
)
7777

7878
ctx := context.Background()
7979
client, err := core.NewClient(
8080
ctx,
81-
option.WithWechatPayAutoAuthCipherUsingDownloaderMgr(mchID, mchCertSerialNo, mchPrivateKey, certDownloadMgr),
81+
option.WithWechatPayAutoAuthCipherUsingDownloaderMgr(
82+
mchID, mchCertificateSerialNumber, mchPrivateKey, certificateDownloaderMgr,
83+
),
8284
)
8385

8486
if err != nil {

0 commit comments

Comments
 (0)