多用充值绑定接口

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);

View File

@ -59,4 +59,14 @@ private static final long serialVersionUID = 1L;
@ApiModelProperty("微信平台证书")
private String platformCertificates;
/** 微信支付公钥id */
@Excel(name = "微信支付公钥id")
@ApiModelProperty("微信支付公钥id")
private Long wechatPublicKeyId;
/** 平台证书序列号 */
@Excel(name = "平台证书序列号")
@ApiModelProperty("平台证书序列号")
private String platformCertificateSerialNumber;
}

View File

@ -24,4 +24,8 @@ public class NgMerchantsDto {
private String platformCertificates;
private String deptName;
private Long wechatPublicKeyId;
private String platformCertificateSerialNumber;
}

View File

@ -15,4 +15,7 @@ public class WeChatRecharge {
private String openId;//openId
private BigDecimal total;//amount中的total
private String currency;//amount中的currency
private Long deptId;
private Long wechatPublicKeyId;
private String platformCertificateSerialNumber;
}

View File

@ -47,6 +47,9 @@ public class NgMerchantsServiceImpl implements INgMerchantsService
NgMerchantsDto ngMerchantsDto = new NgMerchantsDto();
NgMerchants ngMerchants = ngMerchantsMapper.selectNgMerchantsById(id);
SysDept sysDept = sysDeptMapper.selectDeptById(ngMerchants.getDeptId());
ngMerchantsDto.setWechatPublicKeyId(ngMerchants.getWechatPublicKeyId());
ngMerchantsDto.setPlatformCertificateSerialNumber(
ngMerchants.getPlatformCertificateSerialNumber());
ngMerchantsDto.setId(ngMerchants.getId());
ngMerchantsDto.setApiV3Key(ngMerchants.getApiV3Key());
ngMerchantsDto.setMchId(ngMerchants.getMchId());
@ -78,6 +81,9 @@ public class NgMerchantsServiceImpl implements INgMerchantsService
.map(ngMerchants1 -> {
NgMerchantsDto ngMerchantsDto = new NgMerchantsDto();
ngMerchantsDto.setId(ngMerchants1.getId());
ngMerchantsDto.setWechatPublicKeyId(ngMerchants1.getWechatPublicKeyId());
ngMerchantsDto.setPlatformCertificateSerialNumber(
ngMerchants1.getPlatformCertificateSerialNumber());
ngMerchantsDto.setApiV3Key(ngMerchants1.getApiV3Key());
ngMerchantsDto.setMchId(ngMerchants1.getMchId());
ngMerchantsDto.setPrivateKeyPath(ngMerchants1.getPrivateKeyPath());

View File

@ -2,9 +2,12 @@ package com.fastbee.rechargecard.service.impl;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fastbee.common.utils.pay.AesUtil;
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;
@ -41,7 +44,8 @@ public class UserWechatPayServiceImpl implements IUserWechatPayService {
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";
@ -71,6 +75,12 @@ public class UserWechatPayServiceImpl implements IUserWechatPayService {
* @throws Exception
*/
public Map<String, String> CreateOrder(WeChatRecharge recharge) throws Exception {
QueryWrapper queryWrapper=new QueryWrapper();
queryWrapper.eq("dept_id",recharge.getDeptId());
NgMerchants ngMerchants = ngMerchantsMapper.selectOne(queryWrapper);
if (ngMerchants==null){
return null;
}
System.out.println("CreateOrder");
//请求URL
HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi");
@ -86,7 +96,8 @@ public class UserWechatPayServiceImpl implements IUserWechatPayService {
reqdata.put("amount",amount);
reqdata.put("appid",appId);
reqdata.put("mchid",mchId);
// reqdata.put("mchid",mchId);
reqdata.put("mchid",ngMerchants.getMchId());
reqdata.put("description",recharge.getCardnumber()+recharge.getAmount().toString());
reqdata.put("out_trade_no",recharge.getRechargeCode());
reqdata.put("notify_url",notify_url+"/"+mchId);//"\t\n" +
@ -97,13 +108,19 @@ public class UserWechatPayServiceImpl implements IUserWechatPayService {
String nonce_str=info.get("nonce_str");
result.put("timeStamp",timeStamp);
result.put("mchid",mchId);
// result.put("mchid",mchId);
reqdata.put("mchid",ngMerchants.getMchId());
result.put("signType","RSA");
result.put("nonceStr",nonce_str);
result.put("partnerid",mchId);
// result.put("partnerid",mchId);
reqdata.put("mchid",ngMerchants.getMchId());
String Signature=info.get("sign");
String Authorization="WECHATPAY2-SHA256-RSA2048 mchid=\""+mchId+"\",nonce_str=\""+nonce_str+"\",signature=\""+Signature+"\",timestamp=\""+timeStamp+"\",serial_no=\""+serial_no+"\"";
String Authorization="WECHATPAY2-SHA256-RSA2048 mchid=\""+ngMerchants.getMchId()+
"\",nonce_str=\""+nonce_str+"\",signature=\""+Signature+"\",timestamp=\""+timeStamp+"\",serial_no=\""
+
// serial_no
ngMerchants.getSerialNo()+"\"";
StringEntity entity = new StringEntity(JSONUtil.toJsonStr(reqdata), "utf-8");
entity.setContentType("application/json");

View File

@ -13,10 +13,12 @@
<result property="publicKeyPath" column="public_key_path" />
<result property="deptId" column="dept_id" />
<result property="platformCertificates" column="platform_certificates" />
<result property="wechatPublicKeyId" column="wechat_public_key_id" />
<result property="platformCertificateSerialNumber" column="platform_certificate_serial_number" />
</resultMap>
<sql id="selectNgMerchantsVo">
select id, mch_id, private_key_path, serial_no, api_v3_key, public_key_path, dept_id, platform_certificates from ng_merchants
select id, mch_id, private_key_path, serial_no, api_v3_key, public_key_path, dept_id, platform_certificates, wechat_public_key_id, platform_certificate_serial_number from ng_merchants
</sql>
<select id="selectNgMerchantsList" parameterType="NgMerchants" resultMap="NgMerchantsResult">
@ -29,6 +31,8 @@
<if test="publicKeyPath != null and publicKeyPath != ''"> and public_key_path = #{publicKeyPath}</if>
<if test="deptId != null "> and dept_id = #{deptId}</if>
<if test="platformCertificates != null and platformCertificates != ''"> and platform_certificates = #{platformCertificates}</if>
<if test="wechatPublicKeyId != null "> and wechat_public_key_id = #{wechatPublicKeyId}</if>
<if test="platformCertificateSerialNumber != null and platformCertificateSerialNumber != ''"> and platform_certificate_serial_number = #{platformCertificateSerialNumber}</if>
</where>
</select>
@ -47,6 +51,8 @@
<if test="publicKeyPath != null">public_key_path,</if>
<if test="deptId != null">dept_id,</if>
<if test="platformCertificates != null">platform_certificates,</if>
<if test="wechatPublicKeyId != null">wechat_public_key_id,</if>
<if test="platformCertificateSerialNumber != null">platform_certificate_serial_number,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="mchId != null">#{mchId},</if>
@ -56,6 +62,8 @@
<if test="publicKeyPath != null">#{publicKeyPath},</if>
<if test="deptId != null">#{deptId},</if>
<if test="platformCertificates != null">#{platformCertificates},</if>
<if test="wechatPublicKeyId != null">#{wechatPublicKeyId},</if>
<if test="platformCertificateSerialNumber != null">#{platformCertificateSerialNumber},</if>
</trim>
</insert>
@ -69,6 +77,8 @@
<if test="publicKeyPath != null">public_key_path = #{publicKeyPath},</if>
<if test="deptId != null">dept_id = #{deptId},</if>
<if test="platformCertificates != null">platform_certificates = #{platformCertificates},</if>
<if test="wechatPublicKeyId != null">wechat_public_key_id = #{wechatPublicKeyId},</if>
<if test="platformCertificateSerialNumber != null">platform_certificate_serial_number = #{platformCertificateSerialNumber},</if>
</trim>
where id = #{id}
</update>