
1package main
2
3import (
4 "bytes"
5 "demo/wxpay_utility"
6 "encoding/json"
7 "fmt"
8 "net/http"
9 "net/url"
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 := &NotifyRefundRequest{
27 MixTradeNo: wxpay_utility.String("202204022005169952975171534816"),
28 SubMchid: wxpay_utility.String("1900008XXX"),
29 MedRefundTotalFee: wxpay_utility.Int64(45000),
30 MedRefundGovFee: wxpay_utility.Int64(45000),
31 MedRefundSelfFee: wxpay_utility.Int64(45000),
32 MedRefundOtherFee: wxpay_utility.Int64(45000),
33 RefundTime: wxpay_utility.String("2015-05-20T13:29:35+08:00"),
34 OutRefundNo: wxpay_utility.String("R202204022005169952975171534816"),
35 }
36
37 err = NotifyRefund(config, request)
38 if err != nil {
39 fmt.Printf("请求失败: %+v\n", err)
40
41 return
42 }
43
44
45 fmt.Println("请求成功")
46}
47
48func NotifyRefund(config *wxpay_utility.MchConfig, request *NotifyRefundRequest) (err error) {
49 const (
50 host = "https://api.mch.weixin.qq.com"
51 method = "POST"
52 path = "/v3/med-ins/refunds/notify"
53 )
54
55 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
56 if err != nil {
57 return err
58 }
59 query := reqUrl.Query()
60 if request.MixTradeNo != nil {
61 query.Add("mix_trade_no", *request.MixTradeNo)
62 }
63 reqUrl.RawQuery = query.Encode()
64 reqBody, err := json.Marshal(request)
65 if err != nil {
66 return err
67 }
68 httpRequest, err := http.NewRequest(method, reqUrl.String(), bytes.NewReader(reqBody))
69 if err != nil {
70 return err
71 }
72 httpRequest.Header.Set("Accept", "application/json")
73 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
74 httpRequest.Header.Set("Content-Type", "application/json")
75 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), reqBody)
76 if err != nil {
77 return err
78 }
79 httpRequest.Header.Set("Authorization", authorization)
80
81 client := &http.Client{}
82 httpResponse, err := client.Do(httpRequest)
83 if err != nil {
84 return err
85 }
86 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse)
87 if err != nil {
88 return err
89 }
90 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
91
92 err = wxpay_utility.ValidateResponse(
93 config.WechatPayPublicKeyId(),
94 config.WechatPayPublicKey(),
95 &httpResponse.Header,
96 respBody,
97 )
98 if err != nil {
99 return err
100 }
101 return nil
102 } else {
103 return wxpay_utility.NewApiException(
104 httpResponse.StatusCode,
105 httpResponse.Header,
106 respBody,
107 )
108 }
109}
110
111type NotifyRefundRequest struct {
112 MixTradeNo *string `json:"mix_trade_no,omitempty"`
113 SubMchid *string `json:"sub_mchid,omitempty"`
114 MedRefundTotalFee *int64 `json:"med_refund_total_fee,omitempty"`
115 MedRefundGovFee *int64 `json:"med_refund_gov_fee,omitempty"`
116 MedRefundSelfFee *int64 `json:"med_refund_self_fee,omitempty"`
117 MedRefundOtherFee *int64 `json:"med_refund_other_fee,omitempty"`
118 RefundTime *string `json:"refund_time,omitempty"`
119 OutRefundNo *string `json:"out_refund_no,omitempty"`
120}
121
122func (o *NotifyRefundRequest) MarshalJSON() ([]byte, error) {
123 type Alias NotifyRefundRequest
124 a := &struct {
125 MixTradeNo *string `json:"mix_trade_no,omitempty"`
126 *Alias
127 }{
128
129 MixTradeNo: nil,
130 Alias: (*Alias)(o),
131 }
132 return json.Marshal(a)
133}
134