WxPayNotifyController.java 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package thyyxxk.wxservice_server.controller;
  2. import com.alibaba.fastjson.JSONObject;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.springframework.web.bind.annotation.PostMapping;
  5. import org.springframework.web.bind.annotation.RequestBody;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RestController;
  8. import thyyxxk.wxservice_server.dao.WxApiDao;
  9. import thyyxxk.wxservice_server.pojo.wxapi.PaymentNotify;
  10. import thyyxxk.wxservice_server.service.PayMzFeeService;
  11. import thyyxxk.wxservice_server.utils.DateUtil;
  12. import thyyxxk.wxservice_server.utils.WxPaySignUtil;
  13. import java.nio.charset.StandardCharsets;
  14. import java.security.GeneralSecurityException;
  15. import java.util.Date;
  16. @Slf4j
  17. @RestController
  18. @RequestMapping("/wxPayNotify")
  19. public class WxPayNotifyController {
  20. private final WxApiDao dao;
  21. private final PayMzFeeService payMzFeeService;
  22. public WxPayNotifyController(WxApiDao dao, PayMzFeeService payMzFeeService) {
  23. this.dao = dao;
  24. this.payMzFeeService = payMzFeeService;
  25. }
  26. @PostMapping("/notify")
  27. public void paymentNotify(@RequestBody PaymentNotify param) throws GeneralSecurityException {
  28. log.info("微信支付通知: {}", param);
  29. String nonce = param.getResource().getNonce();
  30. String associatedData = param.getResource().getAssociated_data();
  31. String ciphertext = param.getResource().getCiphertext();
  32. ciphertext = WxPaySignUtil.decryptToString(associatedData.getBytes(StandardCharsets.UTF_8),
  33. nonce.getBytes(StandardCharsets.UTF_8), ciphertext);
  34. JSONObject cipherObj = JSONObject.parseObject(ciphertext);
  35. log.info("微信支付通知解密:{}", ciphertext);
  36. JSONObject payer = cipherObj.getJSONObject("payer");
  37. String tradeNo = cipherObj.getString("out_trade_no");
  38. String openId = payer.getString("openid");
  39. if (cipherObj.getString("trade_state").equalsIgnoreCase("SUCCESS")) {
  40. String successTime = cipherObj.getString("success_time");
  41. successTime = successTime.split("\\+")[0].replace("T"," ");
  42. payMzFeeService.saveMzChargeInfo(tradeNo, null);
  43. dao.updatePayStatusAndOpenId(tradeNo, openId, 1, successTime);
  44. } else {
  45. dao.updatePayStatusAndOpenId(tradeNo, openId, 3, DateUtil.formatDatetime(new Date(), "yyyy-MM-dd HH:mm:ss"));
  46. }
  47. }
  48. @PostMapping("/notify2")
  49. public void paymentNotify2(@RequestBody PaymentNotify param) throws GeneralSecurityException {
  50. log.info("微信支付回调2:{}", param);
  51. String ciphertext = WxPaySignUtil.decryptToString(param.getResource().getAssociated_data().getBytes(StandardCharsets.UTF_8),
  52. param.getResource().getNonce().getBytes(StandardCharsets.UTF_8), param.getResource().getCiphertext());
  53. log.info("微信支付回调解密2:{}", ciphertext);
  54. }
  55. }