
1package main
2
3import (
4 "bytes"
5 "demo/wxpay_utility"
6 "encoding/json"
7 "fmt"
8 "net/http"
9 "net/url"
10 "strings"
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 := &InsertCardsRequest{
28 FapiaoApplyId: wxpay_utility.String("4200000444201910177461284488"),
29 SubMchid: wxpay_utility.String("1900000109"),
30 Scene: SCENE_WITH_WECHATPAY.Ptr(),
31 BuyerInformation: &UserTitleEntity{
32 Type: BUYERTYPE_INDIVIDUAL.Ptr(),
33 Name: wxpay_utility.String("深圳市南山区测试企业"),
34 TaxpayerId: wxpay_utility.String("202003261233701778"),
35 Address: wxpay_utility.String("深圳市南山区深南大道10000号"),
36 Telephone: wxpay_utility.String("075512345678"),
37 BankName: wxpay_utility.String("测试银行"),
38 BankAccount: wxpay_utility.String("62001234567890"),
39 Phone: wxpay_utility.String("mI7HGEJ4Q2B91IGjHrl7FNN9QuFPDfzeXoaJM4B8ZghZPzXK+vNotEZu/Gthm87Szv0MK2AoC0/3ZMDgltMtdoY6O0qZ4F1iXiwCuqkkBe+9M4ggvdzpPGM+fyed2QU1seUGbii5RVVVB9s+zLEQ8nv74vsgl77MZx14nd5obtCcfAvPfDJob3oG7FqlThmYKJqjiOwBvvQse7p9R8onj/POzSrbM8re8ZYGp4LcehXopTLdk2ZVWRv8bnJgKZWArAcqMdahq4jY2UVYeY4vpMmq4xuRTYk6xNXvowBBKK2SX8SqM+jm1USyoBIazLu4oaNFNdBO3fip1a1rFW0vRw=="),
40 Email: wxpay_utility.String("NzJy3r0Z2u2Vs5l+WSH0A3CZ1oGlCZ66aa2wUlMXNmACbd4wU8LqqYCuTG4cYWxrVUSmviV59/Uy9vLdIwuHZVrMalYAZGtb8inWGhDj3wUqQnPkmBKBVGIWG5Y6XJmMvpXW6rIKsdzxs8NwWj30cRNfjanLxiWc0aIgl8Knwo0JcxlcYLo38T9ntsrRkQZMQEWHaMYnzjp7ysLbp6yW83OZb/NwEufERBdPnIbDbVE7DUd5MGhvO+tlr2YC1b4VCsrDmjryuTD5nvYYCGHyfXW2CM23hZdBm9tPc+mU18Z9d4XkasnfsecGWd2ISkpPmnk3DtapnD64Nw8JyLtkgw=="),
41 Amount: wxpay_utility.Int64(1000),
42 OutTradeNo: wxpay_utility.String("order_20200701_123456"),
43 FapiaoBillType: FAPIAOBILLTYPE_COMM_FAPIAO.Ptr(),
44 UserApplyMessage: wxpay_utility.String("用户留言"),
45 },
46 FapiaoCardInformation: []FapiaoCardInfo{FapiaoCardInfo{
47 FapiaoMediaId: wxpay_utility.String("ASNFZ4mrze/+3LqYdlQyEA=="),
48 FapiaoNumber: wxpay_utility.String("12897794"),
49 FapiaoCode: wxpay_utility.String("044001911211"),
50 FapiaoTime: wxpay_utility.String("2020-07-01T12:00:00+08:00"),
51 CheckCode: wxpay_utility.String("69001808340631374774"),
52 Password: wxpay_utility.String("006>299-375/326>2+7/*0-+<351059<80<4*/5>+<11631+*3030/5*37+/-243159658+013>3409*044>4-/1+/9->*>69501*6++1997--21"),
53 TotalAmount: wxpay_utility.Int64(382895),
54 TaxAmount: wxpay_utility.Int64(44050),
55 Amount: wxpay_utility.Int64(338845),
56 SellerInformation: &SellerInfo{
57 Name: wxpay_utility.String("深圳市南山区测试公司"),
58 TaxpayerId: wxpay_utility.String("202003261233701778"),
59 Address: wxpay_utility.String("深圳市南山区深南大道10000号"),
60 Telephone: wxpay_utility.String("075512345678"),
61 BankName: wxpay_utility.String("测试银行"),
62 BankAccount: wxpay_utility.String("62001234567890"),
63 },
64 ExtraInformation: &ExtraInfoForInsert{
65 Drawer: wxpay_utility.String("王五"),
66 },
67 Items: []FapiaoItem{FapiaoItem{
68 TaxCode: wxpay_utility.String("3010101020203000000"),
69 GoodsName: wxpay_utility.String("出租汽车客运服务"),
70 Specification: wxpay_utility.String("A4"),
71 Unit: wxpay_utility.String("次"),
72 Quantity: wxpay_utility.Int64(100000000),
73 UnitPrice: wxpay_utility.Int64(380442000000),
74 Amount: wxpay_utility.Int64(380442),
75 TaxAmount: wxpay_utility.Int64(49458),
76 TotalAmount: wxpay_utility.Int64(429900),
77 TaxRate: wxpay_utility.Int64(1300),
78 TaxPreferMark: TAXPREFERMARK_NO_FAVORABLE.Ptr(),
79 Discount: wxpay_utility.Bool(false),
80 }},
81 Remark: wxpay_utility.String("备注"),
82 }},
83 }
84
85 err = InsertCards(config, request)
86 if err != nil {
87 fmt.Printf("请求失败: %+v\n", err)
88
89 return
90 }
91
92
93 fmt.Println("请求成功")
94}
95
96func InsertCards(config *wxpay_utility.MchConfig, request *InsertCardsRequest) (err error) {
97 const (
98 host = "https://api.mch.weixin.qq.com"
99 method = "POST"
100 path = "/v3/new-tax-control-fapiao/fapiao-applications/{fapiao_apply_id}/insert-cards"
101 )
102
103 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
104 if err != nil {
105 return err
106 }
107 reqUrl.Path = strings.Replace(reqUrl.Path, "{fapiao_apply_id}", url.PathEscape(*request.FapiaoApplyId), -1)
108 reqBody, err := json.Marshal(request)
109 if err != nil {
110 return err
111 }
112 httpRequest, err := http.NewRequest(method, reqUrl.String(), bytes.NewReader(reqBody))
113 if err != nil {
114 return err
115 }
116 httpRequest.Header.Set("Accept", "application/json")
117 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
118 httpRequest.Header.Set("Content-Type", "application/json")
119 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), reqBody)
120 if err != nil {
121 return err
122 }
123 httpRequest.Header.Set("Authorization", authorization)
124
125 client := &http.Client{}
126 httpResponse, err := client.Do(httpRequest)
127 if err != nil {
128 return err
129 }
130 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse)
131 if err != nil {
132 return err
133 }
134 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
135
136 err = wxpay_utility.ValidateResponse(
137 config.WechatPayPublicKeyId(),
138 config.WechatPayPublicKey(),
139 &httpResponse.Header,
140 respBody,
141 )
142 if err != nil {
143 return err
144 }
145 return nil
146 } else {
147 return wxpay_utility.NewApiException(
148 httpResponse.StatusCode,
149 httpResponse.Header,
150 respBody,
151 )
152 }
153}
154
155type InsertCardsRequest struct {
156 SubMchid *string `json:"sub_mchid,omitempty"`
157 Scene *Scene `json:"scene,omitempty"`
158 FapiaoApplyId *string `json:"fapiao_apply_id,omitempty"`
159 BuyerInformation *UserTitleEntity `json:"buyer_information,omitempty"`
160 FapiaoCardInformation []FapiaoCardInfo `json:"fapiao_card_information,omitempty"`
161}
162
163func (o *InsertCardsRequest) MarshalJSON() ([]byte, error) {
164 type Alias InsertCardsRequest
165 a := &struct {
166 FapiaoApplyId *string `json:"fapiao_apply_id,omitempty"`
167 *Alias
168 }{
169
170 FapiaoApplyId: nil,
171 Alias: (*Alias)(o),
172 }
173 return json.Marshal(a)
174}
175
176type Scene string
177
178func (e Scene) Ptr() *Scene {
179 return &e
180}
181
182const (
183 SCENE_WITH_WECHATPAY Scene = "WITH_WECHATPAY"
184 SCENE_WITHOUT_WECHATPAY Scene = "WITHOUT_WECHATPAY"
185)
186
187type UserTitleEntity struct {
188 Type *BuyerType `json:"type,omitempty"`
189 Name *string `json:"name,omitempty"`
190 TaxpayerId *string `json:"taxpayer_id,omitempty"`
191 Address *string `json:"address,omitempty"`
192 Telephone *string `json:"telephone,omitempty"`
193 BankName *string `json:"bank_name,omitempty"`
194 BankAccount *string `json:"bank_account,omitempty"`
195 Phone *string `json:"phone,omitempty"`
196 Email *string `json:"email,omitempty"`
197 Amount *int64 `json:"amount,omitempty"`
198 OutTradeNo *string `json:"out_trade_no,omitempty"`
199 FapiaoBillType *FapiaoBillType `json:"fapiao_bill_type,omitempty"`
200 UserApplyMessage *string `json:"user_apply_message,omitempty"`
201}
202
203type FapiaoCardInfo struct {
204 FapiaoMediaId *string `json:"fapiao_media_id,omitempty"`
205 FapiaoNumber *string `json:"fapiao_number,omitempty"`
206 FapiaoCode *string `json:"fapiao_code,omitempty"`
207 FapiaoTime *string `json:"fapiao_time,omitempty"`
208 CheckCode *string `json:"check_code,omitempty"`
209 Password *string `json:"password,omitempty"`
210 TotalAmount *int64 `json:"total_amount,omitempty"`
211 TaxAmount *int64 `json:"tax_amount,omitempty"`
212 Amount *int64 `json:"amount,omitempty"`
213 SellerInformation *SellerInfo `json:"seller_information,omitempty"`
214 ExtraInformation *ExtraInfoForInsert `json:"extra_information,omitempty"`
215 Items []FapiaoItem `json:"items,omitempty"`
216 Remark *string `json:"remark,omitempty"`
217}
218
219type BuyerType string
220
221func (e BuyerType) Ptr() *BuyerType {
222 return &e
223}
224
225const (
226 BUYERTYPE_INDIVIDUAL BuyerType = "INDIVIDUAL"
227 BUYERTYPE_ORGANIZATION BuyerType = "ORGANIZATION"
228)
229
230type FapiaoBillType string
231
232func (e FapiaoBillType) Ptr() *FapiaoBillType {
233 return &e
234}
235
236const (
237 FAPIAOBILLTYPE_COMM_FAPIAO FapiaoBillType = "COMM_FAPIAO"
238 FAPIAOBILLTYPE_VAT_FAPIAO FapiaoBillType = "VAT_FAPIAO"
239)
240
241type SellerInfo struct {
242 Name *string `json:"name,omitempty"`
243 TaxpayerId *string `json:"taxpayer_id,omitempty"`
244 Address *string `json:"address,omitempty"`
245 Telephone *string `json:"telephone,omitempty"`
246 BankName *string `json:"bank_name,omitempty"`
247 BankAccount *string `json:"bank_account,omitempty"`
248}
249
250type ExtraInfoForInsert struct {
251 Drawer *string `json:"drawer,omitempty"`
252}
253
254type FapiaoItem struct {
255 TaxCode *string `json:"tax_code,omitempty"`
256 GoodsName *string `json:"goods_name,omitempty"`
257 Specification *string `json:"specification,omitempty"`
258 Unit *string `json:"unit,omitempty"`
259 Quantity *int64 `json:"quantity,omitempty"`
260 UnitPrice *int64 `json:"unit_price,omitempty"`
261 Amount *int64 `json:"amount,omitempty"`
262 TaxAmount *int64 `json:"tax_amount,omitempty"`
263 TotalAmount *int64 `json:"total_amount,omitempty"`
264 TaxRate *int64 `json:"tax_rate,omitempty"`
265 TaxPreferMark *TaxPreferMark `json:"tax_prefer_mark,omitempty"`
266 Discount *bool `json:"discount,omitempty"`
267}
268
269type TaxPreferMark string
270
271func (e TaxPreferMark) Ptr() *TaxPreferMark {
272 return &e
273}
274
275const (
276 TAXPREFERMARK_NO_FAVORABLE TaxPreferMark = "NO_FAVORABLE"
277 TAXPREFERMARK_OUTSIDE_VAT TaxPreferMark = "OUTSIDE_VAT"
278 TAXPREFERMARK_VAT_EXEMPT TaxPreferMark = "VAT_EXEMPT"
279 TAXPREFERMARK_NORMAL_ZERO_RATED TaxPreferMark = "NORMAL_ZERO_RATED"
280 TAXPREFERMARK_EXPORT_ZERO_RATED TaxPreferMark = "EXPORT_ZERO_RATED"
281)
282