
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 := &IssueRefinedOilFapiaoRequest{
27 SubMchid: wxpay_utility.String("1900000109"),
28 FapiaoApplyId: wxpay_utility.String("4200000444201910177461284488"),
29 BuyerInformation: &UserTitleEntity{
30 Type: BUYERTYPE_INDIVIDUAL.Ptr(),
31 Name: wxpay_utility.String("深圳市南山区测试企业"),
32 TaxpayerId: wxpay_utility.String("202003261233701778"),
33 Address: wxpay_utility.String("深圳市南山区深南大道10000号"),
34 Telephone: wxpay_utility.String("075512345678"),
35 BankName: wxpay_utility.String("测试银行"),
36 BankAccount: wxpay_utility.String("62001234567890"),
37 Phone: wxpay_utility.String("mI7HGEJ4Q2B91IGjHrl7FNN9QuFPDfzeXoaJM4B8ZghZPzXK+vNotEZu/Gthm87Szv0MK2AoC0/3ZMDgltMtdoY6O0qZ4F1iXiwCuqkkBe+9M4ggvdzpPGM+fyed2QU1seUGbii5RVVVB9s+zLEQ8nv74vsgl77MZx14nd5obtCcfAvPfDJob3oG7FqlThmYKJqjiOwBvvQse7p9R8onj/POzSrbM8re8ZYGp4LcehXopTLdk2ZVWRv8bnJgKZWArAcqMdahq4jY2UVYeY4vpMmq4xuRTYk6xNXvowBBKK2SX8SqM+jm1USyoBIazLu4oaNFNdBO3fip1a1rFW0vRw=="),
38 Email: wxpay_utility.String("NzJy3r0Z2u2Vs5l+WSH0A3CZ1oGlCZ66aa2wUlMXNmACbd4wU8LqqYCuTG4cYWxrVUSmviV59/Uy9vLdIwuHZVrMalYAZGtb8inWGhDj3wUqQnPkmBKBVGIWG5Y6XJmMvpXW6rIKsdzxs8NwWj30cRNfjanLxiWc0aIgl8Knwo0JcxlcYLo38T9ntsrRkQZMQEWHaMYnzjp7ysLbp6yW83OZb/NwEufERBdPnIbDbVE7DUd5MGhvO+tlr2YC1b4VCsrDmjryuTD5nvYYCGHyfXW2CM23hZdBm9tPc+mU18Z9d4XkasnfsecGWd2ISkpPmnk3DtapnD64Nw8JyLtkgw=="),
39 Amount: wxpay_utility.Int64(1000),
40 OutTradeNo: wxpay_utility.String("order_20200701_123456"),
41 FapiaoBillType: FAPIAOBILLTYPE_COMM_FAPIAO.Ptr(),
42 UserApplyMessage: wxpay_utility.String("用户留言"),
43 },
44 FapiaoInformation: &IssueRefinedOilFapiaoInfo{
45 FapiaoId: wxpay_utility.String("20200701123456"),
46 TotalAmount: wxpay_utility.Int64(382895),
47 Items: []IssueRefinedOilFapiaoItem{IssueRefinedOilFapiaoItem{
48 TaxCode: wxpay_utility.String("1070101020100000000"),
49 GoodsName: wxpay_utility.String("*燃料油*燃料油"),
50 Specification: wxpay_utility.String("92#"),
51 Unit: REFINEDOILUNIT_TON.Ptr(),
52 Quantity: wxpay_utility.Int64(100000000),
53 TotalAmount: wxpay_utility.Int64(429900),
54 TaxRate: wxpay_utility.Int64(1300),
55 Discount: wxpay_utility.Bool(false),
56 PreferentialPolicyCode: wxpay_utility.Int64(1),
57 RefinedOilInformation: &RefinedOilExtInfo{
58 Station: wxpay_utility.String("中国石油(江北红旗河沟加油站)"),
59 TradeTime: wxpay_utility.String("2025-07-01T00:00:00+08:00"),
60 GunNumber: wxpay_utility.String("1号枪"),
61 },
62 }},
63 ExportBusinessPolicyCode: wxpay_utility.Int64(1),
64 VatRefundLevyCode: wxpay_utility.Int64(1),
65 BillingPersonId: wxpay_utility.String("187776276864872"),
66 BillingPerson: wxpay_utility.String("**伟 0020"),
67 FapiaoBillType: FAPIAOBILLTYPE_COMM_FAPIAO.Ptr(),
68 TransactionInformation: []TransactionInfo{TransactionInfo{
69 PayChannel: PAYCHANNEL_WECHAT_PAY.Ptr(),
70 TransactionId: wxpay_utility.String("4200000008202508080512076713"),
71 OutTradeNo: wxpay_utility.String("order_20200701_123456"),
72 Amount: wxpay_utility.Int64(382895),
73 }},
74 Remark: wxpay_utility.String("备注"),
75 },
76 }
77
78 err = IssueRefinedOilFapiaoApplications(config, request)
79 if err != nil {
80 fmt.Printf("请求失败: %+v\n", err)
81
82 return
83 }
84
85
86 fmt.Println("请求成功")
87}
88
89func IssueRefinedOilFapiaoApplications(config *wxpay_utility.MchConfig, request *IssueRefinedOilFapiaoRequest) (err error) {
90 const (
91 host = "https://api.mch.weixin.qq.com"
92 method = "POST"
93 path = "/v3/new-tax-control-fapiao/fapiao-applications/issue-refined-oil"
94 )
95
96 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
97 if err != nil {
98 return err
99 }
100 reqBody, err := json.Marshal(request)
101 if err != nil {
102 return err
103 }
104 httpRequest, err := http.NewRequest(method, reqUrl.String(), bytes.NewReader(reqBody))
105 if err != nil {
106 return err
107 }
108 httpRequest.Header.Set("Accept", "application/json")
109 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
110 httpRequest.Header.Set("Content-Type", "application/json")
111 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), reqBody)
112 if err != nil {
113 return err
114 }
115 httpRequest.Header.Set("Authorization", authorization)
116
117 client := &http.Client{}
118 httpResponse, err := client.Do(httpRequest)
119 if err != nil {
120 return err
121 }
122 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse)
123 if err != nil {
124 return err
125 }
126 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
127
128 err = wxpay_utility.ValidateResponse(
129 config.WechatPayPublicKeyId(),
130 config.WechatPayPublicKey(),
131 &httpResponse.Header,
132 respBody,
133 )
134 if err != nil {
135 return err
136 }
137 return nil
138 } else {
139 return wxpay_utility.NewApiException(
140 httpResponse.StatusCode,
141 httpResponse.Header,
142 respBody,
143 )
144 }
145}
146
147type IssueRefinedOilFapiaoRequest struct {
148 SubMchid *string `json:"sub_mchid,omitempty"`
149 FapiaoApplyId *string `json:"fapiao_apply_id,omitempty"`
150 BuyerInformation *UserTitleEntity `json:"buyer_information,omitempty"`
151 FapiaoInformation *IssueRefinedOilFapiaoInfo `json:"fapiao_information,omitempty"`
152}
153
154type UserTitleEntity struct {
155 Type *BuyerType `json:"type,omitempty"`
156 Name *string `json:"name,omitempty"`
157 TaxpayerId *string `json:"taxpayer_id,omitempty"`
158 Address *string `json:"address,omitempty"`
159 Telephone *string `json:"telephone,omitempty"`
160 BankName *string `json:"bank_name,omitempty"`
161 BankAccount *string `json:"bank_account,omitempty"`
162 Phone *string `json:"phone,omitempty"`
163 Email *string `json:"email,omitempty"`
164 Amount *int64 `json:"amount,omitempty"`
165 OutTradeNo *string `json:"out_trade_no,omitempty"`
166 FapiaoBillType *FapiaoBillType `json:"fapiao_bill_type,omitempty"`
167 UserApplyMessage *string `json:"user_apply_message,omitempty"`
168}
169
170type IssueRefinedOilFapiaoInfo struct {
171 FapiaoId *string `json:"fapiao_id,omitempty"`
172 TotalAmount *int64 `json:"total_amount,omitempty"`
173 Items []IssueRefinedOilFapiaoItem `json:"items,omitempty"`
174 ExportBusinessPolicyCode *int64 `json:"export_business_policy_code,omitempty"`
175 VatRefundLevyCode *int64 `json:"vat_refund_levy_code,omitempty"`
176 BillingPersonId *string `json:"billing_person_id,omitempty"`
177 BillingPerson *string `json:"billing_person,omitempty"`
178 FapiaoBillType *FapiaoBillType `json:"fapiao_bill_type,omitempty"`
179 TransactionInformation []TransactionInfo `json:"transaction_information,omitempty"`
180 Remark *string `json:"remark,omitempty"`
181}
182
183type BuyerType string
184
185func (e BuyerType) Ptr() *BuyerType {
186 return &e
187}
188
189const (
190 BUYERTYPE_INDIVIDUAL BuyerType = "INDIVIDUAL"
191 BUYERTYPE_ORGANIZATION BuyerType = "ORGANIZATION"
192)
193
194type FapiaoBillType string
195
196func (e FapiaoBillType) Ptr() *FapiaoBillType {
197 return &e
198}
199
200const (
201 FAPIAOBILLTYPE_COMM_FAPIAO FapiaoBillType = "COMM_FAPIAO"
202 FAPIAOBILLTYPE_VAT_FAPIAO FapiaoBillType = "VAT_FAPIAO"
203)
204
205type IssueRefinedOilFapiaoItem struct {
206 TaxCode *string `json:"tax_code,omitempty"`
207 GoodsName *string `json:"goods_name,omitempty"`
208 Specification *string `json:"specification,omitempty"`
209 Unit *RefinedOilUnit `json:"unit,omitempty"`
210 Quantity *int64 `json:"quantity,omitempty"`
211 TotalAmount *int64 `json:"total_amount,omitempty"`
212 TaxRate *int64 `json:"tax_rate,omitempty"`
213 Discount *bool `json:"discount,omitempty"`
214 PreferentialPolicyCode *int64 `json:"preferential_policy_code,omitempty"`
215 RefinedOilInformation *RefinedOilExtInfo `json:"refined_oil_information,omitempty"`
216}
217
218type TransactionInfo struct {
219 PayChannel *PayChannel `json:"pay_channel,omitempty"`
220 TransactionId *string `json:"transaction_id,omitempty"`
221 OutTradeNo *string `json:"out_trade_no,omitempty"`
222 Amount *int64 `json:"amount,omitempty"`
223}
224
225type RefinedOilUnit string
226
227func (e RefinedOilUnit) Ptr() *RefinedOilUnit {
228 return &e
229}
230
231const (
232 REFINEDOILUNIT_TON RefinedOilUnit = "TON"
233 REFINEDOILUNIT_LITER RefinedOilUnit = "LITER"
234)
235
236type RefinedOilExtInfo struct {
237 Station *string `json:"station,omitempty"`
238 TradeTime *string `json:"trade_time,omitempty"`
239 GunNumber *string `json:"gun_number,omitempty"`
240}
241
242type PayChannel string
243
244func (e PayChannel) Ptr() *PayChannel {
245 return &e
246}
247
248const (
249 PAYCHANNEL_WECHAT_PAY PayChannel = "WECHAT_PAY"
250)
251