
1package main
2
3import (
4 "bytes"
5 "demo/wxpay_utility"
6 "encoding/json"
7 "fmt"
8 "net/http"
9 "net/url"
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 := &DeleteReceiverRequest{
27 BrandMchid: wxpay_utility.String("1900000108"),
28 Appid: wxpay_utility.String("wx8888888888888888"),
29 SubAppid: wxpay_utility.String("wx8888888888888889"),
30 Type: wxpay_utility.String("MERCHANT_ID"),
31 Account: wxpay_utility.String("1900000109"),
32 }
33
34 response, err := DeleteReceiver(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 DeleteReceiver(config *wxpay_utility.MchConfig, request *DeleteReceiverRequest) (response *DeleteReceiverResponse, err error) {
46 const (
47 host = "https://api.mch.weixin.qq.com"
48 method = "POST"
49 path = "/v3/brand/profitsharing/receivers/delete"
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_utility.BuildAuthorization(config.MchId(), 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
79 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse)
80 if err != nil {
81 return nil, err
82 }
83
84 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
85
86 err = wxpay_utility.ValidateResponse(
87 config.WechatPayPublicKeyId(),
88 config.WechatPayPublicKey(),
89 &httpResponse.Header,
90 respBody,
91 )
92 if err != nil {
93 return nil, err
94 }
95
96 response := &DeleteReceiverResponse{}
97 if err := json.Unmarshal(respBody, response); err != nil {
98 return nil, err
99 }
100
101 return response, nil
102 } else {
103 return nil, wxpay_utility.NewApiException(
104 httpResponse.StatusCode,
105 httpResponse.Header,
106 respBody,
107 )
108 }
109}
110
111type DeleteReceiverRequest struct {
112 BrandMchid *string `json:"brand_mchid,omitempty"`
113 Appid *string `json:"appid,omitempty"`
114 SubAppid *string `json:"sub_appid,omitempty"`
115 Type *string `json:"type,omitempty"`
116 Account *string `json:"account,omitempty"`
117}
118
119type DeleteReceiverResponse struct {
120 BrandMchid *string `json:"brand_mchid,omitempty"`
121 Type *string `json:"type,omitempty"`
122 Account *string `json:"account,omitempty"`
123}
124