api_refunds_example_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 2021 Tencent Inc. All rights reserved.
  2. //
  3. // 境内普通商户退款API
  4. //
  5. // 境内普通商户退款功能涉及的API文档
  6. //
  7. // API version: 1.1.1
  8. // Code generated by WechatPay APIv3 Generator based on [OpenAPI Generator](https://openapi-generator.tech); DO NOT EDIT.
  9. package refunddomestic_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/refunddomestic"
  16. "git.nanodreamtech.com/sg/wechatpay-go/utils"
  17. )
  18. func ExampleRefundsApiService_Create() {
  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 := refunddomestic.RefundsApiService{Client: client}
  39. resp, result, err := svc.Create(ctx,
  40. refunddomestic.CreateRequest{
  41. SubMchid: core.String("1900000109"),
  42. TransactionId: core.String("1217752501201407033233368018"),
  43. OutTradeNo: core.String("1217752501201407033233368018"),
  44. OutRefundNo: core.String("1217752501201407033233368018"),
  45. Reason: core.String("商品已售完"),
  46. NotifyUrl: core.String("https://weixin.qq.com"),
  47. FundsAccount: refunddomestic.REQFUNDSACCOUNT_AVAILABLE.Ptr(),
  48. Amount: &refunddomestic.AmountReq{
  49. Currency: core.String("CNY"),
  50. From: []refunddomestic.FundsFromItem{refunddomestic.FundsFromItem{
  51. Account: refunddomestic.ACCOUNT_AVAILABLE.Ptr(),
  52. Amount: core.Int64(444),
  53. }},
  54. Refund: core.Int64(888),
  55. Total: core.Int64(888),
  56. },
  57. GoodsDetail: []refunddomestic.GoodsDetail{refunddomestic.GoodsDetail{
  58. GoodsName: core.String("iPhone6s 16G"),
  59. MerchantGoodsId: core.String("1217752501201407033233368018"),
  60. RefundAmount: core.Int64(528800),
  61. RefundQuantity: core.Int64(1),
  62. UnitPrice: core.Int64(528800),
  63. WechatpayGoodsId: core.String("1001"),
  64. }},
  65. },
  66. )
  67. if err != nil {
  68. // 处理错误
  69. log.Printf("call Create err:%s", err)
  70. } else {
  71. // 处理返回结果
  72. log.Printf("status=%d resp=%s", result.Response.StatusCode, resp)
  73. }
  74. }
  75. func ExampleRefundsApiService_QueryByOutRefundNo() {
  76. var (
  77. mchID string = "190000****" // 商户号
  78. mchCertificateSerialNumber string = "3775************************************" // 商户证书序列号
  79. mchAPIv3Key string = "2ab9****************************" // 商户APIv3密钥
  80. )
  81. // 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
  82. mchPrivateKey, err := utils.LoadPrivateKeyWithPath("/path/to/merchant/apiclient_key.pem")
  83. if err != nil {
  84. log.Print("load merchant private key error")
  85. }
  86. ctx := context.Background()
  87. // 使用商户私钥等初始化 client,并使它具有自动定时获取微信支付平台证书的能力
  88. opts := []core.ClientOption{
  89. option.WithWechatPayAutoAuthCipher(mchID, mchCertificateSerialNumber, mchPrivateKey, mchAPIv3Key),
  90. }
  91. client, err := core.NewClient(ctx, opts...)
  92. if err != nil {
  93. log.Printf("new wechat pay client err:%s", err)
  94. }
  95. svc := refunddomestic.RefundsApiService{Client: client}
  96. resp, result, err := svc.QueryByOutRefundNo(ctx,
  97. refunddomestic.QueryByOutRefundNoRequest{
  98. OutRefundNo: core.String("1217752501201407033233368018"),
  99. SubMchid: core.String("1900000109"),
  100. },
  101. )
  102. if err != nil {
  103. // 处理错误
  104. log.Printf("call QueryByOutRefundNo err:%s", err)
  105. } else {
  106. // 处理返回结果
  107. log.Printf("status=%d resp=%s", result.Response.StatusCode, resp)
  108. }
  109. }