支付完成后,微信会把相关支付结果和用户信息发送给商户,商户需要接收处理,并返回应答。
对后台通知交互时,如果微信收到商户的应答不是成功或超时,微信认为通知失败,微信会通过一定的策略定期重新发起通知,尽可能提高通知的成功率,但微信不保证通知最终能成功。 (通知频率为15/15/30/180/1800/1800/1800/1800/3600,单位:秒)
注意:同样的通知可能会多次发送给商户系统。商户系统必须能够正确处理重复的通知。
推荐的做法是,当收到通知进行处理时,首先检查对应业务数据的状态,判断该通知是否已经处理过,如果没有处理过再进行处理,如果处理过直接返回结果成功。在对业务数据进行状态检查和处理之前,要采用数据锁进行并发控制,以避免函数重入造成的数据混乱。
特别提醒:商户系统对于支付结果通知的内容一定要做签名验证,防止数据泄漏导致出现“假通知”,造成资金损失。
模拟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; }}以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。