UnionPayController.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package com.itstyle.modules.unionpay.controller;
  2. import java.util.Enumeration;
  3. import java.util.HashMap;
  4. import java.util.Iterator;
  5. import java.util.Map;
  6. import java.util.Map.Entry;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import org.slf4j.Logger;
  10. import org.slf4j.LoggerFactory;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.stereotype.Controller;
  13. import org.springframework.ui.ModelMap;
  14. import org.springframework.web.bind.annotation.RequestMapping;
  15. import com.itstyle.common.constants.PayWay;
  16. import com.itstyle.common.model.Product;
  17. import com.itstyle.modules.alipay.controller.AliPayController;
  18. import com.itstyle.modules.unionpay.service.IUnionPayService;
  19. import com.itstyle.modules.unionpay.util.AcpService;
  20. import com.itstyle.modules.unionpay.util.SDKConstants;
  21. /**
  22. * 银联支付
  23. * 创建者 科帮网
  24. * 创建时间 2017年8月2日
  25. *
  26. */
  27. @Controller
  28. @RequestMapping(value = "unionpay")
  29. public class UnionPayController {
  30. private static final Logger logger = LoggerFactory.getLogger(AliPayController.class);
  31. @Autowired
  32. private IUnionPayService unionPayService;
  33. @RequestMapping("/index")
  34. public String index() {
  35. return "unionpay/index";
  36. }
  37. @RequestMapping("/pcPay")
  38. public String pcPay(Product product,ModelMap map) {
  39. logger.info("电脑支付");
  40. product.setPayWay(PayWay.PC.getCode());
  41. String form = unionPayService.unionPay(product);
  42. map.addAttribute("form", form);
  43. return "unionpay/pay";
  44. }
  45. @RequestMapping("/mobilePay")
  46. public String mobilePay(Product product,ModelMap map) {
  47. logger.info("手机H5支付");
  48. product.setPayWay(PayWay.MOBILE.getCode());
  49. String form = unionPayService.unionPay(product);
  50. map.addAttribute("form", form);
  51. return "unionpay/pay";
  52. }
  53. /**
  54. * 其实我小时候的梦想并不是要当什么程序员,
  55. * 我只是幻想自己是地主家的少爷,家有良田万顷,
  56. * 终日不学无术,没事领着一群狗奴才上街去调戏一下良家少女。
  57. * 然后这个方法的基本作用就是 银联支付回调 通知我们支付是否成功。
  58. * @Author 科帮网
  59. * @param request
  60. * @param response
  61. * @throws Exception void
  62. * @Date 2017年8月2日
  63. * 更新日志
  64. * 2017年8月2日 科帮网 首次创建
  65. *
  66. */
  67. @RequestMapping(value = "pay")
  68. public void union_notify(HttpServletRequest request, HttpServletResponse response) throws Exception {
  69. logger.info("银联接收后台通知开始");
  70. String encoding = request.getParameter(SDKConstants.param_encoding);
  71. // 获取银联通知服务器发送的后台通知参数
  72. Map<String, String> reqParam = getAllRequestParam(request);
  73. //打印参数
  74. logger.info(reqParam.toString());
  75. Map<String, String> valideData = null;
  76. if (null != reqParam && !reqParam.isEmpty()) {
  77. Iterator<Entry<String, String>> it = reqParam.entrySet().iterator();
  78. valideData = new HashMap<String, String>(reqParam.size());
  79. while (it.hasNext()) {
  80. Entry<String, String> e = it.next();
  81. String key = (String) e.getKey();
  82. String value = (String) e.getValue();
  83. value = new String(value.getBytes(encoding), encoding);
  84. valideData.put(key, value);
  85. }
  86. }
  87. //重要!验证签名前不要修改reqParam中的键值对的内容,否则会验签不过
  88. if (!AcpService.validate(valideData, encoding)) {
  89. logger.info("银联验证签名结果[失败].");
  90. } else {
  91. logger.info("银联验证签名结果[成功].");
  92. String outtradeno =valideData.get("orderId");//订单号
  93. String reqReserved = valideData.get("reqReserved");//辅助信息(字段穿透)
  94. logger.info("处理相关业务逻辑{},{}",outtradeno,reqReserved);
  95. response.getWriter().print("ok");//返回给银联服务器http 200 状态码
  96. }
  97. }
  98. /**
  99. * 获取请求参数中所有的信息
  100. * @Author 科帮网
  101. * @param request
  102. * @return Map<String,String>
  103. * @Date 2017年8月2日
  104. * 更新日志
  105. * 2017年8月2日 科帮网 首次创建
  106. *
  107. */
  108. public static Map<String, String> getAllRequestParam(final HttpServletRequest request) {
  109. Map<String, String> res = new HashMap<String, String>();
  110. Enumeration<?> temp = request.getParameterNames();
  111. if (null != temp) {
  112. while (temp.hasMoreElements()) {
  113. String en = (String) temp.nextElement();
  114. String value = request.getParameter(en);
  115. res.put(en, value);
  116. //在报文上送时,如果字段的值为空,则不上送<下面的处理为在获取所有参数数据时,判断若值为空,则删除这个字段>
  117. //System.out.println("ServletUtil类247行 temp数据的键=="+en+" 值==="+value);
  118. if (null == res.get(en) || "".equals(res.get(en))) {
  119. res.remove(en);
  120. }
  121. }
  122. }
  123. return res;
  124. }
  125. }