
1package main
2
3import (
4 "demo/wxpay_brand_utility"
5 "encoding/json"
6 "fmt"
7 "net/http"
8 "net/url"
9 "time"
10)
11
12func main() {
13
14 config, err := wxpay_brand_utility.CreateBrandConfig(
15 "xxxxxxxx",
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 response, err := GetNotifyConfig(config)
27 if err != nil {
28 fmt.Printf("请求失败: %+v\n", err)
29
30 return
31 }
32
33
34 fmt.Printf("请求成功: %+v\n", response)
35}
36
37func GetNotifyConfig(config *wxpay_brand_utility.BrandConfig) (response *GetNotifyConfigResponse, err error) {
38 const (
39 host = "https://api.mch.weixin.qq.com"
40 method = "GET"
41 path = "/brand/marketing/product-coupon/notify-configs"
42 )
43
44 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
45 if err != nil {
46 return nil, err
47 }
48 if err != nil {
49 return nil, err
50 }
51 httpRequest.Header.Set("Accept", "application/json")
52 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
53 authorization, err := wxpay_brand_utility.BuildAuthorization(config.BrandId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), nil)
54 if err != nil {
55 return nil, err
56 }
57 httpRequest.Header.Set("Authorization", authorization)
58
59 client := &http.Client{}
60 httpResponse, err := client.Do(httpRequest)
61 if err != nil {
62 return nil, err
63 }
64 respBody, err := wxpay_brand_utility.ExtractResponseBody(httpResponse)
65 if err != nil {
66 return nil, err
67 }
68 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
69
70 err = wxpay_brand_utility.ValidateResponse(
71 config.WechatPayPublicKeyId(),
72 config.WechatPayPublicKey(),
73 &httpResponse.Header,
74 respBody,
75 )
76 if err != nil {
77 return nil, err
78 }
79 response := &GetNotifyConfigResponse{}
80 if err := json.Unmarshal(respBody, response); err != nil {
81 return nil, err
82 }
83
84 return response, nil
85 } else {
86 return nil, wxpay_brand_utility.NewApiException(
87 httpResponse.StatusCode,
88 httpResponse.Header,
89 respBody,
90 )
91 }
92}
93
94type GetNotifyConfigResponse struct {
95 NotifyUrl *string `json:"notify_url,omitempty"`
96 UpdateTime *time.Time `json:"update_time,omitempty"`
97}
98