123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- package option
- import (
- "crypto/rsa"
- "crypto/x509"
- "net/http"
- "github.com/wechatpay-apiv3/wechatpay-go/core"
- "github.com/wechatpay-apiv3/wechatpay-go/core/auth"
- "github.com/wechatpay-apiv3/wechatpay-go/core/auth/signers"
- "github.com/wechatpay-apiv3/wechatpay-go/core/auth/validators"
- "github.com/wechatpay-apiv3/wechatpay-go/core/auth/verifiers"
- "github.com/wechatpay-apiv3/wechatpay-go/core/cipher"
- "github.com/wechatpay-apiv3/wechatpay-go/core/cipher/ciphers"
- )
- type withSignerOption struct {
- Signer auth.Signer
- }
- func (w withSignerOption) Apply(o *core.DialSettings) error {
- o.Signer = w.Signer
- return nil
- }
- func WithSigner(signer auth.Signer) core.ClientOption {
- return withSignerOption{Signer: signer}
- }
- func WithMerchantCredential(mchID, certificateSerialNo string, privateKey *rsa.PrivateKey) core.ClientOption {
- signer := &signers.SHA256WithRSASigner{
- MchID: mchID,
- PrivateKey: privateKey,
- CertificateSerialNo: certificateSerialNo,
- }
- return WithSigner(signer)
- }
- type withValidatorOption struct {
- Validator auth.Validator
- }
- func (w withValidatorOption) Apply(o *core.DialSettings) error {
- o.Validator = w.Validator
- return nil
- }
- func WithVerifier(verifier auth.Verifier) core.ClientOption {
- validator := validators.NewWechatPayResponseValidator(verifier)
- return withValidatorOption{Validator: validator}
- }
- func WithWechatPayCertificate(certificateList []*x509.Certificate) core.ClientOption {
- verifier := verifiers.NewSHA256WithRSAVerifier(core.NewCertificateMapWithList(certificateList))
- return WithVerifier(verifier)
- }
- func WithoutValidator() core.ClientOption {
- return withValidatorOption{Validator: &validators.NullValidator{}}
- }
- type withHTTPClientOption struct {
- Client *http.Client
- }
- func (w withHTTPClientOption) Apply(o *core.DialSettings) error {
- o.HTTPClient = w.Client
- return nil
- }
- func WithHTTPClient(client *http.Client) core.ClientOption {
- return withHTTPClientOption{Client: client}
- }
- type withCipherOption struct {
- Cipher cipher.Cipher
- }
- func (w withCipherOption) Apply(o *core.DialSettings) error {
- o.Cipher = w.Cipher
- return nil
- }
- func WithWechatPayCipher(encryptor cipher.Encryptor, decryptor cipher.Decryptor) core.ClientOption {
- return withCipherOption{Cipher: ciphers.NewWechatPayCipher(encryptor, decryptor)}
- }
|