forked from wechatpay-apiv3/wechatpay-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
option.go
120 lines (94 loc) · 3.68 KB
/
option.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
// Copyright 2021 Tencent Inc. All rights reserved.
package option
import (
"crypto/rsa"
"crypto/x509"
"net/http"
"github.com/rjm521/wechatpay-go/core"
"github.com/rjm521/wechatpay-go/core/auth"
"github.com/rjm521/wechatpay-go/core/auth/signers"
"github.com/rjm521/wechatpay-go/core/auth/validators"
"github.com/rjm521/wechatpay-go/core/auth/verifiers"
"github.com/rjm521/wechatpay-go/core/cipher"
"github.com/rjm521/wechatpay-go/core/cipher/ciphers"
)
// region SignerOption
// withSignerOption 为 Client 设置 Signer
type withSignerOption struct {
Signer auth.Signer
}
// Apply 将配置添加到 core.DialSettings 中
func (w withSignerOption) Apply(o *core.DialSettings) error {
o.Signer = w.Signer
return nil
}
// WithSigner 返回一个指定signer的ClientOption
func WithSigner(signer auth.Signer) core.ClientOption {
return withSignerOption{Signer: signer}
}
// WithMerchantCredential 通过商户号、商户证书序列号、商户私钥构建一对 Credential/Signer,用于生成请求头中的 Authorization 信息
func WithMerchantCredential(mchID, certificateSerialNo string, privateKey *rsa.PrivateKey) core.ClientOption {
signer := &signers.SHA256WithRSASigner{
MchID: mchID,
PrivateKey: privateKey,
CertificateSerialNo: certificateSerialNo,
}
return WithSigner(signer)
}
// endregion
// region ValidatorOption
// withValidatorOption 为 Client 设置 Validator
type withValidatorOption struct {
Validator auth.Validator
}
// Apply 将配置添加到 core.DialSettings 中
func (w withValidatorOption) Apply(o *core.DialSettings) error {
o.Validator = w.Validator
return nil
}
// WithVerifier 返回一个指定verifier的ClientOption,用于校验http response header
func WithVerifier(verifier auth.Verifier) core.ClientOption {
validator := validators.NewWechatPayResponseValidator(verifier)
return withValidatorOption{Validator: validator}
}
// WithWechatPayCertificate 设置微信支付平台证书信息,返回一个指定validator的ClientOption,用于校验http response header
func WithWechatPayCertificate(certificateList []*x509.Certificate) core.ClientOption {
verifier := verifiers.NewSHA256WithRSAVerifier(core.NewCertificateMapWithList(certificateList))
return WithVerifier(verifier)
}
// WithoutValidator 返回一个指定validator的ClientOption,不进行验签 用于下载证书和下载账单等不需要进行验签的接口
func WithoutValidator() core.ClientOption {
return withValidatorOption{Validator: &validators.NullValidator{}}
}
// endregion
// region HTTPClientOption
// withHTTPClientOption 为 Client 设置 HTTPClient
type withHTTPClientOption struct {
Client *http.Client
}
// Apply 将配置添加到 core.DialSettings 中
func (w withHTTPClientOption) Apply(o *core.DialSettings) error {
o.HTTPClient = w.Client
return nil
}
// WithHTTPClient 返回一个指定网络通信为HttpClient的ClientOption,指定后使用用户自动创建的的http.client,如果用户不创建,则帮助用户
// 创建一个默认的http.client
func WithHTTPClient(client *http.Client) core.ClientOption {
return withHTTPClientOption{Client: client}
}
// endregion
// region CipherOption
// withCipherOption 为 Client 设置 Cipher
type withCipherOption struct {
Cipher cipher.Cipher
}
// Apply 将配置添加到 core.DialSettings 中
func (w withCipherOption) Apply(o *core.DialSettings) error {
o.Cipher = w.Cipher
return nil
}
// WithWechatPayCipher 返回一个为 Client 设置 WechatPayCipher 的 core.ClientOption
func WithWechatPayCipher(encryptor cipher.Encryptor, decryptor cipher.Decryptor) core.ClientOption {
return withCipherOption{Cipher: ciphers.NewWechatPayCipher(encryptor, decryptor)}
}
// endregion