
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 := &CreateFapiaoCardTemplateRequest{
27 SubMchid: wxpay_utility.String("1900000109"),
28 CardAppid: wxpay_utility.String("wxb1170446a4c0a5a2"),
29 CardTemplateInformation: &CardTemplateInfo{
30 PayeeName: wxpay_utility.String("某公司"),
31 LogoUrl: wxpay_utility.String("http://mmbiz.qpic.cn/mmbiz/iaL1LJM1mF9aRKPZJkmG8xXhiaHqkKSVMMWeN3hLut7X7hicFNjakmxibMLGWpXrEXB33367o7zHN0CwngnQY7zb7g/0"),
32 CustomCell: &CustomCell{
33 Words: wxpay_utility.String("电子发票"),
34 Description: wxpay_utility.String("查看发票"),
35 JumpUrl: wxpay_utility.String("http://www.qq.com"),
36 MiniprogramUserName: wxpay_utility.String("gh_86a091e50ad4@app"),
37 MiniprogramPath: wxpay_utility.String("pages/xxxPage"),
38 },
39 },
40 }
41
42 response, err := CreateFapiaoCardTemplate(config, request)
43 if err != nil {
44 fmt.Printf("请求失败: %+v\n", err)
45
46 return
47 }
48
49
50 fmt.Printf("请求成功: %+v\n", response)
51}
52
53func CreateFapiaoCardTemplate(config *wxpay_utility.MchConfig, request *CreateFapiaoCardTemplateRequest) (response *FapiaoCardTemplateEntity, err error) {
54 const (
55 host = "https://api.mch.weixin.qq.com"
56 method = "POST"
57 path = "/v3/new-tax-control-fapiao/card-template"
58 )
59
60 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
61 if err != nil {
62 return nil, err
63 }
64 reqBody, err := json.Marshal(request)
65 if err != nil {
66 return nil, err
67 }
68 httpRequest, err := http.NewRequest(method, reqUrl.String(), bytes.NewReader(reqBody))
69 if err != nil {
70 return nil, err
71 }
72 httpRequest.Header.Set("Accept", "application/json")
73 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
74 httpRequest.Header.Set("Content-Type", "application/json")
75 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), reqBody)
76 if err != nil {
77 return nil, err
78 }
79 httpRequest.Header.Set("Authorization", authorization)
80
81 client := &http.Client{}
82 httpResponse, err := client.Do(httpRequest)
83 if err != nil {
84 return nil, err
85 }
86 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse)
87 if err != nil {
88 return nil, err
89 }
90 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
91
92 err = wxpay_utility.ValidateResponse(
93 config.WechatPayPublicKeyId(),
94 config.WechatPayPublicKey(),
95 &httpResponse.Header,
96 respBody,
97 )
98 if err != nil {
99 return nil, err
100 }
101 response := &FapiaoCardTemplateEntity{}
102 if err := json.Unmarshal(respBody, response); err != nil {
103 return nil, err
104 }
105
106 return response, nil
107 } else {
108 return nil, wxpay_utility.NewApiException(
109 httpResponse.StatusCode,
110 httpResponse.Header,
111 respBody,
112 )
113 }
114}
115
116type CreateFapiaoCardTemplateRequest struct {
117 SubMchid *string `json:"sub_mchid,omitempty"`
118 CardAppid *string `json:"card_appid,omitempty"`
119 CardTemplateInformation *CardTemplateInfo `json:"card_template_information,omitempty"`
120}
121
122type FapiaoCardTemplateEntity struct {
123 CardAppid *string `json:"card_appid,omitempty"`
124 CardId *string `json:"card_id,omitempty"`
125}
126
127type CardTemplateInfo struct {
128 PayeeName *string `json:"payee_name,omitempty"`
129 LogoUrl *string `json:"logo_url,omitempty"`
130 CustomCell *CustomCell `json:"custom_cell,omitempty"`
131}
132
133type CustomCell struct {
134 Words *string `json:"words,omitempty"`
135 Description *string `json:"description,omitempty"`
136 JumpUrl *string `json:"jump_url,omitempty"`
137 MiniprogramUserName *string `json:"miniprogram_user_name,omitempty"`
138 MiniprogramPath *string `json:"miniprogram_path,omitempty"`
139}
140