mark Pay http://git.oschina.net/littleCrazy/dianshangpingtai-zhifu/blob/master/OrderPayController
@Controller@RequestMapping("/api/pay/")public class OrderPayController extends BaseController{@Autowiredprivate OrderManager orderManager;@Autowiredprivate ProductByIdCacheManager productByIdCacheManager;@AutowiredOrderPaymentTypeRecordManager orderPaymentTypeRecordManager;/*** 订单支付支付* @param request* @return*/@LoginInterceptor(value=LoginInterceptor.LOGIN, contType=LoginInterceptor.CONT_JOSN_TYPE)@RequestMapping(value="/orderPay")public @ResponseBody Map<String, Object> orderPay(final HttpServletRequest request ,final HttpServletResponse response) {Map<String, Object> resultMap = new HashMap<String, Object> ();Long orderId = StringUtil.nullToLong(request.getParameter("orderId"));String payType = StringUtil.null2Str(request.getParameter("payType"));response.setHeader("Access-Control-Allow-Origin", "*");try{String body = "订单支付";String outTradeNo = "";String clientIp = RequestUtil.getClientIp(request);// 是否微信请求boolean isWebRequest = RequestUtil.isWeixin(request);Integer paymentType = 0;Long weChatConfigId = null;if(StringUtil.compareObject(payType, "weixin")){// 微信支付int orderTotal = 0;String notifyURL = RequestUtil.getRequestURL(request) + "/api/order/wxpayNotify.msp";String openId = StringUtil.null2Str(request.getSession().getAttribute(PortalConstants.SESSION_CURRENT_OPEN_ID));Map<String, String> orderPayInfoMap = new HashMap<String, String>();WeChatAppConfig weChatAppConfig = null;if(isWebRequest){//微页面-微信公众号支付weChatAppConfig = Constants.WECHAT_APP_CONFIG_MAP.get(StringUtil.null2Str("test"));orderPayInfoMap = WeiXinPayUtil.getWeixinH5PayInfo(weChatAppConfig, outTradeNo, openId, notifyURL, orderTotal, body, clientIp);if(orderPayInfoMap != null && !StringUtil.isNull(orderPayInfoMap.get("code_url"))){//跨号支付String codeUrl = StringUtil.null2Str(orderPayInfoMap.get("code_url"));if(StringUtil.isNull(codeUrl)){resultMap.put(PortalConstants.CODE, PortalConstants.CODE_ERROR);resultMap.put(PortalConstants.MSG, "支付失败");resultMap.put(PortalConstants.SYSTEMTIME, DateUtil.getCurrentTime());return resultMap;}//生成二维码图片,并返回地址String filePath = MatrixToImageWriterUtil.saveMatrixImage(codeUrl);if(StringUtil.isNull(filePath)){resultMap.put(PortalConstants.CODE, PortalConstants.CODE_ERROR);resultMap.put(PortalConstants.MSG, "支付失败");resultMap.put(PortalConstants.SYSTEMTIME, DateUtil.getCurrentTime());return resultMap;}String requestURL = RequestUtil.getRequestURL(request);filePath = requestURL + filePath;Map<String, String> paramMap = new HashMap<String, String>();paramMap.put("imagePath", filePath);paramMap.put("isOther", "1"); //是否跨号支付resultMap.put("orderPayInfo", paramMap);resultMap.put(PortalConstants.CODE, PortalConstants.CODE_SUCCESS);resultMap.put(PortalConstants.MSG, "拉取支付信息成功");resultMap.put(PortalConstants.SYSTEMTIME, DateUtil.getCurrentTime());return resultMap;}else{orderPayInfoMap.put("isOther", "0"); //是否跨号支付}}else{weChatAppConfig = Constants.WECHAT_APP_CONFIG_MAP.get(StringUtil.null2Str("app_client"));orderPayInfoMap = WeiXinPayUtil.getWeixinAppPayInfo(weChatAppConfig, outTradeNo, openId, notifyURL, orderTotal, body, clientIp);}resultMap.put("orderPayInfo", orderPayInfoMap);paymentType = PaymentType.PAYMENT_TYPE_WECHAT;weChatConfigId = weChatAppConfig.getConfigId();}else{String orderTotal = "";String notifyURL = RequestUtil.getRequestURL(request) + "/api/order/alipayNotify.msp";String returnURL = RequestUtil.getRequestURL(request) + "/api/order/alipayNotify.msp";if(isWebRequest){//浏览器支付宝支付String orderPayURL = RequestUtil.getRequestURL(request) + "/wap/pay.html?orderNo=" + outTradeNo;resultMap.put("orderPayInfo", orderPayURL);}else{//客户端支付宝支付String orderPayInfo = AliPayUtil.getAliPayAppInfo(AliPayUtil.ALI_PAY_CLT_APP_TYPE, outTradeNo, orderTotal, body, notifyURL, returnURL);resultMap.put("orderPayInfo", orderPayInfo);}paymentType = PaymentType.PAYMENT_TYPE_ALIPAY;}//记录客户端调用支付方式OrderPaymentTypeRecord record = new OrderPaymentTypeRecord();record.setOrderId(orderId);record.setPaymentType(paymentType);record.setWeChatConfigId(weChatConfigId);record.setCreateTime(new Date());record.setUpdateTime(new Date());record.setSyncNumber(0);orderPaymentTypeRecordManager.save(record);resultMap.put(PortalConstants.CODE, PortalConstants.CODE_SUCCESS);resultMap.put(PortalConstants.MSG, "拉取支付信息成功");resultMap.put(PortalConstants.SYSTEMTIME, DateUtil.getCurrentTime());return resultMap;}catch(Exception e){e.printStackTrace();}resultMap.put(PortalConstants.CODE, PortalConstants.CODE_ERROR);resultMap.put(PortalConstants.MSG, this.getText("获取支付失败"));resultMap.put(PortalConstants.SYSTEMTIME, DateUtil.getCurrentTime());return resultMap;}}
@Controller@RequestMapping("/api/order/")public class OrderPayNotifyController extends BaseController{// 定义锁对象private static Lock lock = new ReentrantLock();@Autowiredprivate OrderManager orderManager;@Autowiredprivate OrderByIdCacheManager orderByIdCacheManager;@AutowiredOrderPaymentTypeRecordManager orderPaymentTypeRecordManager;/*** 更新订单支付成功信息* @param orderId* @param outTradeNo* @param transactionId* @param typeCode* @return*/public static void updateOrderPaymentSuccStatus(Long orderId, String outTradeNo, String transactionId, Integer paymentType, Long weChatConfigId){}/*** 微信支付回调通知* @param request* @param response*/@RequestMapping(value="/wxpayNotify")public void wxpayNotify(final HttpServletRequest request, HttpServletResponse response) {try{String resultXML = FileIO.inputStream2String(request.getInputStream());log.info(String.format("weChatPayNotify[XML]==>[%s]", resultXML));Map<String, Object> notifyObjectMap = XmlParseUtil.xmlCont2Map(resultXML);if(notifyObjectMap != null&& notifyObjectMap.size() > 0&& notifyObjectMap.containsKey("return_code")&& StringUtil.compareObject("SUCCESS", StringUtil.null2Str(notifyObjectMap.get("return_code")))){String appid = StringUtil.null2Str(notifyObjectMap.get("appid"));String transactionId = StringUtil.null2Str(notifyObjectMap.get("transaction_id"));String outTradeNo = StringUtil.null2Str(notifyObjectMap.get("out_trade_no"));String sign = StringUtil.null2Str(notifyObjectMap.get("sign"));//标识微信支付 1 或者 微信公众号支付 5WeChatAppConfig weChatAppConfig = Constants.WECHAT_APP_ID_MAP.get(appid);if(weChatAppConfig == null || weChatAppConfig.getAppId() == null){this.writeTextResponse(response, "<xml><return_code><![CDATA[FAIL]]></return_code></xml>");return;}notifyObjectMap.remove("sign");String notifySign = WeiXinPayUtil.getNotifySignString(notifyObjectMap, weChatAppConfig.getSecretKey());// 验证签名是否正确if(StringUtil.compareObject(notifySign, sign)){Order order = this.orderManager.getOrderByOrderNo(outTradeNo);if(order != null && order.getOrderId() != null){//根据支付流水号获取支付信息MsgModel<String> msModel = WeiXinPayUtil.getQueryPayInfo(weChatAppConfig, transactionId, null);int orderTotal = WeiXinPayUtil.orderAmountToBranch(order.getOrderAmount());if(StringUtil.nullToBoolean(msModel.getIsSucc()) && StringUtil.compareObject(msModel.getData(), orderTotal)){OrderPayNotifyController.updateOrderPaymentSuccStatus(order.getOrderId(), outTradeNo, transactionId, PaymentType.PAYMENT_TYPE_WECHAT, weChatAppConfig.getConfigId());//删除订单对应的支付方式记录orderPaymentTypeRecordManager.deleteByOrderId(order.getOrderId());this.writeTextResponse(response, "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>");return;}}}}}catch(Exception e){e.printStackTrace();}this.writeTextResponse(response, "<xml><return_code><![CDATA[FAIL]]></return_code></xml>");}/*** 支付宝支付回调通知* @param request* @param response*/@RequestMapping(value="/alipayNotify")public void alipayNotify(final HttpServletRequest request, HttpServletResponse response) {Map<String, String> params = new HashMap<String, String>(); //将异步通知中收到的待验证所有参数都存放到map中Map<String, String[]> requestParams = request.getParameterMap();StringBuffer paramBuffer = new StringBuffer();if(requestParams != null && requestParams.size() > 0){for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext(); ) {String name = iter.next();String[] values = (String[])requestParams.get(name);String valueStr = "";for (int i = 0; i < values.length; i++) {valueStr = i == values.length - 1 ? new StringBuilder().append(valueStr).append(values[i]).toString() : new StringBuilder().append(valueStr).append(values[i]).append(",").toString();}params.put(name, valueStr);paramBuffer.append(new StringBuilder().append(StringUtil.null2Str(name)).append("=").append(StringUtil.null2Str(valueStr)).append(",").toString());}}String out_trade_no = StringUtil.null2Str(params.get("out_trade_no"));String trade_no = StringUtil.null2Str(params.get("trade_no"));String trade_status = StringUtil.null2Str(params.get("trade_status"));log.info(String.format("quickPayment[record]==>[%s]", paramBuffer.toString()));boolean verify = AliPayUtil.verify(params);if(verify){try{if(trade_status.equals("TRADE_FINISHED") || trade_status.equals("TRADE_SUCCESS")){Order order = this.orderManager.getOrderByOrderNo(out_trade_no);if(order != null && order.getOrderId() != null){//根据支付流水号获取支付信息MsgModel<String> msModel = AliPayUtil.getQueryAliPayInfo(out_trade_no, trade_no);if(StringUtil.nullToBoolean(msModel.getIsSucc()) && StringUtil.compareObject(msModel.getData(), StringUtil.formatDouble2Str(order.getOrderAmount()))){OrderPayNotifyController.updateOrderPaymentSuccStatus(order.getOrderId(), out_trade_no, trade_no, PaymentType.PAYMENT_TYPE_ALIPAY, null);//删除订单对应的支付方式记录orderPaymentTypeRecordManager.deleteByOrderId(order.getOrderId());this.writeTextResponse(response, "success");return;}}}}catch(Exception ex){ex.printStackTrace();}}this.writeTextResponse(response, "fail");}}