创建可用商户范围
更新时间:2026.01.15创建可用商户范围。该接口允许服务商创建新的可用商户范围,用于限制额度卡的可用商户范围。服务商可以根据业务需要创建基于统一社会信用代码或微信支付订单号的商户范围,便于管理不同场景下的额度卡使用限制。
接口说明
支持商户:【普通服务商】
请求方式:【POST】/v3/webizpay/avail-mch-ranges
请求域名:【主域名】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 包体参数
sp_mchid 必填 string(32)
【服务商商户号】 是由微信支付系统生成并分配给每个服务商的唯一标识符,具体请参考服务商模式开发必要参数说明。
identifier_type 必填 string
【商户标识类型】 商户标识类型
可选取值
ORGANIZATION_CODE: 统一社会信用代码TRANSACTION_ID: 微信支付订单号
请求示例
POST
1curl -X POST \ 2 https://api.mch.weixin.qq.com/v3/webizpay/avail-mch-ranges \ 3 -H "Authorization: WECHATPAY2-SHA256-RSA2048 mchid=\"1900000001\",..." \ 4 -H "Accept: application/json" \ 5 -H "Content-Type: application/json" \ 6 -d '{ 7 "sp_mchid" : "12341234", 8 "identifier_type" : "ORGANIZATION_CODE" 9 }' 10
需配合微信支付工具库 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 CreateAvailMchRange { 26 private static String HOST = "https://api.mch.weixin.qq.com"; 27 private static String METHOD = "POST"; 28 private static String PATH = "/v3/webizpay/avail-mch-ranges"; 29 30 public static void main(String[] args) { 31 // TODO: 请准备商户开发必要参数,参考:https://pay.weixin.qq.com/doc/v3/partner/4013080340 32 CreateAvailMchRange client = new CreateAvailMchRange( 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 AvailMchRangeCreateRequest request = new AvailMchRangeCreateRequest(); 41 request.spMchid = "12341234"; 42 request.identifierType = MerchantIdentifierType.ORGANIZATION_CODE; 43 try { 44 AvailMchRangeCreateResponse response = client.run(request); 45 // TODO: 请求成功,继续业务逻辑 46 System.out.println(response); 47 } catch (WXPayUtility.ApiException e) { 48 // TODO: 请求失败,根据状态码执行不同的逻辑 49 e.printStackTrace(); 50 } 51 } 52 53 public AvailMchRangeCreateResponse run(AvailMchRangeCreateRequest request) { 54 String uri = PATH; 55 String reqBody = WXPayUtility.toJson(request); 56 57 Request.Builder reqBuilder = new Request.Builder().url(HOST + uri); 58 reqBuilder.addHeader("Accept", "application/json"); 59 reqBuilder.addHeader("Wechatpay-Serial", wechatPayPublicKeyId); 60 reqBuilder.addHeader("Authorization", WXPayUtility.buildAuthorization(mchid, certificateSerialNo,privateKey, METHOD, uri, reqBody)); 61 reqBuilder.addHeader("Content-Type", "application/json"); 62 RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), reqBody); 63 reqBuilder.method(METHOD, requestBody); 64 Request httpRequest = reqBuilder.build(); 65 66 // 发送HTTP请求 67 OkHttpClient client = new OkHttpClient.Builder().build(); 68 try (Response httpResponse = client.newCall(httpRequest).execute()) { 69 String respBody = WXPayUtility.extractBody(httpResponse); 70 if (httpResponse.code() >= 200 && httpResponse.code() < 300) { 71 // 2XX 成功,验证应答签名 72 WXPayUtility.validateResponse(this.wechatPayPublicKeyId, this.wechatPayPublicKey, 73 httpResponse.headers(), respBody); 74 75 // 从HTTP应答报文构建返回数据 76 return WXPayUtility.fromJson(respBody, AvailMchRangeCreateResponse.class); 77 } else { 78 throw new WXPayUtility.ApiException(httpResponse.code(), respBody, httpResponse.headers()); 79 } 80 } catch (IOException e) { 81 throw new UncheckedIOException("Sending request to " + uri + " failed.", e); 82 } 83 } 84 85 private final String mchid; 86 private final String certificateSerialNo; 87 private final PrivateKey privateKey; 88 private final String wechatPayPublicKeyId; 89 private final PublicKey wechatPayPublicKey; 90 91 public CreateAvailMchRange(String mchid, String certificateSerialNo, String privateKeyFilePath, String wechatPayPublicKeyId, String wechatPayPublicKeyFilePath) { 92 this.mchid = mchid; 93 this.certificateSerialNo = certificateSerialNo; 94 this.privateKey = WXPayUtility.loadPrivateKeyFromPath(privateKeyFilePath); 95 this.wechatPayPublicKeyId = wechatPayPublicKeyId; 96 this.wechatPayPublicKey = WXPayUtility.loadPublicKeyFromPath(wechatPayPublicKeyFilePath); 97 } 98 99 public static class AvailMchRangeCreateRequest { 100 @SerializedName("sp_mchid") 101 public String spMchid; 102 103 @SerializedName("identifier_type") 104 public MerchantIdentifierType identifierType; 105 } 106 107 public static class AvailMchRangeCreateResponse { 108 @SerializedName("range_id") 109 public String rangeId; 110 } 111 112 public enum MerchantIdentifierType { 113 @SerializedName("ORGANIZATION_CODE") 114 ORGANIZATION_CODE, 115 @SerializedName("TRANSACTION_ID") 116 TRANSACTION_ID 117 } 118 119} 120
需配合微信支付工具库 wxpay_utility 使用,请参考Go
1package main 2 3import ( 4 "bytes" 5 "demo/wxpay_utility" // 引用微信支付工具库,参考 https://pay.weixin.qq.com/doc/v3/partner/4015119446 6 "encoding/json" 7 "fmt" 8 "net/http" 9 "net/url" 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 := &AvailMchRangeCreateRequest{ 27 SpMchid: wxpay_utility.String("12341234"), 28 IdentifierType: MERCHANTIDENTIFIERTYPE_ORGANIZATION_CODE.Ptr(), 29 } 30 31 response, err := CreateAvailMchRange(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 CreateAvailMchRange(config *wxpay_utility.MchConfig, request *AvailMchRangeCreateRequest) (response *AvailMchRangeCreateResponse, err error) { 43 const ( 44 host = "https://api.mch.weixin.qq.com" 45 method = "POST" 46 path = "/v3/webizpay/avail-mch-ranges" 47 ) 48 49 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path)) 50 if err != nil { 51 return nil, err 52 } 53 reqBody, err := json.Marshal(request) 54 if err != nil { 55 return nil, err 56 } 57 httpRequest, err := http.NewRequest(method, reqUrl.String(), bytes.NewReader(reqBody)) 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 httpRequest.Header.Set("Content-Type", "application/json") 64 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), reqBody) 65 if err != nil { 66 return nil, err 67 } 68 httpRequest.Header.Set("Authorization", authorization) 69 70 client := &http.Client{} 71 httpResponse, err := client.Do(httpRequest) 72 if err != nil { 73 return nil, err 74 } 75 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse) 76 if err != nil { 77 return nil, err 78 } 79 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 { 80 // 2XX 成功,验证应答签名 81 err = wxpay_utility.ValidateResponse( 82 config.WechatPayPublicKeyId(), 83 config.WechatPayPublicKey(), 84 &httpResponse.Header, 85 respBody, 86 ) 87 if err != nil { 88 return nil, err 89 } 90 response := &AvailMchRangeCreateResponse{} 91 if err := json.Unmarshal(respBody, response); err != nil { 92 return nil, err 93 } 94 95 return response, nil 96 } else { 97 return nil, wxpay_utility.NewApiException( 98 httpResponse.StatusCode, 99 httpResponse.Header, 100 respBody, 101 ) 102 } 103} 104 105type AvailMchRangeCreateRequest struct { 106 SpMchid *string `json:"sp_mchid,omitempty"` 107 IdentifierType *MerchantIdentifierType `json:"identifier_type,omitempty"` 108} 109 110type AvailMchRangeCreateResponse struct { 111 RangeId *string `json:"range_id,omitempty"` 112} 113 114type MerchantIdentifierType string 115 116func (e MerchantIdentifierType) Ptr() *MerchantIdentifierType { 117 return &e 118} 119 120const ( 121 MERCHANTIDENTIFIERTYPE_ORGANIZATION_CODE MerchantIdentifierType = "ORGANIZATION_CODE" 122 MERCHANTIDENTIFIERTYPE_TRANSACTION_ID MerchantIdentifierType = "TRANSACTION_ID" 123) 124
应答参数
200 OK
range_id 必填 string(64)
【可用商户范围id】 出资服务商限制的额度卡可用商户范围列表的唯一标识
应答示例
200 OK
1{ 2 "range_id" : "89f5123e-5ad4-4e0e-89b1-63ced0c798c5" 3} 4
错误码
以下是本接口返回的错误码列表。详细错误码规则,请参考微信支付接口规则-错误码和错误提示

