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