
1package main
2
3import (
4 "bytes"
5 "demo/wxpay_brand_utility"
6 "encoding/json"
7 "fmt"
8 "net/http"
9 "net/url"
10 "strings"
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 := &BindRecipientRequest{
28 StoreId: wxpay_brand_utility.String("1234567890123456"),
29 Mchid: wxpay_brand_utility.String("1230000109"),
30 CompanyName: wxpay_brand_utility.String("腾讯科技(深圳)有限公司"),
31 }
32
33 response, err := BindRecipient(config, request)
34 if err != nil {
35 fmt.Printf("请求失败: %+v\n", err)
36
37 return
38 }
39
40
41 fmt.Printf("请求成功: %+v\n", response)
42}
43
44func BindRecipient(config *wxpay_brand_utility.BrandConfig, request *BindRecipientRequest) (response *BrandStoresBindRecipientResponse, err error) {
45 const (
46 host = "https://api.mch.weixin.qq.com"
47 method = "POST"
48 path = "/brand/store/brandstores/{store_id}/bindrecipient"
49 )
50
51 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
52 if err != nil {
53 return nil, err
54 }
55 reqUrl.Path = strings.Replace(reqUrl.Path, "{store_id}", url.PathEscape(*request.StoreId), -1)
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 := &BrandStoresBindRecipientResponse{}
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 BindRecipientRequest struct {
109 StoreId *string `json:"store_id,omitempty"`
110 Mchid *string `json:"mchid,omitempty"`
111 CompanyName *string `json:"company_name,omitempty"`
112}
113
114func (o *BindRecipientRequest) MarshalJSON() ([]byte, error) {
115 type Alias BindRecipientRequest
116 a := &struct {
117 StoreId *string `json:"store_id,omitempty"`
118 *Alias
119 }{
120
121 StoreId: nil,
122 Alias: (*Alias)(o),
123 }
124 return json.Marshal(a)
125}
126
127type BrandStoresBindRecipientResponse struct {
128 StoreId *string `json:"store_id,omitempty"`
129 Mchid *string `json:"mchid,omitempty"`
130 CompanyName *string `json:"company_name,omitempty"`
131 RecipientState *RecipientState `json:"recipient_state,omitempty"`
132}
133
134type RecipientState string
135
136func (e RecipientState) Ptr() *RecipientState {
137 return &e
138}
139
140const (
141 RECIPIENTSTATE_CONFIRMED RecipientState = "CONFIRMED"
142 RECIPIENTSTATE_ADMIN_REJECTED RecipientState = "ADMIN_REJECTED"
143 RECIPIENTSTATE_CONFIRMING RecipientState = "CONFIRMING"
144 RECIPIENTSTATE_TIMEOUT_REJECTED RecipientState = "TIMEOUT_REJECTED"
145)
146