Ver Fonte

移除微信支付相关模块

skyfffire há 1 semana atrás
pai
commit
7b45db4c35

+ 0 - 57
src/main/java/common/utils/wechat/WeChatConfig.java

@@ -1,57 +0,0 @@
-package common.utils.wechat;
-
-import com.wechat.pay.java.core.Config;
-import com.wechat.pay.java.core.RSAAutoCertificateConfig;
-import com.wechat.pay.java.core.RSAPublicKeyConfig;
-import com.wechat.pay.java.core.notification.NotificationConfig;
-import com.wechat.pay.java.service.payments.h5.H5Service;
-
-public class WeChatConfig {
-    public static String merchantId = System.getenv("WX_MERCHANT_ID");
-    public static String privateKeyPath = System.getenv("WX_PRIVATE_KEY_PATH");
-    public static String publicKeyPath = System.getenv("WX_PUBLIC_KEY_PATH");
-    public static String publicKeyId = System.getenv("WX_PUBLIC_KEY_ID");
-    public static String merchantSerialNumber = System.getenv("WX_MERCHANT_SERIAL_NUMBER");
-    public static String apiV3Key = System.getenv("WX_API_V3_KEY");
-    public static String appId = System.getenv("WX_APP_ID");
-    
-    private static Config config;
-    private static NotificationConfig nConfig;
-    private static H5Service service;
-    
-    public static Config getConfig() {
-        if (config == null) {
-           config = new RSAPublicKeyConfig.Builder()
-                   .merchantId(merchantId)
-                   .privateKeyFromPath(privateKeyPath)
-                   .publicKeyFromPath(publicKeyPath)
-                   .publicKeyId(publicKeyId)
-                   .merchantSerialNumber(merchantSerialNumber)
-                   .apiV3Key(apiV3Key)
-                   .build();
-        }
-        
-        return config;
-    }
-
-    public static NotificationConfig getNConfig() {
-        if (nConfig == null) {
-            nConfig = new RSAAutoCertificateConfig.Builder()
-                    .merchantId(merchantId)
-                    .privateKeyFromPath(privateKeyPath)
-                    .merchantSerialNumber(merchantSerialNumber)
-                    .apiV3Key(apiV3Key)
-                    .build();
-        }
-
-        return nConfig;
-    }
-    
-    public static H5Service getService() {        
-        if (service == null) {
-            service = new H5Service.Builder().config(getConfig()).build();
-        }
-        
-        return service;
-    }
-}

+ 0 - 83
src/main/java/common/utils/wechat/WeChatService.java

@@ -1,83 +0,0 @@
-package common.utils.wechat;
-
-import com.wechat.pay.java.core.notification.NotificationConfig;
-import com.wechat.pay.java.core.notification.NotificationParser;
-import com.wechat.pay.java.core.notification.RequestParam;
-import com.wechat.pay.java.service.payments.h5.model.*;
-import com.wechat.pay.java.service.payments.h5.H5Service;
-import com.wechat.pay.java.service.payments.model.Transaction;
-
-public class WeChatService {
-    static H5Service h5Service = WeChatConfig.getService();
-
-    /**
-     * H5支付下单
-     * 
-     * @param amount        订单数量
-     * @param outTradeNo    内部订单号
-     * @param notifyUrl     支付通知url
-     * @return              结果,主要是H5Url
-     */
-    public static PrepayResponse prepay(Integer amount, String outTradeNo, String notifyUrl, SceneInfo sceneInfo) {
-        PrepayRequest request = new PrepayRequest();
-        
-        // 交易数量
-        Amount amountM = new Amount();
-        amountM.setTotal(amount);
-        
-        // 参数配置
-        request.setAmount(amountM);
-        request.setAppid(WeChatConfig.appId);
-        request.setMchid(WeChatConfig.merchantId);
-        request.setDescription("购买ID:" + outTradeNo);
-        request.setNotifyUrl(notifyUrl);
-        request.setOutTradeNo(outTradeNo);
-        request.setSceneInfo(sceneInfo);
-        
-        // 调用接口
-        return h5Service.prepay(request);
-    }
-
-    // 将请求体解析为tx
-    public static Transaction parseTransaction(String requestBody, 
-                                               String wechatPaySerial, 
-                                               String wechatpayNonce, 
-                                               String wechatSignature, 
-                                               String wechatTimestamp) {
-        // 构造 RequestParam
-        RequestParam requestParam = new RequestParam.Builder()
-                .serialNumber(wechatPaySerial)
-                .nonce(wechatpayNonce)
-                .signature(wechatSignature)
-                .timestamp(wechatTimestamp)
-                .body(requestBody)
-                .build();
-
-        // 如果已经初始化了 NotificationConfig,可直接使用
-        NotificationConfig config = WeChatConfig.getNConfig();
-
-        // 初始化 NotificationParser
-        NotificationParser parser = new NotificationParser(config);
-        
-        // 直接返回tx
-        return parser.parse(requestParam, Transaction.class);
-    }
-
-    // 测试支付
-    public static void main(String[] args) {
-        // String notifyUrl = System.getenv("URL_BASE") + "/deposit/deposited/";
-        String notifyUrl = "https://api.dlsh-nft.com.cn" + "/deposit/deposited/";
-        String outTradeNo = "DLTBH_WX_" + System.currentTimeMillis();
-        Integer amount = 100;
-
-        // 场景信息
-        H5Info h5Info = new H5Info();
-        h5Info.setType("Wrp"); // 使用H5支付的场景:Wap、iOS、Android
-        
-        SceneInfo sceneInfo = new SceneInfo();
-        sceneInfo.setPayerClientIp("163.53.18.23");
-        sceneInfo.setH5Info(h5Info);
-        
-        prepay(amount, outTradeNo, notifyUrl, sceneInfo);
-    }
-}

+ 127 - 138
src/main/java/modules/deposit/DepositController.java

@@ -3,26 +3,15 @@ package modules.deposit;
 import com.alibaba.fastjson.JSONObject;
 import com.jfinal.aop.Before;
 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.interceptor.LoginInterceptor;
 import common.interceptor.empty.EmptyInterface;
 import common.interceptor.role.RequiredRoleInterface;
-import common.jfinal.AppConfig;
-import common.model.Deposit;
 import common.model.User;
-import common.utils.IpAddressUtil;
 import common.utils.http.MyController;
 import common.utils.http.MyRet;
-import common.utils.wechat.WeChatService;
 import modules.user.UserController;
 import modules.user.UserService;
 
-import java.util.HashMap;
-import java.util.Map;
-
 public class DepositController extends MyController {
     @Inject
     DepositService service;
@@ -33,134 +22,134 @@ public class DepositController extends MyController {
         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.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);
-        }
-    }
+    // // 微信充值回调地址
+    // 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.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);
+    //     }
+    // }
     
-    // 创建微信支付订单
-    @Before(LoginInterceptor.class)
-    @EmptyInterface({"amount"})
-    public void create() {
-        JSONObject requestBodyJson = MyController.getJsonModelByRequestAndType(getRequest(), JSONObject.class);
-
-        // 获取支付数量(分)
-        String amountStr = requestBodyJson.getString("amount");
-        int amount;
-        try {
-            amount = Integer.parseInt(amountStr);
-
-            if (amount <= 0) {
-                renderJson(MyRet.fail("支付数量(amount)期待是正整数,你传的是: " + amountStr));
-                return;
-            }
-        } catch (Exception e) {
-            renderJson(MyRet.fail("支付数量(amount)格式不正确: " + e.getMessage()));
-            return;
-        }
-        
-        // 获取公网ip地址
-        String ip = IpAddressUtil.getClientIpAddress(this);
-        
-        // 获取当前用户
-        User user = userService.findUserByMobileNumber(getSessionAttr("mobile_number"));
-        
-        // 创建并返回支付链接给前端
-        renderJson(service.createDepositOrder(amount, ip, user.getId()));
-    }
+    // // 创建微信支付订单
+    // @Before(LoginInterceptor.class)
+    // @EmptyInterface({"amount"})
+    // public void create() {
+    //     JSONObject requestBodyJson = MyController.getJsonModelByRequestAndType(getRequest(), JSONObject.class);
+    //
+    //     // 获取支付数量(分)
+    //     String amountStr = requestBodyJson.getString("amount");
+    //     int amount;
+    //     try {
+    //         amount = Integer.parseInt(amountStr);
+    //
+    //         if (amount <= 0) {
+    //             renderJson(MyRet.fail("支付数量(amount)期待是正整数,你传的是: " + amountStr));
+    //             return;
+    //         }
+    //     } catch (Exception e) {
+    //         renderJson(MyRet.fail("支付数量(amount)格式不正确: " + e.getMessage()));
+    //         return;
+    //     }
+    //    
+    //     // 获取公网ip地址
+    //     String ip = IpAddressUtil.getClientIpAddress(this);
+    //    
+    //     // 获取当前用户
+    //     User user = userService.findUserByMobileNumber(getSessionAttr("mobile_number"));
+    //    
+    //     // 创建并返回支付链接给前端
+    //     renderJson(service.createDepositOrder(amount, ip, user.getId()));
+    // }
 
     @Before(LoginInterceptor.class)
     @EmptyInterface({"page_size", "page_number"})

+ 35 - 40
src/main/java/modules/deposit/DepositService.java

@@ -1,9 +1,6 @@
 package modules.deposit;
 
 import com.jfinal.plugin.activerecord.Db;
-import com.wechat.pay.java.service.payments.h5.model.H5Info;
-import com.wechat.pay.java.service.payments.h5.model.PrepayResponse;
-import com.wechat.pay.java.service.payments.h5.model.SceneInfo;
 import common.model.Deposit;
 import common.utils.http.MyRet;
 
@@ -12,48 +9,46 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
-import static common.utils.wechat.WeChatService.prepay;
-
 public class DepositService {
     public String hello() {
         return "Hello deposit";
     }
     
-    public MyRet createDepositOrder(Integer amount, String ip, long userId) {
-        String notifyUrl = System.getenv("URL_BASE") + "/deposit/deposited";
-        String outTradeNo = "DLTBH_WX_" + System.currentTimeMillis() + "_" + userId;
-
-        // 场景信息
-        H5Info h5Info = new H5Info();
-        h5Info.setType("Wrp"); // 使用H5支付的场景:Wap、iOS、Android
-
-        SceneInfo sceneInfo = new SceneInfo();
-        sceneInfo.setPayerClientIp(ip);
-        sceneInfo.setH5Info(h5Info);
-
-        // 方便获取报错信息
-        try {
-            // 获取微信支付订单结果
-            PrepayResponse response = prepay(amount, outTradeNo, notifyUrl, sceneInfo);
-            
-            // 微信支付订单创建成功,创建内部订单
-            Deposit deposit = new Deposit();
-            
-            // put各类参数
-            deposit.setUserId(userId);
-            deposit.setWxOrderNo(outTradeNo);
-            deposit.setAmount(amount);
-            deposit.setCreateTime(System.currentTimeMillis());
-            
-            if (deposit.save()) {
-                return MyRet.ok("微信支付订单创建成功").setData(response.getH5Url());
-            } else {
-                return MyRet.fail("微信支付订单创建失败:Deposit入库失败").setData(deposit);
-            }
-        } catch (Exception e) {
-            return MyRet.fail("微信支付订单创建失败").setData(e.getMessage());
-        }
-    }
+    // public MyRet createDepositOrder(Integer amount, String ip, long userId) {
+    //     String notifyUrl = System.getenv("URL_BASE") + "/deposit/deposited";
+    //     String outTradeNo = "DLTBH_WX_" + System.currentTimeMillis() + "_" + userId;
+    //
+    //     // 场景信息
+    //     H5Info h5Info = new H5Info();
+    //     h5Info.setType("Wrp"); // 使用H5支付的场景:Wap、iOS、Android
+    //
+    //     SceneInfo sceneInfo = new SceneInfo();
+    //     sceneInfo.setPayerClientIp(ip);
+    //     sceneInfo.setH5Info(h5Info);
+    //
+    //     // 方便获取报错信息
+    //     try {
+    //         // 获取微信支付订单结果
+    //         PrepayResponse response = prepay(amount, outTradeNo, notifyUrl, sceneInfo);
+    //        
+    //         // 微信支付订单创建成功,创建内部订单
+    //         Deposit deposit = new Deposit();
+    //        
+    //         // put各类参数
+    //         deposit.setUserId(userId);
+    //         deposit.setWxOrderNo(outTradeNo);
+    //         deposit.setAmount(amount);
+    //         deposit.setCreateTime(System.currentTimeMillis());
+    //        
+    //         if (deposit.save()) {
+    //             return MyRet.ok("微信支付订单创建成功").setData(response.getH5Url());
+    //         } else {
+    //             return MyRet.fail("微信支付订单创建失败:Deposit入库失败").setData(deposit);
+    //         }
+    //     } catch (Exception e) {
+    //         return MyRet.fail("微信支付订单创建失败").setData(e.getMessage());
+    //     }
+    // }
     
     public Deposit findDepositByWxOrderNo(String wxOrderNo) {
         return Deposit.dao.findFirst("SELECT * FROM t_deposit WHERE wx_order_no=?", wxOrderNo);