
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 := &SubmitMchCardConfigRequest{
28 BusinessCode: wxpay_utility.String("1900013511_10000"),
29 BrandId: wxpay_utility.String("1234567"),
30 BrandMiniProgramInfo: &MiniProgramInfo{
31 Appid: wxpay_utility.String("wx1234567890abcdef"),
32 DefaultJumpPath: wxpay_utility.String("pages/shop/index"),
33 ButtonText: wxpay_utility.String("前往小程序"),
34 },
35 BrandCustomerService: &MerchantCardCustomerService{
36 CustomerServiceType: CUSTOMERSERVICETYPE_MINI_PROGRAM.Ptr(),
37 CustomerServicePhone: wxpay_utility.String("12345678900"),
38 CustomerServicePath: wxpay_utility.String("pages/service/index"),
39 Appid: wxpay_utility.String("wx1234567890abcdef"),
40 },
41 ServiceList: []MerchantCardService{MerchantCardService{
42 ServiceClassifyName: wxpay_utility.String("会员服务"),
43 ServiceName: wxpay_utility.String("会员中心"),
44 ServiceJumpType: SERVICEJUMPTYPE_JUMP_MINI_PROGRAM.Ptr(),
45 ServiceJumpPath: wxpay_utility.String("pages/shop/index"),
46 Appid: wxpay_utility.String("wx1234567890abcdef"),
47 }},
48 }
49
50 response, err := SubmitMchCardConfig(config, request)
51 if err != nil {
52 fmt.Printf("请求失败: %+v\n", err)
53
54 return
55 }
56
57
58 fmt.Printf("请求成功: %+v\n", response)
59}
60
61func SubmitMchCardConfig(config *wxpay_utility.MchConfig, request *SubmitMchCardConfigRequest) (response *SubmitMchCardConfigResponse, err error) {
62 const (
63 host = "https://api.mch.weixin.qq.com"
64 method = "POST"
65 path = "/v3/brand/card/card-configs"
66 )
67
68 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
69 if err != nil {
70 return nil, err
71 }
72 reqBody, err := json.Marshal(request)
73 if err != nil {
74 return nil, err
75 }
76 httpRequest, err := http.NewRequest(method, reqUrl.String(), bytes.NewReader(reqBody))
77 if err != nil {
78 return nil, err
79 }
80 httpRequest.Header.Set("Accept", "application/json")
81 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
82 httpRequest.Header.Set("Content-Type", "application/json")
83 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), reqBody)
84 if err != nil {
85 return nil, err
86 }
87 httpRequest.Header.Set("Authorization", authorization)
88
89 client := &http.Client{}
90 httpResponse, err := client.Do(httpRequest)
91 if err != nil {
92 return nil, err
93 }
94 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse)
95 if err != nil {
96 return nil, err
97 }
98 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
99
100 err = wxpay_utility.ValidateResponse(
101 config.WechatPayPublicKeyId(),
102 config.WechatPayPublicKey(),
103 &httpResponse.Header,
104 respBody,
105 )
106 if err != nil {
107 return nil, err
108 }
109 response := &SubmitMchCardConfigResponse{}
110 if err := json.Unmarshal(respBody, response); err != nil {
111 return nil, err
112 }
113
114 return response, nil
115 } else {
116 return nil, wxpay_utility.NewApiException(
117 httpResponse.StatusCode,
118 httpResponse.Header,
119 respBody,
120 )
121 }
122}
123
124type SubmitMchCardConfigRequest struct {
125 BusinessCode *string `json:"business_code,omitempty"`
126 BrandId *string `json:"brand_id,omitempty"`
127 BrandMiniProgramInfo *MiniProgramInfo `json:"brand_mini_program_info,omitempty"`
128 BrandCustomerService *MerchantCardCustomerService `json:"brand_customer_service,omitempty"`
129 ServiceList []MerchantCardService `json:"service_list,omitempty"`
130}
131
132type SubmitMchCardConfigResponse struct {
133 BusinessCode *string `json:"business_code,omitempty"`
134 ApplymentId *string `json:"applyment_id,omitempty"`
135 BrandId *string `json:"brand_id,omitempty"`
136 CardPreviewUrl *string `json:"card_preview_url,omitempty"`
137 UrlExpiredTime *time.Time `json:"url_expired_time,omitempty"`
138}
139
140type MiniProgramInfo struct {
141 Appid *string `json:"appid,omitempty"`
142 DefaultJumpPath *string `json:"default_jump_path,omitempty"`
143 ButtonText *string `json:"button_text,omitempty"`
144}
145
146type MerchantCardCustomerService struct {
147 CustomerServiceType *CustomerServiceType `json:"customer_service_type,omitempty"`
148 CustomerServicePhone *string `json:"customer_service_phone,omitempty"`
149 CustomerServicePath *string `json:"customer_service_path,omitempty"`
150 Appid *string `json:"appid,omitempty"`
151}
152
153type MerchantCardService struct {
154 ServiceClassifyName *string `json:"service_classify_name,omitempty"`
155 ServiceName *string `json:"service_name,omitempty"`
156 ServiceJumpType *ServiceJumpType `json:"service_jump_type,omitempty"`
157 ServiceJumpPath *string `json:"service_jump_path,omitempty"`
158 Appid *string `json:"appid,omitempty"`
159}
160
161type CustomerServiceType string
162
163func (e CustomerServiceType) Ptr() *CustomerServiceType {
164 return &e
165}
166
167const (
168 CUSTOMERSERVICETYPE_MINI_PROGRAM CustomerServiceType = "MINI_PROGRAM"
169 CUSTOMERSERVICETYPE_WECOM CustomerServiceType = "WECOM"
170 CUSTOMERSERVICETYPE_CUSTOMIZE_WEB CustomerServiceType = "CUSTOMIZE_WEB"
171 CUSTOMERSERVICETYPE_CUSTOMIZE_MP CustomerServiceType = "CUSTOMIZE_MP"
172 CUSTOMERSERVICETYPE_SERVICE_PHONE CustomerServiceType = "SERVICE_PHONE"
173)
174
175type ServiceJumpType string
176
177func (e ServiceJumpType) Ptr() *ServiceJumpType {
178 return &e
179}
180
181const (
182 SERVICEJUMPTYPE_JUMP_MINI_PROGRAM ServiceJumpType = "JUMP_MINI_PROGRAM"
183 SERVICEJUMPTYPE_JUMP_WEB_PAGE ServiceJumpType = "JUMP_WEB_PAGE"
184)
185