
1package main
2
3import (
4 "bytes"
5 "demo/wxpay_brand_utility"
6 "encoding/json"
7 "fmt"
8 "net/http"
9 "net/url"
10 "time"
11)
12
13func main() {
14
15 config, err := wxpay_brand_utility.CreateBrandConfig(
16 "xxxxxxxx",
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 := &SetNotifyConfigRequest{
28 NotifyUrl: wxpay_brand_utility.String("https://www.example.com/notify"),
29 }
30
31 response, err := SetNotifyConfig(config, request)
32 if err != nil {
33 fmt.Printf("请求失败: %+v\n", err)
34
35 return
36 }
37
38
39 fmt.Printf("请求成功: %+v\n", response)
40}
41
42func SetNotifyConfig(config *wxpay_brand_utility.BrandConfig, request *SetNotifyConfigRequest) (response *SetNotifyConfigResponse, err error) {
43 const (
44 host = "https://api.mch.weixin.qq.com"
45 method = "POST"
46 path = "/brand/marketing/product-coupon/notify-configs"
47 )
48
49 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
50 if err != nil {
51 return nil, err
52 }
53 reqBody, err := json.Marshal(request)
54 if err != nil {
55 return nil, err
56 }
57 httpRequest, err := http.NewRequest(method, reqUrl.String(), bytes.NewReader(reqBody))
58 if err != nil {
59 return nil, err
60 }
61 httpRequest.Header.Set("Accept", "application/json")
62 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
63 httpRequest.Header.Set("Content-Type", "application/json")
64 authorization, err := wxpay_brand_utility.BuildAuthorization(config.BrandId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), reqBody)
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 respBody, err := wxpay_brand_utility.ExtractResponseBody(httpResponse)
76 if err != nil {
77 return nil, err
78 }
79 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
80
81 err = wxpay_brand_utility.ValidateResponse(
82 config.WechatPayPublicKeyId(),
83 config.WechatPayPublicKey(),
84 &httpResponse.Header,
85 respBody,
86 )
87 if err != nil {
88 return nil, err
89 }
90 response := &SetNotifyConfigResponse{}
91 if err := json.Unmarshal(respBody, response); err != nil {
92 return nil, err
93 }
94
95 return response, nil
96 } else {
97 return nil, wxpay_brand_utility.NewApiException(
98 httpResponse.StatusCode,
99 httpResponse.Header,
100 respBody,
101 )
102 }
103}
104
105type SetNotifyConfigRequest struct {
106 NotifyUrl *string `json:"notify_url,omitempty"`
107}
108
109type SetNotifyConfigResponse struct {
110 NotifyUrl *string `json:"notify_url,omitempty"`
111 UpdateTime *time.Time `json:"update_time,omitempty"`
112}
113