/**
* 微信支付结果通知
*/
@Component
@RequestMapping("/地址1/")
public class WechatPayNativeResultAction {
protected transient Logger logger = Logger.getLogger(this.getClass().getName());
@Autowired
private IWechatTradeFlowService wechatTradeFlowService;
@Autowired
private PayMedicalCardBusiInfoService payMedicalCardBusiInfoService;
@Autowired
private IWXPaySdkService wxPaySdkService;
/**
* 处理支付通知
*
* @return
* @throws UnsupportedEncodingException
*/
@RequestMapping(value = "/地址2/地址3.jhtml", method = RequestMethod.POST)
@ResponseBody
public String payResult(HttpServletRequest request, @PathVariable String accountId) throws UnsupportedEncodingException {
StringBuffer body = null;
try {
body = new StringBuffer();
BufferedReader reader = request.getReader();
String line = null;
while ((line = reader.readLine()) != null) {
body.append(line);
}
System.out.println(" *** 接收的通知 >>> message : " + body);
if (!body.equals("")) {
Map<String, String> resultMap = WXPayUtil.xmlToMap(body.toString());
WechatConfigNative aNative = WechatConfig.WECHAT_NATIVE_PAY_CONFIG.get(accountId);
WXPay wxpay = new WXPay(aNative);
if (!wxpay.isPayResultNotifySignatureValid(resultMap)) {
System.out.println("签名效验失败==============》》》》》》》》》:" + body);
return WXPaySdkServiceImpl.setXml("FAIL", "签名效验失败");
}
//判断支付是否正常
if ("SUCCESS".equals(resultMap.get("return_code")) && resultMap.containsKey("result_code") && "SUCCESS".equals(resultMap.get("result_code"))) {
//正常交易,进行数据修改
JSONObject jsonObj = JSONObject.fromObject(resultMap.get("attach"));
String pkId = jsonObj.getString("p");
//查询订单是否通知过
WechatTradeFlow isHis = wechatTradeFlowService.selectByPrimaryKey(pkId);
if (isHis.getExt1().equals("1")) {
return WXPaySdkServiceImpl.setXml("SUCCESS", "OK");
}
//修改微信交易订单信息
WechatTradeFlow tradeFlow = new WechatTradeFlow();
//=====支付结果信息======//
tradeFlow.setId(pkId);//设置主键
tradeFlow.setTradeType(resultMap.get("trade_type"));//交易类型
tradeFlow.setPayResult(resultMap.get("result_code"));//支付结果
tradeFlow.setPayInfo(resultMap.get("attach"));//支付信息
tradeFlow.setTransactionId(resultMap.get("transaction_id"));//威富通订单号
tradeFlow.setOutTransactionId(resultMap.get("out_trade_no"));//第三方订单号
tradeFlow.setFeeType(resultMap.get("fee_type"));//货币种类
tradeFlow.setBankType(resultMap.get("bank_type"));//付款银行
tradeFlow.setBankBillno(resultMap.get("openid"));//openid
tradeFlow.setTimeEnd(DateUtils.str2Date(resultMap.get("time_end"), "yyyyMMddHHmmss"));//支付完成时间
//=====修改订单状态======//
tradeFlow.setWxStatus("付款完成");
tradeFlow.setExt1("1");//设置已经通知
int code = wechatTradeFlowService.updateByPrimaryKeySelective(tradeFlow);
if (code > 0) {
//支付成功
return WXPaySdkServiceImpl.setXml("SUCCESS", "OK");
}else {
logger.error(" ***修改订单错误 >>> code : " + JSONObject.fromObject(resultMap));
//返回错误信息
return WXPaySdkServiceImpl.setXml("FAIL", "通知交易订单错误");
}
} else {
logger.error(" ***修改订单错误 >>> code : " + JSONObject.fromObject(resultMap));
//返回错误信息
return WXPaySdkServiceImpl.setXml("FAIL", "通知交易订单错误");
}
} else {
logger.error(" *** 支付失败 >>> message : " + body);
return WXPaySdkServiceImpl.setXml("FAIL", "通知交易订单错误");
}
} catch (Exception e) {
e.printStackTrace();
logger.error(" *** 通知交易订单错误 >>> message : " + e.getMessage() + " >>> body" + body);
return WXPaySdkServiceImpl.setXml("FAIL", "通知交易订单错误");
}
}
}
/**
*给微信端返回通知结果的工具类,按标准的通知格式
*/
public class WXPaySdkServiceImpl{
//通过xml 发给微信消息
public static String setXml(String return_code, String return_msg) {
SortedMap<String, String> parameters = new TreeMap<String, String>();
parameters.put("return_code", return_code);
parameters.put("return_msg", return_msg);
return "<xml><return_code><![CDATA[" + return_code + "]]>" +
"</return_code><return_msg><![CDATA[" + return_msg + "]]></return_msg></xml>";
}
}
统一下单时异步通知地址 notify_url 参数必须是外网可访问的地址,不能是本地IP地址。
http://ip:端口号/地址1/地址2/地址3.jhtml
有虚拟地址的加上虚拟地址。