123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407 |
- package com.yihu.wlyy.web.wx;
- import com.alibaba.fastjson.JSON;
- import com.yihu.wlyy.entity.charge.WlyyCharge;
- import com.yihu.wlyy.entity.patient.Patient;
- import com.yihu.wlyy.entity.patient.SignFamily;
- import com.yihu.wlyy.repository.charge.ChargeDao;
- import com.yihu.wlyy.repository.patient.PatientDao;
- import com.yihu.wlyy.repository.patient.SignFamilyDao;
- import com.yihu.wlyy.service.weixin.wxpay.model.BindCard;
- import com.yihu.wlyy.service.weixin.wxpay.model.Charge;
- import com.yihu.wlyy.service.weixin.wxpay.service.OnePayService;
- import com.yihu.wlyy.web.WeixinBaseController;
- import com.ylzinfo.onepay.sdk.OnepayDefaultClient;
- import com.ylzinfo.onepay.sdk.domain.ResponseParams;
- import com.ylzinfo.onepay.sdk.exception.PayException;
- import com.ylzinfo.onepay.sdk.utils.StringUtil;
- import io.swagger.annotations.Api;
- import io.swagger.annotations.ApiOperation;
- import io.swagger.annotations.ApiParam;
- import org.apache.commons.lang3.StringUtils;
- import org.json.JSONObject;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.http.MediaType;
- import org.springframework.stereotype.Controller;
- import org.springframework.util.StreamUtils;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.bind.annotation.ResponseBody;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.URLDecoder;
- import java.nio.charset.Charset;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.regex.Pattern;
- /**
- * Created by hzp on 2017/05/23.
- */
- @Controller
- @RequestMapping(value = "/onepay", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
- @Api(description = "统一支付服务")
- public class OnePayController extends WeixinBaseController {
- private static final Logger LOGGER = LoggerFactory.getLogger(OnePayController.class);
- @Autowired
- private OnePayService pay;
- @Autowired
- private PatientDao patientDao;
- @Autowired
- private SignFamilyDao signFamilyDao;
- @Autowired
- private ChargeDao chargeDao;
- @RequestMapping(value = "expensesStatus", method = RequestMethod.GET)
- @ApiOperation("居民缴费状态查询")
- @ResponseBody
- public String expensesStatus() throws Exception {
- try {
- // 获取居民个人信息 要根据签约code去查找签约扣费状态
- String patient = getUID();
- String singCode = "044701ea-5b23-11e6-8344-fa163e8aee56";
- SignFamily sign = signFamilyDao.findByCode(singCode);
- // String patient = "915cc456-5b1d-11e6-8344-fa163e8aee56";
- SignFamily signFamily = signFamilyDao.findPatientExpensesStatus(patient);
- String expensesStatus = signFamily.getExpensesStatus();
- if ("0".equals(expensesStatus)) {
- return write(200, "未扣费!", "expensesStatus", expensesStatus);
- } else if ("1".equals(expensesStatus)) {
- return write(200, "已扣费!", "expensesStatus", expensesStatus);
- } else if ("2".equals(expensesStatus)) {
- return write(200, "已退费!", "expensesStatus", expensesStatus);
- } else {
- return write(200, "扣费状态数据有误!", "expensesStatus", expensesStatus);
- }
- } catch (Exception e) {
- return error(-1, e.getMessage());
- }
- }
- /**
- * 补贴类型:无补贴01,全补贴02,补个人承担部分(20)03,补统筹基金(70)04
- *
- * @param expensesType 补贴类型
- * @return
- * @throws Exception
- */
- @RequestMapping(value = "bill", method = RequestMethod.GET)
- @ApiOperation("居民缴费账单")
- @ResponseBody
- public String getBill(@RequestParam String expensesType) throws Exception {
- try {
- Pattern pattern = Pattern.compile("[0-9]*");
- Boolean flag = pattern.matcher(expensesType).matches();
- JSONObject json = new JSONObject();
- if (flag) {
- switch (expensesType) {
- case "01":
- json.put("selfpayAmount", 20);
- json.put("financialAmount", 0);
- json.put("insuranceAmount", 70);
- json.put("totalAmount", 90);
- break;
- case "02":
- json.put("selfpayAmount", 0);
- json.put("financialAmount", 90);
- json.put("insuranceAmount", 0);
- json.put("totalAmount", 90);
- break;
- case "03":
- json.put("selfpayAmount", 0);
- json.put("financialAmount", 20);
- json.put("insuranceAmount", 70);
- json.put("totalAmount", 90);
- break;
- case "04":
- json.put("selfpayAmount", 20);
- json.put("financialAmount", 70);
- json.put("insuranceAmount", 0);
- json.put("totalAmount", 90);
- break;
- }
- }
- return write(200, "查询缴费账单成功!", "data", json);
- } catch (Exception e) {
- return error(-1, e.getMessage());
- }
- }
- @RequestMapping(value = "createSicard", method = RequestMethod.POST)
- @ApiOperation("生成电子社保卡")
- @ResponseBody
- public String createSicard() throws Exception {
- try {
- String sicardUrl = pay.createSicard("0cc6e4562de2437ab2dbbf51a9fc3b49", "oULM4xAj9sOsHJ95ttKYd_Fjh6Hc");//getUID(), getOpenid());
- return write(200, "生成电子社保卡成功!", "data", sicardUrl);
- } catch (Exception e) {
- return error(-1, e.getMessage());
- }
- }
- @RequestMapping(value = "bindCard", method = RequestMethod.POST)
- @ApiOperation("查询绑卡信息")
- @ResponseBody
- public String bindCard() throws Exception {
- try {
- // String patient = getUID();
- // String openid = getOpenid();
- String patient = "0cc6e4562de2437ab2dbbf51a9fc3b49";
- String openid = "oULM4xAj9sOsHJ95ttKYd_Fjh6Hc";
- BindCard bindCard = pay.bindCard(patient, openid);//getUID(), getOpenid()); oULM4xAj9sOsHJ95ttKYd_Fjh6Hc
- String sicardUrl = pay.createSicard(patient, openid);//getUID(), getOpenid());
- if (bindCard != null) {
- JSONObject json = new JSONObject(bindCard);
- json.put("bindStatus", "000000");
- json.put("sicardUrl", sicardUrl);
- return write(200, "查询绑卡信息成功!", "data", json);
- } else {
- // 返回电子社保卡链接
- JSONObject json = new JSONObject();
- json.put("sicardUrl", sicardUrl);
- json.put("bindStatus", "030007");
- return write(200, "生成电子社保卡成功!", "data", json);
- }
- // BindCard bindCard = pay.bindCard("0cc6e4562de2437ab2dbbf51a9fc3b49", "oULM4xAj9sOsHJ95ttKYd_Fjh6Hc");//getUID(), getOpenid()); oULM4xAj9sOsHJ95ttKYd_Fjh6Hc
- // return write(200, "查询绑卡信息成功!", "data", bindCard);
- } catch (Exception e) {
- return error(-1, e.getMessage());
- }
- }
- @RequestMapping(value = "charge", method = RequestMethod.POST)
- @ApiOperation("家庭医生签约支付")
- @ResponseBody
- public String charge(
- @ApiParam(name = "orgCode", value = "医疗机构编号", defaultValue = "3502050300")
- @RequestParam String orgCode,
- @ApiParam(name = "chargeType", value = "支付类型", defaultValue = "1")
- @RequestParam String chargeType,
- @ApiParam(name = "chargeRelation", value = "支付关联代码", defaultValue = "044701ea5b2311e68344fa163e8aee56")
- @RequestParam String chargeRelation,
- @ApiParam(name = "totalAmount", value = "交易总金额(分)", defaultValue = "1")
- @RequestParam Integer totalAmount,
- /*@ApiParam(name = "selfpayAmount", value = "自费金额(分)", defaultValue = "0")
- @RequestParam Integer selfpayAmount,
- @ApiParam(name = "insuranceAmount", value = "医保支付金额(分)", defaultValue = "1")
- @RequestParam Integer insuranceAmount,
- @ApiParam(name = "insuranceAmount", value = "区财政补贴金额(分)", defaultValue = "1")
- @RequestParam Integer financialAmount,*/
- @ApiParam(name = "feeDetail", value = "费用明细", defaultValue = "[{\"itemName\":\"家庭医生签约支付\",\"itemDesc\":\"家庭医生签约支付\",\"itemOrigPrice\":\"1\",\"itemNowPrice\":\"1\",\"itemNum\":\"1\",\"itemTotalAmt\":\"1\"}]")
- @RequestParam String feeDetail) throws Exception {
- try {
- // 获取居民个人信息
- String patient = getUID();
- String openId = getOpenid();
- if (StringUtils.isNotEmpty(openId)) {
- String settleNo = pay.charge(orgCode, chargeType, chargeRelation, totalAmount, feeDetail, patient, openId); //ohNH9sh4uwuJCxIwcLJtGTX-BaSk getUID(),getOpenid()
- return write(200, "家庭医生签约支付成功!", "data", settleNo);
- } else {
- return write(-1, "openId为空!");
- }
- // String settleNo = pay.charge(orgCode, chargeType, chargeRelation, totalAmount, selfpayAmount, insuranceAmount, feeDetail, "0cc6e4562de2437ab2dbbf51a9fc3b49", "oULM4xAj9sOsHJ95ttKYd_Fjh6Hc"); //getUID(),getOpenid()
- // return write(200, "家庭医生签约支付成功!", "data", settleNo);
- } catch (Exception e) {
- return error(-1, e.getMessage());
- }
- }
- @RequestMapping(value = "chargeQuery", method = RequestMethod.POST)
- @ApiOperation("家庭医生签约支付查询")
- @ResponseBody
- public String chargeQuery(@ApiParam(name = "code", value = "支付流水号", defaultValue = "")
- @RequestParam String code) throws Exception {
- try {
- Charge charge = pay.chargeQuery(code);
- return write(200, "家庭医生签约支付查询成功!", "data", charge);
- } catch (Exception e) {
- return error(-1, e.getMessage());
- }
- }
- @RequestMapping(value = "chargeList", method = RequestMethod.POST)
- @ApiOperation("获取某次签约的支付记录")
- @ResponseBody
- public String chargeList(@ApiParam(name = "chargeType", value = "支付类型", defaultValue = "1")
- @RequestParam String chargeType,
- @ApiParam(name = "chargeRelation", value = "支付关联代码", defaultValue = "044701ea5b2311e68344fa163e8aee56")
- @RequestParam String chargeRelation) throws Exception {
- try {
- List<WlyyCharge> charge = pay.chargeList(chargeType, chargeRelation);
- return write(200, "获取某次签约的支付记录成功!", "data", charge);
- } catch (Exception e) {
- return error(-1, e.getMessage());
- }
- }
- @RequestMapping(value = "chargeListByPatient", method = RequestMethod.POST)
- @ApiOperation("获取患者的支付记录")
- @ResponseBody
- public String chargeListByPatient(@ApiParam(name = "patient", value = "患者代码", defaultValue = "")
- @RequestParam String patient,
- @ApiParam(name = "page", value = "第几页,从1开始", defaultValue = "1")
- @RequestParam Integer page,
- @ApiParam(name = "size", value = "每页几行", defaultValue = "10")
- @RequestParam Integer size) throws Exception {
- try {
- List<WlyyCharge> charge = pay.chargeListByPatient(patient, page, size);
- return write(200, "获取患者的支付记录成功!", "data", charge);
- } catch (Exception e) {
- return error(-1, e.getMessage());
- }
- }
- /**
- * 解析URL参数串
- *
- * @param formContext
- * @param wordFirstsplitRegex
- * @param wordSecondsplitRegex
- * @return
- */
- private static Map<String, String> resolveFormContext(String formContext, String wordFirstsplitRegex, String wordSecondsplitRegex) {
- if (StringUtil.isEmpty(formContext)) {
- return null;
- }
- Map<String, String> targetMap = new HashMap<String, String>();
- String[] wordSeconds = formContext.split(wordSecondsplitRegex);
- for (String wordSecond : wordSeconds) {
- if (StringUtil.isEmpty(wordSecond)) {
- continue;
- }
- int idx = wordSecond.indexOf(wordFirstsplitRegex);
- targetMap.put(wordSecond.substring(0, idx), wordSecond.substring(idx + 1, wordSecond.length()));
- }
- return targetMap;
- }
- /**
- * 转换URL参数中为Map
- *
- * @param reqQueryString
- * @return
- * @throws PayException
- */
- private Map<String, String> getRequestMap(String reqQueryString) throws PayException {
- if (StringUtil.isEmpty(reqQueryString)) {
- throw new PayException("跳转参数为空");
- }
- Map<String, String> targetMap = null;
- try {
- reqQueryString = URLDecoder.decode(reqQueryString, "utf-8");
- targetMap = resolveFormContext(reqQueryString, "=", "&");
- } catch (Exception e) {
- throw new PayException("跳转参数处理异常," + reqQueryString);
- }
- return targetMap;
- }
- /**
- * @param request
- * @param response
- * @throws IOException
- * @throws PayException
- */
- @RequestMapping(value = "/returnUrl", method = RequestMethod.GET)
- @ApiOperation("商户页面跳转(模拟测试)")
- public void testReturnUrl(HttpServletRequest request, HttpServletResponse response) throws IOException, PayException {
- response.setContentType("text/html;charset=utf-8");
- response.getWriter().write("返回结果:\n" + request.getQueryString() + "\n");
- String onepayUrl = request.getParameter("onepayUrl");
- String appId = "1BHEOI11C00J7B2CA8C0000071FA53E1";
- String appSecret = "1BHEOH8HB0006E0A0A0A00002DB28BC5";
- String signType = "MD5";
- String encryptType = "DES";
- try {
- OnepayDefaultClient client = new OnepayDefaultClient(onepayUrl, appId, appSecret, signType, encryptType);
- Map<String, String> returnParams = getRequestMap(request.getQueryString());
- /* boolean isVerify = client.verifyReturnSign(returnParams, returnParams.get("sign"));
- if (isVerify) {
- response.getWriter().write("签名结果:\nSIGN SUCCESS.\n");
- // TODO 继续处理业务
- // 更改数据 wlyy_sign_family wlyy_charge
- } else {
- response.getWriter().write("签名结果:\nSIGN ERROR !!!\n");
- }*/
- String patient = request.getParameter("userId");
- String chargNo = request.getParameter("chargeNo");
- pay.updateData(patient,chargNo);
- } catch (Exception e) {
- response.getWriter().write(e.getMessage());
- }
- }
- /**
- * 支付真正完成,异步回调返回支付参数 后台做数据操作
- *
- * @param request
- * @param response
- * @throws IOException
- * @throws PayException
- */
- @RequestMapping(value = "/receiveNotify", method = RequestMethod.POST)
- @ApiOperation("异步回调数据更改")
- public void receiveNotify(HttpServletRequest request, HttpServletResponse response) throws IOException, PayException {
- response.setContentType("text/html;charset=utf-8");
- String appId = "1BHEOI11C00J7B2CA8C0000071FA53E1";
- String appSecret = "1BHEOH8HB0006E0A0A0A00002DB28BC5";
- InputStream inputStream = request.getInputStream();
- String params = StreamUtils.copyToString(inputStream, Charset.forName("UTF-8"));
- LOGGER.info("回调参数:{}", params);
- if (StringUtil.isEmpty(params)) {
- response.getWriter().write("empty");
- return;
- }
- // 参数转换
- ResponseParams<?> encryptRes = JSON.parseObject(params, ResponseParams.class);
- OnepayDefaultClient client = new OnepayDefaultClient("", appId, appSecret, encryptRes.getSignType(), encryptRes.getEncryptType());
- try {
- LOGGER.info("解密前:{}", JSON.toJSONString(encryptRes));
- /* ResponseParams<?> decryptRes = client.decryptNotifyResponse(encryptRes);
- LOGGER.info("解密后:{}", JSON.toJSONString(decryptRes));
- boolean isDecrypt = ("-1".equals(decryptRes.getRespCode()) ? false : true); // 是否解密失败
- if (!isDecrypt) {
- response.getWriter().write("DECRYPT FAILURE");
- } else {
- boolean isVerify = client.verifyResponseSign(decryptRes);
- LOGGER.info("验签结果:{}", isVerify);
- if (!isVerify) {
- response.getWriter().write("FAILURE");
- } else {
- // 更改数据 wlyy_sign_family wlyy_charge
- response.getWriter().write("SUCCESS");
- }
- }*/
- String patient = request.getParameter("userId");
- String chargNo = request.getParameter("chargeNo");
- pay.updateData(patient,chargNo);
- } catch (Exception e) {
- response.getWriter().write(e.getMessage());
- }
- }
- }
|