修改获取平台证书接口;创建设备操作日志表
This commit is contained in:
@ -0,0 +1,11 @@
|
||||
package com.fastbee.rechargecard.domain.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class WeChatPlatformCertificate {
|
||||
private String mchId;//商户号
|
||||
private String privateKey;//商户api证书私钥
|
||||
private String serial_no;//商户api证书序列号
|
||||
private String apiV3Key;//商户apiv3Key
|
||||
}
|
@ -25,7 +25,7 @@ public interface IUserWechatPayService {
|
||||
/**
|
||||
* 获取平台证书
|
||||
*/
|
||||
//public Map<String,Object> getPlatformCertificat();
|
||||
public Map<String,Object> getPlatformCertificat(String mchId,String privateKey,String serial_no,String apiV3Key);
|
||||
|
||||
|
||||
}
|
||||
|
@ -8,9 +8,6 @@ import com.fastbee.common.utils.pay.wxPayConfig;
|
||||
import com.fastbee.rechargecard.domain.NgMerchants;
|
||||
import com.fastbee.rechargecard.domain.dto.WeChatRecharge;
|
||||
import com.fastbee.rechargecard.mapper.NgMerchantsMapper;
|
||||
import com.fastbee.rechargecard.service.INgUserRechargeRecordsService;
|
||||
import com.fastbee.rechargecard.service.IUserConsumptionDetailsService;
|
||||
import com.fastbee.rechargecard.service.IUserRechargeCardsService;
|
||||
import com.fastbee.rechargecard.service.IUserWechatPayService;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
@ -21,10 +18,7 @@ import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
@ -35,38 +29,43 @@ import java.util.*;
|
||||
@Service
|
||||
public class UserWechatPayServiceImpl implements IUserWechatPayService {
|
||||
|
||||
@Autowired
|
||||
private NgMerchantsMapper ngMerchantsMapper;
|
||||
|
||||
/** 商户号 */
|
||||
//public static String mchId = "1531795301";
|
||||
public static String mchId = "1503198881";
|
||||
//public static String mchId = "1503198881";
|
||||
/** 商户API私钥文件路径 */
|
||||
//public static String privateKeyPath = "fastbee-common/src/main/java/com/fastbee/common/utils/pay/apiclient_key.pem";
|
||||
// public static String privateKeyPath = "fastbee-common/src/main/java/com/fastbee/common/utils/pay/damogang_apiclient_key.pem";
|
||||
//TODO 生产环境私钥路径
|
||||
//public static String privateKeyPath = "/home/soft/hzwmiot/fastbee-admin/target/damogang_apiclient_key.pem";
|
||||
@Autowired
|
||||
private NgMerchantsMapper ngMerchantsMapper;
|
||||
|
||||
/** 商户API证书序列号 */
|
||||
//public static String serial_no = "3075B63EF52666EDC3EAFC5D4FB35C02CE123A9C";
|
||||
public static String serial_no = "7A55F5763A002C749F1AB10E1D52DE6688DCDDC0";
|
||||
// public static String serial_no = "7A55F5763A002C749F1AB10E1D52DE6688DCDDC0";
|
||||
/** 商户APIV3密钥 */
|
||||
//public static String apiV3Key = "e85a203e8ca146102f5cd7ecff912580";
|
||||
public static String apiV3Key = "damogangguanqunongcunyunshuizhex";
|
||||
//微信小程序appid
|
||||
public static String appId="wx308612d2a8423311";
|
||||
//微信小程序appSecret
|
||||
public static String appSecret="7f591f559929a3bf2dbea4e156b08ae9";
|
||||
// public static String apiV3Key = "damogangguanqunongcunyunshuizhex";
|
||||
|
||||
|
||||
|
||||
//微信支付公钥地址
|
||||
// public static String publicKeyPath="fastbee-common/src/main/java/com/fastbee/common/utils/pay/wechat_public_key.pem";
|
||||
//public static String publicKeyPath="fastbee-common/src/main/java/com/fastbee/common/utils/pay/wechat_public_key.pem";
|
||||
//平台证书地址
|
||||
public static String platformCertificatePath="fastbee-common/src/main/java/com/fastbee/common/utils/pay/damogang_platformCertificate.pem";
|
||||
// public static String platformCertificatePath="fastbee-common/src/main/java/com/fastbee/common/utils/pay/damogang_platformCertificate.pem";
|
||||
|
||||
//支付结果回调地址
|
||||
// public static String notify_url="https://3ffb1c5f.r3.cpolar.cn/pay/getresult";//https://3ffb1c5f.r3.cpolar.cn
|
||||
//TODO 生产环境支付结果异步通知地址
|
||||
public static String notify_url="https://farmh5.hze2.com/prod-api/pay/getresult";
|
||||
//https://5f655ed0.r3.cpolar.cn
|
||||
//微信小程序appid
|
||||
public static String appId="wx308612d2a8423311";
|
||||
//微信小程序appSecret
|
||||
public static String appSecret="7f591f559929a3bf2dbea4e156b08ae9";
|
||||
|
||||
@Override
|
||||
/**
|
||||
* 创建订单,获取prepay_id和paySign
|
||||
@ -166,6 +165,125 @@ public class UserWechatPayServiceImpl implements IUserWechatPayService {
|
||||
httpClient.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取openId
|
||||
* @param code
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Map<String, Object> GetOpenId(String code)
|
||||
{
|
||||
System.out.println(code);
|
||||
//String url = String.format("https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code", appId, appSecret, code);
|
||||
//String url="https://api.weixin.qq.com/sns/oauth2/access_token?appid="+appId+"&secret="+appSecret+"&code="+code+"&grant_type=authorization_code";
|
||||
String url="https://api.weixin.qq.com/sns/jscode2session?appid="+appId+"&secret="+appSecret+"&js_code="+code+"&grant_type=authorization_code";
|
||||
//RestTemplate restTemplate = new RestTemplate();
|
||||
//Map<String, Object> response = restTemplate.getForObject(url, Map.class);
|
||||
|
||||
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
||||
HttpGet httpGet = new HttpGet(url);
|
||||
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
|
||||
String responseString = EntityUtils.toString(response.getEntity());
|
||||
Map<String, Object> responseMap = JSONUtil.toBean(responseString, Map.class);
|
||||
//打印出返回前端的所有参数
|
||||
// 获取键的集合
|
||||
Set<String> keySet = responseMap.keySet();
|
||||
// 遍历键集合
|
||||
for (String key : keySet) {
|
||||
System.out.println(key + ": " + responseMap.get(key));
|
||||
}
|
||||
// 解析responseString以获取openid
|
||||
return responseMap; // 这里返回的是整个响应字符串,需要自行解析出openid
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取平台证书
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Map<String,Object> getPlatformCertificat(String mchId,String privateKey,String serial_no,String apiV3Key)
|
||||
{
|
||||
String url="https://api.mch.weixin.qq.com/v3/certificates";
|
||||
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
||||
HttpGet httpGet = new HttpGet(url);
|
||||
//生成签名
|
||||
Map<String,String> signResult=getSign(null,"GET","/v3/certificates",privateKey);
|
||||
Set<String> keySet2 = signResult.keySet();
|
||||
// 遍历键集合
|
||||
for (String key : keySet2) {
|
||||
System.err.println(key + ": " + signResult.get(key));
|
||||
}
|
||||
String sign=signResult.get("sign").toString();
|
||||
String nonce_str=signResult.get("nonce_str").toString();
|
||||
String timeStamp=signResult.get("timeStamp").toString();
|
||||
String Authorization="WECHATPAY2-SHA256-RSA2048 mchid=\""+mchId+"\",nonce_str=\""+nonce_str+"\",signature=\""+sign+"\",timestamp=\""+timeStamp+"\",serial_no=\""+serial_no+"\"";
|
||||
System.err.println(Authorization);
|
||||
httpGet.setHeader("Accept", "application/json");
|
||||
httpGet.setHeader("Authorization",Authorization);
|
||||
|
||||
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
|
||||
String responseString = EntityUtils.toString(response.getEntity());
|
||||
System.err.println(responseString);
|
||||
Map<String, Object> responseMap = JSONUtil.toBean(responseString, Map.class);
|
||||
// 使用Optional来避免null检查
|
||||
Optional<?> optionalObj = Optional.ofNullable(responseMap.get("data"));
|
||||
System.err.println(optionalObj);
|
||||
// 链式调用map和flatMap方法来安全地转换和获取Map
|
||||
Map<?, ?> map = optionalObj
|
||||
.filter(List.class::isInstance) // 确保obj是List类型
|
||||
.map(List.class::cast) // 将obj转换为List
|
||||
.filter(list -> !list.isEmpty() && list.get(0) instanceof Map) // 确保List不为空,并且第一个元素是Map类型
|
||||
.map(list -> (Map<?, ?>) list.get(0)) // 将List中的第一个元素转换为Map
|
||||
.orElse(null); // 如果任何检查失败,则返回null
|
||||
System.err.println(map);
|
||||
//解密jsonObject对象
|
||||
Map<String,Object> encrypt_certificate= (Map<String, Object>) map.get("encrypt_certificate");
|
||||
System.err.println("111");
|
||||
System.err.println(encrypt_certificate);
|
||||
String serial=map.get("serial_no").toString();
|
||||
System.err.println("平台证书序列号:"+serial);
|
||||
String associated_data=encrypt_certificate.get("associated_data").toString();//加密证书的附加数据,固定为“certificate"
|
||||
String ciphertext=encrypt_certificate.get("ciphertext").toString();//加密证书的随机串
|
||||
String nonce=encrypt_certificate.get("nonce").toString();//加密后的证书内容
|
||||
System.err.println("ciphertext:"+ciphertext);
|
||||
System.err.println("nonce:"+nonce);
|
||||
System.err.println("associated_data:"+associated_data);
|
||||
//使用apiv3key解密
|
||||
String decryptData="";
|
||||
try{
|
||||
decryptData= new AesUtil(apiV3Key.getBytes(StandardCharsets.UTF_8)).decryptToString
|
||||
(associated_data.getBytes(StandardCharsets.UTF_8),
|
||||
nonce.getBytes(StandardCharsets.UTF_8),
|
||||
ciphertext);
|
||||
System.out.println("解密成功:\n"+decryptData);
|
||||
}catch (Exception e)
|
||||
{
|
||||
System.out.println("解密失败");
|
||||
}
|
||||
Map<String,Object> result=new HashMap<>();
|
||||
result.put("platformCertificate",responseMap.toString());
|
||||
result.put("serial",serial);
|
||||
Set<String> keys=result.keySet();
|
||||
for(String key : keys)
|
||||
{
|
||||
System.out.println(key+":"+result.get(key));
|
||||
}
|
||||
// 解析responseString以获取openid
|
||||
return result; // 这里返回的是整个响应字符串,需要自行解析出openid
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成签名
|
||||
*/
|
||||
@ -240,122 +358,6 @@ public class UserWechatPayServiceImpl implements IUserWechatPayService {
|
||||
return Base64.getEncoder().encodeToString(sign.sign());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取openId
|
||||
* @param code
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Map<String, Object> GetOpenId(String code)
|
||||
{
|
||||
System.out.println(code);
|
||||
//String url = String.format("https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code", appId, appSecret, code);
|
||||
//String url="https://api.weixin.qq.com/sns/oauth2/access_token?appid="+appId+"&secret="+appSecret+"&code="+code+"&grant_type=authorization_code";
|
||||
String url="https://api.weixin.qq.com/sns/jscode2session?appid="+appId+"&secret="+appSecret+"&js_code="+code+"&grant_type=authorization_code";
|
||||
//RestTemplate restTemplate = new RestTemplate();
|
||||
//Map<String, Object> response = restTemplate.getForObject(url, Map.class);
|
||||
|
||||
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
||||
HttpGet httpGet = new HttpGet(url);
|
||||
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
|
||||
String responseString = EntityUtils.toString(response.getEntity());
|
||||
Map<String, Object> responseMap = JSONUtil.toBean(responseString, Map.class);
|
||||
//打印出返回前端的所有参数
|
||||
// 获取键的集合
|
||||
Set<String> keySet = responseMap.keySet();
|
||||
// 遍历键集合
|
||||
for (String key : keySet) {
|
||||
System.out.println(key + ": " + responseMap.get(key));
|
||||
}
|
||||
// 解析responseString以获取openid
|
||||
return responseMap; // 这里返回的是整个响应字符串,需要自行解析出openid
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取平台证书
|
||||
* @return
|
||||
*/
|
||||
/*@Override*/
|
||||
/*public Map<String,Object> getPlatformCertificat()
|
||||
{
|
||||
String url="https://api.mch.weixin.qq.com/v3/certificates";
|
||||
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
||||
HttpGet httpGet = new HttpGet(url);
|
||||
//生成签名
|
||||
Map<String,String> signResult=getSign(null,"GET","/v3/certificates");
|
||||
Set<String> keySet2 = signResult.keySet();
|
||||
// 遍历键集合
|
||||
for (String key : keySet2) {
|
||||
System.err.println(key + ": " + signResult.get(key));
|
||||
}
|
||||
String sign=signResult.get("sign").toString();
|
||||
String nonce_str=signResult.get("nonce_str").toString();
|
||||
String timeStamp=signResult.get("timeStamp").toString();
|
||||
String Authorization="WECHATPAY2-SHA256-RSA2048 mchid=\""+mchId+"\",nonce_str=\""+nonce_str+"\",signature=\""+sign+"\",timestamp=\""+timeStamp+"\",serial_no=\""+serial_no+"\"";
|
||||
System.err.println(Authorization);
|
||||
httpGet.setHeader("Accept", "application/json");
|
||||
httpGet.setHeader("Authorization",Authorization);
|
||||
|
||||
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
|
||||
String responseString = EntityUtils.toString(response.getEntity());
|
||||
System.err.println(responseString);
|
||||
Map<String, Object> responseMap = JSONUtil.toBean(responseString, Map.class);
|
||||
// 使用Optional来避免null检查
|
||||
Optional<?> optionalObj = Optional.ofNullable(responseMap.get("data"));
|
||||
System.err.println(optionalObj);
|
||||
// 链式调用map和flatMap方法来安全地转换和获取Map
|
||||
Map<?, ?> map = optionalObj
|
||||
.filter(List.class::isInstance) // 确保obj是List类型
|
||||
.map(List.class::cast) // 将obj转换为List
|
||||
.filter(list -> !list.isEmpty() && list.get(0) instanceof Map) // 确保List不为空,并且第一个元素是Map类型
|
||||
.map(list -> (Map<?, ?>) list.get(0)) // 将List中的第一个元素转换为Map
|
||||
.orElse(null); // 如果任何检查失败,则返回null
|
||||
System.err.println(map);
|
||||
//解密jsonObject对象
|
||||
Map<String,Object> encrypt_certificate= (Map<String, Object>) map.get("encrypt_certificate");
|
||||
System.err.println("111");
|
||||
System.err.println(encrypt_certificate);
|
||||
String serial=map.get("serial_no").toString();
|
||||
System.err.println("平台证书序列号:"+serial);
|
||||
String associated_data=encrypt_certificate.get("associated_data").toString();//加密证书的附加数据,固定为“certificate"
|
||||
String ciphertext=encrypt_certificate.get("ciphertext").toString();//加密证书的随机串
|
||||
String nonce=encrypt_certificate.get("nonce").toString();//加密后的证书内容
|
||||
System.err.println("ciphertext:"+ciphertext);
|
||||
System.err.println("nonce:"+nonce);
|
||||
System.err.println("associated_data:"+associated_data);
|
||||
//使用apiv3key解密
|
||||
String decryptData="";
|
||||
try{
|
||||
decryptData= new AesUtil(apiV3Key.getBytes(StandardCharsets.UTF_8)).decryptToString
|
||||
(associated_data.getBytes(StandardCharsets.UTF_8),
|
||||
nonce.getBytes(StandardCharsets.UTF_8),
|
||||
ciphertext);
|
||||
System.out.println("解密成功:\n"+decryptData);
|
||||
}catch (Exception e)
|
||||
{
|
||||
System.out.println("解密失败");
|
||||
}
|
||||
Map<String,Object> result=new HashMap<>();
|
||||
result.put("platformCertificate",responseMap.toString());
|
||||
result.put("serial",serial);
|
||||
Set<String> keys=result.keySet();
|
||||
for(String key : keys)
|
||||
{
|
||||
System.out.println(key+":"+result.get(key));
|
||||
}
|
||||
// 解析responseString以获取openid
|
||||
return result; // 这里返回的是整个响应字符串,需要自行解析出openid
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}*/
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user