PaymentController.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. * Copyright 2017-2020 吴学文 and java110 team.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.java110.front.controller.app;
  17. import com.alibaba.fastjson.JSONObject;
  18. import com.java110.core.base.controller.BaseController;
  19. import com.java110.core.context.IPageData;
  20. import com.java110.core.context.PageData;
  21. import com.java110.front.smo.payment.*;
  22. import com.java110.utils.constant.CommonConstant;
  23. import com.java110.utils.util.StringUtil;
  24. import org.apache.commons.codec.digest.DigestUtils;
  25. import org.apache.commons.lang.StringUtils;
  26. import org.slf4j.Logger;
  27. import org.slf4j.LoggerFactory;
  28. import org.springframework.beans.factory.annotation.Autowired;
  29. import org.springframework.http.ResponseEntity;
  30. import org.springframework.web.bind.annotation.RequestBody;
  31. import org.springframework.web.bind.annotation.RequestMapping;
  32. import org.springframework.web.bind.annotation.RequestMethod;
  33. import org.springframework.web.bind.annotation.RestController;
  34. import javax.servlet.http.HttpServletRequest;
  35. import java.io.UnsupportedEncodingException;
  36. import java.util.ArrayList;
  37. import java.util.Collections;
  38. import java.util.List;
  39. import java.util.Map;
  40. /**
  41. * 支付 处理类
  42. */
  43. @RestController
  44. @RequestMapping(path = "/app/payment")
  45. public class PaymentController extends BaseController {
  46. private final static Logger logger = LoggerFactory.getLogger(PaymentController.class);
  47. @Autowired
  48. private IToPaySMO toPaySMOImpl;
  49. @Autowired
  50. private IToPayOweFeeSMO toPayOweFeeSMOImpl;
  51. @Autowired
  52. private IRentingToPaySMO rentingToPaySMOImpl;
  53. @Autowired
  54. private IToPayTempCarInoutSMO toPayTempCarInoutSMOImpl;
  55. @Autowired
  56. private IToNotifySMO toNotifySMOImpl;
  57. @Autowired
  58. private IRentingToNotifySMO rentingToNotifySMOImpl;
  59. @Autowired
  60. private IOweFeeToNotifySMO oweFeeToNotifySMOImpl;
  61. @Autowired
  62. private IToQrPayOweFeeSMO toQrPayOweFeeSMOImpl;
  63. @Autowired
  64. private IToPayInGoOutSMO toPayInGoOutSMOImpl;
  65. @Autowired
  66. private IToPayBackCitySMO toPayBackCitySMOImpl;
  67. /**
  68. * <p>统一下单入口</p>
  69. *
  70. * @param request
  71. * @throws Exception
  72. */
  73. @RequestMapping(path = "/toPay", method = RequestMethod.POST)
  74. public ResponseEntity<String> toPay(@RequestBody String postInfo, HttpServletRequest request) {
  75. IPageData pd = (IPageData) request.getAttribute(CommonConstant.CONTEXT_PAGE_DATA);
  76. /*IPageData pd = (IPageData) request.getAttribute(CommonConstant.CONTEXT_PAGE_DATA);*/
  77. String appId = request.getHeader("APP_ID");
  78. if (StringUtil.isEmpty(appId)) {
  79. appId = request.getHeader("APP-ID");
  80. }
  81. IPageData newPd = PageData.newInstance().builder(pd.getUserId(), pd.getUserName(), pd.getToken(), postInfo,
  82. "", "", "", pd.getSessionId(), appId, pd.getPayerObjId(), pd.getPayerObjType(), pd.getEndTime());
  83. return toPaySMOImpl.toPay(newPd);
  84. }
  85. /**
  86. * <p>统一下单入口</p>
  87. *
  88. * @param request
  89. * @throws Exception
  90. */
  91. @RequestMapping(path = "/toOweFeePay", method = RequestMethod.POST)
  92. public ResponseEntity<String> toOweFeePay(@RequestBody String postInfo, HttpServletRequest request) {
  93. IPageData pd = (IPageData) request.getAttribute(CommonConstant.CONTEXT_PAGE_DATA);
  94. /*IPageData pd = (IPageData) request.getAttribute(CommonConstant.CONTEXT_PAGE_DATA);*/
  95. String appId = request.getHeader("APP_ID");
  96. if (StringUtil.isEmpty(appId)) {
  97. appId = request.getHeader("APP-ID");
  98. }
  99. IPageData newPd = PageData.newInstance().builder(pd.getUserId(), pd.getUserName(), pd.getToken(), postInfo,
  100. "", "", "", pd.getSessionId(),
  101. appId);
  102. return toPayOweFeeSMOImpl.toPay(newPd);
  103. }
  104. /**
  105. * <p>统一下单入口</p>
  106. *
  107. * @param request
  108. * @throws Exception
  109. */
  110. @RequestMapping(path = "/toQrInGoOutPay", method = RequestMethod.POST)
  111. public ResponseEntity<String> toQrInGoOutPay(@RequestBody String postInfo, HttpServletRequest request) {
  112. IPageData pd = (IPageData) request.getAttribute(CommonConstant.CONTEXT_PAGE_DATA);
  113. /*IPageData pd = (IPageData) request.getAttribute(CommonConstant.CONTEXT_PAGE_DATA);*/
  114. String appId = request.getHeader("APP_ID");
  115. if (StringUtil.isEmpty(appId)) {
  116. appId = request.getHeader("APP-ID");
  117. }
  118. IPageData newPd = PageData.newInstance().builder(pd.getUserId(), pd.getUserName(), pd.getToken(), postInfo,
  119. "", "", "", pd.getSessionId(),
  120. appId);
  121. return toPayInGoOutSMOImpl.toPay(newPd);
  122. }
  123. /**
  124. * <p>统一下单入口</p>
  125. *
  126. * @param request
  127. * @throws Exception
  128. */
  129. @RequestMapping(path = "/toQrBackCityPay", method = RequestMethod.POST)
  130. public ResponseEntity<String> toQrBackCityPay(@RequestBody String postInfo, HttpServletRequest request) {
  131. IPageData pd = (IPageData) request.getAttribute(CommonConstant.CONTEXT_PAGE_DATA);
  132. /*IPageData pd = (IPageData) request.getAttribute(CommonConstant.CONTEXT_PAGE_DATA);*/
  133. String appId = request.getHeader("APP_ID");
  134. if (StringUtil.isEmpty(appId)) {
  135. appId = request.getHeader("APP-ID");
  136. }
  137. IPageData newPd = PageData.newInstance().builder(pd.getUserId(), pd.getUserName(), pd.getToken(), postInfo,
  138. "", "", "", pd.getSessionId(),
  139. appId);
  140. return toPayBackCitySMOImpl.toPay(newPd);
  141. }
  142. /**
  143. * <p>统一下单入口</p>
  144. *
  145. * @param request
  146. * @throws Exception
  147. */
  148. @RequestMapping(path = "/toPayTempCarInout", method = RequestMethod.POST)
  149. public ResponseEntity<String> toPayTempCarInout(@RequestBody String postInfo, HttpServletRequest request) {
  150. IPageData pd = (IPageData) request.getAttribute(CommonConstant.CONTEXT_PAGE_DATA);
  151. /*IPageData pd = (IPageData) request.getAttribute(CommonConstant.CONTEXT_PAGE_DATA);*/
  152. String appId = request.getHeader("APP_ID");
  153. if (StringUtil.isEmpty(appId)) {
  154. appId = request.getHeader("APP-ID");
  155. }
  156. IPageData newPd = PageData.newInstance().builder(pd.getUserId(), pd.getUserName(), pd.getToken(), postInfo,
  157. "", "", "", pd.getSessionId(),
  158. appId);
  159. return toPayTempCarInoutSMOImpl.toPay(newPd);
  160. }
  161. /**
  162. * <p>支付回调Api</p>
  163. *
  164. * @param request
  165. * @throws Exception
  166. */
  167. @RequestMapping(path = "/notify", method = RequestMethod.POST)
  168. public ResponseEntity<String> notify(@RequestBody String postInfo, HttpServletRequest request) {
  169. logger.debug("微信支付回调报文" + postInfo);
  170. return toNotifySMOImpl.toNotify(postInfo, request);
  171. }
  172. /**
  173. * <p>支付回调Api</p>
  174. *
  175. * @param request
  176. * @throws Exception
  177. */
  178. @RequestMapping(path = "/notifyChinaUms", method = RequestMethod.POST)
  179. public ResponseEntity<String> notifyChinaUms(HttpServletRequest request) {
  180. JSONObject paramIn = new JSONObject();
  181. for (String key : request.getParameterMap().keySet()) {
  182. paramIn.put(key, request.getParameter(key));
  183. logger.debug("银联回调报文form" + key + ":: " + request.getParameter(key));
  184. }
  185. logger.debug("微信支付回调报文" + paramIn.toJSONString());
  186. /*接收参数*/
  187. Map<String, String> params = getRequestParams(request);
  188. System.out.println("params:" + params);
  189. String sign = params.get("sign");
  190. System.out.println(sign);
  191. String preStr = buildSignString(params);
  192. paramIn.put("preSign",preStr);
  193. paramIn.put("sign",sign);
  194. //判断签名是否相等
  195. // 收到通知后记得返回SUCCESS
  196. return toNotifySMOImpl.toNotify(paramIn.toJSONString(), request);
  197. }
  198. /**
  199. * <p>出租统一下单入口</p>
  200. *
  201. * @param request
  202. * @throws Exception
  203. */
  204. @RequestMapping(path = "/rentingToPay", method = RequestMethod.POST)
  205. public ResponseEntity<String> rentingToPay(@RequestBody String postInfo, HttpServletRequest request) {
  206. IPageData pd = (IPageData) request.getAttribute(CommonConstant.CONTEXT_PAGE_DATA);
  207. String appId = request.getHeader("APP_ID");
  208. if (StringUtil.isEmpty(appId)) {
  209. appId = request.getHeader("APP-ID");
  210. }
  211. IPageData newPd = PageData.newInstance().builder(pd.getUserId(), pd.getUserName(), pd.getToken(), postInfo,
  212. "", "", "", pd.getSessionId(),
  213. appId);
  214. return rentingToPaySMOImpl.toPay(newPd);
  215. }
  216. /**
  217. * <p>欠费银联回调</p>
  218. *
  219. * @param request
  220. * @throws Exception
  221. */
  222. @RequestMapping(path = "/oweFeeNotifyChinaUms", method = RequestMethod.POST)
  223. public ResponseEntity<String> oweFeeNotifyChinaUms(HttpServletRequest request) {
  224. JSONObject paramIn = new JSONObject();
  225. for (String key : request.getParameterMap().keySet()) {
  226. paramIn.put(key, request.getParameter(key));
  227. }
  228. logger.debug("微信支付回调报文" + paramIn.toJSONString());
  229. /*接收参数*/
  230. Map<String, String> params = getRequestParams(request);
  231. System.out.println("params:" + params);
  232. String sign = params.get("sign");
  233. System.out.println(sign);
  234. String preStr = buildSignString(params);
  235. paramIn.put("preSign",preStr);
  236. paramIn.put("sign",sign);
  237. return oweFeeToNotifySMOImpl.toNotify(paramIn.toJSONString(), request);
  238. }
  239. /**
  240. * <p>出租统一下单入口</p>
  241. *
  242. * @param request
  243. * @throws Exception
  244. */
  245. @RequestMapping(path = "/oweFeeNotify", method = RequestMethod.POST)
  246. public ResponseEntity<String> oweFeeNotify(@RequestBody String postInfo, HttpServletRequest request) {
  247. logger.debug("微信支付回调报文" + postInfo);
  248. return oweFeeToNotifySMOImpl.toNotify(postInfo, request);
  249. }
  250. /**
  251. * <p>支付回调Api</p>
  252. *
  253. * @param request
  254. * @throws Exception
  255. */
  256. @RequestMapping(path = "/rentingNotify", method = RequestMethod.POST)
  257. public ResponseEntity<String> rentingNotify(@RequestBody String postInfo, HttpServletRequest request) {
  258. logger.debug("微信支付回调报文" + postInfo);
  259. return rentingToNotifySMOImpl.toNotify(postInfo, request);
  260. }
  261. /**
  262. * <p>出租统一下单入口</p>
  263. *
  264. * @param request
  265. * @throws Exception
  266. */
  267. @RequestMapping(path = "/toQrOweFeePay", method = RequestMethod.POST)
  268. public ResponseEntity<String> toQrOweFeePay(@RequestBody String postInfo, HttpServletRequest request) {
  269. IPageData pd = (IPageData) request.getAttribute(CommonConstant.CONTEXT_PAGE_DATA);
  270. String appId = request.getHeader("APP_ID");
  271. if (StringUtil.isEmpty(appId)) {
  272. appId = request.getHeader("APP-ID");
  273. }
  274. IPageData newPd = PageData.newInstance().builder(pd.getUserId(), pd.getUserName(), pd.getToken(), postInfo,
  275. "", "", "", pd.getSessionId(),
  276. appId);
  277. return toQrPayOweFeeSMOImpl.toPay(newPd);
  278. }
  279. // 构建签名字符串
  280. public static String buildSignString(Map<String, String> params) {
  281. // params.put("Zm","test_test");
  282. if (params == null || params.size() == 0) {
  283. return "";
  284. }
  285. List<String> keys = new ArrayList<>(params.size());
  286. for (String key : params.keySet()) {
  287. if ("sign".equals(key))
  288. continue;
  289. if ("wId".equals(key))
  290. continue;
  291. if (StringUtils.isEmpty(params.get(key)))
  292. continue;
  293. keys.add(key);
  294. }
  295. //System.out.println(listToString(keys));
  296. Collections.sort(keys);
  297. StringBuilder buf = new StringBuilder();
  298. for (int i = 0; i < keys.size(); i++) {
  299. String key = keys.get(i);
  300. String value = params.get(key);
  301. if (i == keys.size() - 1) {// 拼接时,不包括最后一个&字符
  302. buf.append(key + "=" + value);
  303. } else {
  304. buf.append(key + "=" + value + "&");
  305. }
  306. }
  307. return buf.toString();
  308. }
  309. }