
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 := &TransferBillGetByOutNoRequest{
28 SubMchid: wxpay_utility.String("1900001109"),
29 OutBillNo: wxpay_utility.String("plfk2020042013"),
30 }
31
32 response, err := TransferBillGetByOutNo(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 TransferBillGetByOutNo(config *wxpay_utility.MchConfig, request *TransferBillGetByOutNoRequest) (response *TransferBillEntity, err error) {
44 const (
45 host = "https://api.mch.weixin.qq.com"
46 method = "GET"
47 path = "/v3/fund-app/mch-transfer/partner/transfer-bills/out-bill-no/{out_bill_no}"
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, "{out_bill_no}", url.PathEscape(*request.OutBillNo), -1)
55 query := reqUrl.Query()
56 query.Add("sub_mchid", *request.SubMchid)
57 reqUrl.RawQuery = query.Encode()
58 httpRequest, err := http.NewRequest(method, reqUrl.String(), nil)
59 if err != nil {
60 return nil, err
61 }
62 httpRequest.Header.Set("Accept", "application/json")
63 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
64 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), nil)
65 if err != nil {
66 return nil, err
67 }
68 httpRequest.Header.Set("Authorization", authorization)
69
70 client := &http.Client{}
71 httpResponse, err := client.Do(httpRequest)
72 if err != nil {
73 return nil, err
74 }
75
76 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse)
77 if err != nil {
78 return nil, err
79 }
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
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 TransferBillGetByOutNoRequest struct {
108 SubMchid *string `json:"sub_mchid,omitempty"`
109 OutBillNo *string `json:"out_bill_no,omitempty"`
110}
111
112func (o *TransferBillGetByOutNoRequest) MarshalJSON() ([]byte, error) {
113 type Alias TransferBillGetByOutNoRequest
114 a := &struct {
115 SubMchid *string `json:"sub_mchid,omitempty"`
116 OutBillNo *string `json:"out_bill_no,omitempty"`
117 *Alias
118 }{
119
120 SubMchid: nil,
121 OutBillNo: nil,
122 Alias: (*Alias)(o),
123 }
124 return json.Marshal(a)
125}
126
127type TransferBillEntity struct {
128 SubMchid *string `json:"sub_mchid,omitempty"`
129 Mchid *string `json:"mchid,omitempty"`
130 OutBillNo *string `json:"out_bill_no,omitempty"`
131 TransferBillNo *string `json:"transfer_bill_no,omitempty"`
132 Appid *string `json:"appid,omitempty"`
133 State *TransferBillStatus `json:"state,omitempty"`
134 TransferAmount *int64 `json:"transfer_amount,omitempty"`
135 TransferRemark *string `json:"transfer_remark,omitempty"`
136 FailReason *string `json:"fail_reason,omitempty"`
137 Openid *string `json:"openid,omitempty"`
138 UserName *string `json:"user_name,omitempty"`
139 CreateTime *time.Time `json:"create_time,omitempty"`
140 UpdateTime *string `json:"update_time,omitempty"`
141}
142
143type TransferBillStatus string
144
145func (e TransferBillStatus) Ptr() *TransferBillStatus {
146 return &e
147}
148
149const (
150 TRANSFERBILLSTATUS_ACCEPTED TransferBillStatus = "ACCEPTED"
151 TRANSFERBILLSTATUS_PROCESSING TransferBillStatus = "PROCESSING"
152 TRANSFERBILLSTATUS_WAIT_USER_CONFIRM TransferBillStatus = "WAIT_USER_CONFIRM"
153 TRANSFERBILLSTATUS_TRANSFERING TransferBillStatus = "TRANSFERING"
154 TRANSFERBILLSTATUS_SUCCESS TransferBillStatus = "SUCCESS"
155 TRANSFERBILLSTATUS_FAIL TransferBillStatus = "FAIL"
156 TRANSFERBILLSTATUS_CANCELING TransferBillStatus = "CANCELING"
157 TRANSFERBILLSTATUS_CANCELLED TransferBillStatus = "CANCELLED"
158)
159