api_call_back_url_example_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2021 Tencent Inc. All rights reserved.
  2. //
  3. // 微信支付营销系统开放API
  4. //
  5. // 新增立减金api
  6. //
  7. // API version: 3.4.0
  8. // Code generated by WechatPay APIv3 Generator based on [OpenAPI Generator](https://openapi-generator.tech); DO NOT EDIT.
  9. package cashcoupons_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/cashcoupons"
  16. "git.nanodreamtech.com/sg/wechatpay-go/utils"
  17. )
  18. func ExampleCallBackUrlApiService_QueryCallback() {
  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.Printf("load merchant private key error:%s", err)
  28. return
  29. }
  30. ctx := context.Background()
  31. // 使用商户私钥等初始化 client,并使它具有自动定时获取微信支付平台证书的能力
  32. opts := []core.ClientOption{
  33. option.WithWechatPayAutoAuthCipher(mchID, mchCertificateSerialNumber, mchPrivateKey, mchAPIv3Key),
  34. }
  35. client, err := core.NewClient(ctx, opts...)
  36. if err != nil {
  37. log.Printf("new wechat pay client err:%s", err)
  38. return
  39. }
  40. svc := cashcoupons.CallBackUrlApiService{Client: client}
  41. resp, result, err := svc.QueryCallback(ctx,
  42. cashcoupons.QueryCallbackRequest{
  43. Mchid: core.String("9856888"),
  44. },
  45. )
  46. if err != nil {
  47. // 处理错误
  48. log.Printf("call QueryCallback err:%s", err)
  49. } else {
  50. // 处理返回结果
  51. log.Printf("status=%d resp=%s", result.Response.StatusCode, resp)
  52. }
  53. }
  54. func ExampleCallBackUrlApiService_SetCallback() {
  55. var (
  56. mchID string = "190000****" // 商户号
  57. mchCertificateSerialNumber string = "3775************************************" // 商户证书序列号
  58. mchAPIv3Key string = "2ab9****************************" // 商户APIv3密钥
  59. )
  60. // 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
  61. mchPrivateKey, err := utils.LoadPrivateKeyWithPath("/path/to/merchant/apiclient_key.pem")
  62. if err != nil {
  63. log.Printf("load merchant private key error:%s", err)
  64. return
  65. }
  66. ctx := context.Background()
  67. // 使用商户私钥等初始化 client,并使它具有自动定时获取微信支付平台证书的能力
  68. opts := []core.ClientOption{
  69. option.WithWechatPayAutoAuthCipher(mchID, mchCertificateSerialNumber, mchPrivateKey, mchAPIv3Key),
  70. }
  71. client, err := core.NewClient(ctx, opts...)
  72. if err != nil {
  73. log.Printf("new wechat pay client err:%s", err)
  74. return
  75. }
  76. svc := cashcoupons.CallBackUrlApiService{Client: client}
  77. resp, result, err := svc.SetCallback(ctx,
  78. cashcoupons.SetCallbackRequest{
  79. Mchid: core.String("9856888"),
  80. NotifyUrl: core.String("https://pay.weixin.qq.com"),
  81. Switch: core.Bool(true),
  82. },
  83. )
  84. if err != nil {
  85. // 处理错误
  86. log.Printf("call SetCallback err:%s", err)
  87. } else {
  88. // 处理返回结果
  89. log.Printf("status=%d resp=%s", result.Response.StatusCode, resp)
  90. }
  91. }