WxPayController.java 8.0 KB

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