APP场景C2C预签约

更新时间:2025.08.13

商户可调用本接口预先指定签约,生成预签约会话及对应的预签约ID,再携带预签约ID(pre_entrustweb_id)参数,通过微信SDK拉起微信支付客户端的签约页面。

接口说明

支持商户:【平台商户】

请求方式:【POST】/v3/ecommerce/individual-contracts/pre-entrust-sign/app

请求域名:【主域名】https://api.mch.weixin.qq.com 使用该域名将访问就近的接入点

     【备域名】https://api2.mch.weixin.qq.com 使用该域名将访问异地的接入点 ,指引点击查看

请求参数

Header  HTTP头参数

 Authorization  必填 string

请参考签名认证生成认证信息


 Accept  必填 string

请设置为application/json


 Content-Type  必填 string

请设置为application/json


body  包体参数

 appid  必填   string(32)

【签约和付款的AppID】 调用方的AppID,获取方式详见:服务商模式开发必要参数说明


 out_contract_code  必填   string(128)

【业务申请单号】 自定义的商户唯一编号,需保证服务商下唯一,仅允许包含英文半角的数字、字母、连接线-和下划线_。

请求示例

Java
Go
curl

需配合微信支付工具库 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 * APP场景c2c预签约
24 */
25public class PreEntrustSign {
26  private static String HOST = "https://api.mch.weixin.qq.com";
27  private static String METHOD = "POST";
28  private static String PATH = "/v3/ecommerce/individual-contracts/pre-entrust-sign/app";
29
30  public static void main(String[] args) {
31    // TODO: 请准备商户开发必要参数,参考:https://pay.weixin.qq.com/doc/v3/partner/4013080340
32    PreEntrustSign client = new PreEntrustSign(
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    PreEntrustSignRequest request = new PreEntrustSignRequest();
41    request.appid = "wxd678efh567h23787";
42    request.outContractCode = "APPLYMENT_00000000001";
43    try {
44      PreEntrustSignResponse 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 PreEntrustSignResponse run(PreEntrustSignRequest request) {
55    String uri = PATH;
56    String reqBody = WXPayUtility.toJson(request);
57
58    Request.Builder reqBuilder = new Request.Builder().url(HOST + uri);
59    reqBuilder.addHeader("Accept", "application/json");
60    reqBuilder.addHeader("Wechatpay-Serial", wechatPayPublicKeyId);
61    reqBuilder.addHeader("Authorization", WXPayUtility.buildAuthorization(mchid, certificateSerialNo,privateKey, METHOD, uri, reqBody));
62    reqBuilder.addHeader("Content-Type", "application/json");
63    RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), reqBody);
64    reqBuilder.method(METHOD, requestBody);
65    Request httpRequest = reqBuilder.build();
66
67    // 发送HTTP请求
68    OkHttpClient client = new OkHttpClient.Builder().build();
69    try (Response httpResponse = client.newCall(httpRequest).execute()) {
70      String respBody = WXPayUtility.extractBody(httpResponse);
71      if (httpResponse.code() >= 200 && httpResponse.code() < 300) {
72        // 2XX 成功,验证应答签名
73        WXPayUtility.validateResponse(this.wechatPayPublicKeyId, this.wechatPayPublicKey,
74            httpResponse.headers(), respBody);
75
76        // 从HTTP应答报文构建返回数据
77        return WXPayUtility.fromJson(respBody, PreEntrustSignResponse.class);
78      } else {
79        throw new WXPayUtility.ApiException(httpResponse.code(), respBody, httpResponse.headers());
80      }
81    } catch (IOException e) {
82      throw new UncheckedIOException("Sending request to " + uri + " failed.", e);
83    }
84  }
85
86  private final String mchid;
87  private final String certificateSerialNo;
88  private final PrivateKey privateKey;
89  private final String wechatPayPublicKeyId;
90  private final PublicKey wechatPayPublicKey;
91
92  public PreEntrustSign(String mchid, String certificateSerialNo, String privateKeyFilePath, String wechatPayPublicKeyId, String wechatPayPublicKeyFilePath) {
93    this.mchid = mchid;
94    this.certificateSerialNo = certificateSerialNo;
95    this.privateKey = WXPayUtility.loadPrivateKeyFromPath(privateKeyFilePath);
96    this.wechatPayPublicKeyId = wechatPayPublicKeyId;
97    this.wechatPayPublicKey = WXPayUtility.loadPublicKeyFromPath(wechatPayPublicKeyFilePath);
98  }
99
100  public static class PreEntrustSignRequest {
101    @SerializedName("appid")
102    public String appid;
103  
104    @SerializedName("out_contract_code")
105    public String outContractCode;
106  }
107  
108  public static class PreEntrustSignResponse {
109    @SerializedName("pre_entrustweb_id")
110    public String preEntrustwebId;
111  }
112  
113}
114

应答参数

200 OK

 pre_entrustweb_id  必填   string(256)

【预签约id】 生成预签约会ID,携带预签约ID(pre_entrustweb_id)参数,通过微信SDK拉起微信支付客户端的签约页面。

应答示例

200 OK

1{
2  "pre_entrustweb_id" : "1121_9f3cec22a2d367737674d08d98e5e07b8388495fafedc0eba83c327e8ad720ed_12bfdd59b387c4bff8543de4719a886a"
3}
4

 

错误码

公共错误码

状态码

错误码

描述

解决方案

400

PARAM_ERROR

参数错误

请根据错误提示正确传入参数

400

INVALID_REQUEST

HTTP 请求不符合微信支付 APIv3 接口规则

请参阅 接口规则

401

SIGN_ERROR

验证不通过

请参阅 签名常见问题

500

SYSTEM_ERROR

系统异常,请稍后重试

请稍后重试

业务错误码

状态码

错误码

描述

解决方案

400

PARAM_ERROR

参数错误

检查请求参数

 

 

反馈
咨询
目录
置顶