---
url: "https://pay.weixin.qq.com/doc/v3/partner/4016184594-go.md"
title: "更新投放计划-请求示例-go"
doc_id: 4016184594
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"
	"strings"
)

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 := &UpdateDeliveryPlanRequest{
		PlanId: wxpay_utility.String("12000"),
		ModifyContent: &PlanModifyContent{
			PlanName:        wxpay_utility.String("冬季促销"),
			DeliveryEndTime: wxpay_utility.String("2025-01-01T00:00:00+08:00"),
			TotalCount:      wxpay_utility.Int64(1),
			UserLimit:       wxpay_utility.Int64(1),
			DailyLimit:      wxpay_utility.Int64(1),
			RecommendWord:   wxpay_utility.String("冬季优惠"),
		},
		OutRequestNo: wxpay_utility.String("abcd-1234-1000"),
	}

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

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

func UpdateDeliveryPlan(config *wxpay_utility.MchConfig, request *UpdateDeliveryPlanRequest) (response *UpdateDeliveryPlanResponse, err error) {
	const (
		host   = "https://api.mch.weixin.qq.com"
		method = "PATCH"
		path   = "/v3/marketing/partner/delivery-plan/delivery-plans/{plan_id}"
	)

	reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
	if err != nil {
		return nil, err
	}
	reqUrl.Path = strings.Replace(reqUrl.Path, "{plan_id}", url.PathEscape(*request.PlanId), -1)
	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 := &UpdateDeliveryPlanResponse{}
		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 UpdateDeliveryPlanRequest struct {
	PlanId        *string            `json:"plan_id,omitempty"`
	ModifyContent *PlanModifyContent `json:"modify_content,omitempty"`
	OutRequestNo  *string            `json:"out_request_no,omitempty"`
}

func (o *UpdateDeliveryPlanRequest) MarshalJSON() ([]byte, error) {
	type Alias UpdateDeliveryPlanRequest
	a := &struct {
		PlanId *string `json:"plan_id,omitempty"`
		*Alias
	}{
		// 序列化时移除非 Body 字段
		PlanId: nil,
		Alias:  (*Alias)(o),
	}
	return json.Marshal(a)
}

type UpdateDeliveryPlanResponse struct {
	Plan *DeliveryPlan `json:"plan,omitempty"`
}

type PlanModifyContent struct {
	PlanName        *string `json:"plan_name,omitempty"`
	DeliveryEndTime *string `json:"delivery_end_time,omitempty"`
	TotalCount      *int64  `json:"total_count,omitempty"`
	UserLimit       *int64  `json:"user_limit,omitempty"`
	DailyLimit      *int64  `json:"daily_limit,omitempty"`
	RecommendWord   *string `json:"recommend_word,omitempty"`
}

type DeliveryPlan struct {
	PlanId            *string           `json:"plan_id,omitempty"`
	PlanName          *string           `json:"plan_name,omitempty"`
	PlanState         *PlanState        `json:"plan_state,omitempty"`
	DeliveryStartTime *string           `json:"delivery_start_time,omitempty"`
	DeliveryEndTime   *string           `json:"delivery_end_time,omitempty"`
	ProductCouponId   *string           `json:"product_coupon_id,omitempty"`
	UsageMode         *ProductUsageMode `json:"usage_mode,omitempty"`
	StockId           *string           `json:"stock_id,omitempty"`
	StockBundleId     *string           `json:"stock_bundle_id,omitempty"`
	RecommendWord     *string           `json:"recommend_word,omitempty"`
	BrandId           *string           `json:"brand_id,omitempty"`
	TotalCount        *int64            `json:"total_count,omitempty"`
	UserLimit         *int64            `json:"user_limit,omitempty"`
	DailyLimit        *int64            `json:"daily_limit,omitempty"`
	ReuseCouponConfig *bool             `json:"reuse_coupon_config,omitempty"`
}

type PlanState string

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

const (
	PLANSTATE_CREATED    PlanState = "CREATED"
	PLANSTATE_TERMINATED PlanState = "TERMINATED"
	PLANSTATE_EXPIRED    PlanState = "EXPIRED"
	PLANSTATE_DELIVERING PlanState = "DELIVERING"
	PLANSTATE_PAUSED     PlanState = "PAUSED"
)

type ProductUsageMode string

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

const (
	PRODUCTUSAGEMODE_SINGLE             ProductUsageMode = "SINGLE"
	PRODUCTUSAGEMODE_PROGRESSIVE_BUNDLE ProductUsageMode = "PROGRESSIVE_BUNDLE"
)

```
