---
url: "https://pay.weixin.qq.com/doc/v3/partner/4013892759-go.md"
title: "商户申请单号查询申请单状态-请求示例-go"
doc_id: 4013892759
example_lang: "go"
---

## Go


需配合微信支付工具库 wxpay\_utility 使用，请参考 [Go](https://pay.weixin.qq.com/doc/v3/partner/4015119446.md)

```
package main

import (
	"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 := &GetApplyByOutRequestNoRequest{
		OutRequestNo: wxpay_utility.String("P202410241010125346"),
	}

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

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

func GetApplyByOutRequestNo(config *wxpay_utility.MchConfig, request *GetApplyByOutRequestNoRequest) (response *CancelWithdrawaApplyData, err error) {
	const (
		host   = "https://api.mch.weixin.qq.com"
		method = "GET"
		path   = "/v3/ecommerce/account/apply-cancel-withdraw/out-request-no/{out_request_no}"
	)

	reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
	if err != nil {
		return nil, err
	}
	reqUrl.Path = strings.Replace(reqUrl.Path, "{out_request_no}", url.PathEscape(*request.OutRequestNo), -1)
	httpRequest, err := http.NewRequest(method, reqUrl.String(), nil)
	if err != nil {
		return nil, err
	}
	httpRequest.Header.Set("Accept", "application/json")
	httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
	authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), nil)
	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 := &CancelWithdrawaApplyData{}
		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 GetApplyByOutRequestNoRequest struct {
	OutRequestNo *string `json:"out_request_no,omitempty"`
}

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

type CancelWithdrawaApplyData struct {
	ApplymentId              *string                 `json:"applyment_id,omitempty"`
	OutRequestNo             *string                 `json:"out_request_no,omitempty"`
	CancelState              *CancelState            `json:"cancel_state,omitempty"`
	CancelStateDescription   *string                 `json:"cancel_state_description,omitempty"`
	Withdraw                 *ApplyWithdraw          `json:"withdraw,omitempty"`
	WithdrawState            *WithdrawState          `json:"withdraw_state,omitempty"`
	WithdrawStateDescription *string                 `json:"withdraw_state_description,omitempty"`
	AccountWithdrawResult    []AccountWithdrawResult `json:"account_withdraw_result,omitempty"`
	ModifyTime               *string                 `json:"modify_time,omitempty"`
	SubMchid                 *string                 `json:"sub_mchid,omitempty"`
	AccountInfo              []AccountInfo           `json:"account_info,omitempty"`
}

type CancelState string

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

const (
	CANCELSTATE_ACCEPTED                 CancelState = "ACCEPTED"
	CANCELSTATE_REVIEWING                CancelState = "REVIEWING"
	CANCELSTATE_REJECTED                 CancelState = "REJECTED"
	CANCELSTATE_WAITING_MERCHANT_CONFIRM CancelState = "WAITING_MERCHANT_CONFIRM"
	CANCELSTATE_REVOKED                  CancelState = "REVOKED"
	CANCELSTATE_SYSTEM_PROCESSING        CancelState = "SYSTEM_PROCESSING"
	CANCELSTATE_CANCELED                 CancelState = "CANCELED"
	CANCELSTATE_FUND_PROCESSING          CancelState = "FUND_PROCESSING"
	CANCELSTATE_FINISH                   CancelState = "FINISH"
)

type ApplyWithdraw string

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

const (
	APPLYWITHDRAW_NOT_APPLY_WITHDRAW ApplyWithdraw = "NOT_APPLY_WITHDRAW"
	APPLYWITHDRAW_APPLY_WITHDRAW     ApplyWithdraw = "APPLY_WITHDRAW"
)

type WithdrawState string

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

const (
	WITHDRAWSTATE_WITHDRAW_PROCESSING WithdrawState = "WITHDRAW_PROCESSING"
	WITHDRAWSTATE_WITHDRAW_EXCEPTION  WithdrawState = "WITHDRAW_EXCEPTION"
	WITHDRAWSTATE_WITHDRAW_SUCCEED    WithdrawState = "WITHDRAW_SUCCEED"
)

type AccountWithdrawResult struct {
	OutAccountType   *OutAccountType `json:"out_account_type,omitempty"`
	PayState         *PayState       `json:"pay_state,omitempty"`
	StateDescription *string         `json:"state_description,omitempty"`
}

type AccountInfo struct {
	OutAccountType *OutAccountType `json:"out_account_type,omitempty"`
	Amount         *int64          `json:"amount,omitempty"`
}

type OutAccountType string

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

const (
	OUTACCOUNTTYPE_BASIC_ACCOUNT     OutAccountType = "BASIC_ACCOUNT"
	OUTACCOUNTTYPE_OPERATE_ACCOUNT   OutAccountType = "OPERATE_ACCOUNT"
	OUTACCOUNTTYPE_MARGIN_ACCOUNT    OutAccountType = "MARGIN_ACCOUNT"
	OUTACCOUNTTYPE_TRADE_FEE_ACCOUNT OutAccountType = "TRADE_FEE_ACCOUNT"
)

type PayState string

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

const (
	PAYSTATE_PAY_PROCESSING PayState = "PAY_PROCESSING"
	PAYSTATE_PAY_SUCCEED    PayState = "PAY_SUCCEED"
	PAYSTATE_PAY_FAIL       PayState = "PAY_FAIL"
	PAYSTATE_BANK_REFUNDED  PayState = "BANK_REFUNDED"
)

```
