|
@@ -0,0 +1,148 @@
|
|
|
+package cn.cslg.permission.service.weixinpay;
|
|
|
+
|
|
|
+import cn.cslg.permission.common.model.weixinpay.GetAuthorizationVO;
|
|
|
+import cn.cslg.permission.common.utils.RandomUtil;
|
|
|
+
|
|
|
+import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
|
|
|
+import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
|
|
+import org.bouncycastle.openssl.PEMParser;
|
|
|
+import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+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;
|
|
|
+import java.util.Base64;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class AuthorizationService {
|
|
|
+ /**
|
|
|
+ * 商户号
|
|
|
+ */
|
|
|
+
|
|
|
+ public static String merchantId = "1718408246";
|
|
|
+ public static String appId = "wx18ac47eed86e5976";
|
|
|
+ /**
|
|
|
+ * 商户API私钥路径
|
|
|
+ */
|
|
|
+ @Value("${Keypath}")
|
|
|
+ public String privateKeyPath ;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 商户证书序列号
|
|
|
+ */
|
|
|
+
|
|
|
+ public static String merchantSerialNumber = "3BBC0C2DA1D49F62CEAC3C3CC0472C1E2466EB63";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 商户APIV3密钥
|
|
|
+ */
|
|
|
+
|
|
|
+ public static String apiV3key = "wL3g4tAlOFe72gAd1THRqNPQsHIVxsYi";
|
|
|
+
|
|
|
+
|
|
|
+ public GetAuthorizationVO getAuthorization(String type, String url, String body) throws Exception {
|
|
|
+ GetAuthorizationVO getAuthorizationVO = new GetAuthorizationVO();
|
|
|
+ String re = "WECHATPAY2-SHA256-RSA2048 ";
|
|
|
+ re += "mchid=" + "\"" + merchantId + "\"";
|
|
|
+ String nonceStr = RandomUtil.generateRandomString(32);
|
|
|
+ long timestamp = System.currentTimeMillis() / 1000;
|
|
|
+ String message = buildMessage(type, url, timestamp, nonceStr, body);
|
|
|
+ String signature = sign(message.getBytes("utf-8"));
|
|
|
+ re += ",nonce_str=" + "\"" + nonceStr + "\"";
|
|
|
+ re += ",signature=" + "\"" + signature + "\"";
|
|
|
+ re += ",timestamp=" + "\"" + timestamp + "\"";
|
|
|
+ re += ",serial_no=" + "\"" + merchantSerialNumber + "\"";
|
|
|
+ getAuthorizationVO.setAuthorization(re);
|
|
|
+ getAuthorizationVO.setSignature(signature);
|
|
|
+ getAuthorizationVO.setTimestamp(timestamp);
|
|
|
+ getAuthorizationVO.setNonceStr(nonceStr);
|
|
|
+ return getAuthorizationVO;
|
|
|
+ }
|
|
|
+
|
|
|
+ String sign(byte[] message) throws Exception {
|
|
|
+
|
|
|
+ Signature sign = Signature.getInstance("SHA256withRSA");
|
|
|
+ // 添加Bouncy Castle作为安全提供者
|
|
|
+ Security.addProvider(new BouncyCastleProvider());
|
|
|
+
|
|
|
+
|
|
|
+ // 使用PEMParser读取PEM文件
|
|
|
+ try (FileReader fileReader = new FileReader(privateKeyPath);
|
|
|
+ PEMParser pemParser = new PEMParser(fileReader)) {
|
|
|
+
|
|
|
+ // 使用JcaPEMKeyConverter将PEM对象转换为PrivateKey
|
|
|
+ JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC");
|
|
|
+ PrivateKey privateKey = converter.getPrivateKey((PrivateKeyInfo) pemParser.readObject());
|
|
|
+ sign.initSign(privateKey);
|
|
|
+
|
|
|
+ sign.update(message);
|
|
|
+
|
|
|
+ return Base64.getEncoder().encodeToString(sign.sign());
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ String buildMessage(String type, String url, long timestamp, String nonceStr, String body) {
|
|
|
+
|
|
|
+ String re = type + "\n"
|
|
|
+
|
|
|
+ + url + "\n"
|
|
|
+
|
|
|
+ + timestamp + "\n"
|
|
|
+
|
|
|
+ + nonceStr + "\n";
|
|
|
+ if (body != null) {
|
|
|
+ re += body;
|
|
|
+ }
|
|
|
+ re += "\n";
|
|
|
+ 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;
|
|
|
+//
|
|
|
+// }
|
|
|
+}
|