WxPayController.java 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package com.jpsoft.smart.modules.pay.weixin;
  2. import cn.hutool.core.date.DateUtil;
  3. import cn.hutool.core.util.StrUtil;
  4. import com.ijpay.core.enums.SignType;
  5. import com.ijpay.core.enums.TradeType;
  6. import com.ijpay.core.kit.HttpKit;
  7. import com.ijpay.core.kit.WxPayKit;
  8. import com.ijpay.wxpay.WxPayApi;
  9. import com.ijpay.wxpay.model.UnifiedOrderModel;
  10. import com.jpsoft.smart.modules.base.entity.ElectricClientInfo;
  11. import com.jpsoft.smart.modules.base.entity.RechargeRecord;
  12. import com.jpsoft.smart.modules.base.service.ElectricClientInfoService;
  13. import com.jpsoft.smart.modules.base.service.RechargeRecordService;
  14. import com.jpsoft.smart.modules.business.service.RechargeService;
  15. import com.jpsoft.smart.modules.common.dto.MessageResult;
  16. import com.jpsoft.smart.modules.communication.server.protocol.MeterReceivePacket;
  17. import com.jpsoft.smart.modules.pay.properties.WxJpsoftProperties;
  18. import io.swagger.annotations.ApiOperation;
  19. import org.slf4j.Logger;
  20. import org.slf4j.LoggerFactory;
  21. import org.springframework.beans.factory.annotation.Autowired;
  22. import org.springframework.web.bind.annotation.*;
  23. import javax.servlet.http.HttpServletRequest;
  24. import java.math.BigDecimal;
  25. import java.util.Date;
  26. import java.util.HashMap;
  27. import java.util.Map;
  28. import java.util.concurrent.TimeUnit;
  29. /**
  30. * @author 墨鱼_mo
  31. * @date 2019-11-26 10:35
  32. */
  33. @RequestMapping("/wxPay")
  34. @RestController
  35. public class WxPayController {
  36. private Logger logger = LoggerFactory.getLogger(getClass());
  37. @Autowired
  38. private WxJpsoftProperties wxJpsoftProperties;
  39. @Autowired
  40. private RechargeRecordService rechargeRecordService;
  41. @Autowired
  42. private RechargeService rechargeService;
  43. @Autowired
  44. private ElectricClientInfoService electricClientInfoService;
  45. @ApiOperation(value = "微信JSAPI支付")
  46. @GetMapping("/webPay")
  47. public MessageResult webPay(String recordId,String openId) {
  48. MessageResult msgResult = new MessageResult<>();
  49. try {
  50. RechargeRecord rechargeRecord = rechargeRecordService.get(recordId);
  51. rechargeRecord.setOpenId(openId);
  52. rechargeRecord.setUpdateTime(new Date());
  53. rechargeRecordService.update(rechargeRecord);
  54. if (rechargeRecord == null) {
  55. throw new Exception("订单不存在");
  56. }
  57. if (StrUtil.isBlank(rechargeRecord.getOpenId())) {
  58. throw new Exception("openId不存在");
  59. }
  60. if (rechargeRecord.getBuyAmount().compareTo(BigDecimal.ZERO) != 1) {
  61. throw new Exception("金额有误");
  62. }
  63. if (rechargeRecord.getPaymentStatus().equals("20")) {
  64. throw new Exception("订单已支付");
  65. }
  66. //金额处理
  67. int wxTotalTee = rechargeRecord.getBuyAmount().multiply(new BigDecimal(100)).intValue();
  68. Map<String, String> params = UnifiedOrderModel
  69. .builder()
  70. .appid(wxJpsoftProperties.getAppId())
  71. .mch_id(wxJpsoftProperties.getMchId())
  72. .sub_mch_id(wxJpsoftProperties.getSubMchId())
  73. .nonce_str(WxPayKit.generateStr())
  74. .body(rechargeRecord.getProductTheme())
  75. .out_trade_no(rechargeRecord.getSerialNumber())
  76. .total_fee(String.valueOf(wxTotalTee))
  77. .spbill_create_ip(wxJpsoftProperties.getIp())
  78. .notify_url(wxJpsoftProperties.getNotifyUrl())
  79. .trade_type(TradeType.JSAPI.getTradeType())
  80. .openid(rechargeRecord.getOpenId())
  81. .build()
  82. .createSign(wxJpsoftProperties.getMchKey(), SignType.HMACSHA256);
  83. String xmlResult = WxPayApi.pushOrder(false, params);
  84. logger.info(xmlResult);
  85. Map<String, String> resultMap = WxPayKit.xmlToMap(xmlResult);
  86. String returnCode = resultMap.get("return_code");
  87. String returnMsg = resultMap.get("return_msg");
  88. if (!WxPayKit.codeIsOk(returnCode)) {
  89. logger.error(returnCode);
  90. throw new Exception(returnMsg);
  91. }
  92. String resultCode = resultMap.get("result_code");
  93. if (!WxPayKit.codeIsOk(resultCode)) {
  94. String errCode = resultMap.get("err_code_des");
  95. logger.error(errCode);
  96. throw new Exception(errCode);
  97. }
  98. // 以下字段在 return_code 和 result_code 都为 SUCCESS 的时候有返回
  99. String prepayId = resultMap.get("prepay_id");
  100. Map<String, String> packageParams = WxPayKit.prepayIdCreateSign(prepayId, wxJpsoftProperties.getAppId(),
  101. wxJpsoftProperties.getMchKey(), SignType.HMACSHA256);
  102. msgResult.setData(packageParams);
  103. msgResult.setResult(true);
  104. } catch (Exception e) {
  105. e.printStackTrace();
  106. logger.error(e.getMessage(), e);
  107. msgResult.setResult(false);
  108. msgResult.setMessage(e.getMessage());
  109. }
  110. return msgResult;
  111. }
  112. @RequestMapping(value = "/payNotify", method = {RequestMethod.POST, RequestMethod.GET})
  113. @ResponseBody
  114. public String payNotify(HttpServletRequest request) {
  115. String xmlMsg = HttpKit.readData(request);
  116. logger.warn("支付通知=" + xmlMsg);
  117. Map<String, String> params = WxPayKit.xmlToMap(xmlMsg);
  118. String returnCode = params.get("return_code");
  119. String outTradeNo = params.get("out_trade_no");
  120. String payTimeStr = params.get("time_end");
  121. Date payTime = DateUtil.parse(payTimeStr);
  122. // 注意此处签名方式需与统一下单的签名类型一致 WxPayApiConfigKit.getWxPayApiConfig().getPartnerKey()
  123. if (WxPayKit.verifyNotify(params, wxJpsoftProperties.getMchKey(), SignType.HMACSHA256)) {
  124. if (WxPayKit.codeIsOk(returnCode)) {
  125. // 更新订单信息
  126. try {
  127. RechargeRecord rechargeRecord = rechargeRecordService.getBySerialNum(outTradeNo);
  128. if (rechargeRecord == null) {
  129. throw new Exception("支付已完成,但未找到此订单号的流水");
  130. }
  131. if (rechargeRecord.getBuyAmount().multiply(new BigDecimal(100)).compareTo(new BigDecimal(params.get("total_fee"))) !=0) {
  132. throw new Exception("支付金额与系统金额不一致");
  133. }
  134. rechargeRecord.setUpdateTime(new Date());
  135. rechargeRecord.setUpdateBy(params.get("openid"));
  136. rechargeRecord.setBuyType("weipay");
  137. rechargeRecord.setPaymentStatus("20");
  138. rechargeRecord.setTransactionNumber(params.get("transaction_id"));
  139. rechargeRecord.setPaymentTime(payTime);
  140. try {
  141. MeterReceivePacket mp = rechargeService.recharge(rechargeRecord.getRoomId(), rechargeRecord.getBuyElectricity(), 3, TimeUnit.SECONDS);
  142. if (mp == null){
  143. rechargeRecord.setErrorLog("相应超时");
  144. }else {
  145. if (!mp.isValidateResult()){
  146. rechargeRecord.setErrorLog("校验失败");
  147. }else {
  148. rechargeRecord.setChargingStatus("20");
  149. rechargeRecord.setDumpEnergy(mp.getRemain()); //当前剩余电量
  150. ElectricClientInfo clientInfo = electricClientInfoService.findOneByRoomId(rechargeRecord.getRoomId());
  151. if (clientInfo!=null){
  152. //记录剩余电量
  153. clientInfo.setElectricityRemaining(mp.getRemain());
  154. //记录充电次数
  155. clientInfo.setRechargeTimes(mp.getRechargeTimes());
  156. clientInfo.setActive(mp.getState()!=1);
  157. clientInfo.setUpdateTime(new Date());
  158. electricClientInfoService.update(clientInfo);
  159. }
  160. }
  161. }
  162. } catch (Exception e) {
  163. rechargeRecord.setErrorLog(e.getMessage());
  164. logger.error(e.getMessage(), e);
  165. e.printStackTrace();
  166. }
  167. rechargeRecordService.update(rechargeRecord);
  168. // 发送通知等
  169. Map<String, String> xml = new HashMap<String, String>(2);
  170. xml.put("return_code", "SUCCESS");
  171. xml.put("return_msg", "OK");
  172. return WxPayKit.toXml(xml);
  173. } catch (Exception e) {
  174. logger.error(e.getMessage(), e);
  175. e.printStackTrace();
  176. }
  177. }
  178. }
  179. return null;
  180. }
  181. }