多用充值绑定接口

This commit is contained in:
小魔仙~
2024-12-27 18:33:12 +08:00
parent ea1d053ba7
commit 22de4635e4
7 changed files with 111 additions and 36 deletions

View File

@ -5,18 +5,24 @@ import cn.hutool.json.JSONException;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.json.ObjectMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dtflys.forest.annotation.Success;
import com.fastbee.common.core.controller.BaseController;
import com.fastbee.common.core.domain.AjaxResult;
import com.fastbee.common.utils.pay.AesUtil;
import com.fastbee.common.utils.pay.RSAUtil;
import com.fastbee.rechargecard.domain.NgMerchants;
import com.fastbee.rechargecard.domain.dto.WeChatRecharge;
import com.fastbee.rechargecard.domain.dto.WeChatRechargeBacktracking;
import com.fastbee.rechargecard.mapper.NgIrrigationControllersMapper;
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 com.fastbee.system.domain.SysRoleDept;
import com.fastbee.system.mapper.SysDeptMapper;
import com.fastbee.system.mapper.SysRoleDeptMapper;
import com.fasterxml.jackson.databind.JsonNode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@ -68,9 +74,9 @@ public class WeChatPayController extends BaseController {
private IUserConsumptionDetailsService userConsumptionDetailsService;
@Autowired
private IUserWechatPayService userWechatPayService;
@Autowired
private SysDeptMapper sysDeptMapper;
@Autowired
private NgMerchantsMapper ngMerchantsMapper;
/**
* 获取平台证书
* @return
@ -126,6 +132,7 @@ public class WeChatPayController extends BaseController {
@ApiOperation("生成订单")
@PostMapping("/order")
public AjaxResult BuildOrder(@RequestBody WeChatRecharge recharge) throws Exception {
System.out.println("生成订单");
String out_trade_no=UUID.randomUUID().toString().replace("-", "");
while(ngUserRechargeRecordsService.SelectRechargeRecodeByRechargeCode(out_trade_no) !=null)
@ -135,6 +142,9 @@ public class WeChatPayController extends BaseController {
System.err.println("随机字符串"+out_trade_no);
recharge.setRechargeCode(out_trade_no);
Map<String, String> result = userWechatPayService.CreateOrder(recharge); // 生成订单
if (result==null){
return error("未查到相应的微信支付数据");
}
System.err.println("创建订单完成:" +result);
//打印出返回前端的所有参数
// 获取键的集合
@ -161,23 +171,22 @@ public class WeChatPayController extends BaseController {
return error("系统内部订单生成失败");
}
@PostMapping("/getresult/{merchantId}")
@PostMapping("/getresult")
@ApiOperation("支付通知结果回溯")
@ResponseBody
public ResponseEntity<?> getResult(@RequestParam String merchantId,@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {//WeChatRechargeBacktracking backtracking
// 从HTTP请求头中获取微信签名和时间戳等值
System.out.println("merchantId"+merchantId);
public ResponseEntity<?> getResult(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {//WeChatRechargeBacktracking backtracking
System.out.println("微信回调开始---------");
String wechatpaySignature = request.getHeader("Wechatpay-Signature");//验签的签名值
String wechatpaySerial = request.getHeader("Wechatpay-Serial");//序列号
String wechatpayTimestamp = request.getHeader("Wechatpay-Timestamp");//时间戳
String wechatpayNonce = request.getHeader("Wechatpay-Nonce");//随机字符串
System.err.println("wechatpaySignature"+wechatpaySignature);
System.err.println("wechatpaySerial"+wechatpaySerial);
System.err.println("wechatpayTimestamp"+wechatpayTimestamp);
System.err.println("wechatpayNonce"+wechatpayNonce);
// System.err.println("wechatpaySignature"+wechatpaySignature);
// System.err.println("wechatpaySerial"+wechatpaySerial);
// System.err.println("wechatpayTimestamp"+wechatpayTimestamp);
// System.err.println("wechatpayNonce"+wechatpayNonce);
String json=jsonObject.toString();
System.out.println("微信回调报文:{"+json+"}");
// System.out.println("微信回调报文:{"+json+"}");
// 构建应答报文
Map<String, String> responseBody = new HashMap<>();
//解密jsonObject对象
@ -186,6 +195,39 @@ public class WeChatPayController extends BaseController {
System.out.println("ciphertext:"+ciphertext);
String nonce = (String) JSONUtil.getByPath(JSONUtil.parse(json), "resource.nonce");
QueryWrapper queryWrapper = new QueryWrapper();
queryWrapper.eq("wechat_public_key_id",wechatpaySerial);
NgMerchants ngMerchants = ngMerchantsMapper.selectOne(queryWrapper);
QueryWrapper queryWrappers = new QueryWrapper();
queryWrapper.eq("platform_certificate_serial_number",wechatpaySerial);
NgMerchants ngMerchants1 = ngMerchantsMapper.selectOne(queryWrappers);
boolean isVerified = false;
if(ngMerchants!=null){
String publicKey= wxPayConfig.getPublicKey(ngMerchants.getPublicKeyPath());//读取并预处理后的微信支付公钥
isVerified = verifySignature(wechatpaySignature, wechatpayTimestamp, wechatpayNonce, json, publicKey);//微信支付公钥验签方式
}else if (ngMerchants1!=null){
String publicKey=wxPayConfig.getPublicKeyByCertificat(ngMerchants1.getPlatformCertificates());//读取预处理后的平台证书
System.err.println("publickey:"+publicKey);
isVerified = verifySignatureByCertificate(wechatpaySignature, wechatpayTimestamp, wechatpayNonce, json, publicKey);
}else {
System.out.println("解密失败");
responseBody.put("code", "FAIL");
responseBody.put("message", "解密失败");
return new ResponseEntity<>(responseBody, HttpStatus.INTERNAL_SERVER_ERROR);
}
if (isVerified) {
System.out.println("签名验证成功");
} else {
System.out.println("签名验证失败");
responseBody.put("code", "FAIL");
responseBody.put("message", "签名验证失败");
// 返回500状态码以及符合要求的应答报文
return new ResponseEntity<>(responseBody, HttpStatus.INTERNAL_SERVER_ERROR);
//return error("签名验证失败");
}
/**
* 验签字符串
* 应答时间戳\n
@ -203,31 +245,14 @@ public class WeChatPayController extends BaseController {
* publicKey 微信支付公钥PEM 格式,去掉头尾并解码为二进制)
*/
//微信支付公钥验签
//String publicKey= wxPayConfig.getPublicKey(publicKeyPath);//读取并预处理后的微信支付公钥
//boolean isVerified = verifySignature(wechatpaySignature, wechatpayTimestamp, wechatpayNonce, json, publicKey);//微信支付公钥验签方式
//平台证书验签
/*String publicKey=wxPayConfig.getPublicKeyByCertificat(platformCertificatePath);//读取预处理后的平台证书
System.err.println("publickey:"+publicKey);
boolean isVerified = verifySignatureByCertificate(wechatpaySignature, wechatpayTimestamp, wechatpayNonce, json, publicKey);
if (isVerified) {
System.out.println("签名验证成功");
} else {
System.out.println("签名验证失败");
responseBody.put("code", "FAIL");
responseBody.put("message", "签名验证失败");
// 返回500状态码以及符合要求的应答报文
return new ResponseEntity<>(responseBody, HttpStatus.INTERNAL_SERVER_ERROR);
//return error("签名验证失败");
}*/
NgMerchants resultl = ngMerchants != null ? ngMerchants : ngMerchants1;
//使用apiv3key解密
String decryptData="";
try{
decryptData= new AesUtil(apiV3Key.getBytes(StandardCharsets.UTF_8)).decryptToString
decryptData= new AesUtil(resultl.getApiV3Key().getBytes(StandardCharsets.UTF_8)).decryptToString
(associated_data.getBytes(StandardCharsets.UTF_8),
nonce.getBytes(StandardCharsets.UTF_8),
ciphertext);