api_coupon_example_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 ExampleCouponApiService_ListCouponsByFilter() {
  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.CouponApiService{Client: client}
  41. resp, result, err := svc.ListCouponsByFilter(ctx,
  42. cashcoupons.ListCouponsByFilterRequest{
  43. Openid: core.String("Openid_example"),
  44. Appid: core.String("Appid_example"),
  45. StockId: core.String("9865000"),
  46. Status: core.String("USED"),
  47. CreatorMchid: core.String("9865002"),
  48. SenderMchid: core.String("9865001"),
  49. AvailableMchid: core.String("9865000"),
  50. Offset: core.Int64(0),
  51. Limit: core.Int64(20),
  52. },
  53. )
  54. if err != nil {
  55. // 处理错误
  56. log.Printf("call ListCouponsByFilter err:%s", err)
  57. } else {
  58. // 处理返回结果
  59. log.Printf("status=%d resp=%s", result.Response.StatusCode, resp)
  60. }
  61. }
  62. func ExampleCouponApiService_QueryCoupon() {
  63. var (
  64. mchID string = "190000****" // 商户号
  65. mchCertificateSerialNumber string = "3775************************************" // 商户证书序列号
  66. mchAPIv3Key string = "2ab9****************************" // 商户APIv3密钥
  67. )
  68. // 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
  69. mchPrivateKey, err := utils.LoadPrivateKeyWithPath("/path/to/merchant/apiclient_key.pem")
  70. if err != nil {
  71. log.Printf("load merchant private key error:%s", err)
  72. return
  73. }
  74. ctx := context.Background()
  75. // 使用商户私钥等初始化 client,并使它具有自动定时获取微信支付平台证书的能力
  76. opts := []core.ClientOption{
  77. option.WithWechatPayAutoAuthCipher(mchID, mchCertificateSerialNumber, mchPrivateKey, mchAPIv3Key),
  78. }
  79. client, err := core.NewClient(ctx, opts...)
  80. if err != nil {
  81. log.Printf("new wechat pay client err:%s", err)
  82. return
  83. }
  84. svc := cashcoupons.CouponApiService{Client: client}
  85. resp, result, err := svc.QueryCoupon(ctx,
  86. cashcoupons.QueryCouponRequest{
  87. CouponId: core.String("9856888"),
  88. Appid: core.String("Appid_example"),
  89. Openid: core.String("Openid_example"),
  90. },
  91. )
  92. if err != nil {
  93. // 处理错误
  94. log.Printf("call QueryCoupon err:%s", err)
  95. } else {
  96. // 处理返回结果
  97. log.Printf("status=%d resp=%s", result.Response.StatusCode, resp)
  98. }
  99. }
  100. func ExampleCouponApiService_SendCoupon() {
  101. var (
  102. mchID string = "190000****" // 商户号
  103. mchCertificateSerialNumber string = "3775************************************" // 商户证书序列号
  104. mchAPIv3Key string = "2ab9****************************" // 商户APIv3密钥
  105. )
  106. // 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
  107. mchPrivateKey, err := utils.LoadPrivateKeyWithPath("/path/to/merchant/apiclient_key.pem")
  108. if err != nil {
  109. log.Printf("load merchant private key error:%s", err)
  110. return
  111. }
  112. ctx := context.Background()
  113. // 使用商户私钥等初始化 client,并使它具有自动定时获取微信支付平台证书的能力
  114. opts := []core.ClientOption{
  115. option.WithWechatPayAutoAuthCipher(mchID, mchCertificateSerialNumber, mchPrivateKey, mchAPIv3Key),
  116. }
  117. client, err := core.NewClient(ctx, opts...)
  118. if err != nil {
  119. log.Printf("new wechat pay client err:%s", err)
  120. return
  121. }
  122. svc := cashcoupons.CouponApiService{Client: client}
  123. resp, result, err := svc.SendCoupon(ctx,
  124. cashcoupons.SendCouponRequest{
  125. Openid: core.String("Openid_example"),
  126. StockId: core.String("example_stock_id"),
  127. OutRequestNo: core.String("example_out_request_no"),
  128. Appid: core.String("example_appid"),
  129. StockCreatorMchid: core.String("example_stock_creator_mchid"),
  130. CouponValue: core.Int64(1),
  131. CouponMinimum: core.Int64(1),
  132. },
  133. )
  134. if err != nil {
  135. // 处理错误
  136. log.Printf("call SendCoupon err:%s", err)
  137. } else {
  138. // 处理返回结果
  139. log.Printf("status=%d resp=%s", result.Response.StatusCode, resp)
  140. }
  141. }