package com.llisoft.pay.controller; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; 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.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.llisoft.pay.service.AliPayService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; @Api(tags="支付宝回调") @Controller @RequestMapping("/alipay") public class AliPayController { private Logger logger = LoggerFactory.getLogger(AliPayController.class); @Autowired private AliPayService aliPayService; @ApiOperation(value="同步回调") @GetMapping("/return") public String returns(HttpServletRequest request) throws Exception { Map resultMap = this.getParamMap(request); logger.debug("收到支付宝同步回调: {}", resultMap); return "redirect:" + aliPayService.doReturn(resultMap); } @ApiOperation(value="异步通知") @PostMapping(value="/notify") public @ResponseBody String notify(HttpServletRequest request) throws Exception { Map resultMap = this.getParamMap(request); logger.debug("收到支付宝异步通知: {}", resultMap); try { if (aliPayService.doNotify(resultMap)) { logger.debug("支付宝异步通知处理成功!"); return "success"; } } catch (Exception e) { e.printStackTrace(); } logger.error("支付宝异步通知处理失败!"); return null; } /** * 获取参数Map * @param request * @return */ private Map getParamMap(HttpServletRequest request){ Map returnMap = new HashMap(); Map paramMap = request.getParameterMap(); for (String key: paramMap.keySet()) { String[] values = paramMap.get(key); if (values!=null && values.length>0) { returnMap.put(key, values[0]); } } return returnMap; } }