1 Star 0 Fork 0

eden-w2w / wechatpay-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
option.go 3.68 KB
一键复制 编辑 原始数据 按行查看 历史
文兄 提交于 2022-01-07 17:45 . change mod name
// Copyright 2021 Tencent Inc. All rights reserved.
package option
import (
"crypto/rsa"
"crypto/x509"
"net/http"
"gitee.com/eden-w2w/wechatpay-go/core"
"gitee.com/eden-w2w/wechatpay-go/core/auth"
"gitee.com/eden-w2w/wechatpay-go/core/auth/signers"
"gitee.com/eden-w2w/wechatpay-go/core/auth/validators"
"gitee.com/eden-w2w/wechatpay-go/core/auth/verifiers"
"gitee.com/eden-w2w/wechatpay-go/core/cipher"
"gitee.com/eden-w2w/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
1
https://gitee.com/eden-w2w/wechatpay-go.git
git@gitee.com:eden-w2w/wechatpay-go.git
eden-w2w
wechatpay-go
wechatpay-go
v0.2.12

搜索帮助