
1package main
2
3import (
4	"demo/wxpay_utility" 
5	"encoding/json"
6	"fmt"
7	"net/http"
8	"net/url"
9	"strings"
10)
11
12func main() {
13	
14	config, err := wxpay_utility.CreateMchConfig(
15		"19xxxxxxxx",                 
16		"1DDE55AD98Exxxxxxxxxx",      
17		"/path/to/apiclient_key.pem", 
18		"PUB_KEY_ID_xxxxxxxxxxxxx",   
19		"/path/to/wxp_pub.pem",       
20	)
21	if err != nil {
22		fmt.Println(err)
23		return
24	}
25
26	request := &QueryStateRequest{
27		BusinessCode: wxpay_utility.String("1900013511_10000"),
28	}
29
30	response, err := QueryState(config, request)
31	if err != nil {
32		fmt.Printf("请求失败: %+v\n", err)
33		
34		return
35	}
36
37	
38	fmt.Printf("请求成功: %+v\n", response)
39}
40
41func QueryState(config *wxpay_utility.MchConfig, request *QueryStateRequest) (response *QueryStateResp, err error) {
42	const (
43		host   = "https://api.mch.weixin.qq.com"
44		method = "GET"
45		path   = "/v3/applyment4sub/applyment/business_code/{business_code}"
46	)
47
48	reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
49	if err != nil {
50		return nil, err
51	}
52	reqUrl.Path = strings.Replace(reqUrl.Path, "{business_code}", url.PathEscape(*request.BusinessCode), -1)
53	httpRequest, err := http.NewRequest(method, reqUrl.String(), nil)
54	if err != nil {
55		return nil, err
56	}
57	httpRequest.Header.Set("Accept", "application/json")
58	httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
59	authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), nil)
60	if err != nil {
61		return nil, err
62	}
63	httpRequest.Header.Set("Authorization", authorization)
64
65	client := &http.Client{}
66	httpResponse, err := client.Do(httpRequest)
67	if err != nil {
68		return nil, err
69	}
70	respBody, err := wxpay_utility.ExtractResponseBody(httpResponse)
71	if err != nil {
72		return nil, err
73	}
74	if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
75		
76		err = wxpay_utility.ValidateResponse(
77			config.WechatPayPublicKeyId(),
78			config.WechatPayPublicKey(),
79			&httpResponse.Header,
80			respBody,
81		)
82		if err != nil {
83			return nil, err
84		}
85		response := &QueryStateResp{}
86		if err := json.Unmarshal(respBody, response); err != nil {
87			return nil, err
88		}
89
90		return response, nil
91	} else {
92		return nil, wxpay_utility.NewApiException(
93			httpResponse.StatusCode,
94			httpResponse.Header,
95			respBody,
96		)
97	}
98}
99
100type QueryStateRequest struct {
101	BusinessCode *string `json:"business_code,omitempty"`
102}
103
104func (o *QueryStateRequest) MarshalJSON() ([]byte, error) {
105	type Alias QueryStateRequest
106	a := &struct {
107		BusinessCode *string `json:"business_code,omitempty"`
108		*Alias
109	}{
110		
111		BusinessCode: nil,
112		Alias:        (*Alias)(o),
113	}
114	return json.Marshal(a)
115}
116
117type QueryStateResp struct {
118	BusinessCode      *string         `json:"business_code,omitempty"`
119	ApplymentId       *int64          `json:"applyment_id,omitempty"`
120	SubMchid          *string         `json:"sub_mchid,omitempty"`
121	SignUrl           *string         `json:"sign_url,omitempty"`
122	ApplymentState    *ApplymentState `json:"applyment_state,omitempty"`
123	ApplymentStateMsg *string         `json:"applyment_state_msg,omitempty"`
124	AuditDetail       []AuditDetail   `json:"audit_detail,omitempty"`
125}
126
127type ApplymentState string
128
129func (e ApplymentState) Ptr() *ApplymentState {
130	return &e
131}
132
133const (
134	APPLYMENTSTATE_APPLYMENT_STATE_EDITTING        ApplymentState = "APPLYMENT_STATE_EDITTING"
135	APPLYMENTSTATE_APPLYMENT_STATE_AUDITING        ApplymentState = "APPLYMENT_STATE_AUDITING"
136	APPLYMENTSTATE_APPLYMENT_STATE_REJECTED        ApplymentState = "APPLYMENT_STATE_REJECTED"
137	APPLYMENTSTATE_APPLYMENT_STATE_TO_BE_CONFIRMED ApplymentState = "APPLYMENT_STATE_TO_BE_CONFIRMED"
138	APPLYMENTSTATE_APPLYMENT_STATE_TO_BE_SIGNED    ApplymentState = "APPLYMENT_STATE_TO_BE_SIGNED"
139	APPLYMENTSTATE_APPLYMENT_STATE_FINISHED        ApplymentState = "APPLYMENT_STATE_FINISHED"
140	APPLYMENTSTATE_APPLYMENT_STATE_CANCELED        ApplymentState = "APPLYMENT_STATE_CANCELED"
141	APPLYMENTSTATE_APPLYMENT_STATE_SIGNING         ApplymentState = "APPLYMENT_STATE_SIGNING"
142)
143
144type AuditDetail struct {
145	Field        *string `json:"field,omitempty"`
146	FieldName    *string `json:"field_name,omitempty"`
147	RejectReason *string `json:"reject_reason,omitempty"`
148}
149