
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 := &UnionCloseRequest{
28 CombineOutTradeNo: wxpay_utility.String("1217752501201407033233368018"),
29 CombineAppid: wxpay_utility.String("wxd678efh567hg6787"),
30 SubOrders: []UnionCloseSubOrder{UnionCloseSubOrder{
31 Mchid: wxpay_utility.String("1900000109"),
32 OutTradeNo: wxpay_utility.String("20150806125346"),
33 }},
34 }
35
36 err = UnionClose(config, request)
37 if err != nil {
38 fmt.Printf("请求失败: %+v\n", err)
39
40 return
41 }
42
43
44 fmt.Println("请求成功")
45}
46
47func UnionClose(config *wxpay_utility.MchConfig, request *UnionCloseRequest) (err error) {
48 const (
49 host = "https://api.mch.weixin.qq.com"
50 method = "POST"
51 path = "/v3/combine-transactions/out-trade-no/{combine_out_trade_no}/close"
52 )
53
54 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
55 if err != nil {
56 return err
57 }
58 reqUrl.Path = strings.Replace(reqUrl.Path, "{combine_out_trade_no}", url.PathEscape(*request.CombineOutTradeNo), -1)
59 reqBody, err := json.Marshal(request)
60 if err != nil {
61 return err
62 }
63 httpRequest, err := http.NewRequest(method, reqUrl.String(), bytes.NewReader(reqBody))
64 if err != nil {
65 return err
66 }
67 httpRequest.Header.Set("Accept", "application/json")
68 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
69 httpRequest.Header.Set("Content-Type", "application/json")
70 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), reqBody)
71 if err != nil {
72 return err
73 }
74 httpRequest.Header.Set("Authorization", authorization)
75
76 client := &http.Client{}
77 httpResponse, err := client.Do(httpRequest)
78 if err != nil {
79 return err
80 }
81 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse)
82 if err != nil {
83 return err
84 }
85 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
86
87 err = wxpay_utility.ValidateResponse(
88 config.WechatPayPublicKeyId(),
89 config.WechatPayPublicKey(),
90 &httpResponse.Header,
91 respBody,
92 )
93 if err != nil {
94 return err
95 }
96 return nil
97 } else {
98 return wxpay_utility.NewApiException(
99 httpResponse.StatusCode,
100 httpResponse.Header,
101 respBody,
102 )
103 }
104}
105
106type UnionCloseRequest struct {
107 CombineAppid *string `json:"combine_appid,omitempty"`
108 CombineOutTradeNo *string `json:"combine_out_trade_no,omitempty"`
109 SubOrders []UnionCloseSubOrder `json:"sub_orders,omitempty"`
110}
111
112func (o *UnionCloseRequest) MarshalJSON() ([]byte, error) {
113 type Alias UnionCloseRequest
114 a := &struct {
115 CombineOutTradeNo *string `json:"combine_out_trade_no,omitempty"`
116 *Alias
117 }{
118
119 CombineOutTradeNo: nil,
120 Alias: (*Alias)(o),
121 }
122 return json.Marshal(a)
123}
124
125type UnionCloseSubOrder struct {
126 Mchid *string `json:"mchid,omitempty"`
127 OutTradeNo *string `json:"out_trade_no,omitempty"`
128}
129