应用场景:
支付完成后,微信会把相关支付结果和用户信息发送给商户,商户需要接收处理,并返回应答。 对后台通知交互时,如果微信收到商户的应答不是成功或超时,微信认为通知失败,微信会通过一定的策略定期重新发起通知,尽可能提高通知的成功率,但微信不保证通知最终能成功。 (通知频率为15/15/30/180/1800/1800/1800/1800/3600,单位:秒) 注意:同样的通知可能会多次发送给商户系统。商户系统必须能够正确处理重复的通知。 推荐的做法是,当收到通知进行处理时,首先检查对应业务数据的状态,判断该通知是否已经处理过,如果没有处理过再进行处理,如果处理过直接返回结果成功。在对业务数据进行状态检查和处理之前,要采用数据锁进行并发控制,以避免函数重入造成的数据混乱。 特别提醒:商户系统对于支付结果通知的内容一定要做签名验证,防止数据泄漏导致出现“假通知”,造成资金损失。 //支付结果通知接口 @RequestMapping(“/qlydweixinotify.do”) public void weixinotify(HttpServletRequest request, HttpServletResponse response) { PrintWriter out = null; StringBuffer xmlStr = new StringBuffer(); try { BufferedReader reader = request.getReader(); String line = null; while ((line = reader.readLine()) != null) { xmlStr.append(line); } Logger.getLogger(getClass()).debug(“支付回调通知:”+xmlStr.toString()); //检查xml是否有效 boolean flag=Signature.checkIsSignValidFromResponseString(xmlStr.toString()); WeixinNotifyResult result=null; if(flag){ NotifyResData wxData=(NotifyResData) Util.getObjectFromXML(xmlStr.toString(),NotifyResData.class); if(wxData !=null){ if(“SUCCESS”.equals(wxData.getReturn_code())){ OrderPayInfo orderPayInfo = new OrderPayInfo(); orderPayInfo.setOrderNum(wxData.getOut_trade_no()); orderPayInfo.setPayNum(wxData.getTransaction_id()); orderPayInfo.setPayPrice((double)wxData.getTotal_fee()/100+”“); orderPayInfo.setPaySource(wxData.getOpenid()); orderPayInfo.setPayTime(wxData.getTime_end()); orderPayInfo.setPayType(“2”);//1支付宝,2微信支付 OrderMessage returnMessage = orderProductServer .completeProductOrder(orderPayInfo); if (OrderStatus.FAIL.equals(returnMessage .getOrderStatus())) { Logger.getLogger(getClass()).error(“远程接口完成订单失败”); result=new WeixinNotifyResult(“FAIL”); result.setReturn_msg(“远程接口完成订单失败”); } else { result=new WeixinNotifyResult(“SUCCESS”); result.setReturn_msg(“成功”); } }else{ result=new WeixinNotifyResult(“FAIL”); result.setReturn_msg(“失败”); } }else{ result=new WeixinNotifyResult(“FAIL”); result.setReturn_msg(“解析参数格式失败”); } }else{ result=new WeixinNotifyResult(“FAIL”); result.setReturn_msg(“签名失败”); } response.getWriter().write(result.toString()); } catch (Exception e) { Logger.getLogger(getClass()).error(“qlydweixinotify.do”, e); ResponeDeal.getInstance().sendResponseStr(response, “404”, “连接超时”); } finally { if (out != null) { out.close(); } } }模拟http请求工具类:
HttpsRequestUtil.java package com.qlwb.weixin.util;import java.io.IOException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.RequestEntity; import org.apache.commons.httpclient.methods.StringRequestEntity; import org.apache.log4j.Logger;import com.qlwb.weixin.common.Configure;
import com.qlwb.weixin.common.Util; import com.qlwb.weixin.protocol.pay_protocol.WxPayReqData; import com.qlwb.weixin.protocol.payquery_protocol.PayQueryReqData;public class HttpsRequestUtil {
/** * * @方法名称:sendWxPayRequest * @内容摘要: <发送统一下单请求> * @param body * @param outTradeNo * @param totalFee * @param spBillCreateIP * @return * String * @exception * @author:鹿伟伟 * @创建日期:2016年2月19日-下午2:24:05 */public String sendWxPayRequest(String body,String detail,String outTradeNo,int totalFee,String spBillCreateIP ){ // 构造HTTP请求 HttpClient httpclient = new HttpClient(); PostMethod postMethod = new PostMethod(Configure.PAY_API); WxPayReqData wxdata = new WxPayReqData(body,detail,outTradeNo,totalFee,spBillCreateIP); String requestStr=""; requestStr=Util.ConvertObj2Xml(wxdata); // 发送请求 String strResponse = null; try { RequestEntity entity = new StringRequestEntity( requestStr.toString(), "text/xml", "UTF-8"); postMethod.setRequestEntity(entity); httpclient.executeMethod(postMethod); strResponse = new String(postMethod.getResponseBody(), "utf-8"); Logger.getLogger(getClass()).debug(strResponse); } catch (HttpException e) { Logger.getLogger(getClass()).error("sendWxPayRequest", e); } catch (IOException e) { Logger.getLogger(getClass()).error("sendWxPayRequest", e); } finally { postMethod.releaseConnection(); } return strResponse;}/** * * @方法名称:orderQueryRequest * @内容摘要: <查询订单信息> * @param transaction_id 微信的订单号,优先使用 * @return * String * @exception * @author:鹿伟伟 * @创建日期:2016年2月19日-下午2:44:11 */public String orderQueryRequest(String transactionID, String outTradeNo ){ // 构造HTTP请求 HttpClient httpclient = new HttpClient(); PostMethod postMethod = new PostMethod(Configure.PAY_QUERY_API); PayQueryReqData wxdata = new PayQueryReqData(transactionID,outTradeNo); String requestStr=""; requestStr=Util.ConvertObj2Xml(wxdata); // 发送请求 String strResponse = null; try { RequestEntity entity = new StringRequestEntity( requestStr.toString(), "text/xml", "UTF-8"); postMethod.setRequestEntity(entity); httpclient.executeMethod(postMethod); strResponse = new String(postMethod.getResponseBody(), "utf-8"); } catch (HttpException e) { Logger.getLogger(getClass()).error("orderQueryRequest", e); } catch (IOException e) { Logger.getLogger(getClass()).error("orderQueryRequest", e); } finally { postMethod.releaseConnection(); } return strResponse;}
}