---
url: "https://pay.weixin.qq.com/doc/v3/partner/4015897268-go.md"
title: "同步积分兑券结果-请求示例-go"
doc_id: 4015897268
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 := &ConfirmPointExchangeCouponRequest{
		RecordId:                 wxpay_utility.String("pbLatjvWOibDc5-TBnbUk1pD12o0"),
		ExchangeCouponTemplateId: wxpay_utility.String("0Flpo7FqTQ"),
		BrandId:                  wxpay_utility.String("1004"),
		CardId:                   wxpay_utility.String("pbLatjvWOibDc5-TBnbUk1pD12o0"),
		Openid:                   wxpay_utility.String("obLatjnx9gnqzS4myYGmLZ7LgLBA"),
		UserCardCode:             wxpay_utility.String("478515832665"),
		Result:                   POINTEXCHANGECOUPONRESULT_POINT_EXCHANGE_COUPON_ALLOW.Ptr(),
		RejectReason:             wxpay_utility.String("积分不足"),
	}

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

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

func ConfirmPointExchangeCoupon(config *wxpay_utility.MchConfig, request *ConfirmPointExchangeCouponRequest) (response *PointExchangeCoupon, err error) {
	const (
		host   = "https://api.mch.weixin.qq.com"
		method = "POST"
		path   = "/v3/brand/partner/card-member/user-points/exchange-coupon/confirm"
	)

	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 := &PointExchangeCoupon{}
		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 ConfirmPointExchangeCouponRequest struct {
	RecordId                 *string                    `json:"record_id,omitempty"`
	ExchangeCouponTemplateId *string                    `json:"exchange_coupon_template_id,omitempty"`
	BrandId                  *string                    `json:"brand_id,omitempty"`
	CardId                   *string                    `json:"card_id,omitempty"`
	Openid                   *string                    `json:"openid,omitempty"`
	UserCardCode             *string                    `json:"user_card_code,omitempty"`
	Result                   *PointExchangeCouponResult `json:"result,omitempty"`
	RejectReason             *string                    `json:"reject_reason,omitempty"`
}

type PointExchangeCoupon struct {
	RecordId                 *string                   `json:"record_id,omitempty"`
	ExchangeCouponTemplateId *string                   `json:"exchange_coupon_template_id,omitempty"`
	BrandId                  *string                   `json:"brand_id,omitempty"`
	CardId                   *string                   `json:"card_id,omitempty"`
	Openid                   *string                   `json:"openid,omitempty"`
	UserCardCode             *string                   `json:"user_card_code,omitempty"`
	State                    *PointExchangeCouponState `json:"state,omitempty"`
	ProductCouponId          *string                   `json:"product_coupon_id,omitempty"`
	ProductCouponStockType   *ProductCouponStockType   `json:"product_coupon_stock_type,omitempty"`
	StockId                  *string                   `json:"stock_id,omitempty"`
	CouponCode               *string                   `json:"coupon_code,omitempty"`
	RejectReason             *string                   `json:"reject_reason,omitempty"`
}

type PointExchangeCouponResult string

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

const (
	POINTEXCHANGECOUPONRESULT_POINT_EXCHANGE_COUPON_ALLOW  PointExchangeCouponResult = "POINT_EXCHANGE_COUPON_ALLOW"
	POINTEXCHANGECOUPONRESULT_POINT_EXCHANGE_COUPON_REJECT PointExchangeCouponResult = "POINT_EXCHANGE_COUPON_REJECT"
)

type PointExchangeCouponState string

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

const (
	POINTEXCHANGECOUPONSTATE_POINT_EXCHANGE_COUPON_SUCCESS PointExchangeCouponState = "POINT_EXCHANGE_COUPON_SUCCESS"
	POINTEXCHANGECOUPONSTATE_POINT_EXCHANGE_COUPON_FAIL    PointExchangeCouponState = "POINT_EXCHANGE_COUPON_FAIL"
)

type ProductCouponStockType string

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

const (
	PRODUCTCOUPONSTOCKTYPE_PRODUCT_COUPON_STOCK_TYPE_BUSI_FAVOR  ProductCouponStockType = "PRODUCT_COUPON_STOCK_TYPE_BUSI_FAVOR"
	PRODUCTCOUPONSTOCKTYPE_PRODUCT_COUPON_STOCK_TYPE_CASH_FAVOR  ProductCouponStockType = "PRODUCT_COUPON_STOCK_TYPE_CASH_FAVOR"
	PRODUCTCOUPONSTOCKTYPE_PRODUCT_COUPON_STOCK_TYPE_BRAND_FAVOR ProductCouponStockType = "PRODUCT_COUPON_STOCK_TYPE_BRAND_FAVOR"
)

```
