api_app_example_test.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. // Copyright 2021 Tencent Inc. All rights reserved.
  2. //
  3. // APP支付
  4. //
  5. // APP支付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 app_test
  10. import (
  11. "context"
  12. "log"
  13. "time"
  14. "git.nanodreamtech.com/sg/wechatpay-go/core"
  15. "git.nanodreamtech.com/sg/wechatpay-go/core/option"
  16. "git.nanodreamtech.com/sg/wechatpay-go/services/payments/app"
  17. "git.nanodreamtech.com/sg/wechatpay-go/utils"
  18. )
  19. func ExampleAppApiService_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 := app.AppApiService{Client: client}
  40. result, err := svc.CloseOrder(ctx,
  41. app.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 ExampleAppApiService_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 := app.AppApiService{Client: client}
  75. resp, result, err := svc.Prepay(ctx,
  76. app.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: &app.Amount{
  88. Currency: core.String("CNY"),
  89. Total: core.Int64(100),
  90. },
  91. Detail: &app.Detail{
  92. CostPrice: core.Int64(608800),
  93. GoodsDetail: []app.GoodsDetail{app.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: &app.SceneInfo{
  103. DeviceId: core.String("013467007045764"),
  104. PayerClientIp: core.String("14.23.150.211"),
  105. StoreInfo: &app.StoreInfo{
  106. Address: core.String("广东省深圳市南山区科技中一道10000号"),
  107. AreaCode: core.String("440305"),
  108. Id: core.String("0001"),
  109. Name: core.String("腾讯大厦分店"),
  110. },
  111. },
  112. SettleInfo: &app.SettleInfo{
  113. ProfitSharing: core.Bool(false),
  114. },
  115. },
  116. )
  117. if err != nil {
  118. // 处理错误
  119. log.Printf("call Prepay err:%s", err)
  120. } else {
  121. // 处理返回结果
  122. log.Printf("status=%d resp=%s", result.Response.StatusCode, resp)
  123. }
  124. }
  125. func ExampleAppApiService_QueryOrderById() {
  126. var (
  127. mchID string = "190000****" // 商户号
  128. mchCertificateSerialNumber string = "3775************************************" // 商户证书序列号
  129. mchAPIv3Key string = "2ab9****************************" // 商户APIv3密钥
  130. )
  131. // 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
  132. mchPrivateKey, err := utils.LoadPrivateKeyWithPath("/path/to/merchant/apiclient_key.pem")
  133. if err != nil {
  134. log.Print("load merchant private key error")
  135. }
  136. ctx := context.Background()
  137. // 使用商户私钥等初始化 client,并使它具有自动定时获取微信支付平台证书的能力
  138. opts := []core.ClientOption{
  139. option.WithWechatPayAutoAuthCipher(mchID, mchCertificateSerialNumber, mchPrivateKey, mchAPIv3Key),
  140. }
  141. client, err := core.NewClient(ctx, opts...)
  142. if err != nil {
  143. log.Printf("new wechat pay client err:%s", err)
  144. }
  145. svc := app.AppApiService{Client: client}
  146. resp, result, err := svc.QueryOrderById(ctx,
  147. app.QueryOrderByIdRequest{
  148. TransactionId: core.String("TransactionId_example"),
  149. Mchid: core.String("Mchid_example"),
  150. },
  151. )
  152. if err != nil {
  153. // 处理错误
  154. log.Printf("call QueryOrderById err:%s", err)
  155. } else {
  156. // 处理返回结果
  157. log.Printf("status=%d resp=%s", result.Response.StatusCode, resp)
  158. }
  159. }
  160. func ExampleAppApiService_QueryOrderByOutTradeNo() {
  161. var (
  162. mchID string = "190000****" // 商户号
  163. mchCertificateSerialNumber string = "3775************************************" // 商户证书序列号
  164. mchAPIv3Key string = "2ab9****************************" // 商户APIv3密钥
  165. )
  166. // 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
  167. mchPrivateKey, err := utils.LoadPrivateKeyWithPath("/path/to/merchant/apiclient_key.pem")
  168. if err != nil {
  169. log.Print("load merchant private key error")
  170. }
  171. ctx := context.Background()
  172. // 使用商户私钥等初始化 client,并使它具有自动定时获取微信支付平台证书的能力
  173. opts := []core.ClientOption{
  174. option.WithWechatPayAutoAuthCipher(mchID, mchCertificateSerialNumber, mchPrivateKey, mchAPIv3Key),
  175. }
  176. client, err := core.NewClient(ctx, opts...)
  177. if err != nil {
  178. log.Printf("new wechat pay client err:%s", err)
  179. }
  180. svc := app.AppApiService{Client: client}
  181. resp, result, err := svc.QueryOrderByOutTradeNo(ctx,
  182. app.QueryOrderByOutTradeNoRequest{
  183. OutTradeNo: core.String("OutTradeNo_example"),
  184. Mchid: core.String("Mchid_example"),
  185. },
  186. )
  187. if err != nil {
  188. // 处理错误
  189. log.Printf("call QueryOrderByOutTradeNo err:%s", err)
  190. } else {
  191. // 处理返回结果
  192. log.Printf("status=%d resp=%s", result.Response.StatusCode, resp)
  193. }
  194. }