
1package main
2
3import (
4 "demo/wxpay_utility"
5 "encoding/json"
6 "fmt"
7 "net/http"
8 "net/url"
9 "strings"
10 "time"
11)
12
13func main() {
14
15 config, err := wxpay_utility.CreateMchConfig(
16 "19xxxxxxxx",
17 "1DDE55AD98Exxxxxxxxxx",
18 "/path/to/apiclient_key.pem",
19 "PUB_KEY_ID_xxxxxxxxxxxxx",
20 "/path/to/wxp_pub.pem",
21 )
22 if err != nil {
23 fmt.Println(err)
24 return
25 }
26
27 request := &QueryReturnAdvanceRequest{
28 RefundId: wxpay_utility.String("50000000382019052709732678859"),
29 SubMchid: wxpay_utility.String("1900000109"),
30 }
31
32 response, err := QueryReturnAdvance(config, request)
33 if err != nil {
34 fmt.Printf("请求失败: %+v\n", err)
35
36 return
37 }
38
39
40 fmt.Printf("请求成功: %+v\n", response)
41}
42
43func QueryReturnAdvance(config *wxpay_utility.MchConfig, request *QueryReturnAdvanceRequest) (response *ReturnAdvance, err error) {
44 const (
45 host = "https://api.mch.weixin.qq.com"
46 method = "GET"
47 path = "/v3/ecommerce/refunds/{refund_id}/return-advance"
48 )
49
50 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
51 if err != nil {
52 return nil, err
53 }
54 reqUrl.Path = strings.Replace(reqUrl.Path, "{refund_id}", url.PathEscape(*request.RefundId), -1)
55 query := reqUrl.Query()
56 if request.SubMchid != nil {
57 query.Add("sub_mchid", *request.SubMchid)
58 }
59 reqUrl.RawQuery = query.Encode()
60 httpRequest, err := http.NewRequest(method, reqUrl.String(), nil)
61 if err != nil {
62 return nil, err
63 }
64 httpRequest.Header.Set("Accept", "application/json")
65 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
66 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), nil)
67 if err != nil {
68 return nil, err
69 }
70 httpRequest.Header.Set("Authorization", authorization)
71
72 client := &http.Client{}
73 httpResponse, err := client.Do(httpRequest)
74 if err != nil {
75 return nil, err
76 }
77 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse)
78 if err != nil {
79 return nil, err
80 }
81 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
82
83 err = wxpay_utility.ValidateResponse(
84 config.WechatPayPublicKeyId(),
85 config.WechatPayPublicKey(),
86 &httpResponse.Header,
87 respBody,
88 )
89 if err != nil {
90 return nil, err
91 }
92 response := &ReturnAdvance{}
93 if err := json.Unmarshal(respBody, response); err != nil {
94 return nil, err
95 }
96
97 return response, nil
98 } else {
99 return nil, wxpay_utility.NewApiException(
100 httpResponse.StatusCode,
101 httpResponse.Header,
102 respBody,
103 )
104 }
105}
106
107type QueryReturnAdvanceRequest struct {
108 RefundId *string `json:"refund_id,omitempty"`
109 SubMchid *string `json:"sub_mchid,omitempty"`
110}
111
112func (o *QueryReturnAdvanceRequest) MarshalJSON() ([]byte, error) {
113 type Alias QueryReturnAdvanceRequest
114 a := &struct {
115 RefundId *string `json:"refund_id,omitempty"`
116 SubMchid *string `json:"sub_mchid,omitempty"`
117 *Alias
118 }{
119
120 RefundId: nil,
121 SubMchid: nil,
122 Alias: (*Alias)(o),
123 }
124 return json.Marshal(a)
125}
126
127type ReturnAdvance struct {
128 RefundId *string `json:"refund_id,omitempty"`
129 AdvanceReturnId *string `json:"advance_return_id,omitempty"`
130 ReturnAmount *int64 `json:"return_amount,omitempty"`
131 PayerMchid *string `json:"payer_mchid,omitempty"`
132 PayerAccount *string `json:"payer_account,omitempty"`
133 PayeeMchid *string `json:"payee_mchid,omitempty"`
134 PayeeAccount *string `json:"payee_account,omitempty"`
135 Result *string `json:"result,omitempty"`
136 SuccessTime *time.Time `json:"success_time,omitempty"`
137}
138