|
|
@@ -0,0 +1,127 @@
|
|
|
+package modules.deposit;
|
|
|
+
|
|
|
+import com.jfinal.aop.Inject;
|
|
|
+import com.jfinal.json.Json;
|
|
|
+import com.jfinal.kit.HttpKit;
|
|
|
+import com.jfinal.kit.StrKit;
|
|
|
+import com.wechat.pay.java.service.payments.model.Transaction;
|
|
|
+import common.jfinal.AppConfig;
|
|
|
+import common.model.Deposit;
|
|
|
+import common.model.User;
|
|
|
+import common.utils.http.MyController;
|
|
|
+import common.utils.http.MyRet;
|
|
|
+import common.utils.wechat.WeChatService;
|
|
|
+import modules.user.UserService;
|
|
|
+
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+public class DepositController extends MyController {
|
|
|
+ @Inject
|
|
|
+ DepositService service;
|
|
|
+ @Inject
|
|
|
+ UserService userService;
|
|
|
+
|
|
|
+ public void hello() {
|
|
|
+ renderJson(MyRet.ok(service.hello()));
|
|
|
+ }
|
|
|
+
|
|
|
+ // 微信充值回调地址
|
|
|
+ public void deposited() {
|
|
|
+ String originalBody = HttpKit.readData(getRequest());
|
|
|
+ AppConfig.LOGGER.info("wechat notify {}", originalBody);
|
|
|
+
|
|
|
+ // 保证每条回调都保存到数据库
|
|
|
+ Deposit deposit = new Deposit();
|
|
|
+ deposit.setCreateTime(System.currentTimeMillis());
|
|
|
+ deposit.setWxOriginal(originalBody);
|
|
|
+ deposit.setIsDeleted(0);
|
|
|
+
|
|
|
+ // 获取回调通知中的相关数据
|
|
|
+ String wechatPaySerial = getRequest().getHeader("Wechatpay-Serial");
|
|
|
+ String wechatpayNonce = getRequest().getHeader("Wechatpay-Nonce");
|
|
|
+ String wechatSignature = getRequest().getHeader("Wechatpay-Signature");
|
|
|
+ String wechatTimestamp = getRequest().getHeader("Wechatpay-Timestamp");
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 解析完成
|
|
|
+ Transaction tx = WeChatService.parseTransaction(originalBody, wechatPaySerial, wechatpayNonce, wechatSignature, wechatTimestamp);
|
|
|
+ Json json = Json.getJson();
|
|
|
+ String txJson = json.toJson(tx);
|
|
|
+
|
|
|
+ // 商户下单时传入的商户系统内部订单号。
|
|
|
+ String orderNo = tx.getOutTradeNo();
|
|
|
+ // 获取库中存放的tx
|
|
|
+ Deposit dbDeposit = service.findDepositByWxOrderNo(orderNo);
|
|
|
+ // 付款数量
|
|
|
+ Integer paymentAmount = tx.getAmount().getTotal();
|
|
|
+ // 状态描述
|
|
|
+ String status = tx.getTradeState().toString();
|
|
|
+
|
|
|
+ if (dbDeposit == null) {
|
|
|
+ deposit.setWxDecrypted(txJson);
|
|
|
+ deposit.setWxOrderStatus(status);
|
|
|
+ deposit.setDescription("未提前入库的订单,wx:" + tx.getTradeStateDesc());
|
|
|
+ deposit.setWxOrderStatus(tx.getTradeState().toString());
|
|
|
+ deposit.setAmount(paymentAmount);
|
|
|
+ deposit.save();
|
|
|
+
|
|
|
+ AppConfig.LOGGER.warn("未提前入库的订单 {}", deposit);
|
|
|
+ } else {
|
|
|
+ deposit = dbDeposit;
|
|
|
+
|
|
|
+ // 记录解密后的信息
|
|
|
+ deposit.setAmount(paymentAmount);
|
|
|
+ deposit.setWxDecrypted(txJson);
|
|
|
+
|
|
|
+ // 不要重复处理已存在的充值单号,并且要加款成功
|
|
|
+ if (StrKit.isBlank(deposit.getWxOrderStatus())) {
|
|
|
+ User user = userService.findUserById(deposit.getUserId() + "");
|
|
|
+
|
|
|
+ // 记录订单状态
|
|
|
+ deposit.setWxOrderStatus(tx.getTradeState().toString());
|
|
|
+
|
|
|
+ // 有充值信息但是未找到充值者
|
|
|
+ if (user == null) {
|
|
|
+ deposit.setDescription("未找到充值者,wx:" + tx.getTradeStateDesc());
|
|
|
+ } else {
|
|
|
+ // 判断订单状态
|
|
|
+ if (tx.getTradeState() == Transaction.TradeStateEnum.SUCCESS) {
|
|
|
+ // 如果充值成功,对用户进行充值
|
|
|
+ user.setBalance(user.getBalance() + paymentAmount);
|
|
|
+ user.update();
|
|
|
+
|
|
|
+ deposit.setDescription("用户充值成功,wx:" + tx.getTradeStateDesc());
|
|
|
+ } else {
|
|
|
+ deposit.setDescription("用户充值失败,wx:" + tx.getTradeStateDesc());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 充值记录入库
|
|
|
+ deposit.update();
|
|
|
+
|
|
|
+ AppConfig.LOGGER.info("入库成功 {}", deposit);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 成功入库后给微信的回调信息
|
|
|
+ getResponse().setStatus(200);
|
|
|
+ renderText("");
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 回调日志入库
|
|
|
+ if (StrKit.notBlank(wechatPaySerial, wechatpayNonce, wechatSignature, wechatTimestamp)) {
|
|
|
+ deposit.save();
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<String, Object> responseJson = new HashMap<>();
|
|
|
+ responseJson.put("code", "FAIL");
|
|
|
+ responseJson.put("message", "回调失败 " + e.getMessage());
|
|
|
+
|
|
|
+ AppConfig.LOGGER.error("回调失败 {}", e.getMessage());
|
|
|
+
|
|
|
+ // 返回500错误码
|
|
|
+ getResponse().setStatus(500);
|
|
|
+ renderJson(responseJson);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|