api_receivers_example_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2021 Tencent Inc. All rights reserved.
  2. //
  3. // 微信支付分账API
  4. //
  5. // 微信支付分账API
  6. //
  7. // API version: 0.0.3
  8. // Code generated by WechatPay APIv3 Generator based on [OpenAPI Generator](https://openapi-generator.tech); DO NOT EDIT.
  9. package profitsharing_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/profitsharing"
  16. "github.com/wechatpay-apiv3/wechatpay-go/utils"
  17. )
  18. func ExampleReceiversApiService_AddReceiver() {
  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.Print("load merchant private key error")
  28. }
  29. ctx := context.Background()
  30. // 使用商户私钥等初始化 client,并使它具有自动定时获取微信支付平台证书的能力
  31. opts := []core.ClientOption{
  32. option.WithWechatPayAutoAuthCipher(mchID, mchCertificateSerialNumber, mchPrivateKey, mchAPIv3Key),
  33. }
  34. client, err := core.NewClient(ctx, opts...)
  35. if err != nil {
  36. log.Printf("new wechat pay client err:%s", err)
  37. }
  38. svc := profitsharing.ReceiversApiService{Client: client}
  39. resp, result, err := svc.AddReceiver(ctx,
  40. profitsharing.AddReceiverRequest{
  41. Account: core.String("86693852"),
  42. Appid: core.String("wx8888888888888888"),
  43. CustomRelation: core.String("代理商"),
  44. Name: core.String("hu89ohu89ohu89o"),
  45. RelationType: profitsharing.RECEIVERRELATIONTYPE_SERVICE_PROVIDER.Ptr(),
  46. SubAppid: core.String("wx8888888888888889"),
  47. SubMchid: core.String("1900000109"),
  48. Type: profitsharing.RECEIVERTYPE_MERCHANT_ID.Ptr(),
  49. },
  50. )
  51. if err != nil {
  52. // 处理错误
  53. log.Printf("call AddReceiver err:%s", err)
  54. } else {
  55. // 处理返回结果
  56. log.Printf("status=%d resp=%s", result.Response.StatusCode, resp)
  57. }
  58. }
  59. func ExampleReceiversApiService_DeleteReceiver() {
  60. var (
  61. mchID string = "190000****" // 商户号
  62. mchCertificateSerialNumber string = "3775************************************" // 商户证书序列号
  63. mchAPIv3Key string = "2ab9****************************" // 商户APIv3密钥
  64. )
  65. // 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
  66. mchPrivateKey, err := utils.LoadPrivateKeyWithPath("/path/to/merchant/apiclient_key.pem")
  67. if err != nil {
  68. log.Print("load merchant private key error")
  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. }
  79. svc := profitsharing.ReceiversApiService{Client: client}
  80. resp, result, err := svc.DeleteReceiver(ctx,
  81. profitsharing.DeleteReceiverRequest{
  82. Account: core.String("86693852"),
  83. Appid: core.String("wx8888888888888888"),
  84. SubAppid: core.String("wx8888888888888889"),
  85. SubMchid: core.String("1900000109"),
  86. Type: profitsharing.RECEIVERTYPE_MERCHANT_ID.Ptr(),
  87. },
  88. )
  89. if err != nil {
  90. // 处理错误
  91. log.Printf("call DeleteReceiver err:%s", err)
  92. } else {
  93. // 处理返回结果
  94. log.Printf("status=%d resp=%s", result.Response.StatusCode, resp)
  95. }
  96. }