|
@@ -1,9 +1,19 @@
|
|
|
package com.ruoyi.web.work.service.impl;
|
|
|
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
import cn.hutool.http.HttpUtil;
|
|
|
import cn.hutool.json.JSONObject;
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.ijpay.core.IJPayHttpResponse;
|
|
|
+import com.ijpay.core.enums.RequestMethodEnum;
|
|
|
+import com.ijpay.core.kit.PayKit;
|
|
|
+import com.ijpay.core.kit.WxPayKit;
|
|
|
+import com.ijpay.wxpay.WxPayApi;
|
|
|
+import com.ijpay.wxpay.enums.WxDomainEnum;
|
|
|
+import com.ijpay.wxpay.enums.v3.TransferApiEnum;
|
|
|
+import com.ijpay.wxpay.model.v3.BatchTransferModel;
|
|
|
+import com.ijpay.wxpay.model.v3.TransferDetailInput;
|
|
|
import com.ruoyi.common.constant.CacheConstants;
|
|
|
import com.ruoyi.common.core.domain.AjaxResult;
|
|
|
import com.ruoyi.common.core.redis.RedisCache;
|
|
@@ -18,14 +28,18 @@ import com.ruoyi.web.work.domain.dto.PayCashOutDto;
|
|
|
import com.ruoyi.web.work.mapper.PayMapper;
|
|
|
import com.ruoyi.web.work.service.IPayService;
|
|
|
import com.ruoyi.web.work.service.IUserService;
|
|
|
+import com.ruoyi.web.work.wxpay.WxPayBean;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.core.env.Environment;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.math.RoundingMode;
|
|
|
import java.rmi.ServerException;
|
|
|
+import java.security.cert.X509Certificate;
|
|
|
+import java.util.Collections;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
|
|
@@ -35,6 +49,10 @@ import java.util.List;
|
|
|
*/
|
|
|
@Service
|
|
|
public class PayServiceImpl extends ServiceImpl<PayMapper, Pay> implements IPayService {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ WxPayBean wxPayV3Bean;
|
|
|
+
|
|
|
@Autowired
|
|
|
private PayMapper payMapper;
|
|
|
|
|
@@ -47,6 +65,8 @@ public class PayServiceImpl extends ServiceImpl<PayMapper, Pay> implements IPayS
|
|
|
@Autowired
|
|
|
private Environment env;
|
|
|
|
|
|
+ private String serialNo;
|
|
|
+
|
|
|
@Override
|
|
|
public List<Pay> selectList(Pay pay) {
|
|
|
return payMapper.selectList(pay);
|
|
@@ -98,12 +118,13 @@ public class PayServiceImpl extends ServiceImpl<PayMapper, Pay> implements IPayS
|
|
|
if (!userService.updateById(user)) {
|
|
|
throw new ServerException("更新账户余额失败,请联系平台");
|
|
|
}
|
|
|
+
|
|
|
return AjaxResult.success();
|
|
|
}
|
|
|
|
|
|
- @Transactional
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
@Override
|
|
|
- public AjaxResult audit(PayAuditDto dto) throws ServerException {
|
|
|
+ public AjaxResult audit(PayAuditDto dto) throws Exception {
|
|
|
Pay pay = getById(dto.getId());
|
|
|
if (pay == null || pay.getType() != 2) {
|
|
|
return AjaxResult.error("提现信息不存在");
|
|
@@ -117,14 +138,29 @@ public class PayServiceImpl extends ServiceImpl<PayMapper, Pay> implements IPayS
|
|
|
if (!updateById(pay)) {
|
|
|
throw new ServerException("提现失败,请联系平台");
|
|
|
}
|
|
|
+ User user = userService.getById(pay.getUserId());
|
|
|
//提现驳回需要退回到用户账户余额
|
|
|
if (dto.getState() == 2) {
|
|
|
- User user = userService.getById(pay.getUserId());
|
|
|
user.setMoney(user.getMoney().add(pay.getMoney()));
|
|
|
if (!userService.updateById(user)) {
|
|
|
throw new ServerException("更新用户账户余额失败,请联系平台");
|
|
|
}
|
|
|
}
|
|
|
+ //转账到微信零钱
|
|
|
+ if (pay.getWay() == 1 && pay.getState() == 1) {
|
|
|
+ BatchTransferModel batchTransferModel = new BatchTransferModel().setAppid(wxPayV3Bean.getAppId()).setOut_batch_no(PayKit.generateStr()).setBatch_name("账户余额提现到零钱").setBatch_remark("账户余额提现到零钱").setTotal_amount(pay.getMoney().multiply(new BigDecimal("100")).intValue()).setTotal_num(1).setTransfer_detail_list(Collections.singletonList(new TransferDetailInput().setOut_detail_no(PayKit.generateStr()).setTransfer_amount(pay.getMoney().multiply(new BigDecimal("100")).intValue()).setTransfer_remark("账户余额提现到零钱").setOpenid(user.getOpenId())));
|
|
|
+ IJPayHttpResponse response = WxPayApi.v3(RequestMethodEnum.POST, WxDomainEnum.CHINA.toString(), TransferApiEnum.TRANSFER_BATCHES.toString(), wxPayV3Bean.getMchId(), getSerialNumber(), null, wxPayV3Bean.getKeyPath(), JSONUtil.toJsonStr(batchTransferModel));
|
|
|
+ String body = response.getBody();
|
|
|
+ // 根据证书序列号查询对应的证书来验证签名结果
|
|
|
+ boolean verifySignature = WxPayKit.verifySignature(response, wxPayV3Bean.getPlatformCertPath());
|
|
|
+ if (response.getStatus() == 200 && verifySignature) {
|
|
|
+ sendMessage(pay);
|
|
|
+ return AjaxResult.success("转账到微信零钱成功");
|
|
|
+ } else {
|
|
|
+ com.alibaba.fastjson2.JSONObject jsonObject =com.alibaba.fastjson2.JSONObject.parseObject(body);
|
|
|
+ throw new ServerException("转账到微信零钱失败:"+jsonObject.getString("message"));
|
|
|
+ }
|
|
|
+ }
|
|
|
sendMessage(pay);
|
|
|
return AjaxResult.success();
|
|
|
}
|
|
@@ -161,4 +197,14 @@ public class PayServiceImpl extends ServiceImpl<PayMapper, Pay> implements IPayS
|
|
|
System.out.println("body:" + body);
|
|
|
System.out.println("小程序订阅消息:" + result);
|
|
|
}
|
|
|
+
|
|
|
+ private String getSerialNumber() {
|
|
|
+ if (StrUtil.isEmpty(serialNo)) {
|
|
|
+ X509Certificate certificate = PayKit.getCertificate(wxPayV3Bean.getCertPath());
|
|
|
+ if (certificate != null) {
|
|
|
+ serialNo = certificate.getSerialNumber().toString(16).toUpperCase();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return serialNo;
|
|
|
+ }
|
|
|
}
|