OnePayController.java 9.7 KB

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