-
Notifications
You must be signed in to change notification settings - Fork 0
/
wechat_pay_encryptor.go
50 lines (39 loc) · 1.49 KB
/
wechat_pay_encryptor.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
// Copyright 2021 Tencent Inc. All rights reserved.
package encryptors
import (
"context"
"fmt"
"github.com/Marek-maxs/wechatpay-go/core"
"github.com/Marek-maxs/wechatpay-go/utils"
)
// WechatPayEncryptor 微信支付字符串加密器
type WechatPayEncryptor struct {
// 微信支付平台证书提供器
certGetter core.CertificateGetter
}
// NewWechatPayEncryptor 新建一个 WechatPayEncryptor
func NewWechatPayEncryptor(certProvider core.CertificateGetter) *WechatPayEncryptor {
return &WechatPayEncryptor{certGetter: certProvider}
}
// SelectCertificate 选择合适的微信支付平台证书用于加密
// 根据微信支付平台证书文档说明,应优先使用最新的证书(即启用时间最晚)
// https://wechatpay-api.gitbook.io/wechatpay-api-v3/jie-kou-wen-dang/ping-tai-zheng-shu#zhu-yi-shi-xiang
func (e *WechatPayEncryptor) SelectCertificate(ctx context.Context) (serial string, err error) {
newestSerial := e.certGetter.GetNewestSerial(ctx)
if newestSerial == "" {
return "", fmt.Errorf("no certificate for encryption")
}
return newestSerial, nil
}
// Encrypt 对字符串加密
func (e *WechatPayEncryptor) Encrypt(ctx context.Context, serial, plaintext string) (ciphertext string, err error) {
cert, ok := e.certGetter.Get(ctx, serial)
if !ok {
return plaintext, fmt.Errorf("cert for EncryptSerial(%v) not found", serial)
}
// 不需要对空串进行加密
if plaintext == "" {
return "", nil
}
return utils.EncryptOAEPWithCertificate(plaintext, cert)
}