OnePayController.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. package com.yihu.wlyy.web.wx;
  2. import com.yihu.wlyy.entity.charge.WlyyCharge;
  3. import com.yihu.wlyy.entity.patient.Patient;
  4. import com.yihu.wlyy.entity.patient.SignFamily;
  5. import com.yihu.wlyy.repository.patient.PatientDao;
  6. import com.yihu.wlyy.repository.patient.SignFamilyDao;
  7. import com.yihu.wlyy.service.weixin.wxpay.model.BindCard;
  8. import com.yihu.wlyy.service.weixin.wxpay.model.Charge;
  9. import com.yihu.wlyy.service.weixin.wxpay.service.OnePayService;
  10. import com.yihu.wlyy.web.WeixinBaseController;
  11. import com.ylzinfo.onepay.sdk.OnepayDefaultClient;
  12. import com.ylzinfo.onepay.sdk.exception.PayException;
  13. import com.ylzinfo.onepay.sdk.utils.StringUtil;
  14. import io.swagger.annotations.Api;
  15. import io.swagger.annotations.ApiOperation;
  16. import io.swagger.annotations.ApiParam;
  17. import org.apache.commons.lang3.StringUtils;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.http.MediaType;
  20. import org.springframework.stereotype.Controller;
  21. import org.springframework.web.bind.annotation.RequestMapping;
  22. import org.springframework.web.bind.annotation.RequestMethod;
  23. import org.springframework.web.bind.annotation.RequestParam;
  24. import org.springframework.web.bind.annotation.ResponseBody;
  25. import javax.servlet.http.HttpServletRequest;
  26. import javax.servlet.http.HttpServletResponse;
  27. import java.io.IOException;
  28. import java.net.URLDecoder;
  29. import java.util.Date;
  30. import java.util.HashMap;
  31. import java.util.List;
  32. import java.util.Map;
  33. /**
  34. * Created by hzp on 2017/05/23.
  35. */
  36. @Controller
  37. @RequestMapping(value = "/onepay", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  38. @Api(description = "统一支付服务")
  39. public class OnePayController extends WeixinBaseController {
  40. @Autowired
  41. private OnePayService pay;
  42. @Autowired
  43. private PatientDao patientDao;
  44. @Autowired
  45. private SignFamilyDao signFamilyDao;
  46. @RequestMapping(value = "expensesStatus", method = RequestMethod.GET)
  47. @ApiOperation("居民缴费状态查询")
  48. @ResponseBody
  49. public String expensesStatus() throws Exception {
  50. try {
  51. // 获取居民个人信息 要根据签约code去查找签约扣费状态
  52. String userCode = getUID();
  53. // String userCode = "915cc456-5b1d-11e6-8344-fa163e8aee56";
  54. SignFamily signFamily = signFamilyDao.findPatientExpensesStatus(userCode);
  55. String expensesStatus = signFamily.getExpensesStatus();
  56. if ("0".equals(expensesStatus)){
  57. return write(200, "未扣费!","expensesStatus",expensesStatus);
  58. }else if ("1".equals(expensesStatus)){
  59. return write(200, "已扣费!","expensesStatus",expensesStatus);
  60. }else if ("2".equals(expensesStatus)){
  61. return write(200, "已退费!","expensesStatus",expensesStatus);
  62. }else {
  63. return write(200, "扣费状态数据有误!","expensesStatus",expensesStatus);
  64. }
  65. } catch (Exception e) {
  66. return error(-1, e.getMessage());
  67. }
  68. }
  69. @RequestMapping(value = "bindCard", method = RequestMethod.POST)
  70. @ApiOperation("查询绑卡信息")
  71. @ResponseBody
  72. public String bindCard() throws Exception {
  73. try {
  74. // 获取居民个人信息
  75. String userCode = getUID();
  76. String openId = getOpenid();
  77. if (StringUtils.isNotEmpty(openId)) {
  78. BindCard bindCard = pay.bindCard(userCode, openId);
  79. return write(200, "查询绑卡信息成功!", "data", bindCard);
  80. } else {
  81. return write(-1, "openId为空!");
  82. }
  83. // BindCard bindCard = pay.bindCard("915cc456-5b1d-11e6-8344-fa163e8aee56","o4Ma2t5665rS7uNfA9EF-VnOJx30"); //ohNH9sh4uwuJCxIwcLJtGTX-BaSk //o4Ma2t5665rS7uNfA9EF-VnOJx30 getUID()getOpenid()
  84. // BindCard bindCard = pay.bindCard("915cc456-5b1d-11e6-8344-fa163e8aee56","ohNH9sh4uwuJCxIwcLJtGTX-BaSk"); //ohNH9sh4uwuJCxIwcLJtGTX-BaSk //o4Ma2t5665rS7uNfA9EF-VnOJx30 getUID()getOpenid()
  85. // return write(200, "查询绑卡信息成功!","data",bindCard);
  86. } catch (Exception e) {
  87. return error(-1, e.getMessage());
  88. }
  89. }
  90. @RequestMapping(value = "charge", method = RequestMethod.POST)
  91. @ApiOperation("家庭医生签约支付")
  92. @ResponseBody
  93. public String charge(
  94. @ApiParam(name = "orgCode", value = "医疗机构编号", defaultValue = "3502050300")
  95. @RequestParam String orgCode,
  96. @ApiParam(name = "chargeType", value = "支付类型", defaultValue = "1")
  97. @RequestParam String chargeType,
  98. @ApiParam(name = "chargeRelation", value = "支付关联代码", defaultValue = "044701ea5b2311e68344fa163e8aee56")
  99. @RequestParam String chargeRelation,
  100. @ApiParam(name = "totalAmount", value = "交易总金额(分)", defaultValue = "1")
  101. @RequestParam Integer totalAmount,
  102. @ApiParam(name = "selfpayAmount", value = "自费金额(分)", defaultValue = "0")
  103. @RequestParam Integer selfpayAmount,
  104. @ApiParam(name = "insuranceAmount", value = "医保支付金额(分)", defaultValue = "1")
  105. @RequestParam Integer insuranceAmount,
  106. @ApiParam(name = "feeDetail", value = "费用明细", defaultValue = "[{\"itemName\":\"家庭医生签约支付\",\"itemDesc\":\"家庭医生签约支付\",\"itemOrigPrice\":\"1\",\"itemNowPrice\":\"1\",\"itemNum\":\"1\",\"itemTotalAmt\":\"1\"}]")
  107. @RequestParam String feeDetail) throws Exception {
  108. try {
  109. // 获取居民个人信息
  110. String userCode = getUID();
  111. String openId = getOpenid();
  112. if (StringUtils.isNotEmpty(openId)) {
  113. String settleNo = pay.charge(orgCode, chargeType, chargeRelation, totalAmount, selfpayAmount, insuranceAmount, feeDetail, userCode, openId); //ohNH9sh4uwuJCxIwcLJtGTX-BaSk getUID(),getOpenid()
  114. // 更新签约表的缴费状态(begin字段),表示签约正式开始
  115. signFamilyDao.updatePatientBegin(userCode, new Date());
  116. return write(200, "家庭医生签约支付成功!", "data", settleNo);
  117. } else {
  118. return write(-1, "openId为空!");
  119. }
  120. // String settleNo = pay.charge(orgCode, chargeType, chargeRelation, totalAmount, selfpayAmount, insuranceAmount, feeDetail, "915cc456-5b1d-11e6-8344-fa163e8aee56", "o4Ma2t5665rS7uNfA9EF-VnOJx30"); //ohNH9sh4uwuJCxIwcLJtGTX-BaSk getUID(),getOpenid()
  121. // String settleNo = pay.charge(orgCode, chargeType, chargeRelation, totalAmount, selfpayAmount, insuranceAmount, feeDetail, "915cc456-5b1d-11e6-8344-fa163e8aee56", "ohNH9sh4uwuJCxIwcLJtGTX-BaSk"); //ohNH9sh4uwuJCxIwcLJtGTX-BaSk getUID(),getOpenid()
  122. // return write(200, "家庭医生签约支付成功!", "data", settleNo);
  123. } catch (Exception e) {
  124. return error(-1, e.getMessage());
  125. }
  126. }
  127. @RequestMapping(value = "chargeQuery", method = RequestMethod.POST)
  128. @ApiOperation("家庭医生签约支付查询")
  129. @ResponseBody
  130. public String chargeQuery(@ApiParam(name = "code", value = "支付流水号", defaultValue = "")
  131. @RequestParam String code) throws Exception {
  132. try {
  133. Charge charge = pay.chargeQuery(code);
  134. return write(200, "家庭医生签约支付查询成功!", "data", charge);
  135. } catch (Exception e) {
  136. return error(-1, e.getMessage());
  137. }
  138. }
  139. @RequestMapping(value = "createSicard", method = RequestMethod.POST)
  140. @ApiOperation("生成电子社保卡")
  141. @ResponseBody
  142. public String createSicard() throws Exception {
  143. try {
  144. // 获取居民个人信息
  145. String userCode = getUID();
  146. String openId = getOpenid();
  147. if (StringUtils.isNotEmpty(openId)) {
  148. String sicardUrl = pay.createSicard(userCode, openId); //ohNH9sh4uwuJCxIwcLJtGTX-BaSk //o4Ma2t5665rS7uNfA9EF-VnOJx30 getUID(),getOpenid()
  149. return write(200, "生成电子社保卡成功!", "data", sicardUrl);
  150. }else {
  151. return write(-1, "openId为空!");
  152. }
  153. // String sicardUrl = pay.createSicard("915cc456-5b1d-11e6-8344-fa163e8aee56", "o4Ma2t5665rS7uNfA9EF-VnOJx30"); //ohNH9sh4uwuJCxIwcLJtGTX-BaSk //o4Ma2t5665rS7uNfA9EF-VnOJx30 getUID(),getOpenid()
  154. // return write(200, "生成电子社保卡成功!", "data", sicardUrl);
  155. } catch (Exception e) {
  156. return error(-1, e.getMessage());
  157. }
  158. }
  159. @RequestMapping(value = "chargeList", method = RequestMethod.POST)
  160. @ApiOperation("获取某次签约的支付记录")
  161. @ResponseBody
  162. public String chargeList(@ApiParam(name = "chargeType", value = "支付类型", defaultValue = "1")
  163. @RequestParam String chargeType,
  164. @ApiParam(name = "chargeRelation", value = "支付关联代码", defaultValue = "044701ea5b2311e68344fa163e8aee56")
  165. @RequestParam String chargeRelation) throws Exception {
  166. try {
  167. List<WlyyCharge> charge = pay.chargeList(chargeType, chargeRelation);
  168. return write(200, "获取某次签约的支付记录成功!", "data", charge);
  169. } catch (Exception e) {
  170. return error(-1, e.getMessage());
  171. }
  172. }
  173. @RequestMapping(value = "chargeListByPatient", method = RequestMethod.POST)
  174. @ApiOperation("获取患者的支付记录")
  175. @ResponseBody
  176. public String chargeListByPatient(@ApiParam(name = "patient", value = "患者代码", defaultValue = "")
  177. @RequestParam String patient,
  178. @ApiParam(name = "page", value = "第几页,从1开始", defaultValue = "1")
  179. @RequestParam Integer page,
  180. @ApiParam(name = "size", value = "每页几行", defaultValue = "10")
  181. @RequestParam Integer size) throws Exception {
  182. try {
  183. List<WlyyCharge> charge = pay.chargeListByPatient(patient, page, size);
  184. return write(200, "获取患者的支付记录成功!", "data", charge);
  185. } catch (Exception e) {
  186. return error(-1, e.getMessage());
  187. }
  188. }
  189. /**
  190. * 解析URL参数串
  191. *
  192. * @param formContext
  193. * @param wordFirstsplitRegex
  194. * @param wordSecondsplitRegex
  195. * @return
  196. */
  197. private static Map<String, String> resolveFormContext(String formContext, String wordFirstsplitRegex, String wordSecondsplitRegex) {
  198. if (StringUtil.isEmpty(formContext)) {
  199. return null;
  200. }
  201. Map<String, String> targetMap = new HashMap<String, String>();
  202. String[] wordSeconds = formContext.split(wordSecondsplitRegex);
  203. for (String wordSecond : wordSeconds) {
  204. if (StringUtil.isEmpty(wordSecond)) {
  205. continue;
  206. }
  207. int idx = wordSecond.indexOf(wordFirstsplitRegex);
  208. targetMap.put(wordSecond.substring(0, idx), wordSecond.substring(idx + 1, wordSecond.length()));
  209. }
  210. return targetMap;
  211. }
  212. /**
  213. * 转换URL参数中为Map
  214. *
  215. * @param reqQueryString
  216. * @return
  217. * @throws PayException
  218. */
  219. private Map<String, String> getRequestMap(String reqQueryString) throws PayException {
  220. if (StringUtil.isEmpty(reqQueryString)) {
  221. throw new PayException("跳转参数为空");
  222. }
  223. Map<String, String> targetMap = null;
  224. try {
  225. reqQueryString = URLDecoder.decode(reqQueryString, "utf-8");
  226. targetMap = resolveFormContext(reqQueryString, "=", "&");
  227. } catch (Exception e) {
  228. throw new PayException("跳转参数处理异常," + reqQueryString);
  229. }
  230. return targetMap;
  231. }
  232. @RequestMapping(value = "/returnUrl", method = RequestMethod.GET)
  233. @ApiOperation("商户页面跳转(模拟测试)")
  234. public void testReturnUrl(HttpServletRequest request, HttpServletResponse response) throws IOException, PayException {
  235. response.setContentType("text/html;charset=utf-8");
  236. response.getWriter().write("返回结果:\n" + request.getQueryString() + "\n");
  237. String onepayUrl = request.getParameter("onepayUrl");
  238. String appId = "1BHEOI11C00J7B2CA8C0000071FA53E1";
  239. String appSecret = "1BHEOH8HB0006E0A0A0A00002DB28BC5";
  240. String signType = "MD5";
  241. String encryptType = "DES";
  242. try {
  243. OnepayDefaultClient client = new OnepayDefaultClient(onepayUrl, appId, appSecret, signType, encryptType);
  244. Map<String, String> returnParams = getRequestMap(request.getQueryString());
  245. /*boolean isVerify = client.verifyReturnSign(returnParams, returnParams.get("sign"));
  246. if (isVerify) {
  247. response.getWriter().write("签名结果:\nSIGN SUCCESS.\n");
  248. // TODO 继续处理业务
  249. // 如:查询显示订单信息
  250. } else {
  251. response.getWriter().write("签名结果:\nSIGN ERROR !!!\n");
  252. }*/
  253. } catch (Exception e) {
  254. response.getWriter().write(e.getMessage());
  255. }
  256. }
  257. }