---
url: "https://pay.weixin.qq.com/doc/v3/partner/4019557765-go.md"
title: "同步支付结果通知-请求示例-go"
doc_id: 4019557765
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 := &SyncParkingPaymentRequest{
		OutSerialNumber: wxpay_utility.String("PARK20260423001"),
		PlateNumber:     wxpay_utility.String("粤B12345"),
		ParkingId:       wxpay_utility.String("21232735744117624001123604298240"),
		ParkingState:    PARKINGSTATUS_PARKING_STATUS_UNKNOWN.Ptr(),
		PayType:         PAYTYPE_PAY_TYPE_UNKNOWN.Ptr(),
		Openid:          wxpay_utility.String("oUpF8uMuAJO_M2pxb1Q9zNjWeS6o"),
		SubMchid:        wxpay_utility.String("1900000109"),
		TotalAmount:     wxpay_utility.Int64(1),
		PaidAmount:      wxpay_utility.Int64(1),
		OutTradeNo:      wxpay_utility.String("4200001234567890123456789012"),
		PayChannel:      PAYCHANNEL_PAY_CHANNEL_UNKNOWN.Ptr(),
		PayTime:         wxpay_utility.Int64(1719128975),
		Token:           wxpay_utility.String("msg_pay_token_example"),
		WxTradeNo:       wxpay_utility.String("4200001234567890123456789012"),
	}

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

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

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

	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 := &SyncParkingPaymentResponse{}
		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 SyncParkingPaymentRequest struct {
	OutSerialNumber *string        `json:"out_serial_number,omitempty"`
	PlateNumber     *string        `json:"plate_number,omitempty"`
	ParkingId       *string        `json:"parking_id,omitempty"`
	ParkingState    *ParkingStatus `json:"parking_state,omitempty"`
	PayType         *PayType       `json:"pay_type,omitempty"`
	Openid          *string        `json:"openid,omitempty"`
	SubMchid        *string        `json:"sub_mchid,omitempty"`
	TotalAmount     *int64         `json:"total_amount,omitempty"`
	PaidAmount      *int64         `json:"paid_amount,omitempty"`
	OutTradeNo      *string        `json:"out_trade_no,omitempty"`
	PayChannel      *PayChannel    `json:"pay_channel,omitempty"`
	PayTime         *int64         `json:"pay_time,omitempty"`
	Token           *string        `json:"token,omitempty"`
	WxTradeNo       *string        `json:"wx_trade_no,omitempty"`
}

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

type ParkingStatus string

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

const (
	PARKINGSTATUS_PARKING_STATUS_UNKNOWN     ParkingStatus = "PARKING_STATUS_UNKNOWN"
	PARKINGSTATUS_PARKING_STATUS_PARKING     ParkingStatus = "PARKING_STATUS_PARKING"
	PARKINGSTATUS_PARKING_STATUS_NOT_PARKING ParkingStatus = "PARKING_STATUS_NOT_PARKING"
)

type PayType string

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

const (
	PAYTYPE_PAY_TYPE_UNKNOWN PayType = "PAY_TYPE_UNKNOWN"
	PAYTYPE_PAY_TYPE_WXPAY   PayType = "PAY_TYPE_WXPAY"
	PAYTYPE_PAY_TYPE_OTHERS  PayType = "PAY_TYPE_OTHERS"
)

type PayChannel string

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

const (
	PAYCHANNEL_PAY_CHANNEL_UNKNOWN          PayChannel = "PAY_CHANNEL_UNKNOWN"
	PAYCHANNEL_PAY_CHANNEL_WECHAT_PAY_NOTIF PayChannel = "PAY_CHANNEL_WECHAT_PAY_NOTIF"
	PAYCHANNEL_PAY_CHANNEL_SCAN_QRCODE      PayChannel = "PAY_CHANNEL_SCAN_QRCODE"
	PAYCHANNEL_PAY_CHANNEL_OFFICIAL_ACCOUNT PayChannel = "PAY_CHANNEL_OFFICIAL_ACCOUNT"
	PAYCHANNEL_PAY_CHANNEL_MCH_MINI_PROG    PayChannel = "PAY_CHANNEL_MCH_MINI_PROG"
	PAYCHANNEL_PAY_CHANNEL_OTHERS           PayChannel = "PAY_CHANNEL_OTHERS"
)

```
