package thyyxxk.wxservice_server.controller; import com.alibaba.fastjson.JSONObject; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import thyyxxk.wxservice_server.dao.WxApiDao; import thyyxxk.wxservice_server.pojo.wxapi.PaymentNotify; import thyyxxk.wxservice_server.service.PayMzFeeService; import thyyxxk.wxservice_server.utils.DateUtil; import thyyxxk.wxservice_server.utils.WxPaySignUtil; import java.nio.charset.StandardCharsets; import java.security.GeneralSecurityException; import java.util.Date; @Slf4j @RestController @RequestMapping("/wxPayNotify") public class WxPayNotifyController { private final WxApiDao dao; private final PayMzFeeService payMzFeeService; public WxPayNotifyController(WxApiDao dao, PayMzFeeService payMzFeeService) { this.dao = dao; this.payMzFeeService = payMzFeeService; } @PostMapping("/notify") public void paymentNotify(@RequestBody PaymentNotify param) throws GeneralSecurityException { log.info("微信支付通知: {}", param); String nonce = param.getResource().getNonce(); String associatedData = param.getResource().getAssociated_data(); String ciphertext = param.getResource().getCiphertext(); ciphertext = WxPaySignUtil.decryptToString(associatedData.getBytes(StandardCharsets.UTF_8), nonce.getBytes(StandardCharsets.UTF_8), ciphertext); JSONObject cipherObj = JSONObject.parseObject(ciphertext); log.info("微信支付通知解密:{}", ciphertext); JSONObject payer = cipherObj.getJSONObject("payer"); String tradeNo = cipherObj.getString("out_trade_no"); String openId = payer.getString("openid"); if (cipherObj.getString("trade_state").equalsIgnoreCase("SUCCESS")) { String successTime = cipherObj.getString("success_time"); successTime = successTime.split("\\+")[0].replace("T"," "); payMzFeeService.saveMzChargeInfo(tradeNo, null); dao.updatePayStatusAndOpenId(tradeNo, openId, 1, successTime); } else { dao.updatePayStatusAndOpenId(tradeNo, openId, 3, DateUtil.formatDatetime(new Date(), "yyyy-MM-dd HH:mm:ss")); } } @PostMapping("/notify2") public void paymentNotify2(@RequestBody PaymentNotify param) throws GeneralSecurityException { log.info("微信支付回调2:{}", param); String ciphertext = WxPaySignUtil.decryptToString(param.getResource().getAssociated_data().getBytes(StandardCharsets.UTF_8), param.getResource().getNonce().getBytes(StandardCharsets.UTF_8), param.getResource().getCiphertext()); log.info("微信支付回调解密2:{}", ciphertext); } }