查询合单订单
更新时间:2025.01.17合单支付下单成功后,服务商(合单发起方)可调用该接口查询合单订单的交易状态。
|
接口说明
支持商户:【普通服务商】
请求方式:【GET】/v3/combine-transactions/out-trade-no/{combine_out_trade_no}
请求域名:【主域名】https://api.mch.weixin.qq.com 使用该域名将访问就近的接入点
【备域名】https://api2.mch.weixin.qq.com 使用该域名将访问异地的接入点 ,指引点击查看
请求参数
Header HTTP头参数
Authorization 必填 string
请参考签名认证生成认证信息
Accept 必填 string
请设置为application/json
path 路径参数
combine_out_trade_no 必填 string(32)
【合单商户订单号】下单时传入的合单商户订单号。
请求示例
GET
1curl -X GET \ 2 https://api.mch.weixin.qq.com/v3/combine-transactions/out-trade-no/P20150806125346 \ 3 -H "Authorization: WECHATPAY2-SHA256-RSA2048 mchid=\"1900000001\",..." \ 4 -H "Accept: application/json" 5
需配合微信支付工具库 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 UnionQueryByOutTradeNo { 26 private static String HOST = "https://api.mch.weixin.qq.com"; 27 private static String METHOD = "GET"; 28 private static String PATH = "/v3/combine-transactions/out-trade-no/{combine_out_trade_no}"; 29 30 public static void main(String[] args) { 31 // TODO: 请准备商户开发必要参数,参考:https://pay.weixin.qq.com/doc/v3/partner/4013080340 32 UnionQueryByOutTradeNo client = new UnionQueryByOutTradeNo( 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 UnionQueryByOutTradeNoRequest request = new UnionQueryByOutTradeNoRequest(); 41 request.combineOutTradeNo = "P20150806125346"; 42 try { 43 UnionAPIv3UnionQueryResponse response = client.run(request); 44 // TODO: 请求成功,继续业务逻辑 45 System.out.println(response); 46 } catch (WXPayUtility.ApiException e) { 47 // TODO: 请求失败,根据状态码执行不同的逻辑 48 e.printStackTrace(); 49 } 50 } 51 52 public UnionAPIv3UnionQueryResponse run(UnionQueryByOutTradeNoRequest request) { 53 String uri = PATH; 54 uri = uri.replace("{combine_out_trade_no}", WXPayUtility.urlEncode(request.combineOutTradeNo)); 55 56 Request.Builder reqBuilder = new Request.Builder().url(HOST + uri); 57 reqBuilder.addHeader("Accept", "application/json"); 58 reqBuilder.addHeader("Wechatpay-Serial", wechatPayPublicKeyId); 59 reqBuilder.addHeader("Authorization", WXPayUtility.buildAuthorization(mchid, certificateSerialNo, privateKey, METHOD, uri, null)); 60 reqBuilder.method(METHOD, null); 61 Request httpRequest = reqBuilder.build(); 62 63 // 发送HTTP请求 64 OkHttpClient client = new OkHttpClient.Builder().build(); 65 try (Response httpResponse = client.newCall(httpRequest).execute()) { 66 String respBody = WXPayUtility.extractBody(httpResponse); 67 if (httpResponse.code() >= 200 && httpResponse.code() < 300) { 68 // 2XX 成功,验证应答签名 69 WXPayUtility.validateResponse(this.wechatPayPublicKeyId, this.wechatPayPublicKey, 70 httpResponse.headers(), respBody); 71 72 // 从HTTP应答报文构建返回数据 73 return WXPayUtility.fromJson(respBody, UnionAPIv3UnionQueryResponse.class); 74 } else { 75 throw new WXPayUtility.ApiException(httpResponse.code(), respBody, httpResponse.headers()); 76 } 77 } catch (IOException e) { 78 throw new UncheckedIOException("Sending request to " + uri + " failed.", e); 79 } 80 } 81 82 private final String mchid; 83 private final String certificateSerialNo; 84 private final PrivateKey privateKey; 85 private final String wechatPayPublicKeyId; 86 private final PublicKey wechatPayPublicKey; 87 88 public UnionQueryByOutTradeNo(String mchid, String certificateSerialNo, String privateKeyFilePath, String wechatPayPublicKeyId, String wechatPayPublicKeyFilePath) { 89 this.mchid = mchid; 90 this.certificateSerialNo = certificateSerialNo; 91 this.privateKey = WXPayUtility.loadPrivateKeyFromPath(privateKeyFilePath); 92 this.wechatPayPublicKeyId = wechatPayPublicKeyId; 93 this.wechatPayPublicKey = WXPayUtility.loadPublicKeyFromPath(wechatPayPublicKeyFilePath); 94 } 95 96 public static class UnionQueryByOutTradeNoRequest { 97 @SerializedName("combine_out_trade_no") 98 @Expose(serialize = false) 99 public String combineOutTradeNo; 100 } 101 102 public static class UnionAPIv3UnionQueryResponse { 103 @SerializedName("combine_appid") 104 public String combineAppid; 105 106 @SerializedName("combine_mchid") 107 public String combineMchid; 108 109 @SerializedName("combine_out_trade_no") 110 public String combineOutTradeNo; 111 112 @SerializedName("combine_payer_info") 113 public UnionCommRespPayerInfo combinePayerInfo; 114 115 @SerializedName("scene_info") 116 public UnionCommRespSceneInfo sceneInfo; 117 118 @SerializedName("sub_orders") 119 public List<UnionSubOrder> subOrders; 120 } 121 122 public static class UnionCommRespPayerInfo { 123 @SerializedName("openid") 124 public String openid; 125 } 126 127 public static class UnionCommRespSceneInfo { 128 @SerializedName("device_id") 129 public String deviceId; 130 } 131 132 public static class UnionSubOrder { 133 @SerializedName("mchid") 134 public String mchid; 135 136 @SerializedName("sub_mchid") 137 public String subMchid; 138 139 @SerializedName("sub_appid") 140 public String subAppid; 141 142 @SerializedName("sub_openid") 143 public String subOpenid; 144 145 @SerializedName("out_trade_no") 146 public String outTradeNo; 147 148 @SerializedName("transaction_id") 149 public String transactionId; 150 151 @SerializedName("trade_type") 152 public String tradeType; 153 154 @SerializedName("trade_state") 155 public String tradeState; 156 157 @SerializedName("bank_type") 158 public String bankType; 159 160 @SerializedName("attach") 161 public String attach; 162 163 @SerializedName("success_time") 164 public String successTime; 165 166 @SerializedName("amount") 167 public UnionCommRespAmountInfo amount; 168 169 @SerializedName("promotion_detail") 170 public List<UnionPromotionDetail> promotionDetail; 171 } 172 173 public static class UnionCommRespAmountInfo { 174 @SerializedName("total_amount") 175 public Long totalAmount; 176 177 @SerializedName("payer_amount") 178 public Long payerAmount; 179 180 @SerializedName("currency") 181 public String currency; 182 183 @SerializedName("payer_currency") 184 public String payerCurrency; 185 186 @SerializedName("settlement_rate") 187 public Long settlementRate; 188 } 189 190 public static class UnionPromotionDetail { 191 @SerializedName("coupon_id") 192 public String couponId; 193 194 @SerializedName("name") 195 public String name; 196 197 @SerializedName("scope") 198 public String scope; 199 200 @SerializedName("type") 201 public String type; 202 203 @SerializedName("amount") 204 public Long amount; 205 206 @SerializedName("stock_id") 207 public String stockId; 208 209 @SerializedName("wechatpay_contribute") 210 public Long wechatpayContribute; 211 212 @SerializedName("merchant_contribute") 213 public Long merchantContribute; 214 215 @SerializedName("other_contribute") 216 public Long otherContribute; 217 218 @SerializedName("currency") 219 public String currency; 220 221 @SerializedName("goods_detail") 222 public List<GoodsDetailInPromotion> goodsDetail; 223 } 224 225 public static class GoodsDetailInPromotion { 226 @SerializedName("goods_id") 227 public String goodsId; 228 229 @SerializedName("quantity") 230 public Long quantity; 231 232 @SerializedName("unit_price") 233 public Long unitPrice; 234 235 @SerializedName("discount_amount") 236 public Long discountAmount; 237 238 @SerializedName("goods_remark") 239 public String goodsRemark; 240 } 241 242} 243
需配合微信支付工具库 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 := &UnionQueryByOutTradeNoRequest{ 27 CombineOutTradeNo: wxpay_utility.String("P20150806125346"), 28 } 29 30 response, err := UnionQueryByOutTradeNo(config, request) 31 if err != nil { 32 fmt.Printf("请求失败: %+v\n", err) 33 // TODO: 请求失败,根据状态码执行不同的处理 34 return 35 } 36 37 // TODO: 请求成功,继续业务逻辑 38 fmt.Printf("请求成功: %+v\n", response) 39} 40 41func UnionQueryByOutTradeNo(config *wxpay_utility.MchConfig, request *UnionQueryByOutTradeNoRequest) (response *UnionApiv3UnionQueryResponse, err error) { 42 const ( 43 host = "https://api.mch.weixin.qq.com" 44 method = "GET" 45 path = "/v3/combine-transactions/out-trade-no/{combine_out_trade_no}" 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, "{combine_out_trade_no}", url.PathEscape(*request.CombineOutTradeNo), -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 // 2XX 成功,验证应答签名 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 := &UnionApiv3UnionQueryResponse{} 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 UnionQueryByOutTradeNoRequest struct { 101 CombineOutTradeNo *string `json:"combine_out_trade_no,omitempty"` 102} 103 104func (o *UnionQueryByOutTradeNoRequest) MarshalJSON() ([]byte, error) { 105 type Alias UnionQueryByOutTradeNoRequest 106 a := &struct { 107 CombineOutTradeNo *string `json:"combine_out_trade_no,omitempty"` 108 *Alias 109 }{ 110 // 序列化时移除非 Body 字段 111 CombineOutTradeNo: nil, 112 Alias: (*Alias)(o), 113 } 114 return json.Marshal(a) 115} 116 117type UnionApiv3UnionQueryResponse struct { 118 CombineAppid *string `json:"combine_appid,omitempty"` 119 CombineMchid *string `json:"combine_mchid,omitempty"` 120 CombineOutTradeNo *string `json:"combine_out_trade_no,omitempty"` 121 CombinePayerInfo *UnionCommRespPayerInfo `json:"combine_payer_info,omitempty"` 122 SceneInfo *UnionCommRespSceneInfo `json:"scene_info,omitempty"` 123 SubOrders []UnionSubOrder `json:"sub_orders,omitempty"` 124} 125 126type UnionCommRespPayerInfo struct { 127 Openid *string `json:"openid,omitempty"` 128} 129 130type UnionCommRespSceneInfo struct { 131 DeviceId *string `json:"device_id,omitempty"` 132} 133 134type UnionSubOrder struct { 135 Mchid *string `json:"mchid,omitempty"` 136 SubMchid *string `json:"sub_mchid,omitempty"` 137 SubAppid *string `json:"sub_appid,omitempty"` 138 SubOpenid *string `json:"sub_openid,omitempty"` 139 OutTradeNo *string `json:"out_trade_no,omitempty"` 140 TransactionId *string `json:"transaction_id,omitempty"` 141 TradeType *string `json:"trade_type,omitempty"` 142 TradeState *string `json:"trade_state,omitempty"` 143 BankType *string `json:"bank_type,omitempty"` 144 Attach *string `json:"attach,omitempty"` 145 SuccessTime *string `json:"success_time,omitempty"` 146 Amount *UnionCommRespAmountInfo `json:"amount,omitempty"` 147 PromotionDetail []UnionPromotionDetail `json:"promotion_detail,omitempty"` 148} 149 150type UnionCommRespAmountInfo struct { 151 TotalAmount *int64 `json:"total_amount,omitempty"` 152 PayerAmount *int64 `json:"payer_amount,omitempty"` 153 Currency *string `json:"currency,omitempty"` 154 PayerCurrency *string `json:"payer_currency,omitempty"` 155 SettlementRate *int64 `json:"settlement_rate,omitempty"` 156} 157 158type UnionPromotionDetail struct { 159 CouponId *string `json:"coupon_id,omitempty"` 160 Name *string `json:"name,omitempty"` 161 Scope *string `json:"scope,omitempty"` 162 Type *string `json:"type,omitempty"` 163 Amount *int64 `json:"amount,omitempty"` 164 StockId *string `json:"stock_id,omitempty"` 165 WechatpayContribute *int64 `json:"wechatpay_contribute,omitempty"` 166 MerchantContribute *int64 `json:"merchant_contribute,omitempty"` 167 OtherContribute *int64 `json:"other_contribute,omitempty"` 168 Currency *string `json:"currency,omitempty"` 169 GoodsDetail []GoodsDetailInPromotion `json:"goods_detail,omitempty"` 170} 171 172type GoodsDetailInPromotion struct { 173 GoodsId *string `json:"goods_id,omitempty"` 174 Quantity *int64 `json:"quantity,omitempty"` 175 UnitPrice *int64 `json:"unit_price,omitempty"` 176 DiscountAmount *int64 `json:"discount_amount,omitempty"` 177 GoodsRemark *string `json:"goods_remark,omitempty"` 178} 179
应答参数
200 OK
combine_appid 必填 string(32)
【合单服务商APPID】下单时传入的服务商的APPID。
combine_mchid 必填 string(32)
【合单服务商商户号】下单时传入的服务商商户号。
combine_payer_info 选填 object
【合单支付者信息】 合单订单支付者的信息,支付成功后返回。
属性 | |
openid 选填 string(128) 【用户服务商标识】实际支付的用户在 |
sub_orders 选填 array[object]
【子单信息列表】】合单下单时传入的子单信息列表。
属性 | |||||||||||||
mchid 必填 string(32) 【服务商商户号】合单下单时传入的服务商商户号。 trade_type 选填 string 【交易类型】 当前订单的交易类型,支付成功后返回,枚举值: trade_state 必填 string 【交易状态】 交易状态,详细业务流转状态处理请参考开发指引-订单状态流转图。枚举值: bank_type 选填 string(32) 【银行类型】 用户支付方式,订单支付成功后返回, attach 选填 string(128) 【商户数据包】服务商下单时传入的自定义数据包,用户不可见,长度不超过128字符,若下单传入该参数,则订单支付成功后此接口和合单订单支付成功回调通知接口以及交易账单中会原样返回;若下单未传该参数,则不会返回。 success_time 选填 string(32) 【支付完成时间】 amount 选填 object 【子单金额信息】 子订单收款金额信息,订单支付成功后返回。
transaction_id 选填 string(32) 【子单微信支付订单号】微信支付侧为每个子单分配的唯一标识,订单支付成功后返回。 out_trade_no 必填 string(32) 【子单商户订单号】合单下单时传入的子单商户订单号。 sub_mchid 必填 string(32) 【子商户号(也叫特约商户号)】 合单下单时传入的子商户号。 sub_appid 选填 string(32) 【子商户APPID】 合单下单时传入的子商户APPID,sub_mchid绑定的sub_appid。 sub_openid 选填 string(128) 【用户子商户标识】下单时传入了sub_appid且支付成功后返回。 promotion_detail 选填 array[object] 【优惠功能】 代金券信息,当订单支付时,有使用代金券时,该字段将返回所使用的代金券信息。
|
scene_info 选填 object
【场景信息】下单时传入的支付场景描述,若下单传入该参数,则原样返回;若下单未传该参数,则不会返回。
属性 | |
device_id 选填 string(32) 【商户端设备号】 终端设备号(门店号或收银设备ID) |
combine_out_trade_no 必填 string(32)
【合单商户订单号】 合单下单时传入的合单商户订单号。
应答示例
200 OK
1{ 2 "combine_appid" : "wxd678efh567hg6787", 3 "combine_mchid" : "1230000109", 4 "combine_out_trade_no" : "P20150806125346", 5 "combine_payer_info" : { 6 "openid" : "oUpF8uMuAJO_M2pxb1Q9zNjWeS6o" 7 }, 8 "scene_info" : { 9 "device_id" : "POS1:1" 10 }, 11 "sub_orders" : [ 12 { 13 "mchid" : "1900000109", 14 "sub_mchid" : "1230000109", 15 "sub_appid" : "wxd678efh567hg6999", 16 "sub_openid" : "oUpF8uMuAJO_M2pxb1Q9zNjWeS6o", 17 "out_trade_no" : "20150806125346", 18 "transaction_id" : "1009660380201506130728806387", 19 "trade_type" : "JSAPI", 20 "trade_state" : "SUCCESS", 21 "bank_type" : "CMC", 22 "attach" : "深圳分店", 23 "success_time" : "2015-05-20T13:29:35.120+08:00", 24 "amount" : { 25 "total_amount" : 10, 26 "payer_amount" : 10, 27 "currency" : "CNY", 28 "payer_currency" : "CNY", 29 "settlement_rate" : 650000000 30 }, 31 "promotion_detail" : [ 32 { 33 "coupon_id" : "109519", 34 "name" : "单品惠-6", 35 "scope" : "GLOBAL", 36 "type" : "CASH", 37 "amount" : 100, 38 "stock_id" : "931386", 39 "wechatpay_contribute" : 0, 40 "merchant_contribute" : 0, 41 "other_contribute" : 0, 42 "currency" : "CNY", 43 "goods_detail" : [ 44 { 45 "goods_id" : "M1006", 46 "quantity" : 1, 47 "unit_price" : 100, 48 "discount_amount" : 1, 49 "goods_remark" : "商品备注信息" 50 } 51 ] 52 } 53 ] 54 } 55 ] 56} 57
错误码
公共错误码
状态码 | 错误码 | 描述 | 解决方案 |
---|---|---|---|
400 | PARAM_ERROR | 参数错误 | 请根据错误提示正确传入参数 |
400 | INVALID_REQUEST | HTTP 请求不符合微信支付 APIv3 接口规则 | 请参阅接口规则检查传入的参数 |
401 | SIGN_ERROR | 验证不通过 | 请参阅签名常见问题排查 |
500 | SYSTEM_ERROR | 系统异常,请稍后重试 | 请稍后重试 |
业务错误码
状态码 | 错误码 | 描述 | 解决方案 |
---|---|---|---|
400 | APPID_MCHID_NOT_MATCH | AppID和mch_id不匹配 | 请确认AppID和mch_id是否匹配,参考:查询商户号绑定的APPID |
400 | INVALID_REQUEST | 无效请求 | 请根据接口返回的详细信息检查 |
400 | MCH_NOT_EXISTS | 商户号不存在 | 请检查商户号是否正确,商户号获取方法参考:服务商模式开发必要参数说明 |
400 | ORDER_CLOSED | 订单已关闭 | 当前订单已关闭,请重新下单 |
401 | SIGN_ERROR | 签名错误 | 请检查签名参数和方法是否都符合签名算法要求,参考:如何生成签名 |
403 | NOAUTH | 商户无权限 | 请商户联系对接运营申请此接口相关权限,参考:权限申请 |
403 | OUT_TRADE_NO_USED | 商户订单号重复 | 请核实商户订单号是否重复提交 |
403 | RULELIMIT | 业务规则限制 | 因业务规则限制请求频率,请查看接口返回的详细信息 |
429 | FREQUENCY_LIMITED | 频率超限 | 请降低请求接口频率 |
500 | SYSTEMERROR | 系统错误 | 系统异常,请用相同参数重新调用 |