WechatPayService.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package com.fdkankan.sale.pay.wx;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.fdkankan.common.constant.Constant;
  4. import com.fdkankan.common.util.MD5;
  5. import com.fdkankan.sale.common.CacheUtil;
  6. import com.fdkankan.sale.common.ResultCode;
  7. import com.fdkankan.sale.entity.PriceList;
  8. import com.fdkankan.sale.entity.Repair;
  9. import com.fdkankan.sale.entity.RepairPay;
  10. import com.fdkankan.sale.exception.BusinessException;
  11. import com.fdkankan.sale.service.IPriceListService;
  12. import com.fdkankan.sale.service.IRepairLogService;
  13. import com.fdkankan.sale.service.IRepairPayService;
  14. import com.fdkankan.sale.service.IRepairService;
  15. import com.fdkankan.sale.util.DateUtil;
  16. import com.fdkankan.sale.vo.request.WechatMobileParam;
  17. import lombok.extern.slf4j.Slf4j;
  18. import org.apache.commons.lang3.StringUtils;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.stereotype.Service;
  21. import javax.servlet.http.HttpServletRequest;
  22. import javax.servlet.http.HttpServletResponse;
  23. import java.io.BufferedOutputStream;
  24. import java.math.BigDecimal;
  25. import java.net.URLEncoder;
  26. import java.util.HashMap;
  27. import java.util.Map;
  28. import java.util.Random;
  29. @Service
  30. @Slf4j
  31. public class WechatPayService {
  32. @Autowired
  33. IPriceListService priceListService;
  34. @Autowired
  35. IRepairPayService repairPayService;
  36. @Autowired
  37. IRepairService repairService;
  38. @Autowired
  39. IRepairLogService repairLogService;
  40. public Object wechatMobilePay(WechatMobileParam param, String ip) throws Exception {
  41. if(StringUtils.isBlank(param.getRepairId())){
  42. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  43. }
  44. Repair repair = repairService.getById(param.getRepairId());
  45. if(repair == null){
  46. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  47. }
  48. if(repair.getStatus() != 9 && repair.getStatus() != 4){
  49. throw new BusinessException(ResultCode.REPAIR_STATUS_NOT_EXITS);
  50. }
  51. RepairPay repairPay = repairPayService.getByRepairId(param.getRepairId(),1);
  52. if(repairPay != null ){
  53. throw new BusinessException(ResultCode.ORDER_PAY_ERROR);
  54. }
  55. String orderSn = null;
  56. BigDecimal amount = BigDecimal.ZERO;
  57. String subject = null;
  58. Integer orderType = null;
  59. if(repair.getStatus() == 9){
  60. amount = priceListService.getAmountByRepairId(param.getRepairId());
  61. orderSn = "se_s"+DateUtil.getDate(DateUtil.repairIdFmt);
  62. subject = "维修订单";
  63. orderType = 0;
  64. }
  65. if(repair.getStatus() == 3){
  66. amount = priceListService.getRobAmountByRepairId(param.getRepairId());
  67. orderSn = "se_c"+DateUtil.getDate(DateUtil.repairIdFmt);
  68. subject = "取消维修订单";
  69. orderType = 1;
  70. }
  71. repairPay = new RepairPay();
  72. repairPay.setRepairId(param.getRepairId());
  73. repairPay.setPayType(1);
  74. repairPay.setOrderSn(orderSn);
  75. repairPay.setPayAmount(amount);
  76. repairPay.setOrderType(orderType);
  77. repairPayService.save(repairPay);
  78. BigDecimal totalFee = amount.multiply(new BigDecimal(100));
  79. String body = subject;
  80. return this.wechatH5Pay(param.getRepairId(),orderSn, subject, body, totalFee, ip);
  81. }
  82. public JSONObject wechatH5Pay(String repairId,String orderSn, String subject, String body, BigDecimal totalFee, String ipAddress) throws Exception {
  83. log.warn("ipAddress:"+ipAddress);
  84. Map<String, String> data = new HashMap<String, String>();
  85. data.put("body", subject);
  86. data.put("detail", body);
  87. data.put("out_trade_no", orderSn);
  88. data.put("device_info", "H5");
  89. data.put("fee_type", "CNY");
  90. data.put("total_fee", totalFee.stripTrailingZeros().toPlainString());
  91. data.put("spbill_create_ip", ipAddress);
  92. data.put("notify_url", CacheUtil.mainUrl + WXPayDefaultConfig.notifyURL);
  93. data.put("trade_type", "MWEB"); // 此处指定为扫码支付
  94. data.put("product_id", repairId); //多个商品,使用订单号
  95. data.put("nonce_str", MD5.getMessageDigest(String.valueOf(new Random().nextInt(10000)).getBytes()));
  96. String mweb_url = "";
  97. Map<String, String> resp = WXPay.unifiedOrder(data);
  98. String return_code = resp.get("return_code");
  99. String return_msg = resp.get("return_msg");
  100. if ("SUCCESS".equals(return_code) && "OK".equals(return_msg)) {
  101. mweb_url = resp.get("mweb_url");//调微信支付接口地址
  102. mweb_url += "&redirect_url=" + URLEncoder.encode(CacheUtil.mainUrl + WXPayDefaultConfig.h5RedirectURL, "UTF-8");
  103. log.warn("mweb_url=" + mweb_url);
  104. } else {
  105. log.error("微信统一支付接口获取预支付订单出错",resp);
  106. }
  107. JSONObject j = new JSONObject();
  108. j.put("mweb_url", mweb_url);
  109. j.put("orderSn", orderSn);
  110. return j;
  111. }
  112. public void callBack(HttpServletRequest request, HttpServletResponse response) {
  113. log.warn("微信回调接口方法 start");
  114. String inputLine = "";
  115. String notifyXml = "";
  116. try {
  117. while((inputLine = request.getReader().readLine()) != null){
  118. notifyXml += inputLine;
  119. }
  120. //关闭流
  121. request.getReader().close();
  122. log.warn("微信回调内容信息:"+notifyXml);
  123. //解析成Map
  124. Map<String,String> map = WXPayUtil.xmlToMap(notifyXml);
  125. if (WXPayUtil.isSignatureValid(map, WXPayDefaultConfig.key, WXPayConstants.SignType.MD5) || WXPayUtil.isSignatureValid(map, WXPayDefaultConfig.key, WXPayConstants.SignType.HMACSHA256)) {
  126. //判断 支付是否成功
  127. if("SUCCESS".equals(map.get("result_code"))){
  128. log.warn("微信回调返回是否支付成功:是");
  129. //获得 返回的商户订单号
  130. String out_trade_no = map.get("out_trade_no");
  131. String trade_no = map.get("transaction_id");
  132. String openid = map.get("openid");
  133. log.warn("微信回调返回商户订单号:" + out_trade_no);
  134. //修改订单状态
  135. repairService.paySuccess(out_trade_no,trade_no,openid);
  136. log.warn("微信支付成功,订单号:" + out_trade_no);
  137. //通知微信服务器已经支付成功
  138. notifyXml = "<xml><return_code><![CDATA[SUCCESS]]></return_code>"
  139. + "<return_msg><![CDATA[OK]]></return_msg></xml> ";
  140. }else{
  141. notifyXml = "<xml><return_code><![CDATA[FAIL]]></return_code>"
  142. + "<return_msg><![CDATA[报文为空]]></return_msg></xml> ";
  143. }
  144. } else{
  145. log.error("签名验证错误");
  146. notifyXml = "<xml><return_code><![CDATA[FAIL]]></return_code>"
  147. + "<return_msg><![CDATA[签名验证错误]]></return_msg></xml>";
  148. }
  149. BufferedOutputStream out = new BufferedOutputStream(
  150. response.getOutputStream());
  151. out.write(notifyXml.getBytes());
  152. out.flush();
  153. out.close();
  154. } catch (Exception e) {
  155. log.error("微信支付回调数据异常, errorMsg:{}", e.getMessage());
  156. }
  157. }
  158. public Object queryStatus(WechatMobileParam param) {
  159. if(StringUtils.isBlank(param.getOrderSn())){
  160. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  161. }
  162. RepairPay repairPay = repairPayService.getByOrderSn(param.getOrderSn());
  163. if(repairPay == null){
  164. throw new BusinessException(ResultCode.ORDER_PAY_NOT_EXITS);
  165. }
  166. return repairPay.getPayStatus();
  167. }
  168. }