api_h5_example_test.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // Copyright 2021 Tencent Inc. All rights reserved.
  2. //
  3. // H5支付
  4. //
  5. // H5支付API
  6. //
  7. // API version: 1.2.3
  8. // Code generated by WechatPay APIv3 Generator based on [OpenAPI Generator](https://openapi-generator.tech); DO NOT EDIT.
  9. package h5_test
  10. import (
  11. "context"
  12. "log"
  13. "time"
  14. "github.com/wechatpay-apiv3/wechatpay-go/core"
  15. "github.com/wechatpay-apiv3/wechatpay-go/core/option"
  16. "github.com/wechatpay-apiv3/wechatpay-go/services/payments/h5"
  17. "github.com/wechatpay-apiv3/wechatpay-go/utils"
  18. )
  19. func ExampleH5ApiService_CloseOrder() {
  20. var (
  21. mchID string = "190000****" // 商户号
  22. mchCertificateSerialNumber string = "3775************************************" // 商户证书序列号
  23. mchAPIv3Key string = "2ab9****************************" // 商户APIv3密钥
  24. )
  25. // 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
  26. mchPrivateKey, err := utils.LoadPrivateKeyWithPath("/path/to/merchant/apiclient_key.pem")
  27. if err != nil {
  28. log.Print("load merchant private key error")
  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. }
  39. svc := h5.H5ApiService{Client: client}
  40. result, err := svc.CloseOrder(ctx,
  41. h5.CloseOrderRequest{
  42. OutTradeNo: core.String("OutTradeNo_example"),
  43. Mchid: core.String("1230000109"),
  44. },
  45. )
  46. if err != nil {
  47. // 处理错误
  48. log.Printf("call CloseOrder err:%s", err)
  49. } else {
  50. // 处理返回结果
  51. log.Printf("status=%d", result.Response.StatusCode)
  52. }
  53. }
  54. func ExampleH5ApiService_Prepay() {
  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.Print("load merchant private key error")
  64. }
  65. ctx := context.Background()
  66. // 使用商户私钥等初始化 client,并使它具有自动定时获取微信支付平台证书的能力
  67. opts := []core.ClientOption{
  68. option.WithWechatPayAutoAuthCipher(mchID, mchCertificateSerialNumber, mchPrivateKey, mchAPIv3Key),
  69. }
  70. client, err := core.NewClient(ctx, opts...)
  71. if err != nil {
  72. log.Printf("new wechat pay client err:%s", err)
  73. }
  74. svc := h5.H5ApiService{Client: client}
  75. resp, result, err := svc.Prepay(ctx,
  76. h5.PrepayRequest{
  77. Appid: core.String("wxd678efh567hg6787"),
  78. Mchid: core.String("1230000109"),
  79. Description: core.String("Image形象店-深圳腾大-QQ公仔"),
  80. OutTradeNo: core.String("1217752501201407033233368018"),
  81. TimeExpire: core.Time(time.Now()),
  82. Attach: core.String("自定义数据说明"),
  83. NotifyUrl: core.String("https://www.weixin.qq.com/wxpay/pay.php"),
  84. GoodsTag: core.String("WXG"),
  85. LimitPay: []string{"LimitPay_example"},
  86. SupportFapiao: core.Bool(false),
  87. Amount: &h5.Amount{
  88. Currency: core.String("CNY"),
  89. Total: core.Int64(100),
  90. },
  91. Detail: &h5.Detail{
  92. CostPrice: core.Int64(608800),
  93. GoodsDetail: []h5.GoodsDetail{h5.GoodsDetail{
  94. GoodsName: core.String("iPhoneX 256G"),
  95. MerchantGoodsId: core.String("ABC"),
  96. Quantity: core.Int64(1),
  97. UnitPrice: core.Int64(828800),
  98. WechatpayGoodsId: core.String("1001"),
  99. }},
  100. InvoiceId: core.String("wx123"),
  101. },
  102. SceneInfo: &h5.SceneInfo{
  103. DeviceId: core.String("013467007045764"),
  104. H5Info: &h5.H5Info{
  105. AppName: core.String("王者荣耀"),
  106. AppUrl: core.String("https://pay.qq.com"),
  107. BundleId: core.String("com.tencent.wzryiOS"),
  108. PackageName: core.String("com.tencent.tmgp.sgame"),
  109. Type: core.String("iOS"),
  110. },
  111. PayerClientIp: core.String("14.23.150.211"),
  112. StoreInfo: &h5.StoreInfo{
  113. Address: core.String("广东省深圳市南山区科技中一道10000号"),
  114. AreaCode: core.String("440305"),
  115. Id: core.String("0001"),
  116. Name: core.String("腾讯大厦分店"),
  117. },
  118. },
  119. SettleInfo: &h5.SettleInfo{
  120. ProfitSharing: core.Bool(false),
  121. },
  122. },
  123. )
  124. if err != nil {
  125. // 处理错误
  126. log.Printf("call Prepay err:%s", err)
  127. } else {
  128. // 处理返回结果
  129. log.Printf("status=%d resp=%s", result.Response.StatusCode, resp)
  130. }
  131. }
  132. func ExampleH5ApiService_QueryOrderById() {
  133. var (
  134. mchID string = "190000****" // 商户号
  135. mchCertificateSerialNumber string = "3775************************************" // 商户证书序列号
  136. mchAPIv3Key string = "2ab9****************************" // 商户APIv3密钥
  137. )
  138. // 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
  139. mchPrivateKey, err := utils.LoadPrivateKeyWithPath("/path/to/merchant/apiclient_key.pem")
  140. if err != nil {
  141. log.Print("load merchant private key error")
  142. }
  143. ctx := context.Background()
  144. // 使用商户私钥等初始化 client,并使它具有自动定时获取微信支付平台证书的能力
  145. opts := []core.ClientOption{
  146. option.WithWechatPayAutoAuthCipher(mchID, mchCertificateSerialNumber, mchPrivateKey, mchAPIv3Key),
  147. }
  148. client, err := core.NewClient(ctx, opts...)
  149. if err != nil {
  150. log.Printf("new wechat pay client err:%s", err)
  151. }
  152. svc := h5.H5ApiService{Client: client}
  153. resp, result, err := svc.QueryOrderById(ctx,
  154. h5.QueryOrderByIdRequest{
  155. TransactionId: core.String("TransactionId_example"),
  156. Mchid: core.String("Mchid_example"),
  157. },
  158. )
  159. if err != nil {
  160. // 处理错误
  161. log.Printf("call QueryOrderById err:%s", err)
  162. } else {
  163. // 处理返回结果
  164. log.Printf("status=%d resp=%s", result.Response.StatusCode, resp)
  165. }
  166. }
  167. func ExampleH5ApiService_QueryOrderByOutTradeNo() {
  168. var (
  169. mchID string = "190000****" // 商户号
  170. mchCertificateSerialNumber string = "3775************************************" // 商户证书序列号
  171. mchAPIv3Key string = "2ab9****************************" // 商户APIv3密钥
  172. )
  173. // 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
  174. mchPrivateKey, err := utils.LoadPrivateKeyWithPath("/path/to/merchant/apiclient_key.pem")
  175. if err != nil {
  176. log.Print("load merchant private key error")
  177. }
  178. ctx := context.Background()
  179. // 使用商户私钥等初始化 client,并使它具有自动定时获取微信支付平台证书的能力
  180. opts := []core.ClientOption{
  181. option.WithWechatPayAutoAuthCipher(mchID, mchCertificateSerialNumber, mchPrivateKey, mchAPIv3Key),
  182. }
  183. client, err := core.NewClient(ctx, opts...)
  184. if err != nil {
  185. log.Printf("new wechat pay client err:%s", err)
  186. }
  187. svc := h5.H5ApiService{Client: client}
  188. resp, result, err := svc.QueryOrderByOutTradeNo(ctx,
  189. h5.QueryOrderByOutTradeNoRequest{
  190. OutTradeNo: core.String("OutTradeNo_example"),
  191. Mchid: core.String("Mchid_example"),
  192. },
  193. )
  194. if err != nil {
  195. // 处理错误
  196. log.Printf("call QueryOrderByOutTradeNo err:%s", err)
  197. } else {
  198. // 处理返回结果
  199. log.Printf("status=%d resp=%s", result.Response.StatusCode, resp)
  200. }
  201. }