---
url: "https://pay.weixin.qq.com/doc/v3/partner/4016760559-go.md"
title: "开具成品油行业电子发票-请求示例-go"
doc_id: 4016760559
example_lang: "go"
---

## Go


需配合微信支付工具库 wxpay\_utility 使用，请参考 [Go](https://pay.weixin.qq.com/doc/v3/partner/4015119446.md)

```
package main

import (
	"bytes"
	"demo/wxpay_utility" // 引用微信支付工具库，参考 https://pay.weixin.qq.com/doc/v3/partner/4015119446
	"encoding/json"
	"fmt"
	"net/http"
	"net/url"
)

func main() {
	// TODO: 请准备商户开发必要参数，参考：https://pay.weixin.qq.com/doc/v3/partner/4013080340
	config, err := wxpay_utility.CreateMchConfig(
		"19xxxxxxxx",                 // 商户号，是由微信支付系统生成并分配给每个商户的唯一标识符，商户号获取方式参考 https://pay.weixin.qq.com/doc/v3/partner/4013080340
		"1DDE55AD98Exxxxxxxxxx",      // 商户API证书序列号，如何获取请参考 https://pay.weixin.qq.com/doc/v3/partner/4013058924
		"/path/to/apiclient_key.pem", // 商户API证书私钥文件路径，本地文件路径
		"PUB_KEY_ID_xxxxxxxxxxxxx",   // 微信支付公钥ID，如何获取请参考 https://pay.weixin.qq.com/doc/v3/partner/4013038589
		"/path/to/wxp_pub.pem",       // 微信支付公钥文件路径，本地文件路径
	)
	if err != nil {
		fmt.Println(err)
		return
	}

	request := &IssueRefinedOilFapiaoRequest{
		SubMchid:      wxpay_utility.String("1900000109"),
		FapiaoApplyId: wxpay_utility.String("4200000444201910177461284488"),
		BuyerInformation: &UserTitleEntity{
			Type:             BUYERTYPE_INDIVIDUAL.Ptr(),
			Name:             wxpay_utility.String("深圳市南山区测试企业"),
			TaxpayerId:       wxpay_utility.String("202003261233701778"),
			Address:          wxpay_utility.String("深圳市南山区深南大道10000号"),
			Telephone:        wxpay_utility.String("075512345678"),
			BankName:         wxpay_utility.String("测试银行"),
			BankAccount:      wxpay_utility.String("62001234567890"),
			Phone:            wxpay_utility.String("mI7HGEJ4Q2B91IGjHrl7FNN9QuFPDfzeXoaJM4B8ZghZPzXK+vNotEZu/Gthm87Szv0MK2AoC0/3ZMDgltMtdoY6O0qZ4F1iXiwCuqkkBe+9M4ggvdzpPGM+fyed2QU1seUGbii5RVVVB9s+zLEQ8nv74vsgl77MZx14nd5obtCcfAvPfDJob3oG7FqlThmYKJqjiOwBvvQse7p9R8onj/POzSrbM8re8ZYGp4LcehXopTLdk2ZVWRv8bnJgKZWArAcqMdahq4jY2UVYeY4vpMmq4xuRTYk6xNXvowBBKK2SX8SqM+jm1USyoBIazLu4oaNFNdBO3fip1a1rFW0vRw=="), /*请传入wxpay_utility.EncryptOAEPWithPublicKey 加密结果*/
			Email:            wxpay_utility.String("NzJy3r0Z2u2Vs5l+WSH0A3CZ1oGlCZ66aa2wUlMXNmACbd4wU8LqqYCuTG4cYWxrVUSmviV59/Uy9vLdIwuHZVrMalYAZGtb8inWGhDj3wUqQnPkmBKBVGIWG5Y6XJmMvpXW6rIKsdzxs8NwWj30cRNfjanLxiWc0aIgl8Knwo0JcxlcYLo38T9ntsrRkQZMQEWHaMYnzjp7ysLbp6yW83OZb/NwEufERBdPnIbDbVE7DUd5MGhvO+tlr2YC1b4VCsrDmjryuTD5nvYYCGHyfXW2CM23hZdBm9tPc+mU18Z9d4XkasnfsecGWd2ISkpPmnk3DtapnD64Nw8JyLtkgw=="), /*请传入wxpay_utility.EncryptOAEPWithPublicKey 加密结果*/
			Amount:           wxpay_utility.Int64(1000),
			OutTradeNo:       wxpay_utility.String("order_20200701_123456"),
			FapiaoBillType:   FAPIAOBILLTYPE_COMM_FAPIAO.Ptr(),
			UserApplyMessage: wxpay_utility.String("用户留言"),
		},
		FapiaoInformation: &IssueRefinedOilFapiaoInfo{
			FapiaoId:    wxpay_utility.String("20200701123456"),
			TotalAmount: wxpay_utility.Int64(382895),
			Items: []IssueRefinedOilFapiaoItem{IssueRefinedOilFapiaoItem{
				TaxCode:                wxpay_utility.String("1070101020100000000"),
				GoodsName:              wxpay_utility.String("*燃料油*燃料油"),
				Specification:          wxpay_utility.String("92#"),
				Unit:                   REFINEDOILUNIT_TON.Ptr(),
				Quantity:               wxpay_utility.Int64(100000000),
				TotalAmount:            wxpay_utility.Int64(429900),
				TaxRate:                wxpay_utility.Int64(1300),
				Discount:               wxpay_utility.Bool(false),
				PreferentialPolicyCode: wxpay_utility.Int64(1),
				RefinedOilInformation: &RefinedOilExtInfo{
					Station:   wxpay_utility.String("中国石油(江北红旗河沟加油站)"),
					TradeTime: wxpay_utility.String("2025-07-01T00:00:00+08:00"),
					GunNumber: wxpay_utility.String("1号枪"),
				},
			}},
			ExportBusinessPolicyCode: wxpay_utility.Int64(1),
			VatRefundLevyCode:        wxpay_utility.Int64(1),
			BillingPersonId:          wxpay_utility.String("187776276864872"),
			BillingPerson:            wxpay_utility.String("**伟 0020"),
			FapiaoBillType:           FAPIAOBILLTYPE_COMM_FAPIAO.Ptr(),
			TransactionInformation: []TransactionInfo{TransactionInfo{
				PayChannel:    PAYCHANNEL_WECHAT_PAY.Ptr(),
				TransactionId: wxpay_utility.String("4200000008202508080512076713"),
				OutTradeNo:    wxpay_utility.String("order_20200701_123456"),
				Amount:        wxpay_utility.Int64(382895),
			}},
			Remark: wxpay_utility.String("备注"),
		},
	}

	err = IssueRefinedOilFapiaoApplications(config, request)
	if err != nil {
		fmt.Printf("请求失败: %+v\n", err)
		// TODO: 请求失败，根据状态码执行不同的处理
		return
	}

	// TODO: 请求成功，继续业务逻辑
	fmt.Println("请求成功")
}

func IssueRefinedOilFapiaoApplications(config *wxpay_utility.MchConfig, request *IssueRefinedOilFapiaoRequest) (err error) {
	const (
		host   = "https://api.mch.weixin.qq.com"
		method = "POST"
		path   = "/v3/new-tax-control-fapiao/fapiao-applications/issue-refined-oil"
	)

	reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
	if err != nil {
		return err
	}
	reqBody, err := json.Marshal(request)
	if err != nil {
		return err
	}
	httpRequest, err := http.NewRequest(method, reqUrl.String(), bytes.NewReader(reqBody))
	if err != nil {
		return err
	}
	httpRequest.Header.Set("Accept", "application/json")
	httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
	httpRequest.Header.Set("Content-Type", "application/json")
	authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), reqBody)
	if err != nil {
		return err
	}
	httpRequest.Header.Set("Authorization", authorization)

	client := &http.Client{}
	httpResponse, err := client.Do(httpRequest)
	if err != nil {
		return err
	}
	respBody, err := wxpay_utility.ExtractResponseBody(httpResponse)
	if err != nil {
		return err
	}
	if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
		// 2XX 成功，验证应答签名
		err = wxpay_utility.ValidateResponse(
			config.WechatPayPublicKeyId(),
			config.WechatPayPublicKey(),
			&httpResponse.Header,
			respBody,
		)
		if err != nil {
			return err
		}
		return nil
	} else {
		return wxpay_utility.NewApiException(
			httpResponse.StatusCode,
			httpResponse.Header,
			respBody,
		)
	}
}

type IssueRefinedOilFapiaoRequest struct {
	SubMchid          *string                    `json:"sub_mchid,omitempty"`
	FapiaoApplyId     *string                    `json:"fapiao_apply_id,omitempty"`
	BuyerInformation  *UserTitleEntity           `json:"buyer_information,omitempty"`
	FapiaoInformation *IssueRefinedOilFapiaoInfo `json:"fapiao_information,omitempty"`
}

type UserTitleEntity struct {
	Type             *BuyerType      `json:"type,omitempty"`
	Name             *string         `json:"name,omitempty"`
	TaxpayerId       *string         `json:"taxpayer_id,omitempty"`
	Address          *string         `json:"address,omitempty"`
	Telephone        *string         `json:"telephone,omitempty"`
	BankName         *string         `json:"bank_name,omitempty"`
	BankAccount      *string         `json:"bank_account,omitempty"`
	Phone            *string         `json:"phone,omitempty"`
	Email            *string         `json:"email,omitempty"`
	Amount           *int64          `json:"amount,omitempty"`
	OutTradeNo       *string         `json:"out_trade_no,omitempty"`
	FapiaoBillType   *FapiaoBillType `json:"fapiao_bill_type,omitempty"`
	UserApplyMessage *string         `json:"user_apply_message,omitempty"`
}

type IssueRefinedOilFapiaoInfo struct {
	FapiaoId                 *string                     `json:"fapiao_id,omitempty"`
	TotalAmount              *int64                      `json:"total_amount,omitempty"`
	Items                    []IssueRefinedOilFapiaoItem `json:"items,omitempty"`
	ExportBusinessPolicyCode *int64                      `json:"export_business_policy_code,omitempty"`
	VatRefundLevyCode        *int64                      `json:"vat_refund_levy_code,omitempty"`
	BillingPersonId          *string                     `json:"billing_person_id,omitempty"`
	BillingPerson            *string                     `json:"billing_person,omitempty"`
	FapiaoBillType           *FapiaoBillType             `json:"fapiao_bill_type,omitempty"`
	TransactionInformation   []TransactionInfo           `json:"transaction_information,omitempty"`
	Remark                   *string                     `json:"remark,omitempty"`
}

type BuyerType string

func (e BuyerType) Ptr() *BuyerType {
	return &e
}

const (
	BUYERTYPE_INDIVIDUAL   BuyerType = "INDIVIDUAL"
	BUYERTYPE_ORGANIZATION BuyerType = "ORGANIZATION"
)

type FapiaoBillType string

func (e FapiaoBillType) Ptr() *FapiaoBillType {
	return &e
}

const (
	FAPIAOBILLTYPE_COMM_FAPIAO FapiaoBillType = "COMM_FAPIAO"
	FAPIAOBILLTYPE_VAT_FAPIAO  FapiaoBillType = "VAT_FAPIAO"
)

type IssueRefinedOilFapiaoItem struct {
	TaxCode                *string            `json:"tax_code,omitempty"`
	GoodsName              *string            `json:"goods_name,omitempty"`
	Specification          *string            `json:"specification,omitempty"`
	Unit                   *RefinedOilUnit    `json:"unit,omitempty"`
	Quantity               *int64             `json:"quantity,omitempty"`
	TotalAmount            *int64             `json:"total_amount,omitempty"`
	TaxRate                *int64             `json:"tax_rate,omitempty"`
	Discount               *bool              `json:"discount,omitempty"`
	PreferentialPolicyCode *int64             `json:"preferential_policy_code,omitempty"`
	RefinedOilInformation  *RefinedOilExtInfo `json:"refined_oil_information,omitempty"`
}

type TransactionInfo struct {
	PayChannel    *PayChannel `json:"pay_channel,omitempty"`
	TransactionId *string     `json:"transaction_id,omitempty"`
	OutTradeNo    *string     `json:"out_trade_no,omitempty"`
	Amount        *int64      `json:"amount,omitempty"`
}

type RefinedOilUnit string

func (e RefinedOilUnit) Ptr() *RefinedOilUnit {
	return &e
}

const (
	REFINEDOILUNIT_TON   RefinedOilUnit = "TON"
	REFINEDOILUNIT_LITER RefinedOilUnit = "LITER"
)

type RefinedOilExtInfo struct {
	Station   *string `json:"station,omitempty"`
	TradeTime *string `json:"trade_time,omitempty"`
	GunNumber *string `json:"gun_number,omitempty"`
}

type PayChannel string

func (e PayChannel) Ptr() *PayChannel {
	return &e
}

const (
	PAYCHANNEL_WECHAT_PAY PayChannel = "WECHAT_PAY"
)

```
