
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 := &CancelApplymentRequest{
27 BusinessCode: wxpay_utility.String("1900013511_10000"),
28 ApplymentId: wxpay_utility.String("1111111111"),
29 BrandId: wxpay_utility.String("1234567"),
30 }
31
32 response, err := CancelApplyment(config, request)
33 if err != nil {
34 fmt.Printf("请求失败: %+v\n", err)
35
36 return
37 }
38
39
40 fmt.Printf("请求成功: %+v\n", response)
41}
42
43func CancelApplyment(config *wxpay_utility.MchConfig, request *CancelApplymentRequest) (response *CancelApplymentResponse, err error) {
44 const (
45 host = "https://api.mch.weixin.qq.com"
46 method = "POST"
47 path = "/v3/brand/card/card-configs/cancel-applyment"
48 )
49
50 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
51 if err != nil {
52 return nil, err
53 }
54 reqBody, err := json.Marshal(request)
55 if err != nil {
56 return nil, err
57 }
58 httpRequest, err := http.NewRequest(method, reqUrl.String(), bytes.NewReader(reqBody))
59 if err != nil {
60 return nil, err
61 }
62 httpRequest.Header.Set("Accept", "application/json")
63 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
64 httpRequest.Header.Set("Content-Type", "application/json")
65 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), reqBody)
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 := &CancelApplymentResponse{}
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 CancelApplymentRequest struct {
107 BusinessCode *string `json:"business_code,omitempty"`
108 ApplymentId *string `json:"applyment_id,omitempty"`
109 BrandId *string `json:"brand_id,omitempty"`
110}
111
112type CancelApplymentResponse struct {
113 BusinessCode *string `json:"business_code,omitempty"`
114 ApplymentId *string `json:"applyment_id,omitempty"`
115 BrandId *string `json:"brand_id,omitempty"`
116 ApplymentState *ApplymentState `json:"applyment_state,omitempty"`
117}
118
119type ApplymentState string
120
121func (e ApplymentState) Ptr() *ApplymentState {
122 return &e
123}
124
125const (
126 APPLYMENTSTATE_STATE_UNKNOWN ApplymentState = "STATE_UNKNOWN"
127 APPLYMENTSTATE_DRAFTING ApplymentState = "DRAFTING"
128 APPLYMENTSTATE_AUDITING ApplymentState = "AUDITING"
129 APPLYMENTSTATE_AUDIT_REJECTED ApplymentState = "AUDIT_REJECTED"
130 APPLYMENTSTATE_PENDING_PUBLISH ApplymentState = "PENDING_PUBLISH"
131 APPLYMENTSTATE_PUBLISHED ApplymentState = "PUBLISHED"
132 APPLYMENTSTATE_CANCELED ApplymentState = "CANCELED"
133)
134