|
@@ -1,8 +1,12 @@
|
|
|
package com.example.xiaoshiweixinback.service.weixinpay;
|
|
|
|
|
|
import com.alibaba.druid.sql.ast.statement.SQLForeignKeyImpl;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.example.xiaoshiweixinback.business.utils.AesUtil;
|
|
|
+import com.example.xiaoshiweixinback.business.utils.MD5Util;
|
|
|
import com.example.xiaoshiweixinback.business.utils.RandomUtil;
|
|
|
import com.example.xiaoshiweixinback.entity.weixinPay.GetAuthorizationVO;
|
|
|
+import com.example.xiaoshiweixinback.entity.weixinPay.WeixinSuccessVO;
|
|
|
import okhttp3.HttpUrl;
|
|
|
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
|
|
|
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
|
@@ -10,7 +14,10 @@ import org.bouncycastle.openssl.PEMParser;
|
|
|
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import javax.crypto.SecretKey;
|
|
|
+import javax.crypto.spec.SecretKeySpec;
|
|
|
import java.io.FileReader;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
import java.security.PrivateKey;
|
|
|
import java.security.Security;
|
|
|
import java.security.Signature;
|
|
@@ -25,7 +32,7 @@ public class AuthorizationService {
|
|
|
*/
|
|
|
|
|
|
public static String merchantId = "1673179188";
|
|
|
-
|
|
|
+ public static String appId = "wxaefb842bd0b93ff0";
|
|
|
/**
|
|
|
* 商户API私钥路径
|
|
|
*/
|
|
@@ -42,7 +49,7 @@ public class AuthorizationService {
|
|
|
* 商户APIV3密钥
|
|
|
*/
|
|
|
|
|
|
- public static String apiV3key = "wL3g4tAlOFe72gAd1THRqNPQsHIVxsYih6s20gN035oKRM3IBcd4c747zlQ1fLu8NrPCvGCo01Ox2jbDwkHJNaS9Yyn2R1NsTgoGWPObH6DWajwNBMrM3hjSC92XV3hJGfO7dkOsikNtHCigwFZk3DAXQmW6JLcl";
|
|
|
+ public static String apiV3key = "wL3g4tAlOFe72gAd1THRqNPQsHIVxsYi";
|
|
|
|
|
|
// public String getToken(String method, S, String body) throws Exception {
|
|
|
//
|
|
@@ -83,13 +90,12 @@ public class AuthorizationService {
|
|
|
// }
|
|
|
|
|
|
public GetAuthorizationVO getAuthorization(String type, String url, String body) throws Exception {
|
|
|
- GetAuthorizationVO getAuthorizationVO =new GetAuthorizationVO();
|
|
|
+ GetAuthorizationVO getAuthorizationVO = new GetAuthorizationVO();
|
|
|
String re = "WECHATPAY2-SHA256-RSA2048 ";
|
|
|
re += "mchid=" + "\"" + merchantId + "\"";
|
|
|
- Map<String, Object> map = new HashMap<>();
|
|
|
String nonceStr = RandomUtil.generateRandomString(32);
|
|
|
long timestamp = System.currentTimeMillis() / 1000;
|
|
|
- String message = buildMessage(type,url,timestamp, nonceStr, body);
|
|
|
+ String message = buildMessage(type, url, timestamp, nonceStr, body);
|
|
|
String signature = sign(message.getBytes("utf-8"));
|
|
|
re += ",nonce_str=" + "\"" + nonceStr + "\"";
|
|
|
re += ",signature=" + "\"" + signature + "\"";
|
|
@@ -100,8 +106,6 @@ public class AuthorizationService {
|
|
|
getAuthorizationVO.setTimestamp(timestamp);
|
|
|
getAuthorizationVO.setNonceStr(nonceStr);
|
|
|
return getAuthorizationVO;
|
|
|
-
|
|
|
-
|
|
|
}
|
|
|
|
|
|
String sign(byte[] message) throws Exception {
|
|
@@ -127,7 +131,7 @@ public class AuthorizationService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- String buildMessage(String type,String url,long timestamp, String nonceStr, String body) {
|
|
|
+ String buildMessage(String type, String url, long timestamp, String nonceStr, String body) {
|
|
|
|
|
|
String re = type + "\n"
|
|
|
|
|
@@ -143,4 +147,43 @@ public class AuthorizationService {
|
|
|
return re;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ String buildFrontMessage(String prepayId, long timestamp, String nonceStr) {
|
|
|
+
|
|
|
+ String re = appId + "\n"
|
|
|
+
|
|
|
+ + timestamp + "\n"
|
|
|
+
|
|
|
+ + nonceStr + "\n"
|
|
|
+
|
|
|
+ + "prepay_id=" + prepayId + "\n";
|
|
|
+
|
|
|
+ return re;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public GetAuthorizationVO getFrontAuthorization(String prepayId) throws Exception {
|
|
|
+ GetAuthorizationVO getAuthorizationVO = new GetAuthorizationVO();
|
|
|
+
|
|
|
+ String nonceStr = RandomUtil.generateRandomString(32);
|
|
|
+ long timestamp = System.currentTimeMillis() / 1000;
|
|
|
+ String message = buildFrontMessage(prepayId, timestamp, nonceStr);
|
|
|
+ String signature = sign(message.getBytes("utf-8"));
|
|
|
+ getAuthorizationVO.setSignature(signature);
|
|
|
+ getAuthorizationVO.setTimestamp(timestamp);
|
|
|
+ getAuthorizationVO.setNonceStr(nonceStr);
|
|
|
+ return getAuthorizationVO;
|
|
|
+ }
|
|
|
+
|
|
|
+ public WeixinSuccessVO decryptMessage(String associatedData, String nonce, String ciphertext) throws Exception {
|
|
|
+ Integer q= apiV3key.length();
|
|
|
+ System.out.println(q);
|
|
|
+ String key ="wL3g4tAlOFe72gAd1THRqNPQsHIVxsYi";
|
|
|
+ AesUtil aesUtil = new AesUtil(key.getBytes(StandardCharsets.UTF_8));
|
|
|
+ String d = aesUtil.decryptToString(associatedData.getBytes(StandardCharsets.UTF_8), nonce.getBytes(StandardCharsets.UTF_8),ciphertext);
|
|
|
+
|
|
|
+ WeixinSuccessVO weixinSuccessVO= JSONObject.parseObject(d,WeixinSuccessVO.class);
|
|
|
+ return weixinSuccessVO;
|
|
|
+
|
|
|
+ }
|
|
|
}
|