api_certificates_example_test.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright 2021 Tencent Inc. All rights reserved.
  2. //
  3. // 微信支付平台证书下载服务
  4. //
  5. // 为了确保在定期更换平台证书时,不影响商户使用微信支付的各种功能,微信支付提供API接口供商户下载最新的平台证书。 商户可使用该接口实现平台证书的平滑切换。
  6. //
  7. // API version: 1.0.0
  8. // Code generated by WechatPay APIv3 Generator based on [OpenAPI Generator](https://openapi-generator.tech); DO NOT EDIT.
  9. package certificates_test
  10. import (
  11. "context"
  12. "log"
  13. "git.nanodreamtech.com/sg/wechatpay-go/core"
  14. "git.nanodreamtech.com/sg/wechatpay-go/core/option"
  15. "git.nanodreamtech.com/sg/wechatpay-go/services/certificates"
  16. "git.nanodreamtech.com/sg/wechatpay-go/utils"
  17. )
  18. func ExampleCertificatesApiService_DownloadCertificates() {
  19. var (
  20. mchID string = "190000****" // 商户号
  21. mchCertificateSerialNumber string = "3775************************************" // 商户证书序列号
  22. mchAPIv3Key string = "2ab9****************************" // 商户APIv3密钥
  23. )
  24. // 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
  25. mchPrivateKey, err := utils.LoadPrivateKeyWithPath("/path/to/merchant/apiclient_key.pem")
  26. if err != nil {
  27. log.Print("load merchant private key error")
  28. }
  29. ctx := context.Background()
  30. // 使用商户私钥等初始化 client,并使它具有自动定时获取微信支付平台证书的能力
  31. opts := []core.ClientOption{
  32. option.WithWechatPayAutoAuthCipher(mchID, mchCertificateSerialNumber, mchPrivateKey, mchAPIv3Key),
  33. }
  34. client, err := core.NewClient(ctx, opts...)
  35. if err != nil {
  36. log.Printf("new wechat pay client err:%s", err)
  37. }
  38. svc := certificates.CertificatesApiService{Client: client}
  39. resp, result, err := svc.DownloadCertificates(ctx)
  40. if err != nil {
  41. // 处理错误
  42. log.Printf("call DownloadCertificates err:%s", err)
  43. } else {
  44. // 处理返回结果
  45. log.Printf("status=%d resp=%s", result.Response.StatusCode, resp)
  46. }
  47. }