---
url: "https://pay.weixin.qq.com/doc/v3/partner/4019557757-go.md"
title: "同步车辆入场通知-请求示例-go"
doc_id: 4019557757
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 := &SyncParkingEntryRequest{
		OutSerialNumber:    wxpay_utility.String("PARK20260423001"),
		ParkingId:          wxpay_utility.String("21232735744117624001123604298240"),
		PlateNumber:        wxpay_utility.String("粤B12345"),
		EnterTimestamp:     wxpay_utility.Int64(1719128975),
		PlateColor:         PLATECOLOR_PLATE_COLOR_UNKNOWN.Ptr(),
		CarType:            PLATETYPE_PLATE_TYPE_UNKNOWN.Ptr(),
		VehicleType:        VEHICLETYPE_VEHICLE_TYPE_UNKNOWN.Ptr(),
		EntranceNumber:     wxpay_utility.String("A01"),
		EntranceName:       wxpay_utility.String("东门入口"),
		DiscountTemplateId: []int64{int64(1)},
	}

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

	// TODO: 请求成功，继续业务逻辑
	fmt.Printf("请求成功: %+v\n", response)
}

func SyncParkingEntry(config *wxpay_utility.MchConfig, request *SyncParkingEntryRequest) (response *SyncParkingEntryResponse, err error) {
	const (
		host   = "https://api.mch.weixin.qq.com"
		method = "POST"
		path   = "/v3/parking/reminders/entry"
	)

	reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
	if err != nil {
		return nil, err
	}
	reqBody, err := json.Marshal(request)
	if err != nil {
		return nil, err
	}
	httpRequest, err := http.NewRequest(method, reqUrl.String(), bytes.NewReader(reqBody))
	if err != nil {
		return nil, 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 nil, err
	}
	httpRequest.Header.Set("Authorization", authorization)

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

		return response, nil
	} else {
		return nil, wxpay_utility.NewApiException(
			httpResponse.StatusCode,
			httpResponse.Header,
			respBody,
		)
	}
}

type SyncParkingEntryRequest struct {
	OutSerialNumber    *string      `json:"out_serial_number,omitempty"`
	ParkingId          *string      `json:"parking_id,omitempty"`
	PlateNumber        *string      `json:"plate_number,omitempty"`
	EnterTimestamp     *int64       `json:"enter_timestamp,omitempty"`
	PlateColor         *PlateColor  `json:"plate_color,omitempty"`
	CarType            *PlateType   `json:"car_type,omitempty"`
	VehicleType        *VehicleType `json:"vehicle_type,omitempty"`
	EntranceNumber     *string      `json:"entrance_number,omitempty"`
	EntranceName       *string      `json:"entrance_name,omitempty"`
	DiscountTemplateId []int64      `json:"discount_template_id,omitempty"`
}

type SyncParkingEntryResponse struct {
	Code         *string `json:"code,omitempty"`
	Message      *string `json:"message,omitempty"`
	SerialNumber *string `json:"serial_number,omitempty"`
}

type PlateColor string

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

const (
	PLATECOLOR_PLATE_COLOR_UNKNOWN             PlateColor = "PLATE_COLOR_UNKNOWN"
	PLATECOLOR_PLATE_COLOR_BLUE                PlateColor = "PLATE_COLOR_BLUE"
	PLATECOLOR_PLATE_COLOR_YELLOW              PlateColor = "PLATE_COLOR_YELLOW"
	PLATECOLOR_PLATE_COLOR_BLACK               PlateColor = "PLATE_COLOR_BLACK"
	PLATECOLOR_PLATE_COLOR_WHITE               PlateColor = "PLATE_COLOR_WHITE"
	PLATECOLOR_PLATE_COLOR_GREEN               PlateColor = "PLATE_COLOR_GREEN"
	PLATECOLOR_PLATE_COLOR_YELLOW_GREEN        PlateColor = "PLATE_COLOR_YELLOW_GREEN"
	PLATECOLOR_PLATE_COLOR_AGRICULTURAL_YELLOW PlateColor = "PLATE_COLOR_AGRICULTURAL_YELLOW"
	PLATECOLOR_PLATE_COLOR_OTHER               PlateColor = "PLATE_COLOR_OTHER"
)

type PlateType string

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

const (
	PLATETYPE_PLATE_TYPE_UNKNOWN   PlateType = "PLATE_TYPE_UNKNOWN"
	PLATETYPE_PLATE_TYPE_TEMPORARY PlateType = "PLATE_TYPE_TEMPORARY"
	PLATETYPE_PLATE_TYPE_MONTHLY   PlateType = "PLATE_TYPE_MONTHLY"
	PLATETYPE_PLATE_TYPE_FREE      PlateType = "PLATE_TYPE_FREE"
	PLATETYPE_PLATE_TYPE_PREPAID   PlateType = "PLATE_TYPE_PREPAID"
	PLATETYPE_PLATE_TYPE_OTHERS    PlateType = "PLATE_TYPE_OTHERS"
)

type VehicleType string

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

const (
	VEHICLETYPE_VEHICLE_TYPE_UNKNOWN     VehicleType = "VEHICLE_TYPE_UNKNOWN"
	VEHICLETYPE_VEHICLE_TYPE_SMALL       VehicleType = "VEHICLE_TYPE_SMALL"
	VEHICLETYPE_VEHICLE_TYPE_LARGE       VehicleType = "VEHICLE_TYPE_LARGE"
	VEHICLETYPE_VEHICLE_TYPE_EXTRA_LARGE VehicleType = "VEHICLE_TYPE_EXTRA_LARGE"
	VEHICLETYPE_VEHICLE_TYPE_ALL_TYPE    VehicleType = "VEHICLE_TYPE_ALL_TYPE"
)

```
