option.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2021 Tencent Inc. All rights reserved.
  2. package option
  3. import (
  4. "crypto/rsa"
  5. "crypto/x509"
  6. "net/http"
  7. "git.nanodreamtech.com/sg/wechatpay-go/core"
  8. "git.nanodreamtech.com/sg/wechatpay-go/core/auth"
  9. "git.nanodreamtech.com/sg/wechatpay-go/core/auth/signers"
  10. "git.nanodreamtech.com/sg/wechatpay-go/core/auth/validators"
  11. "git.nanodreamtech.com/sg/wechatpay-go/core/auth/verifiers"
  12. "git.nanodreamtech.com/sg/wechatpay-go/core/cipher"
  13. "git.nanodreamtech.com/sg/wechatpay-go/core/cipher/ciphers"
  14. )
  15. // region SignerOption
  16. // withSignerOption 为 Client 设置 Signer
  17. type withSignerOption struct {
  18. Signer auth.Signer
  19. }
  20. // Apply 将配置添加到 core.DialSettings 中
  21. func (w withSignerOption) Apply(o *core.DialSettings) error {
  22. o.Signer = w.Signer
  23. return nil
  24. }
  25. // WithSigner 返回一个指定signer的ClientOption
  26. func WithSigner(signer auth.Signer) core.ClientOption {
  27. return withSignerOption{Signer: signer}
  28. }
  29. // WithMerchantCredential 通过商户号、商户证书序列号、商户私钥构建一对 Credential/Signer,用于生成请求头中的 Authorization 信息
  30. func WithMerchantCredential(mchID, certificateSerialNo string, privateKey *rsa.PrivateKey) core.ClientOption {
  31. signer := &signers.SHA256WithRSASigner{
  32. MchID: mchID,
  33. PrivateKey: privateKey,
  34. CertificateSerialNo: certificateSerialNo,
  35. }
  36. return WithSigner(signer)
  37. }
  38. // endregion
  39. // region ValidatorOption
  40. // withValidatorOption 为 Client 设置 Validator
  41. type withValidatorOption struct {
  42. Validator auth.Validator
  43. }
  44. // Apply 将配置添加到 core.DialSettings 中
  45. func (w withValidatorOption) Apply(o *core.DialSettings) error {
  46. o.Validator = w.Validator
  47. return nil
  48. }
  49. // WithVerifier 返回一个指定verifier的ClientOption,用于校验http response header
  50. func WithVerifier(verifier auth.Verifier) core.ClientOption {
  51. validator := validators.NewWechatPayResponseValidator(verifier)
  52. return withValidatorOption{Validator: validator}
  53. }
  54. // WithWechatPayCertificate 设置微信支付平台证书信息,返回一个指定validator的ClientOption,用于校验http response header
  55. func WithWechatPayCertificate(certificateList []*x509.Certificate) core.ClientOption {
  56. verifier := verifiers.NewSHA256WithRSAVerifier(core.NewCertificateMapWithList(certificateList))
  57. return WithVerifier(verifier)
  58. }
  59. // WithoutValidator 返回一个指定validator的ClientOption,不进行验签 用于下载证书和下载账单等不需要进行验签的接口
  60. func WithoutValidator() core.ClientOption {
  61. return withValidatorOption{Validator: &validators.NullValidator{}}
  62. }
  63. // endregion
  64. // region HTTPClientOption
  65. // withHTTPClientOption 为 Client 设置 HTTPClient
  66. type withHTTPClientOption struct {
  67. Client *http.Client
  68. }
  69. // Apply 将配置添加到 core.DialSettings 中
  70. func (w withHTTPClientOption) Apply(o *core.DialSettings) error {
  71. o.HTTPClient = w.Client
  72. return nil
  73. }
  74. // WithHTTPClient 返回一个指定网络通信为HttpClient的ClientOption,指定后使用用户自动创建的的http.client,如果用户不创建,则帮助用户
  75. // 创建一个默认的http.client
  76. func WithHTTPClient(client *http.Client) core.ClientOption {
  77. return withHTTPClientOption{Client: client}
  78. }
  79. // endregion
  80. // region CipherOption
  81. // withCipherOption 为 Client 设置 Cipher
  82. type withCipherOption struct {
  83. Cipher cipher.Cipher
  84. }
  85. // Apply 将配置添加到 core.DialSettings 中
  86. func (w withCipherOption) Apply(o *core.DialSettings) error {
  87. o.Cipher = w.Cipher
  88. return nil
  89. }
  90. // WithWechatPayCipher 返回一个为 Client 设置 WechatPayCipher 的 core.ClientOption
  91. func WithWechatPayCipher(encryptor cipher.Encryptor, decryptor cipher.Decryptor) core.ClientOption {
  92. return withCipherOption{Cipher: ciphers.NewWechatPayCipher(encryptor, decryptor)}
  93. }
  94. // endregion