Преглед на файлове

修复微信使用旧版证书的问题,完全转为新版密钥方式

skyfffire преди 1 месец
родител
ревизия
6ed594f3e6
променени са 2 файла, в които са добавени 27 реда и са изтрити 5 реда
  1. 6 1
      src/main/java/common/utils/wechat/WeChatConfig.java
  2. 21 4
      src/main/java/common/utils/wechat/WeChatService.java

+ 6 - 1
src/main/java/common/utils/wechat/WeChatConfig.java

@@ -2,12 +2,15 @@ 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");
@@ -18,9 +21,11 @@ public class WeChatConfig {
     
     public static Config getConfig() {
         if (config == null) {
-           config = new RSAAutoCertificateConfig.Builder()
+           config = new RSAPublicKeyConfig.Builder()
                    .merchantId(merchantId)
                    .privateKeyFromPath(privateKeyPath)
+                   .publicKeyFromPath(publicKeyPath)
+                   .publicKeyId(publicKeyId)
                    .merchantSerialNumber(merchantSerialNumber)
                    .apiV3Key(apiV3Key)
                    .build();

+ 21 - 4
src/main/java/common/utils/wechat/WeChatService.java

@@ -3,9 +3,7 @@ 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.Amount;
-import com.wechat.pay.java.service.payments.h5.model.PrepayRequest;
-import com.wechat.pay.java.service.payments.h5.model.PrepayResponse;
+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;
 
@@ -20,7 +18,7 @@ public class WeChatService {
      * @param notifyUrl     支付通知url
      * @return              结果,主要是H5Url
      */
-    public static PrepayResponse prepay(Integer amount, String outTradeNo, String notifyUrl) {
+    public static PrepayResponse prepay(Integer amount, String outTradeNo, String notifyUrl, SceneInfo sceneInfo) {
         PrepayRequest request = new PrepayRequest();
         
         // 交易数量
@@ -34,6 +32,7 @@ public class WeChatService {
         request.setDescription("购买ID:" + outTradeNo);
         request.setNotifyUrl(notifyUrl);
         request.setOutTradeNo(outTradeNo);
+        request.setSceneInfo(sceneInfo);
         
         // 调用接口
         return h5Service.prepay(request);
@@ -63,4 +62,22 @@ public class WeChatService {
         // 直接返回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);
+    }
 }