获取并解析平台证书以及平台证书验签以及微信支付插入信息添加用户id
This commit is contained in:
@ -1,9 +1,13 @@
|
||||
package com.fastbee.rechargecard.service.impl;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.fastbee.common.exception.ServiceException;
|
||||
import com.fastbee.common.utils.DateUtils;
|
||||
import com.fastbee.rechargecard.domain.UserRechargeCards;
|
||||
import com.fastbee.rechargecard.domain.dto.RechargecardUser;
|
||||
import com.fastbee.rechargecard.domain.dto.WeChatRecharge;
|
||||
import com.fastbee.rechargecard.mapper.UserRechargeCardsMapper;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import com.fastbee.rechargecard.mapper.NgUserRechargeRecordsMapper;
|
||||
@ -21,6 +25,8 @@ public class NgUserRechargeRecordsServiceImpl implements INgUserRechargeRecordsS
|
||||
{
|
||||
@Autowired
|
||||
private NgUserRechargeRecordsMapper ngUserRechargeRecordsMapper;
|
||||
@Autowired
|
||||
private UserRechargeCardsMapper userRechargeCardsMapper;
|
||||
|
||||
@Override
|
||||
public NgUserRechargeRecords SelectRechargeRecodeByRechargeCode(String rechargeCode) {
|
||||
@ -29,7 +35,21 @@ public class NgUserRechargeRecordsServiceImpl implements INgUserRechargeRecordsS
|
||||
|
||||
@Override
|
||||
public int insertNgUserRechargeRecordsWeChat(WeChatRecharge rechargecardUser) {
|
||||
// 根据id查询用户充值卡信息
|
||||
UserRechargeCards info = userRechargeCardsMapper.selectUserRechargeCardsByCardnumber(String.valueOf(rechargecardUser.getCardnumber()));
|
||||
if (info == null) {
|
||||
// 如果没有找到对应的记录,返回0或者一个错误码
|
||||
throw new ServiceException("卡号不存在!");
|
||||
}
|
||||
NgUserRechargeRecords ngUserRechargeRecords=new NgUserRechargeRecords();
|
||||
if(info.getUserId()!=null)
|
||||
{
|
||||
ngUserRechargeRecords.setUserId(info.getUserId());
|
||||
}
|
||||
if(info.getUserName()!=null)
|
||||
{
|
||||
ngUserRechargeRecords.setUserName(info.getUserName());
|
||||
}
|
||||
ngUserRechargeRecords.setRechargeTime(DateUtils.getNowDate());
|
||||
ngUserRechargeRecords.setBalance(rechargecardUser.getAmount());
|
||||
ngUserRechargeRecords.setAmount(rechargecardUser.getAmount());
|
||||
|
@ -121,6 +121,10 @@ public class UserRechargeCardsServiceImpl implements IUserRechargeCardsService
|
||||
throw new Exception("订单信息不存在");
|
||||
}
|
||||
ngUserRechargeRecords=userRechargeRecordsMapper.selectNgUserRechargeRecordsByRechargeCode(rechargeCode);//查询订单信息
|
||||
if(ngUserRechargeRecords.getStatus().equals(1))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
ngUserRechargeRecords.setStatus(1);//状态更改为已支付
|
||||
userRechargeRecordsMapper.updateNgUserRechargeRecords(ngUserRechargeRecords);
|
||||
|
||||
|
@ -2,6 +2,7 @@ package com.fastbee.rechargecard.service.impl;
|
||||
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.fastbee.common.utils.pay.AesUtil;
|
||||
import com.fastbee.common.utils.pay.wxPayConfig;
|
||||
import com.fastbee.rechargecard.domain.dto.WeChatRecharge;
|
||||
import com.fastbee.rechargecard.service.INgUserRechargeRecordsService;
|
||||
@ -17,10 +18,13 @@ 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;
|
||||
import java.security.PrivateKey;
|
||||
import java.security.Signature;
|
||||
import java.util.*;
|
||||
@ -34,9 +38,9 @@ public class UserWechatPayServiceImpl implements IUserWechatPayService {
|
||||
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";
|
||||
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";
|
||||
//public static String privateKeyPath = "/home/soft/hzwmiot/fastbee-admin/target/damogang_apiclient_key.pem";
|
||||
|
||||
|
||||
/** 商户API证书序列号 */
|
||||
@ -51,11 +55,13 @@ public class UserWechatPayServiceImpl implements IUserWechatPayService {
|
||||
public static String appSecret="7f591f559929a3bf2dbea4e156b08ae9";
|
||||
//微信支付公钥地址
|
||||
//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 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 notify_url="https://5f655ed0.r3.cpolar.cn/pay/getresult";//https://5f655ed0.r3.cpolar.cn
|
||||
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";
|
||||
//public static String notify_url="https://farmh5.hze2.com/prod-api/pay/getresult";
|
||||
//https://5f655ed0.r3.cpolar.cn
|
||||
@Override
|
||||
/**
|
||||
@ -86,7 +92,7 @@ public class UserWechatPayServiceImpl implements IUserWechatPayService {
|
||||
reqdata.put("notify_url",notify_url);//"\t\n" +
|
||||
reqdata.put("payer",payer);
|
||||
//String Signature=getSign(reqdata);
|
||||
Map<String,String> info=getSign(reqdata);
|
||||
Map<String,String> info=getSign(reqdata,"POST","/v3/pay/transactions/jsapi");
|
||||
String timeStamp=info.get("timeStamp");
|
||||
String nonce_str=info.get("nonce_str");
|
||||
|
||||
@ -145,11 +151,11 @@ public class UserWechatPayServiceImpl implements IUserWechatPayService {
|
||||
/**
|
||||
* 生成签名
|
||||
*/
|
||||
private Map<String,String> getSign(Map<String,Object> reqBody) throws Exception {
|
||||
private Map<String,String> getSign(Map<String,Object> reqBody,String reqMethod,String url) throws Exception {
|
||||
//获取时间戳
|
||||
|
||||
String reqMethod="POST";
|
||||
String url="/v3/pay/transactions/jsapi";
|
||||
/* String reqMethod="POST";
|
||||
String url="/v3/pay/transactions/jsapi";*/
|
||||
/*String reqMethod="GET";
|
||||
String url="/v3/refund/domestic/refunds/123123123123";*/
|
||||
String timeStamp = String.valueOf(System.currentTimeMillis() / 1000);
|
||||
@ -157,6 +163,10 @@ public class UserWechatPayServiceImpl implements IUserWechatPayService {
|
||||
String nonce_str= UUID.randomUUID().toString();
|
||||
//nonce_str="593BEC0C930BF1AFEB40B4A08C8FB242";
|
||||
String reqParam= JSONUtil.toJsonStr(reqBody);
|
||||
if(reqBody==null)
|
||||
{
|
||||
reqParam="";
|
||||
}
|
||||
|
||||
/*HTTP请求方法\n
|
||||
URL\n
|
||||
@ -165,6 +175,7 @@ public class UserWechatPayServiceImpl implements IUserWechatPayService {
|
||||
请求报文主体\n
|
||||
*/
|
||||
String signStr=reqMethod+"\n"+url+"\n"+timeStamp+"\n"+nonce_str+"\n"+reqParam +"\n";
|
||||
System.err.println("signStr:"+signStr);
|
||||
//String signStr=reqMethod+"\n"+url+"\n"+timeStamp+"\n"+nonce_str+"\n\n";
|
||||
String sign=sign(signStr.getBytes("utf-8"));
|
||||
Map<String,String> info=new HashMap<>();
|
||||
@ -249,11 +260,74 @@ public class UserWechatPayServiceImpl implements IUserWechatPayService {
|
||||
* 获取平台证书
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public String getPlatformCertificat()
|
||||
/*@Override*/
|
||||
public String 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);
|
||||
|
||||
return null;
|
||||
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 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("解密失败");
|
||||
}
|
||||
|
||||
// 解析responseString以获取openid
|
||||
return responseMap.toString(); // 这里返回的是整个响应字符串,需要自行解析出openid
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user