api_transfer_batch_example_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright 2021 Tencent Inc. All rights reserved.
  2. //
  3. // 商家转账对外API
  4. //
  5. // * 场景及业务流程: 商户可通过该产品实现同时向多个用户微信零钱进行转账的操作,可用于发放奖金补贴、佣金货款结算、员工报销等场景。 [https://pay.weixin.qq.com/index.php/public/product/detail?pid=108&productType=0](https://pay.weixin.qq.com/index.php/public/product/detail?pid=108&productType=0) * 接入步骤: * 商户在微信支付商户平台开通“批量转账到零钱”产品权限,并勾选“使用API方式发起转账”。 * 调用批量转账接口,对多个用户微信零钱发起转账。 * 调用查询批次接口,可获取到转账批次详情及当前状态。 * 调用查询明细接口,可获取到单条转账明细详情及当前状态。
  6. //
  7. // API version: 1.0.5
  8. // Code generated by WechatPay APIv3 Generator based on [OpenAPI Generator](https://openapi-generator.tech); DO NOT EDIT.
  9. package transferbatch_test
  10. import (
  11. "context"
  12. "log"
  13. "github.com/wechatpay-apiv3/wechatpay-go/core"
  14. "github.com/wechatpay-apiv3/wechatpay-go/core/option"
  15. "github.com/wechatpay-apiv3/wechatpay-go/services/transferbatch"
  16. "github.com/wechatpay-apiv3/wechatpay-go/utils"
  17. )
  18. func ExampleTransferBatchApiService_GetTransferBatchByNo() {
  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 := transferbatch.TransferBatchApiService{Client: client}
  41. resp, result, err := svc.GetTransferBatchByNo(ctx,
  42. transferbatch.GetTransferBatchByNoRequest{
  43. BatchId: core.String("1030000071100999991182020050700019480001"),
  44. NeedQueryDetail: core.Bool(true),
  45. Offset: core.Int64(0),
  46. Limit: core.Int64(20),
  47. DetailStatus: core.String("FAIL"),
  48. },
  49. )
  50. if err != nil {
  51. // 处理错误
  52. log.Printf("call GetTransferBatchByNo err:%s", err)
  53. } else {
  54. // 处理返回结果
  55. log.Printf("status=%d resp=%s", result.Response.StatusCode, resp)
  56. }
  57. }
  58. func ExampleTransferBatchApiService_GetTransferBatchByOutNo() {
  59. var (
  60. mchID string = "190000****" // 商户号
  61. mchCertificateSerialNumber string = "3775************************************" // 商户证书序列号
  62. mchAPIv3Key string = "2ab9****************************" // 商户APIv3密钥
  63. )
  64. // 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
  65. mchPrivateKey, err := utils.LoadPrivateKeyWithPath("/path/to/merchant/apiclient_key.pem")
  66. if err != nil {
  67. log.Printf("load merchant private key error:%s", err)
  68. return
  69. }
  70. ctx := context.Background()
  71. // 使用商户私钥等初始化 client,并使它具有自动定时获取微信支付平台证书的能力
  72. opts := []core.ClientOption{
  73. option.WithWechatPayAutoAuthCipher(mchID, mchCertificateSerialNumber, mchPrivateKey, mchAPIv3Key),
  74. }
  75. client, err := core.NewClient(ctx, opts...)
  76. if err != nil {
  77. log.Printf("new wechat pay client err:%s", err)
  78. return
  79. }
  80. svc := transferbatch.TransferBatchApiService{Client: client}
  81. resp, result, err := svc.GetTransferBatchByOutNo(ctx,
  82. transferbatch.GetTransferBatchByOutNoRequest{
  83. OutBatchNo: core.String("plfk2020042013"),
  84. NeedQueryDetail: core.Bool(true),
  85. Offset: core.Int64(0),
  86. Limit: core.Int64(20),
  87. DetailStatus: core.String("FAIL"),
  88. },
  89. )
  90. if err != nil {
  91. // 处理错误
  92. log.Printf("call GetTransferBatchByOutNo err:%s", err)
  93. } else {
  94. // 处理返回结果
  95. log.Printf("status=%d resp=%s", result.Response.StatusCode, resp)
  96. }
  97. }
  98. func ExampleTransferBatchApiService_InitiateBatchTransfer() {
  99. var (
  100. mchID string = "190000****" // 商户号
  101. mchCertificateSerialNumber string = "3775************************************" // 商户证书序列号
  102. mchAPIv3Key string = "2ab9****************************" // 商户APIv3密钥
  103. )
  104. // 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
  105. mchPrivateKey, err := utils.LoadPrivateKeyWithPath("/path/to/merchant/apiclient_key.pem")
  106. if err != nil {
  107. log.Printf("load merchant private key error:%s", err)
  108. return
  109. }
  110. ctx := context.Background()
  111. // 使用商户私钥等初始化 client,并使它具有自动定时获取微信支付平台证书的能力
  112. opts := []core.ClientOption{
  113. option.WithWechatPayAutoAuthCipher(mchID, mchCertificateSerialNumber, mchPrivateKey, mchAPIv3Key),
  114. }
  115. client, err := core.NewClient(ctx, opts...)
  116. if err != nil {
  117. log.Printf("new wechat pay client err:%s", err)
  118. return
  119. }
  120. svc := transferbatch.TransferBatchApiService{Client: client}
  121. resp, result, err := svc.InitiateBatchTransfer(ctx,
  122. transferbatch.InitiateBatchTransferRequest{
  123. Appid: core.String("wxf636efh567hg4356"),
  124. OutBatchNo: core.String("plfk2020042013"),
  125. BatchName: core.String("2019年1月深圳分部报销单"),
  126. BatchRemark: core.String("2019年1月深圳分部报销单"),
  127. TotalAmount: core.Int64(4000000),
  128. TotalNum: core.Int64(200),
  129. TransferDetailList: []transferbatch.TransferDetailInput{transferbatch.TransferDetailInput{
  130. OutDetailNo: core.String("x23zy545Bd5436"),
  131. TransferAmount: core.Int64(200000),
  132. TransferRemark: core.String("2020年4月报销"),
  133. Openid: core.String("o-MYE42l80oelYMDE34nYD456Xoy"),
  134. UserName: core.String("757b340b45ebef5467rter35gf464344v3542sdf4t6re4tb4f54ty45t4yyry45"),
  135. }},
  136. TransferSceneId: core.String("1000"),
  137. NotifyUrl: core.String("https://www.weixin.qq.com/wxpay/pay.php"),
  138. },
  139. )
  140. if err != nil {
  141. // 处理错误
  142. log.Printf("call InitiateBatchTransfer err:%s", err)
  143. } else {
  144. // 处理返回结果
  145. log.Printf("status=%d resp=%s", result.Response.StatusCode, resp)
  146. }
  147. }