OnePayController.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. package com.yihu.wlyy.web.wx;
  2. import com.alibaba.fastjson.JSON;
  3. import com.yihu.wlyy.entity.charge.WlyyCharge;
  4. import com.yihu.wlyy.entity.patient.Patient;
  5. import com.yihu.wlyy.entity.patient.SignFamily;
  6. import com.yihu.wlyy.repository.charge.ChargeDao;
  7. import com.yihu.wlyy.repository.patient.PatientDao;
  8. import com.yihu.wlyy.repository.patient.SignFamilyDao;
  9. import com.yihu.wlyy.service.weixin.wxpay.model.BindCard;
  10. import com.yihu.wlyy.service.weixin.wxpay.model.Charge;
  11. import com.yihu.wlyy.service.weixin.wxpay.service.OnePayService;
  12. import com.yihu.wlyy.web.WeixinBaseController;
  13. import com.ylzinfo.onepay.sdk.OnepayDefaultClient;
  14. import com.ylzinfo.onepay.sdk.domain.ResponseParams;
  15. import com.ylzinfo.onepay.sdk.exception.PayException;
  16. import com.ylzinfo.onepay.sdk.utils.StringUtil;
  17. import io.swagger.annotations.Api;
  18. import io.swagger.annotations.ApiOperation;
  19. import io.swagger.annotations.ApiParam;
  20. import org.apache.commons.lang3.StringUtils;
  21. import org.json.JSONObject;
  22. import org.slf4j.Logger;
  23. import org.slf4j.LoggerFactory;
  24. import org.springframework.beans.factory.annotation.Autowired;
  25. import org.springframework.http.MediaType;
  26. import org.springframework.stereotype.Controller;
  27. import org.springframework.util.StreamUtils;
  28. import org.springframework.web.bind.annotation.RequestMapping;
  29. import org.springframework.web.bind.annotation.RequestMethod;
  30. import org.springframework.web.bind.annotation.RequestParam;
  31. import org.springframework.web.bind.annotation.ResponseBody;
  32. import javax.servlet.http.HttpServletRequest;
  33. import javax.servlet.http.HttpServletResponse;
  34. import java.io.IOException;
  35. import java.io.InputStream;
  36. import java.net.URLDecoder;
  37. import java.nio.charset.Charset;
  38. import java.util.Date;
  39. import java.util.HashMap;
  40. import java.util.List;
  41. import java.util.Map;
  42. import java.util.regex.Pattern;
  43. /**
  44. * Created by hzp on 2017/05/23.
  45. */
  46. @Controller
  47. @RequestMapping(value = "/onepay", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  48. @Api(description = "统一支付服务")
  49. public class OnePayController extends WeixinBaseController {
  50. private static final Logger LOGGER = LoggerFactory.getLogger(OnePayController.class);
  51. @Autowired
  52. private OnePayService pay;
  53. @Autowired
  54. private PatientDao patientDao;
  55. @Autowired
  56. private SignFamilyDao signFamilyDao;
  57. @Autowired
  58. private ChargeDao chargeDao;
  59. @RequestMapping(value = "expensesStatus", method = RequestMethod.GET)
  60. @ApiOperation("居民缴费状态查询")
  61. @ResponseBody
  62. public String expensesStatus() throws Exception {
  63. try {
  64. // 获取居民个人信息 要根据签约code去查找签约扣费状态
  65. String patient = getUID();
  66. String singCode = "044701ea-5b23-11e6-8344-fa163e8aee56";
  67. SignFamily sign = signFamilyDao.findByCode(singCode);
  68. // String patient = "915cc456-5b1d-11e6-8344-fa163e8aee56";
  69. SignFamily signFamily = signFamilyDao.findPatientExpensesStatus(patient);
  70. String expensesStatus = signFamily.getExpensesStatus();
  71. if ("0".equals(expensesStatus)) {
  72. return write(200, "未扣费!", "expensesStatus", expensesStatus);
  73. } else if ("1".equals(expensesStatus)) {
  74. return write(200, "已扣费!", "expensesStatus", expensesStatus);
  75. } else if ("2".equals(expensesStatus)) {
  76. return write(200, "已退费!", "expensesStatus", expensesStatus);
  77. } else {
  78. return write(200, "扣费状态数据有误!", "expensesStatus", expensesStatus);
  79. }
  80. } catch (Exception e) {
  81. return error(-1, e.getMessage());
  82. }
  83. }
  84. /**
  85. * 补贴类型:无补贴01,全补贴02,补个人承担部分(20)03,补统筹基金(70)04
  86. *
  87. * @param expensesType 补贴类型
  88. * @return
  89. * @throws Exception
  90. */
  91. @RequestMapping(value = "bill", method = RequestMethod.GET)
  92. @ApiOperation("居民缴费账单")
  93. @ResponseBody
  94. public String getBill(@RequestParam String expensesType) throws Exception {
  95. try {
  96. Pattern pattern = Pattern.compile("[0-9]*");
  97. Boolean flag = pattern.matcher(expensesType).matches();
  98. JSONObject json = new JSONObject();
  99. if (flag) {
  100. switch (expensesType) {
  101. case "01":
  102. json.put("selfpayAmount", 20);
  103. json.put("financialAmount", 0);
  104. json.put("insuranceAmount", 70);
  105. json.put("totalAmount", 90);
  106. break;
  107. case "02":
  108. json.put("selfpayAmount", 0);
  109. json.put("financialAmount", 90);
  110. json.put("insuranceAmount", 0);
  111. json.put("totalAmount", 90);
  112. break;
  113. case "03":
  114. json.put("selfpayAmount", 0);
  115. json.put("financialAmount", 20);
  116. json.put("insuranceAmount", 70);
  117. json.put("totalAmount", 90);
  118. break;
  119. case "04":
  120. json.put("selfpayAmount", 20);
  121. json.put("financialAmount", 70);
  122. json.put("insuranceAmount", 0);
  123. json.put("totalAmount", 90);
  124. break;
  125. }
  126. }
  127. return write(200, "查询缴费账单成功!", "data", json);
  128. } catch (Exception e) {
  129. return error(-1, e.getMessage());
  130. }
  131. }
  132. @RequestMapping(value = "createSicard", method = RequestMethod.POST)
  133. @ApiOperation("生成电子社保卡")
  134. @ResponseBody
  135. public String createSicard() throws Exception {
  136. try {
  137. String sicardUrl = pay.createSicard("0cc6e4562de2437ab2dbbf51a9fc3b49", "oULM4xAj9sOsHJ95ttKYd_Fjh6Hc");//getUID(), getOpenid());
  138. return write(200, "生成电子社保卡成功!", "data", sicardUrl);
  139. } catch (Exception e) {
  140. return error(-1, e.getMessage());
  141. }
  142. }
  143. @RequestMapping(value = "bindCard", method = RequestMethod.POST)
  144. @ApiOperation("查询绑卡信息")
  145. @ResponseBody
  146. public String bindCard() throws Exception {
  147. try {
  148. // String patient = getUID();
  149. // String openid = getOpenid();
  150. String patient = "0cc6e4562de2437ab2dbbf51a9fc3b49";
  151. String openid = "oULM4xAj9sOsHJ95ttKYd_Fjh6Hc";
  152. BindCard bindCard = pay.bindCard(patient, openid);//getUID(), getOpenid()); oULM4xAj9sOsHJ95ttKYd_Fjh6Hc
  153. String sicardUrl = pay.createSicard(patient, openid);//getUID(), getOpenid());
  154. if (bindCard != null) {
  155. JSONObject json = new JSONObject(bindCard);
  156. json.put("bindStatus", "000000");
  157. json.put("sicardUrl", sicardUrl);
  158. return write(200, "查询绑卡信息成功!", "data", json);
  159. } else {
  160. // 返回电子社保卡链接
  161. JSONObject json = new JSONObject();
  162. json.put("sicardUrl", sicardUrl);
  163. json.put("bindStatus", "030007");
  164. return write(200, "生成电子社保卡成功!", "data", json);
  165. }
  166. // BindCard bindCard = pay.bindCard("0cc6e4562de2437ab2dbbf51a9fc3b49", "oULM4xAj9sOsHJ95ttKYd_Fjh6Hc");//getUID(), getOpenid()); oULM4xAj9sOsHJ95ttKYd_Fjh6Hc
  167. // return write(200, "查询绑卡信息成功!", "data", bindCard);
  168. } catch (Exception e) {
  169. return error(-1, e.getMessage());
  170. }
  171. }
  172. @RequestMapping(value = "charge", method = RequestMethod.POST)
  173. @ApiOperation("家庭医生签约支付")
  174. @ResponseBody
  175. public String charge(
  176. @ApiParam(name = "orgCode", value = "医疗机构编号", defaultValue = "3502050300")
  177. @RequestParam String orgCode,
  178. @ApiParam(name = "chargeType", value = "支付类型", defaultValue = "1")
  179. @RequestParam String chargeType,
  180. @ApiParam(name = "chargeRelation", value = "支付关联代码", defaultValue = "044701ea5b2311e68344fa163e8aee56")
  181. @RequestParam String chargeRelation,
  182. @ApiParam(name = "totalAmount", value = "交易总金额(分)", defaultValue = "1")
  183. @RequestParam Integer totalAmount,
  184. /*@ApiParam(name = "selfpayAmount", value = "自费金额(分)", defaultValue = "0")
  185. @RequestParam Integer selfpayAmount,
  186. @ApiParam(name = "insuranceAmount", value = "医保支付金额(分)", defaultValue = "1")
  187. @RequestParam Integer insuranceAmount,
  188. @ApiParam(name = "insuranceAmount", value = "区财政补贴金额(分)", defaultValue = "1")
  189. @RequestParam Integer financialAmount,*/
  190. @ApiParam(name = "feeDetail", value = "费用明细", defaultValue = "[{\"itemName\":\"家庭医生签约支付\",\"itemDesc\":\"家庭医生签约支付\",\"itemOrigPrice\":\"1\",\"itemNowPrice\":\"1\",\"itemNum\":\"1\",\"itemTotalAmt\":\"1\"}]")
  191. @RequestParam String feeDetail) throws Exception {
  192. try {
  193. // 获取居民个人信息
  194. String patient = getUID();
  195. String openId = getOpenid();
  196. if (StringUtils.isNotEmpty(openId)) {
  197. String settleNo = pay.charge(orgCode, chargeType, chargeRelation, totalAmount, feeDetail, patient, openId); //ohNH9sh4uwuJCxIwcLJtGTX-BaSk getUID(),getOpenid()
  198. return write(200, "家庭医生签约支付成功!", "data", settleNo);
  199. } else {
  200. return write(-1, "openId为空!");
  201. }
  202. // String settleNo = pay.charge(orgCode, chargeType, chargeRelation, totalAmount, selfpayAmount, insuranceAmount, feeDetail, "0cc6e4562de2437ab2dbbf51a9fc3b49", "oULM4xAj9sOsHJ95ttKYd_Fjh6Hc"); //getUID(),getOpenid()
  203. // return write(200, "家庭医生签约支付成功!", "data", settleNo);
  204. } catch (Exception e) {
  205. return error(-1, e.getMessage());
  206. }
  207. }
  208. @RequestMapping(value = "chargeQuery", method = RequestMethod.POST)
  209. @ApiOperation("家庭医生签约支付查询")
  210. @ResponseBody
  211. public String chargeQuery(@ApiParam(name = "code", value = "支付流水号", defaultValue = "")
  212. @RequestParam String code) throws Exception {
  213. try {
  214. Charge charge = pay.chargeQuery(code);
  215. return write(200, "家庭医生签约支付查询成功!", "data", charge);
  216. } catch (Exception e) {
  217. return error(-1, e.getMessage());
  218. }
  219. }
  220. @RequestMapping(value = "chargeList", method = RequestMethod.POST)
  221. @ApiOperation("获取某次签约的支付记录")
  222. @ResponseBody
  223. public String chargeList(@ApiParam(name = "chargeType", value = "支付类型", defaultValue = "1")
  224. @RequestParam String chargeType,
  225. @ApiParam(name = "chargeRelation", value = "支付关联代码", defaultValue = "044701ea5b2311e68344fa163e8aee56")
  226. @RequestParam String chargeRelation) throws Exception {
  227. try {
  228. List<WlyyCharge> charge = pay.chargeList(chargeType, chargeRelation);
  229. return write(200, "获取某次签约的支付记录成功!", "data", charge);
  230. } catch (Exception e) {
  231. return error(-1, e.getMessage());
  232. }
  233. }
  234. @RequestMapping(value = "chargeListByPatient", method = RequestMethod.POST)
  235. @ApiOperation("获取患者的支付记录")
  236. @ResponseBody
  237. public String chargeListByPatient(@ApiParam(name = "patient", value = "患者代码", defaultValue = "")
  238. @RequestParam String patient,
  239. @ApiParam(name = "page", value = "第几页,从1开始", defaultValue = "1")
  240. @RequestParam Integer page,
  241. @ApiParam(name = "size", value = "每页几行", defaultValue = "10")
  242. @RequestParam Integer size) throws Exception {
  243. try {
  244. List<WlyyCharge> charge = pay.chargeListByPatient(patient, page, size);
  245. return write(200, "获取患者的支付记录成功!", "data", charge);
  246. } catch (Exception e) {
  247. return error(-1, e.getMessage());
  248. }
  249. }
  250. /**
  251. * 解析URL参数串
  252. *
  253. * @param formContext
  254. * @param wordFirstsplitRegex
  255. * @param wordSecondsplitRegex
  256. * @return
  257. */
  258. private static Map<String, String> resolveFormContext(String formContext, String wordFirstsplitRegex, String wordSecondsplitRegex) {
  259. if (StringUtil.isEmpty(formContext)) {
  260. return null;
  261. }
  262. Map<String, String> targetMap = new HashMap<String, String>();
  263. String[] wordSeconds = formContext.split(wordSecondsplitRegex);
  264. for (String wordSecond : wordSeconds) {
  265. if (StringUtil.isEmpty(wordSecond)) {
  266. continue;
  267. }
  268. int idx = wordSecond.indexOf(wordFirstsplitRegex);
  269. targetMap.put(wordSecond.substring(0, idx), wordSecond.substring(idx + 1, wordSecond.length()));
  270. }
  271. return targetMap;
  272. }
  273. /**
  274. * 转换URL参数中为Map
  275. *
  276. * @param reqQueryString
  277. * @return
  278. * @throws PayException
  279. */
  280. private Map<String, String> getRequestMap(String reqQueryString) throws PayException {
  281. if (StringUtil.isEmpty(reqQueryString)) {
  282. throw new PayException("跳转参数为空");
  283. }
  284. Map<String, String> targetMap = null;
  285. try {
  286. reqQueryString = URLDecoder.decode(reqQueryString, "utf-8");
  287. targetMap = resolveFormContext(reqQueryString, "=", "&");
  288. } catch (Exception e) {
  289. throw new PayException("跳转参数处理异常," + reqQueryString);
  290. }
  291. return targetMap;
  292. }
  293. /**
  294. * @param request
  295. * @param response
  296. * @throws IOException
  297. * @throws PayException
  298. */
  299. @RequestMapping(value = "/returnUrl", method = RequestMethod.GET)
  300. @ApiOperation("商户页面跳转(模拟测试)")
  301. public void testReturnUrl(HttpServletRequest request, HttpServletResponse response) throws IOException, PayException {
  302. response.setContentType("text/html;charset=utf-8");
  303. response.getWriter().write("返回结果:\n" + request.getQueryString() + "\n");
  304. String onepayUrl = request.getParameter("onepayUrl");
  305. String appId = "1BHEOI11C00J7B2CA8C0000071FA53E1";
  306. String appSecret = "1BHEOH8HB0006E0A0A0A00002DB28BC5";
  307. String signType = "MD5";
  308. String encryptType = "DES";
  309. try {
  310. OnepayDefaultClient client = new OnepayDefaultClient(onepayUrl, appId, appSecret, signType, encryptType);
  311. Map<String, String> returnParams = getRequestMap(request.getQueryString());
  312. /* boolean isVerify = client.verifyReturnSign(returnParams, returnParams.get("sign"));
  313. if (isVerify) {
  314. response.getWriter().write("签名结果:\nSIGN SUCCESS.\n");
  315. // TODO 继续处理业务
  316. // 更改数据 wlyy_sign_family wlyy_charge
  317. } else {
  318. response.getWriter().write("签名结果:\nSIGN ERROR !!!\n");
  319. }*/
  320. String patient = request.getParameter("userId");
  321. String chargNo = request.getParameter("chargeNo");
  322. pay.updateData(patient,chargNo);
  323. } catch (Exception e) {
  324. response.getWriter().write(e.getMessage());
  325. }
  326. }
  327. /**
  328. * 支付真正完成,异步回调返回支付参数 后台做数据操作
  329. *
  330. * @param request
  331. * @param response
  332. * @throws IOException
  333. * @throws PayException
  334. */
  335. @RequestMapping(value = "/receiveNotify", method = RequestMethod.POST)
  336. @ApiOperation("异步回调数据更改")
  337. public void receiveNotify(HttpServletRequest request, HttpServletResponse response) throws IOException, PayException {
  338. response.setContentType("text/html;charset=utf-8");
  339. String appId = "1BHEOI11C00J7B2CA8C0000071FA53E1";
  340. String appSecret = "1BHEOH8HB0006E0A0A0A00002DB28BC5";
  341. InputStream inputStream = request.getInputStream();
  342. String params = StreamUtils.copyToString(inputStream, Charset.forName("UTF-8"));
  343. LOGGER.info("回调参数:{}", params);
  344. if (StringUtil.isEmpty(params)) {
  345. response.getWriter().write("empty");
  346. return;
  347. }
  348. // 参数转换
  349. ResponseParams<?> encryptRes = JSON.parseObject(params, ResponseParams.class);
  350. OnepayDefaultClient client = new OnepayDefaultClient("", appId, appSecret, encryptRes.getSignType(), encryptRes.getEncryptType());
  351. try {
  352. LOGGER.info("解密前:{}", JSON.toJSONString(encryptRes));
  353. /* ResponseParams<?> decryptRes = client.decryptNotifyResponse(encryptRes);
  354. LOGGER.info("解密后:{}", JSON.toJSONString(decryptRes));
  355. boolean isDecrypt = ("-1".equals(decryptRes.getRespCode()) ? false : true); // 是否解密失败
  356. if (!isDecrypt) {
  357. response.getWriter().write("DECRYPT FAILURE");
  358. } else {
  359. boolean isVerify = client.verifyResponseSign(decryptRes);
  360. LOGGER.info("验签结果:{}", isVerify);
  361. if (!isVerify) {
  362. response.getWriter().write("FAILURE");
  363. } else {
  364. // 更改数据 wlyy_sign_family wlyy_charge
  365. response.getWriter().write("SUCCESS");
  366. }
  367. }*/
  368. String patient = request.getParameter("userId");
  369. String chargNo = request.getParameter("chargeNo");
  370. pay.updateData(patient,chargNo);
  371. } catch (Exception e) {
  372. response.getWriter().write(e.getMessage());
  373. }
  374. }
  375. }