
1package main
2
3import (
4 "bytes"
5 "demo/wxpay_brand_utility"
6 "encoding/json"
7 "fmt"
8 "net/http"
9 "net/url"
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 request := &CreateUserFeedsRequest{
27 CardId: wxpay_brand_utility.String("pbLatjvWOibDc5-TBnbUk1pD12o0"),
28 UserCardCode: wxpay_brand_utility.String("478515832665"),
29 Openid: wxpay_brand_utility.String("obLatjnx9gnqzS4myYGmLZ7LgLBA"),
30 OutRequestNo: wxpay_brand_utility.String("100002322019090134234sfdf"),
31 Cell: wxpay_brand_utility.String("15_1200_1"),
32 }
33
34 response, err := CreateUserFeeds(config, request)
35 if err != nil {
36 fmt.Printf("请求失败: %+v\n", err)
37
38 return
39 }
40
41
42 fmt.Printf("请求成功: %+v\n", response)
43}
44
45func CreateUserFeeds(config *wxpay_brand_utility.BrandConfig, request *CreateUserFeedsRequest) (response *UserFeeds, err error) {
46 const (
47 host = "https://api.mch.weixin.qq.com"
48 method = "POST"
49 path = "/brand/card-member/user-feeds"
50 )
51
52 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
53 if err != nil {
54 return nil, err
55 }
56 reqBody, err := json.Marshal(request)
57 if err != nil {
58 return nil, err
59 }
60 httpRequest, err := http.NewRequest(method, reqUrl.String(), bytes.NewReader(reqBody))
61 if err != nil {
62 return nil, err
63 }
64 httpRequest.Header.Set("Accept", "application/json")
65 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
66 httpRequest.Header.Set("Content-Type", "application/json")
67 authorization, err := wxpay_brand_utility.BuildAuthorization(config.BrandId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), reqBody)
68 if err != nil {
69 return nil, err
70 }
71 httpRequest.Header.Set("Authorization", authorization)
72
73 client := &http.Client{}
74 httpResponse, err := client.Do(httpRequest)
75 if err != nil {
76 return nil, err
77 }
78 respBody, err := wxpay_brand_utility.ExtractResponseBody(httpResponse)
79 if err != nil {
80 return nil, err
81 }
82 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
83
84 err = wxpay_brand_utility.ValidateResponse(
85 config.WechatPayPublicKeyId(),
86 config.WechatPayPublicKey(),
87 &httpResponse.Header,
88 respBody,
89 )
90 if err != nil {
91 return nil, err
92 }
93 response := &UserFeeds{}
94 if err := json.Unmarshal(respBody, response); err != nil {
95 return nil, err
96 }
97
98 return response, nil
99 } else {
100 return nil, wxpay_brand_utility.NewApiException(
101 httpResponse.StatusCode,
102 httpResponse.Header,
103 respBody,
104 )
105 }
106}
107
108type CreateUserFeedsRequest struct {
109 CardId *string `json:"card_id,omitempty"`
110 UserCardCode *string `json:"user_card_code,omitempty"`
111 Openid *string `json:"openid,omitempty"`
112 OutRequestNo *string `json:"out_request_no,omitempty"`
113 Cell *string `json:"cell,omitempty"`
114}
115
116type UserFeeds struct {
117 BrandId *string `json:"brand_id,omitempty"`
118 CardId *string `json:"card_id,omitempty"`
119 UserCardCode *string `json:"user_card_code,omitempty"`
120 OutRequestNo *string `json:"out_request_no,omitempty"`
121 Cell *string `json:"cell,omitempty"`
122}
123