应用场景:

支付完成后,微信会把相关支付结果和用户信息发送给商户,商户需要接收处理,并返回应答。

对后台通知交互时,如果微信收到商户的应答不是成功或超时,微信认为通知失败,微信会通过一定的策略定期重新发起通知,尽可能提高通知的成功率,但微信不保证通知最终能成功。 (通知频率为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;
}

}

最新文章

  1. git常用命令(持续更新中)
  2. nginx配置文件简单说明
  3. 处理session跨域几种的方案
  4. Python函数式编程:从入门到走火入魔
  5. STL数组处理常用函数
  6. NDK-gdb
  7. mysql-5.6.17-win32免安装版配置
  8. ASP.NET MVC 4源码分析之如何定位控制器
  9. 学习Shell脚本编程(第5期)_表达式的比较
  10. UIPickView之自定义生日键盘和城市键盘
  11. 读书与写论文的引导书——leo鉴书60
  12. JavaScript 运动框架 Step by step(转)
  13. 如何使用JavaScript实现纯前端读取和导出excel文件
  14. java 连接数组
  15. vue2.0 项目搭建 和vue 2.0 electron 项目搭建
  16. 描述整体程序的 app
  17. qualcomm batch 烧录脚本
  18. 7.MySQL必知必会之用通配符进行过滤-like
  19. Go语言反射之值反射
  20. IPv4协议及VLSM可变长子网划分和CIDR无类域间路由

热门文章

  1. MySQL_各城市在线产品天订单数据20161130
  2. bzoj 2865 字符串识别 —— 后缀数组
  3. vijos:P1053Easy sssp(spfa判环)
  4. css使图片变成黑白效果
  5. SSM之全局异常处理器
  6. fastreport整理
  7. excel批量提取网页标题
  8. windows64下安装MySQLdb连接数据库
  9. C#生成满足特定要求的密码
  10. 封装类似thinkphp连贯操作数据库的Db类(简单版)。