|
@@ -1,18 +1,20 @@
|
|
|
package thyyxxk.wxservice_server.service;
|
|
|
|
|
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.wechat.pay.java.core.RSAAutoCertificateConfig;
|
|
|
+import com.wechat.pay.java.core.notification.NotificationParser;
|
|
|
+import com.wechat.pay.java.core.notification.RequestParam;
|
|
|
import com.wechat.pay.java.service.payments.model.Transaction;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import thyyxxk.wxservice_server.constant.OrderType;
|
|
|
import thyyxxk.wxservice_server.dao.WxApiDao;
|
|
|
-import thyyxxk.wxservice_server.entity.wxapi.PaymentNotify;
|
|
|
import thyyxxk.wxservice_server.entity.wxapi.WxPayOrder;
|
|
|
import thyyxxk.wxservice_server.utils.TradeVectorUtil;
|
|
|
-import thyyxxk.wxservice_server.utils.WxPaySignUtil;
|
|
|
+import thyyxxk.wxservice_server.utils.wxpay.WxPayConfigUtil;
|
|
|
|
|
|
-import java.nio.charset.StandardCharsets;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.io.BufferedReader;
|
|
|
|
|
|
/**
|
|
|
* @author dj
|
|
@@ -29,40 +31,30 @@ public class WxPayNotifyService {
|
|
|
this.savePayResultService = savePayResultService;
|
|
|
}
|
|
|
|
|
|
- public void paymentNotify(PaymentNotify param) throws Exception {
|
|
|
- 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");
|
|
|
+ public void nativeNotify(HttpServletRequest request) throws Exception {
|
|
|
+ Transaction transaction = decryptNotifyBody(request);
|
|
|
+ String tradeNo = transaction.getOutTradeNo();
|
|
|
WxPayOrder order = dao.selectOrderByTradeNo(tradeNo);
|
|
|
- if (null != order) {
|
|
|
- Transaction.TradeStateEnum tradeState = order.getTradeState();
|
|
|
- if (tradeState == Transaction.TradeStateEnum.SUCCESS ||
|
|
|
- tradeState == Transaction.TradeStateEnum.REFUND) {
|
|
|
- log.info("订单号【{}】已解密过,无需继续解密。", tradeNo);
|
|
|
- return;
|
|
|
- }
|
|
|
+ Transaction.TradeStateEnum tradeState = order.getTradeState();
|
|
|
+ if (tradeState == Transaction.TradeStateEnum.SUCCESS ||
|
|
|
+ tradeState == Transaction.TradeStateEnum.REFUND) {
|
|
|
+ log.info("订单号【{}】已解密过,无需继续解密。", tradeNo);
|
|
|
+ return;
|
|
|
}
|
|
|
if (TradeVectorUtil.tradeNoBeingQuery(tradeNo)) {
|
|
|
log.info("订单号【{}】正在业务中,无需继续解密。", tradeNo);
|
|
|
return;
|
|
|
}
|
|
|
- String openId = payer.getString("openid");
|
|
|
- String tradeStateStr = cipherObj.getString("trade_state");
|
|
|
- if (!tradeStateStr.equals("SUCCESS")) {
|
|
|
- dao.updatePayStatusAndQueryTimesAndOpenId(tradeNo, tradeStateStr, openId);
|
|
|
+ String openId = transaction.getPayer().getOpenid();
|
|
|
+ tradeState = transaction.getTradeState();
|
|
|
+ if (tradeState != Transaction.TradeStateEnum.SUCCESS) {
|
|
|
+ dao.updatePayStatusAndQueryTimesAndOpenId(tradeNo, tradeState, openId);
|
|
|
return;
|
|
|
}
|
|
|
- String successTime = cipherObj.getString("success_time")
|
|
|
+ order.setTradeState(Transaction.TradeStateEnum.SUCCESS);
|
|
|
+ String successTime = transaction.getSuccessTime()
|
|
|
.split("\\+")[0].replace("T", " ");
|
|
|
- dao.updatePayStatusAndPayTimeAndOpenId(tradeNo, tradeStateStr, successTime, openId);
|
|
|
- order = dao.selectOrderByTradeNo(tradeNo);
|
|
|
+ dao.updatePayStatusAndPayTimeAndOpenId(tradeNo, tradeState, successTime, openId);
|
|
|
OrderType orderType = OrderType.get(order.getOrderType());
|
|
|
switch (orderType) {
|
|
|
case OUTPATIENT:
|
|
@@ -74,10 +66,30 @@ public class WxPayNotifyService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public void paymentNotify2(PaymentNotify param) throws Exception {
|
|
|
- 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);
|
|
|
+ public void jsapiNotify(HttpServletRequest request) throws Exception {
|
|
|
+ decryptNotifyBody(request);
|
|
|
+ }
|
|
|
+
|
|
|
+ private Transaction decryptNotifyBody(HttpServletRequest request) throws Exception {
|
|
|
+ BufferedReader reader = request.getReader();
|
|
|
+ StringBuilder requestBody = new StringBuilder();
|
|
|
+ String line;
|
|
|
+ while ((line = reader.readLine()) != null) {
|
|
|
+ requestBody.append(line);
|
|
|
+ }
|
|
|
+ RequestParam requestParam = new RequestParam.Builder()
|
|
|
+ .serialNumber(request.getHeader("Wechatpay-Serial"))
|
|
|
+ .nonce(request.getHeader("Wechatpay-Nonce"))
|
|
|
+ .signature(request.getHeader("Wechatpay-Signature"))
|
|
|
+ .timestamp(request.getHeader("Wechatpay-Timestamp"))
|
|
|
+ .body(requestBody.toString())
|
|
|
+ .build();
|
|
|
+
|
|
|
+ log.info("微信回调SDK解密开始:{}", requestParam);
|
|
|
+ RSAAutoCertificateConfig config = WxPayConfigUtil.getInstance().getConfig();
|
|
|
+ NotificationParser parser = new NotificationParser(config);
|
|
|
+ Transaction transaction = parser.parse(requestParam, Transaction.class);
|
|
|
+ log.info("微信回调SDK解密结束:{}", transaction);
|
|
|
+ return transaction;
|
|
|
}
|
|
|
}
|