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