WeixinPayController.java 3.0 KB
Newer Older
苗卫卫 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
package com.boco.nbd.wios.wx.controller;

import com.boco.nbd.cams.core.constant.CamsConstant;
import com.boco.nbd.wios.manage.service.impl.UserPayBillService;
import com.boco.nbd.wios.wx.service.WeixinPayCore;
import com.ihidea.core.util.ServletUtilsEx;
import com.ihidea.core.util.XMLUtilsEx;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.Charset;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 微信支付回调接口
 * @author kevin
 */
@Controller
public class WeixinPayController {
	private final static Logger logger = LoggerFactory.getLogger(WeixinPayController.class);

	@Autowired
	private UserPayBillService userPayBillService;

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/weixin.pay.notify.do")
	public void notify(HttpServletRequest request, HttpServletResponse response) throws Exception {

		String xmlStr = IOUtils.toString(request.getInputStream(), Charset.defaultCharset());
		logger.info("收到微信支付回调,数据:"+xmlStr);

		Map<String, String> requestMap = XMLUtilsEx.deserialize(xmlStr, Map.class);

		// 通知状态
		String returnCode = requestMap.get("return_code");
		
		// 订单状态
		String resultCode = requestMap.get("result_code");
		
		String signStr = requestMap.get("sign");
		// 商户订单号
		String out_trade_no = requestMap.get("out_trade_no");

		requestMap.remove("sign");
		String signedStr = WeixinPayCore.getSign(requestMap);

		Map<String, String> responseMap = new HashMap<>(8);

		if (StringUtils.equals("SUCCESS", returnCode) && StringUtils.equals("SUCCESS", resultCode) && StringUtils.isNotBlank(signStr) && signStr.equals(signedStr)) {
			// 支付成功更新账单
			payBillSuccess(requestMap);

			responseMap.put("return_code", "SUCCESS");
			responseMap.put("return_msg", "ok");
		} else {
			responseMap.put("return_code", "FAIL");
			responseMap.put("return_msg", requestMap.get("return_msg"));
		}

		ServletUtilsEx.renderText(response, "<xml>" + XMLUtilsEx.serialize(responseMap) + "</xml>");
	}

	public void payBillSuccess(Map requestMap){
		// 商户订单号
		String out_trade_no = (String)requestMap.get("out_trade_no");

		logger.info("支付成功:"+out_trade_no);

		// 交易号
		String transaction_id = (String)requestMap.get("transaction_id");
		// 微信openId
		String openId = (String)requestMap.get("openid");
		// 附加数据
		String attach = (String)requestMap.get("attach");

		// 更新支付信息
		int source = StringUtils.equals(attach, "2") ? 2 : 1;
		userPayBillService.paySuccess(out_trade_no.split(CamsConstant.UNDER_LINE)[0], 3, openId, transaction_id, new Date(), source);

	}

}