-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmch_v3_cert.go
62 lines (53 loc) · 1.21 KB
/
mch_v3_cert.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
// Copyright 2022 YBCZ, Inc. All rights reserved.
//
// Use of this source code is governed by a MIT license
// that can be found in the LICENSE file in the root of the source
// tree.
package wx
import (
"crypto/x509"
"time"
)
type PayCert struct {
SerialNo string `json:"serial_no"`
EffectiveTime time.Time `json:"effective_time"`
ExpireTime time.Time `json:"expire_time"`
cert *x509.Certificate
}
type PayCertManager struct {
certs []PayCert
}
func NewPayCerManager() PayCertManager {
pcm := PayCertManager{}
return pcm
}
func (pcm *PayCertManager) GetCert() *x509.Certificate {
for _, cert := range pcm.certs {
if cert.ExpireTime.After(time.Now()) {
return cert.cert
}
}
return nil
}
func (pcm *PayCertManager) GetCertBySerialNo(no string) *x509.Certificate {
for _, cert := range pcm.certs {
if cert.SerialNo == no {
return cert.cert
}
}
return nil
}
func (pcm *PayCertManager) GetSerialNo() string {
for _, cert := range pcm.certs {
if cert.ExpireTime.After(time.Now()) {
return cert.SerialNo
}
}
return ""
}
func (pcm *PayCertManager) IsEmpty() bool {
return len(pcm.certs) == 0
}
func (pcm *PayCertManager) Add(pc PayCert) {
pcm.certs = append(pcm.certs, pc)
}