本文对接银联商务公众号+服务窗支付,实现支付下单、订单查询、订单退款、退款查询和订单关闭几个功能,使用到银联商务的公众+服务窗支付接口规范,请自行百度下载。
在上一篇支付下单中,已经实现公众号下单支付,本篇将介绍下单查询接口的对接实现,并将我使用到的银联商务的请求接口和响应接口进行封装,使用测试网页输出测试信息。
一、接口及代码实现
1.下单查询接口
1.1接口规范
接口规范请查看下载的银联商务公众号+服务窗接口规范下单查询接口部分,这里不做展示。
1.2 代码实现
1.2.1 UnifiedQueryInfoController代码
/**
*
* 根据商户订单号查询支付状态
* @param request
* @param response
* @param merOrderId 商户订单号
* @return
* @throws UnsupportedEncodingException
*/
@RequestMapping(value = "/OrderQuery", method = RequestMethod.POST)
public Map<String, Object> query(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonreq) throws UnsupportedEncodingException {
Map<String,Object> reqmap = new HashMap<String,Object>(); //客户端原始请求map
Map<String,Object> map = new HashMap<String,Object>(); //请求银联商务map
Map<String,Object> datamap = new HashMap<String,Object>(); //客户端原始请求Data数据
Map<String,Object> resultmap = new HashMap<String,Object>(); //返回结果
//接收客户端请求数据并转换成map
reqmap = JsonUtils.JsonToMapObj(jsonreq);
logger.info("unifiedquery reqmap = " + reqmap);
datamap = (Map<String, Object>) reqmap.get("tradeParam"); //tradeParam请求体数据
logger.info("unifiedquery datamap = " + datamap);
//---------------------step1 验证签名-----------------------------
String sign = (String) reqmap.get("sign"); //获取sign
String key = UnionPayConstants.GGMD5KEY; //国光MD5密钥
try {
if (!PayUtil.verifySign(datamap,key,sign)) {
resultmap.put("returnCode", "Bad_Sign");
resultmap.put("returnInfo", "签名错误");
return resultmap;
}
//-----------------------step2 验证消息类型 tradeType:query--------------------
if (!"query".equals(reqmap.get("tradeType"))) {
resultmap.put("returnCode", "TradeType_Error");
resultmap.put("returnInfo", "消息类型不符");
return resultmap;
}
//------------------------step3 验证传参完整性----------------------------------
//验证公共参数完整性
if(!PayUtil.verifyParameter(datamap)) {
resultmap.put("returnCode", "Common_Value_Error");
resultmap.put("returnInfo", "缺少必要公共参数");
return resultmap;
}
//验证接口参数完整性
if(datamap.get("merOrderId").equals(" ")) {
resultmap.put("returnCode", "Value_Error");
resultmap.put("returnInfo", "缺少必要接口参数");
return resultmap;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
resultmap.put("returnCode", "System_Error");
resultmap.put("returnInfo", "系统异常");
return resultmap;
}
//------------------------step4 传值并调用订单查询接口----------------------------------
map.put("mid", datamap.get("mid")); //商户号
map.put("tid", datamap.get("tid")); //终端号
map.put("instMid", datamap.get("instMid")); //机构商户号
map.put("msgSrc", datamap.get("msgSrc")); //消息来源
map.put("merOrderId", datamap.get("merOrderId")); //需查询的商户的订单号
//map.put("msgType", reqmap.get("tradeType")); //消息类型 query
Map<String, Object> querymap = queryinfoserviceimpl.QueryInfo(map);
logger.info("return querymap = " + querymap);
//根据post返回码,判断是否接收银联网付响应信息
if(!"200".equals(querymap.get("statuscode"))) {
resultmap.put("returnCode", "Union_Connect_Error");
resultmap.put("returnInfo", "银联网付连接失败");
return resultmap;
}
//------------------------step5接口返回数据----------------------------------
if(querymap.get("errCode").equals("SUCCESS")) {
Map<String,Object> resultdatamap = new HashMap<String,Object>(); //resultmap中data数据
resultdatamap.put("errCode", querymap.get("errCode")); //平台错误码
resultdatamap.put("errMsg", querymap.get("errMsg")); //平台错误信息
resultdatamap.put("status", querymap.get("status")); //交易状态
resultdatamap.put("totalAmount", querymap.get("totalAmount")); //支付总金额
resultdatamap.put("targetOrderId", querymap.get("targetOrderId")); //第三方订单号
resultdatamap.put("targetSys", querymap.get("targetSys")); //目标平台代码(第三方支付状态)
resultdatamap.put("targetStatus", querymap.get("targetStatus")); //目标平台状态
resultdatamap.put("payTime", querymap.get("payTime")); //支付时间
resultdatamap.put("messageType", querymap.get("msgType")); //消息类型
resultmap.put("data", resultdatamap);
resultmap.put("returnCode", querymap.get("errCode"));
//resultmap.put("returnInfo", querymap.get("errMsg"));
return resultmap;
}else{
//错误返回信息
resultmap.put("returnCode", querymap.get("errCode"));
resultmap.put("returnInfo", querymap.get("errMsg"));
return resultmap;
}
}
}
1.2.2 queryinfoserviceimpl代码
本部分代码为银联商务支付下单查询接口服务。
@Override
public Map<String, Object> QueryInfo(Map<String, Object> map) throws UnsupportedEncodingException {
// TODO Auto-generated method stub
logger.info("------------------unfiedquery--------------------------");
Map<String, Object> reqmap = new HashMap<String, Object>(); //请求map
Map<String, Object> resp = new HashMap<String, Object>(); //响应resp
//reqmap.put("mid", this.mid); //内部测试
reqmap.put("mid", map.get("mid").toString()); //商户号
reqmap.put("tid", map.get("tid").toString()); //终端号
reqmap.put("instMid", map.get("instMid").toString());
reqmap.put("msgSrc", map.get("msgSrc").toString()); //消息来源
reqmap.put("msgId", "UnionPay_F002"); //msgId,查询接口
reqmap.put("msgType", "query"); //消息类型
//报文请求时间
String aligetTime = PayUtil.aligetTime();
logger.info("请求时间aligetTime = " + aligetTime);
reqmap.put("requestTimestamp", aligetTime);
reqmap.put("merOrderId", map.get("merOrderId").toString()); //商户订单号,前端传入
//生成待签名字符串并进行MD5加密
String builderSignStr = "";
try {
builderSignStr = PayUtil.builderSignStr(reqmap,UnionPayConstants.MD5KEY);
//signString = PayUtil.generateSignature(reqmap, UnionPayConstants.MD5KEY);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
reqmap.put("sign", builderSignStr);
logger.info("reqmap= " + reqmap);
String jsonstring = GGitUtil.MapToJson2(reqmap); //请求map转成json
logger.info("发送query post请求消息:" + jsonstring);
//接收银联商务返回map
resp = unionpayrequest.dopost(UnionPayConstants.queryURL, jsonstring);
return resp;
}
其中,queryURL为请求银联商务地址:
https://qr-test2.chinaums.com/netpay-route-server/api/