OnePayController.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 = "createSicard", method = RequestMethod.POST)
  70. @ApiOperation("生成电子社保卡")
  71. @ResponseBody
  72. public String createSicard() throws Exception {
  73. try {
  74. String sicardUrl = pay.createSicard("0cc6e4562de2437ab2dbbf51a9fc3b49","oULM4xAj9sOsHJ95ttKYd_Fjh6Hc");//getUID(), getOpenid());
  75. return write(200, "生成电子社保卡成功!", "data", sicardUrl);
  76. } catch (Exception e) {
  77. return error(-1, e.getMessage());
  78. }
  79. }
  80. @RequestMapping(value = "bindCard", method = RequestMethod.POST)
  81. @ApiOperation("查询绑卡信息")
  82. @ResponseBody
  83. public String bindCard() throws Exception {
  84. try {
  85. BindCard bindCard = pay.bindCard("0cc6e4562de2437ab2dbbf51a9fc3b49","oULM4xAj9sOsHJ95ttKYd_Fjh6Hc");//getUID(), getOpenid()); oULM4xAj9sOsHJ95ttKYd_Fjh6Hc
  86. return write(200, "查询绑卡信息成功!","data",bindCard);
  87. } catch (Exception e) {
  88. return error(-1, e.getMessage());
  89. }
  90. }
  91. @RequestMapping(value = "charge", method = RequestMethod.POST)
  92. @ApiOperation("家庭医生签约支付")
  93. @ResponseBody
  94. public String charge(
  95. @ApiParam(name = "orgCode", value = "医疗机构编号", defaultValue = "3502050300")
  96. @RequestParam String orgCode,
  97. @ApiParam(name = "chargeType", value = "支付类型", defaultValue = "1")
  98. @RequestParam String chargeType,
  99. @ApiParam(name = "chargeRelation", value = "支付关联代码", defaultValue = "044701ea5b2311e68344fa163e8aee56")
  100. @RequestParam String chargeRelation,
  101. @ApiParam(name = "totalAmount", value = "交易总金额(分)", defaultValue = "1")
  102. @RequestParam Integer totalAmount,
  103. @ApiParam(name = "selfpayAmount", value = "自费金额(分)", defaultValue = "0")
  104. @RequestParam Integer selfpayAmount,
  105. @ApiParam(name = "insuranceAmount", value = "医保支付金额(分)", defaultValue = "1")
  106. @RequestParam Integer insuranceAmount,
  107. @ApiParam(name = "feeDetail", value = "费用明细", defaultValue = "[{\"itemName\":\"家庭医生签约支付\",\"itemDesc\":\"家庭医生签约支付\",\"itemOrigPrice\":\"1\",\"itemNowPrice\":\"1\",\"itemNum\":\"1\",\"itemTotalAmt\":\"1\"}]")
  108. @RequestParam String feeDetail) throws Exception {
  109. try {
  110. // 获取居民个人信息
  111. /*String userCode = getUID();
  112. Patient patient = patientDao.findByCode(userCode);
  113. String openId = patient.getOpenid();
  114. if (StringUtils.isNotEmpty(openId)) {
  115. String settleNo = pay.charge(orgCode, chargeType, chargeRelation, totalAmount, selfpayAmount, insuranceAmount, feeDetail, userCode, openId); //ohNH9sh4uwuJCxIwcLJtGTX-BaSk getUID(),getOpenid()
  116. // 更新签约表的缴费状态(begin字段),表示签约正式开始
  117. signFamilyDao.updatePatientBegin(userCode, new Date());
  118. return write(200, "家庭医生签约支付成功!", "data", settleNo);
  119. } else {
  120. return write(-1, "openId为空!");
  121. }*/
  122. String settleNo = pay.charge(orgCode, chargeType, chargeRelation, totalAmount, selfpayAmount, insuranceAmount, feeDetail,"0cc6e4562de2437ab2dbbf51a9fc3b49", "oULM4xAj9sOsHJ95ttKYd_Fjh6Hc"); //getUID(),getOpenid()
  123. return write(200, "家庭医生签约支付成功!", "data", settleNo);
  124. } catch (Exception e) {
  125. return error(-1, e.getMessage());
  126. }
  127. }
  128. @RequestMapping(value = "chargeQuery", method = RequestMethod.POST)
  129. @ApiOperation("家庭医生签约支付查询")
  130. @ResponseBody
  131. public String chargeQuery(@ApiParam(name = "code", value = "支付流水号", defaultValue = "")
  132. @RequestParam String code) throws Exception {
  133. try {
  134. Charge charge = pay.chargeQuery(code);
  135. return write(200, "家庭医生签约支付查询成功!", "data", charge);
  136. } catch (Exception e) {
  137. return error(-1, e.getMessage());
  138. }
  139. }
  140. @RequestMapping(value = "chargeList", method = RequestMethod.POST)
  141. @ApiOperation("获取某次签约的支付记录")
  142. @ResponseBody
  143. public String chargeList(@ApiParam(name = "chargeType", value = "支付类型", defaultValue = "1")
  144. @RequestParam String chargeType,
  145. @ApiParam(name = "chargeRelation", value = "支付关联代码", defaultValue = "044701ea5b2311e68344fa163e8aee56")
  146. @RequestParam String chargeRelation) throws Exception {
  147. try {
  148. List<WlyyCharge> charge = pay.chargeList(chargeType, chargeRelation);
  149. return write(200, "获取某次签约的支付记录成功!", "data", charge);
  150. } catch (Exception e) {
  151. return error(-1, e.getMessage());
  152. }
  153. }
  154. @RequestMapping(value = "chargeListByPatient", method = RequestMethod.POST)
  155. @ApiOperation("获取患者的支付记录")
  156. @ResponseBody
  157. public String chargeListByPatient(@ApiParam(name = "patient", value = "患者代码", defaultValue = "")
  158. @RequestParam String patient,
  159. @ApiParam(name = "page", value = "第几页,从1开始", defaultValue = "1")
  160. @RequestParam Integer page,
  161. @ApiParam(name = "size", value = "每页几行", defaultValue = "10")
  162. @RequestParam Integer size) throws Exception {
  163. try {
  164. List<WlyyCharge> charge = pay.chargeListByPatient(patient, page, size);
  165. return write(200, "获取患者的支付记录成功!", "data", charge);
  166. } catch (Exception e) {
  167. return error(-1, e.getMessage());
  168. }
  169. }
  170. /**
  171. * 解析URL参数串
  172. *
  173. * @param formContext
  174. * @param wordFirstsplitRegex
  175. * @param wordSecondsplitRegex
  176. * @return
  177. */
  178. private static Map<String, String> resolveFormContext(String formContext, String wordFirstsplitRegex, String wordSecondsplitRegex) {
  179. if (StringUtil.isEmpty(formContext)) {
  180. return null;
  181. }
  182. Map<String, String> targetMap = new HashMap<String, String>();
  183. String[] wordSeconds = formContext.split(wordSecondsplitRegex);
  184. for (String wordSecond : wordSeconds) {
  185. if (StringUtil.isEmpty(wordSecond)) {
  186. continue;
  187. }
  188. int idx = wordSecond.indexOf(wordFirstsplitRegex);
  189. targetMap.put(wordSecond.substring(0, idx), wordSecond.substring(idx + 1, wordSecond.length()));
  190. }
  191. return targetMap;
  192. }
  193. /**
  194. * 转换URL参数中为Map
  195. *
  196. * @param reqQueryString
  197. * @return
  198. * @throws PayException
  199. */
  200. private Map<String, String> getRequestMap(String reqQueryString) throws PayException {
  201. if (StringUtil.isEmpty(reqQueryString)) {
  202. throw new PayException("跳转参数为空");
  203. }
  204. Map<String, String> targetMap = null;
  205. try {
  206. reqQueryString = URLDecoder.decode(reqQueryString, "utf-8");
  207. targetMap = resolveFormContext(reqQueryString, "=", "&");
  208. } catch (Exception e) {
  209. throw new PayException("跳转参数处理异常," + reqQueryString);
  210. }
  211. return targetMap;
  212. }
  213. @RequestMapping(value = "/returnUrl", method = RequestMethod.GET)
  214. @ApiOperation("商户页面跳转(模拟测试)")
  215. public void testReturnUrl(HttpServletRequest request, HttpServletResponse response) throws IOException, PayException {
  216. response.setContentType("text/html;charset=utf-8");
  217. response.getWriter().write("返回结果:\n" + request.getQueryString() + "\n");
  218. String onepayUrl = request.getParameter("onepayUrl");
  219. String appId = "1BHEOI11C00J7B2CA8C0000071FA53E1";
  220. String appSecret = "1BHEOH8HB0006E0A0A0A00002DB28BC5";
  221. String signType = "MD5";
  222. String encryptType = "DES";
  223. try {
  224. OnepayDefaultClient client = new OnepayDefaultClient(onepayUrl, appId, appSecret, signType, encryptType);
  225. Map<String, String> returnParams = getRequestMap(request.getQueryString());
  226. /*boolean isVerify = client.verifyReturnSign(returnParams, returnParams.get("sign"));
  227. if (isVerify) {
  228. response.getWriter().write("签名结果:\nSIGN SUCCESS.\n");
  229. // TODO 继续处理业务
  230. // 如:查询显示订单信息
  231. } else {
  232. response.getWriter().write("签名结果:\nSIGN ERROR !!!\n");
  233. }*/
  234. } catch (Exception e) {
  235. response.getWriter().write(e.getMessage());
  236. }
  237. }
  238. }