---
url: "https://pay.weixin.qq.com/doc/v3/partner/4012476217-go.md"
title: "提交注销申请单-请求示例-go"
doc_id: 4012476217
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 := &CancelApplicationCreateRequest{
		SubMchid:   wxpay_utility.String("1900000109"),
		OutApplyNo: wxpay_utility.String("2019061122222222122"),
		ApplicationInfo: []CancelApplicationInfo{CancelApplicationInfo{
			ApplicationType:    APPLICATIONTYPE_SP_CANCEL_ACCOUNT_APPLICATION.Ptr(),
			ApplicationMediaId: wxpay_utility.String("abc123456"),
		}},
	}

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

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

func CreateCancelApplication(config *wxpay_utility.MchConfig, request *CancelApplicationCreateRequest) (response *CancelApplicationsEntity, err error) {
	const (
		host   = "https://api.mch.weixin.qq.com"
		method = "POST"
		path   = "/v3/ecommerce/account/cancel-applications"
	)

	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 := &CancelApplicationsEntity{}
		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 CancelApplicationCreateRequest struct {
	SubMchid        *string                 `json:"sub_mchid,omitempty"`
	OutApplyNo      *string                 `json:"out_apply_no,omitempty"`
	ApplicationInfo []CancelApplicationInfo `json:"application_info,omitempty"`
}

type CancelApplicationsEntity struct {
	OutApplyNo   *string      `json:"out_apply_no,omitempty"`
	SubMchid     *string      `json:"sub_mchid,omitempty"`
	RejectReason *string      `json:"reject_reason,omitempty"`
	CancelState  *CancelState `json:"cancel_state,omitempty"`
	UpdateTime   *string      `json:"update_time,omitempty"`
}

type CancelApplicationInfo struct {
	ApplicationType    *ApplicationType `json:"application_type,omitempty"`
	ApplicationMediaId *string          `json:"application_media_id,omitempty"`
}

type CancelState string

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

const (
	CANCELSTATE_REVIEWING      CancelState = "REVIEWING"
	CANCELSTATE_REJECTED       CancelState = "REJECTED"
	CANCELSTATE_CANCEL_SUCCESS CancelState = "CANCEL_SUCCESS"
)

type ApplicationType string

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

const (
	APPLICATIONTYPE_SP_MERCHANT_APPLICATION        ApplicationType = "SP_MERCHANT_APPLICATION"
	APPLICATIONTYPE_SUB_MERCHANT_APPLICATION       ApplicationType = "SUB_MERCHANT_APPLICATION"
	APPLICATIONTYPE_MISSING_OFFICIAL_SEAL_LETTER   ApplicationType = "MISSING_OFFICIAL_SEAL_LETTER"
	APPLICATIONTYPE_SP_CANCEL_ACCOUNT_APPLICATION  ApplicationType = "SP_CANCEL_ACCOUNT_APPLICATION"
	APPLICATIONTYPE_SUB_CANCEL_ACCOUNT_APPLICATION ApplicationType = "SUB_CANCEL_ACCOUNT_APPLICATION"
)

```
