根据业务申请单号查签约信息
更新时间:2025.08.13商户通过预签约接口生成预签约id,当用户通过微信SDK拉起微信支付客户端的签约页面并完成签约后,可以通过预签约id查询到对应的签约信息和用户openid
接口说明
支持商户:【平台商户】
请求方式:【GET】/v3/ecommerce/individual-contracts/out-contract-code/{out_contract_code}
请求域名:【主域名】https://api.mch.weixin.qq.com 使用该域名将访问就近的接入点
【备域名】https://api2.mch.weixin.qq.com 使用该域名将访问异地的接入点 ,指引点击查看
请求参数
Header HTTP头参数
Authorization 必填 string
请参考签名认证生成认证信息
Accept 必填 string
请设置为application/json
path 路径参数
out_contract_code 必填 string(128)
【业务申请单号】 自定义的商户唯一编号,需保证服务商下唯一,仅允许包含英文半角的数字、字母、连接线-和下划线_。
query 查询参数
appid 必填 string(32)
【签约和付款的AppID】 调用方的AppID,获取方式详见:服务商模式开发必要参数说明
请求示例
需配合微信支付工具库 WXPayUtility 使用,请参考 Java
1package com.java.demo; 2 3import com.java.utils.WXPayUtility; // 引用微信支付工具库,参考:https://pay.weixin.qq.com/doc/v3/partner/4014985777 4 5import com.google.gson.annotations.SerializedName; 6import com.google.gson.annotations.Expose; 7import okhttp3.MediaType; 8import okhttp3.OkHttpClient; 9import okhttp3.Request; 10import okhttp3.RequestBody; 11import okhttp3.Response; 12 13import java.io.IOException; 14import java.io.UncheckedIOException; 15import java.security.PrivateKey; 16import java.security.PublicKey; 17import java.util.ArrayList; 18import java.util.HashMap; 19import java.util.List; 20import java.util.Map; 21 22/** 23 * 根据业务申请单号查签约信息 24 */ 25public class QueryContractsByCode { 26 private static String HOST = "https://api.mch.weixin.qq.com"; 27 private static String METHOD = "GET"; 28 private static String PATH = "/v3/ecommerce/individual-contracts/out-contract-code/{out_contract_code}"; 29 30 public static void main(String[] args) { 31 // TODO: 请准备商户开发必要参数,参考:https://pay.weixin.qq.com/doc/v3/partner/4013080340 32 QueryContractsByCode client = new QueryContractsByCode( 33 "19xxxxxxxx", // 商户号,是由微信支付系统生成并分配给每个商户的唯一标识符,商户号获取方式参考 https://pay.weixin.qq.com/doc/v3/partner/4013080340 34 "1DDE55AD98Exxxxxxxxxx", // 商户API证书序列号,如何获取请参考 https://pay.weixin.qq.com/doc/v3/partner/4013058924 35 "/path/to/apiclient_key.pem", // 商户API证书私钥文件路径,本地文件路径 36 "PUB_KEY_ID_xxxxxxxxxxxxx", // 微信支付公钥ID,如何获取请参考 https://pay.weixin.qq.com/doc/v3/partner/4013038589 37 "/path/to/wxp_pub.pem" // 微信支付公钥文件路径,本地文件路径 38 ); 39 40 QueryContractsByCodeRequest request = new QueryContractsByCodeRequest(); 41 request.outContractCode = "APPLYMENT_00000000001"; 42 request.appid = "wxd678efh567h23787"; 43 try { 44 QueryContractsByCodeResponse response = client.run(request); 45 46 // TODO: 请求成功,继续业务逻辑 47 System.out.println(response); 48 } catch (WXPayUtility.ApiException e) { 49 // TODO: 请求失败,根据状态码执行不同的逻辑 50 e.printStackTrace(); 51 } 52 } 53 54 public QueryContractsByCodeResponse run(QueryContractsByCodeRequest request) { 55 String uri = PATH; 56 uri = uri.replace("{out_contract_code}", WXPayUtility.urlEncode(request.outContractCode)); 57 Map<String, Object> args = new HashMap<>(); 58 args.put("appid", request.appid); 59 uri = uri + "?" + WXPayUtility.urlEncode(args); 60 61 Request.Builder reqBuilder = new Request.Builder().url(HOST + uri); 62 reqBuilder.addHeader("Accept", "application/json"); 63 reqBuilder.addHeader("Wechatpay-Serial", wechatPayPublicKeyId); 64 reqBuilder.addHeader("Authorization", WXPayUtility.buildAuthorization(mchid, certificateSerialNo, privateKey, METHOD, uri, null)); 65 reqBuilder.method(METHOD, null); 66 Request httpRequest = reqBuilder.build(); 67 68 // 发送HTTP请求 69 OkHttpClient client = new OkHttpClient.Builder().build(); 70 try (Response httpResponse = client.newCall(httpRequest).execute()) { 71 String respBody = WXPayUtility.extractBody(httpResponse); 72 if (httpResponse.code() >= 200 && httpResponse.code() < 300) { 73 // 2XX 成功,验证应答签名 74 WXPayUtility.validateResponse(this.wechatPayPublicKeyId, this.wechatPayPublicKey, 75 httpResponse.headers(), respBody); 76 77 // 从HTTP应答报文构建返回数据 78 return WXPayUtility.fromJson(respBody, QueryContractsByCodeResponse.class); 79 } else { 80 throw new WXPayUtility.ApiException(httpResponse.code(), respBody, httpResponse.headers()); 81 } 82 } catch (IOException e) { 83 throw new UncheckedIOException("Sending request to " + uri + " failed.", e); 84 } 85 } 86 87 private final String mchid; 88 private final String certificateSerialNo; 89 private final PrivateKey privateKey; 90 private final String wechatPayPublicKeyId; 91 private final PublicKey wechatPayPublicKey; 92 93 public QueryContractsByCode(String mchid, String certificateSerialNo, String privateKeyFilePath, String wechatPayPublicKeyId, String wechatPayPublicKeyFilePath) { 94 this.mchid = mchid; 95 this.certificateSerialNo = certificateSerialNo; 96 this.privateKey = WXPayUtility.loadPrivateKeyFromPath(privateKeyFilePath); 97 this.wechatPayPublicKeyId = wechatPayPublicKeyId; 98 this.wechatPayPublicKey = WXPayUtility.loadPublicKeyFromPath(wechatPayPublicKeyFilePath); 99 } 100 101 public static class QueryContractsByCodeRequest { 102 @SerializedName("appid") 103 @Expose(serialize = false) 104 public String appid; 105 106 @SerializedName("out_contract_code") 107 @Expose(serialize = false) 108 public String outContractCode; 109 } 110 111 public static class QueryContractsByCodeResponse { 112 @SerializedName("individual_auth_id") 113 public String individualAuthId; 114 115 @SerializedName("signed_time") 116 public String signedTime; 117 118 @SerializedName("auth_state") 119 public AuthState authState; 120 121 @SerializedName("individual_openid") 122 public String individualOpenid; 123 } 124 125 public enum AuthState { 126 @SerializedName("AUTHORIZED") 127 AUTHORIZED, 128 @SerializedName("UNAUTHORIZED") 129 UNAUTHORIZED 130 } 131 132} 133
需配合微信支付工具库 wxpay_utility 使用,请参考 Go
1package main 2 3import ( 4 "demo/wxpay_utility" // 引用微信支付工具库,参考 https://pay.weixin.qq.com/doc/v3/partner/4015119446 5 "encoding/json" 6 "fmt" 7 "net/http" 8 "net/url" 9 "strings" 10) 11 12func main() { 13 // TODO: 请准备商户开发必要参数,参考:https://pay.weixin.qq.com/doc/v3/partner/4013080340 14 config, err := wxpay_utility.CreateMchConfig( 15 "19xxxxxxxx", // 商户号,是由微信支付系统生成并分配给每个商户的唯一标识符,商户号获取方式参考 https://pay.weixin.qq.com/doc/v3/partner/4013080340 16 "1DDE55AD98Exxxxxxxxxx", // 商户API证书序列号,如何获取请参考 https://pay.weixin.qq.com/doc/v3/partner/4013058924 17 "/path/to/apiclient_key.pem", // 商户API证书私钥文件路径,本地文件路径 18 "PUB_KEY_ID_xxxxxxxxxxxxx", // 微信支付公钥ID,如何获取请参考 https://pay.weixin.qq.com/doc/v3/partner/4013038589 19 "/path/to/wxp_pub.pem", // 微信支付公钥文件路径,本地文件路径 20 ) 21 if err != nil { 22 fmt.Println(err) 23 return 24 } 25 26 request := &QueryContractsByCodeRequest{ 27 Appid: wxpay_utility.String("wxd678efh567h23787"), 28 OutContractCode: wxpay_utility.String("APPLYMENT_00000000001"), 29 } 30 31 response, err := QueryContractsByCode(config, request) 32 if err != nil { 33 fmt.Printf("请求失败: %+v\n", err) 34 // TODO: 请求失败,根据状态码执行不同的处理 35 return 36 } 37 38 // TODO: 请求成功,继续业务逻辑 39 fmt.Printf("请求成功: %+v\n", response) 40} 41 42func QueryContractsByCode(config *wxpay_utility.MchConfig, request *QueryContractsByCodeRequest) (response *QueryContractsByCodeResponse, err error) { 43 const ( 44 host = "https://api.mch.weixin.qq.com" 45 method = "GET" 46 path = "/v3/ecommerce/individual-contracts/out-contract-code/{out_contract_code}" 47 ) 48 49 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path)) 50 if err != nil { 51 return nil, err 52 } 53 reqUrl.Path = strings.Replace(reqUrl.Path, "{out_contract_code}", url.PathEscape(*request.OutContractCode), -1) 54 query := reqUrl.Query() 55 query.Add("appid", *request.Appid) 56 reqUrl.RawQuery = query.Encode() 57 httpRequest, err := http.NewRequest(method, reqUrl.String(), nil) 58 if err != nil { 59 return nil, err 60 } 61 httpRequest.Header.Set("Accept", "application/json") 62 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId()) 63 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), nil) 64 if err != nil { 65 return nil, err 66 } 67 httpRequest.Header.Set("Authorization", authorization) 68 69 client := &http.Client{} 70 httpResponse, err := client.Do(httpRequest) 71 if err != nil { 72 return nil, err 73 } 74 75 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse) 76 if err != nil { 77 return nil, err 78 } 79 80 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 { 81 // 2XX 成功,验证应答签名 82 err = wxpay_utility.ValidateResponse( 83 config.WechatPayPublicKeyId(), 84 config.WechatPayPublicKey(), 85 &httpResponse.Header, 86 respBody, 87 ) 88 if err != nil { 89 return nil, err 90 } 91 92 response := &QueryContractsByCodeResponse{} 93 if err := json.Unmarshal(respBody, response); err != nil { 94 return nil, err 95 } 96 97 return response, nil 98 } else { 99 return nil, wxpay_utility.NewApiException( 100 httpResponse.StatusCode, 101 httpResponse.Header, 102 respBody, 103 ) 104 } 105} 106 107type QueryContractsByCodeRequest struct { 108 Appid *string `json:"appid,omitempty"` 109 OutContractCode *string `json:"out_contract_code,omitempty"` 110} 111 112func (o *QueryContractsByCodeRequest) MarshalJSON() ([]byte, error) { 113 type Alias QueryContractsByCodeRequest 114 a := &struct { 115 Appid *string `json:"appid,omitempty"` 116 OutContractCode *string `json:"out_contract_code,omitempty"` 117 *Alias 118 }{ 119 // 序列化时移除非 Body 字段 120 Appid: nil, 121 OutContractCode: nil, 122 Alias: (*Alias)(o), 123 } 124 return json.Marshal(a) 125} 126 127type QueryContractsByCodeResponse struct { 128 IndividualAuthId *string `json:"individual_auth_id,omitempty"` 129 SignedTime *string `json:"signed_time,omitempty"` 130 AuthState *AuthState `json:"auth_state,omitempty"` 131 IndividualOpenid *string `json:"individual_openid,omitempty"` 132} 133 134type AuthState string 135 136func (e AuthState) Ptr() *AuthState { 137 return &e 138} 139 140const ( 141 AUTHSTATE_AUTHORIZED AuthState = "AUTHORIZED" 142 AUTHSTATE_UNAUTHORIZED AuthState = "UNAUTHORIZED" 143) 144
GET
1curl -X GET \ 2 https://api.mch.weixin.qq.com/v3/ecommerce/individual-contracts/out-contract-code/APPLYMENT_00000000001?appid=wxd678efh567h23787 \ 3 -H "Authorization: WECHATPAY2-SHA256-RSA2048 mchid=\"1900000001\",..." \ 4 -H "Accept: application/json" 5
应答参数
200 OK
individual_auth_id 选填 string(128)
【授权ID】 标识一个微信用户在该商户用于对应权限的微信支付账户,平台邀请微信用户完成个人授权后获得此ID
,当用户用预签约id完成签约后返回
signed_time 必填 string
【签约时间】 遵循RFC3339标准格式,格式为yyyy-MM-DDTHH:mm:ss+TIMEZONE,yyyy-MM-DD表示年月日,T出现在字符串中,表示time元素的开头,HH:mm:ss表示时分秒,TIMEZONE表示时区(+08:00表示东八区时间,领先UTC8小时,即北京时间)。例如:2015-05-20T13:29:35+08:00表示,北京时间2015年5月20日13点29分35秒。
auth_state 必填 string
【授权状态】 授权状态枚举
可选取值
AUTHORIZED
: 已授权UNAUTHORIZED
: 未授权
individual_openid 选填 string
【用户OpenID】 个人收款方在平台appid
下的唯一标识,当用户用预签约id完成签约后返回
应答示例
200 OK
1{ 2 "individual_auth_id" : "1900000109", 3 "signed_time" : "2018-06-08T10:34:56+08:00", 4 "auth_state" : "AUTHORIZED", 5 "individual_openid" : "oUpF8uMuAJO_M2pxb1Q9zNjWeS6o\t" 6} 7
错误码
公共错误码
状态码 | 错误码 | 描述 | 解决方案 |
---|---|---|---|
400 | PARAM_ERROR | 参数错误 | 请根据错误提示正确传入参数 |
400 | INVALID_REQUEST | HTTP 请求不符合微信支付 APIv3 接口规则 | 请参阅 接口规则 |
401 | SIGN_ERROR | 验证不通过 | 请参阅 签名常见问题 |
500 | SYSTEM_ERROR | 系统异常,请稍后重试 | 请稍后重试 |
业务错误码
状态码 | 错误码 | 描述 | 解决方案 |
---|---|---|---|
400 | PARAM_ERROR | 参数错误 | 检查请求参数 |