
1package main
2
3import (
4 "demo/wxpay_utility"
5 "encoding/json"
6 "fmt"
7 "net/http"
8 "net/url"
9 "strings"
10)
11
12func main() {
13
14 config, err := wxpay_utility.CreateMchConfig(
15 "19xxxxxxxx",
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 request := &QueryMembershipActivityRequest{
27 BrandId: wxpay_utility.String("1004"),
28 ActivityId: wxpay_utility.Int64(25),
29 }
30
31 response, err := QueryMembershipActivity(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 QueryMembershipActivity(config *wxpay_utility.MchConfig, request *QueryMembershipActivityRequest) (response *MembershipActivity, err error) {
43 const (
44 host = "https://api.mch.weixin.qq.com"
45 method = "GET"
46 path = "/v3/brand/partner/card-member/membership-activities/{activity_id}"
47 )
48
49 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
50 if err != nil {
51 return nil, err
52 }
53 reqUrl.Path = strings.Replace(reqUrl.Path, "{activity_id}", url.PathEscape(fmt.Sprintf("%v", *request.ActivityId)), -1)
54 query := reqUrl.Query()
55 if request.BrandId != nil {
56 query.Add("brand_id", *request.BrandId)
57 }
58 reqUrl.RawQuery = query.Encode()
59 httpRequest, err := http.NewRequest(method, reqUrl.String(), nil)
60 if err != nil {
61 return nil, err
62 }
63 httpRequest.Header.Set("Accept", "application/json")
64 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
65 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), nil)
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 := &MembershipActivity{}
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 QueryMembershipActivityRequest struct {
107 BrandId *string `json:"brand_id,omitempty"`
108 ActivityId *int64 `json:"activity_id,omitempty"`
109}
110
111func (o *QueryMembershipActivityRequest) MarshalJSON() ([]byte, error) {
112 type Alias QueryMembershipActivityRequest
113 a := &struct {
114 BrandId *string `json:"brand_id,omitempty"`
115 ActivityId *int64 `json:"activity_id,omitempty"`
116 *Alias
117 }{
118
119 BrandId: nil,
120 ActivityId: nil,
121 Alias: (*Alias)(o),
122 }
123 return json.Marshal(a)
124}
125
126type MembershipActivity struct {
127 OutRequestNo *string `json:"out_request_no,omitempty"`
128 BrandId *string `json:"brand_id,omitempty"`
129 CardId *string `json:"card_id,omitempty"`
130 ActivityChannel *MembershipActivityChannel `json:"activity_channel,omitempty"`
131 Title *string `json:"title,omitempty"`
132 SubTitle *string `json:"sub_title,omitempty"`
133 BeginTime *string `json:"begin_time,omitempty"`
134 EndTime *string `json:"end_time,omitempty"`
135 ApplyTotal *int64 `json:"apply_total,omitempty"`
136 ApplyAvailable *int64 `json:"apply_available,omitempty"`
137 ProductCouponStockList []ProductCouponStock `json:"product_coupon_stock_list,omitempty"`
138 ActivityId *int64 `json:"activity_id,omitempty"`
139 ActivityState *MembershipGiftActivityState `json:"activity_state,omitempty"`
140 CreateTime *string `json:"create_time,omitempty"`
141 ModifyTime *string `json:"modify_time,omitempty"`
142}
143
144type MembershipActivityChannel string
145
146func (e MembershipActivityChannel) Ptr() *MembershipActivityChannel {
147 return &e
148}
149
150const (
151 MEMBERSHIPACTIVITYCHANNEL_MECHANT_APP_COMPONENT MembershipActivityChannel = "MECHANT_APP_COMPONENT"
152)
153
154type ProductCouponStock struct {
155 ProductCouponId *string `json:"product_coupon_id,omitempty"`
156 StockId *string `json:"stock_id,omitempty"`
157}
158
159type MembershipGiftActivityState string
160
161func (e MembershipGiftActivityState) Ptr() *MembershipGiftActivityState {
162 return &e
163}
164
165const (
166 MEMBERSHIPGIFTACTIVITYSTATE_MEMBERSHIP_ACTIVITY_CREATED MembershipGiftActivityState = "MEMBERSHIP_ACTIVITY_CREATED"
167 MEMBERSHIPGIFTACTIVITYSTATE_MEMBERSHIP_ACTIVITY_EFFECTIVE MembershipGiftActivityState = "MEMBERSHIP_ACTIVITY_EFFECTIVE"
168 MEMBERSHIPGIFTACTIVITYSTATE_MEMBERSHIP_ACTIVITY_TERMINATED MembershipGiftActivityState = "MEMBERSHIP_ACTIVITY_TERMINATED"
169 MEMBERSHIPGIFTACTIVITYSTATE_MEMBERSHIP_ACTIVITY_EXPIRED MembershipGiftActivityState = "MEMBERSHIP_ACTIVITY_EXPIRED"
170)
171