
1package main
2
3import (
4 "bytes"
5 "demo/wxpay_utility"
6 "encoding/json"
7 "fmt"
8 "net/http"
9 "net/url"
10 "strings"
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 := &ReverseFapiaoApplicationsRequest{
28 FapiaoApplyId: wxpay_utility.String("4200000444201910177461284488"),
29 SubMchid: wxpay_utility.String("1900000109"),
30 ReverseReason: wxpay_utility.String("非数电模式:退款;数电模式:ISSUE_ERROR|SALES_RETURN|SERVICE_SUSPENSION|SALES_DISCOUNT"),
31 FapiaoInformation: []ReverseFapiaoInfo{ReverseFapiaoInfo{
32 FapiaoId: wxpay_utility.String("20200701123456"),
33 FapiaoNumber: wxpay_utility.String("12897794"),
34 }},
35 }
36
37 err = ReverseFapiaoApplications(config, request)
38 if err != nil {
39 fmt.Printf("请求失败: %+v\n", err)
40
41 return
42 }
43
44
45 fmt.Println("请求成功")
46}
47
48func ReverseFapiaoApplications(config *wxpay_utility.MchConfig, request *ReverseFapiaoApplicationsRequest) (err error) {
49 const (
50 host = "https://api.mch.weixin.qq.com"
51 method = "POST"
52 path = "/v3/new-tax-control-fapiao/fapiao-applications/{fapiao_apply_id}/reverse"
53 )
54
55 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
56 if err != nil {
57 return err
58 }
59 reqUrl.Path = strings.Replace(reqUrl.Path, "{fapiao_apply_id}", url.PathEscape(*request.FapiaoApplyId), -1)
60 reqBody, err := json.Marshal(request)
61 if err != nil {
62 return err
63 }
64 httpRequest, err := http.NewRequest(method, reqUrl.String(), bytes.NewReader(reqBody))
65 if err != nil {
66 return err
67 }
68 httpRequest.Header.Set("Accept", "application/json")
69 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
70 httpRequest.Header.Set("Content-Type", "application/json")
71 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), reqBody)
72 if err != nil {
73 return err
74 }
75 httpRequest.Header.Set("Authorization", authorization)
76
77 client := &http.Client{}
78 httpResponse, err := client.Do(httpRequest)
79 if err != nil {
80 return err
81 }
82 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse)
83 if err != nil {
84 return err
85 }
86 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
87
88 err = wxpay_utility.ValidateResponse(
89 config.WechatPayPublicKeyId(),
90 config.WechatPayPublicKey(),
91 &httpResponse.Header,
92 respBody,
93 )
94 if err != nil {
95 return err
96 }
97 return nil
98 } else {
99 return wxpay_utility.NewApiException(
100 httpResponse.StatusCode,
101 httpResponse.Header,
102 respBody,
103 )
104 }
105}
106
107type ReverseFapiaoApplicationsRequest struct {
108 SubMchid *string `json:"sub_mchid,omitempty"`
109 FapiaoApplyId *string `json:"fapiao_apply_id,omitempty"`
110 ReverseReason *string `json:"reverse_reason,omitempty"`
111 FapiaoInformation []ReverseFapiaoInfo `json:"fapiao_information,omitempty"`
112}
113
114func (o *ReverseFapiaoApplicationsRequest) MarshalJSON() ([]byte, error) {
115 type Alias ReverseFapiaoApplicationsRequest
116 a := &struct {
117 FapiaoApplyId *string `json:"fapiao_apply_id,omitempty"`
118 *Alias
119 }{
120
121 FapiaoApplyId: nil,
122 Alias: (*Alias)(o),
123 }
124 return json.Marshal(a)
125}
126
127type ReverseFapiaoInfo struct {
128 FapiaoId *string `json:"fapiao_id,omitempty"`
129 FapiaoNumber *string `json:"fapiao_number,omitempty"`
130}
131