|
|
|
@ -1,12 +1,22 @@ |
|
|
|
package com.bnyer.img.service.impl; |
|
|
|
|
|
|
|
import cn.hutool.core.util.IdUtil; |
|
|
|
import com.alibaba.fastjson.JSON; |
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
import com.alipay.api.AlipayApiException; |
|
|
|
import com.alipay.api.AlipayClient; |
|
|
|
import com.alipay.api.CertAlipayRequest; |
|
|
|
import com.alipay.api.DefaultAlipayClient; |
|
|
|
import com.alipay.api.domain.AlipayFundTransUniTransferModel; |
|
|
|
import com.alipay.api.domain.Participant; |
|
|
|
import com.alipay.api.request.AlipayFundTransUniTransferRequest; |
|
|
|
import com.alipay.api.response.AlipayFundTransUniTransferResponse; |
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
|
|
import com.bnyer.common.core.exception.ServiceException; |
|
|
|
import com.bnyer.common.core.utils.DesensitizedUtils; |
|
|
|
import com.bnyer.common.core.utils.Sm4Util; |
|
|
|
import com.bnyer.common.core.utils.StringUtils; |
|
|
|
import com.bnyer.img.config.AlipayConfig; |
|
|
|
import com.bnyer.img.constants.TiktokConstant; |
|
|
|
import com.bnyer.img.constants.WithdrawConstant; |
|
|
|
import com.bnyer.img.domain.Creator; |
|
|
|
@ -15,13 +25,16 @@ import com.bnyer.img.dto.VerifyDto; |
|
|
|
import com.bnyer.img.dto.WithdrawPageDto; |
|
|
|
import com.bnyer.img.mapper.CreatorMapper; |
|
|
|
import com.bnyer.img.mapper.WithdrawLogMapper; |
|
|
|
import com.bnyer.img.service.CreatorAccountService; |
|
|
|
import com.bnyer.img.service.WithdrawLogService; |
|
|
|
import com.bnyer.img.vo.CreatorAccountVo; |
|
|
|
import com.bnyer.img.vo.WithdrawLogVo; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
|
|
|
|
import javax.annotation.Resource; |
|
|
|
import java.math.BigDecimal; |
|
|
|
import java.util.Date; |
|
|
|
import java.util.List; |
|
|
|
@ -40,6 +53,13 @@ public class WithdrawLogServiceImpl implements WithdrawLogService { |
|
|
|
@Autowired |
|
|
|
private CreatorMapper creatorMapper; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private CreatorAccountService creatorAccountService; |
|
|
|
|
|
|
|
@Resource |
|
|
|
private AlipayConfig alipayConfig; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Transactional(rollbackFor = Exception.class) |
|
|
|
@ -127,6 +147,69 @@ public class WithdrawLogServiceImpl implements WithdrawLogService { |
|
|
|
return withdrawLogMapper.queryFrontDetails(orderId); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 支付宝打款到指定支付宝账号 |
|
|
|
* @param orderId 订单id |
|
|
|
* @param amt 金额 |
|
|
|
* @param aliPayNo 艺术家支付宝账号 |
|
|
|
* @param name 姓名 |
|
|
|
* @return - |
|
|
|
*/ |
|
|
|
private AlipayFundTransUniTransferResponse trans(String orderId,String aliPayNo,String name,BigDecimal amt) throws AlipayApiException { |
|
|
|
//构造client
|
|
|
|
CertAlipayRequest certAlipayRequest = new CertAlipayRequest(); |
|
|
|
//设置网关地址
|
|
|
|
certAlipayRequest.setServerUrl(alipayConfig.getGatewayUrl()); |
|
|
|
//设置应用Id
|
|
|
|
certAlipayRequest.setAppId(alipayConfig.getAppId()); |
|
|
|
//设置应用私钥
|
|
|
|
certAlipayRequest.setPrivateKey(alipayConfig.getPrivateKey()); |
|
|
|
//设置请求格式,固定值json
|
|
|
|
certAlipayRequest.setFormat("json"); |
|
|
|
//设置字符集
|
|
|
|
certAlipayRequest.setCharset("UTF8"); |
|
|
|
//设置签名类型
|
|
|
|
certAlipayRequest.setSignType("RSA2"); |
|
|
|
//设置应用公钥证书路径
|
|
|
|
certAlipayRequest.setCertPath(alipayConfig.getCertPath()); |
|
|
|
//设置支付宝公钥证书路径
|
|
|
|
certAlipayRequest.setAlipayPublicCertPath(alipayConfig.getAlipayPublicCertPath()); |
|
|
|
//设置支付宝根证书路径
|
|
|
|
certAlipayRequest.setRootCertPath(alipayConfig.getRootPath()); |
|
|
|
//构造client
|
|
|
|
AlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest); |
|
|
|
AlipayFundTransUniTransferRequest request = new AlipayFundTransUniTransferRequest(); |
|
|
|
AlipayFundTransUniTransferModel model = new AlipayFundTransUniTransferModel(); |
|
|
|
model.setOutBizNo(orderId); |
|
|
|
model.setTransAmount(amt.toString()); |
|
|
|
model.setProductCode("TRANS_ACCOUNT_NO_PWD"); |
|
|
|
/** 场景码,单笔无密转账到支付宝账户固定为:DIRECT_TRANSFER **/ |
|
|
|
model.setBizScene("DIRECT_TRANSFER"); |
|
|
|
/** 转账业务的标题,用于在支付宝用户的账单里显示 **/ |
|
|
|
model.setOrderTitle("次元hub提现打款"); |
|
|
|
Participant participant = new Participant(); |
|
|
|
/** 参与方的唯一标识,收款支付宝账号或者支付宝吧账号唯一会员ID **/ |
|
|
|
participant.setIdentity(aliPayNo); |
|
|
|
/** 参与方的标识类型:ALIPAY_USER_ID 支付宝的会员ID ALIPAY_LOGON_ID手机号登录 **/ |
|
|
|
participant.setIdentityType("ALIPAY_LOGON_ID"); |
|
|
|
/** 参与方真实姓名,如果非空,将校验收款支付宝账号姓名一致性。当identity_type=ALIPAY_LOGON_ID时,本字段必填 **/ |
|
|
|
participant.setName(name); |
|
|
|
model.setPayeeInfo(participant); |
|
|
|
/** 业务备注 **/ |
|
|
|
model.setRemark("提现打款"); |
|
|
|
request.setBizModel(model); |
|
|
|
log.info("支付宝提现入参为:{}", JSON.toJSONString(request)); |
|
|
|
AlipayFundTransUniTransferResponse response = null; |
|
|
|
try { |
|
|
|
response = alipayClient.certificateExecute(request); |
|
|
|
} catch (AlipayApiException e) { |
|
|
|
throw e; |
|
|
|
} |
|
|
|
/** 获取接口调用结果,如果调用失败,可根据返回错误信息到该文档寻找排查方案:https://opensupport.alipay.com/support/helpcenter/107 **/ |
|
|
|
log.info("支付宝提现响应结果为:{}",response.getBody()); |
|
|
|
return response; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
@Transactional(rollbackFor = Exception.class) |
|
|
|
public int verify(VerifyDto params) { |
|
|
|
@ -142,15 +225,43 @@ public class WithdrawLogServiceImpl implements WithdrawLogService { |
|
|
|
return withdrawLogMapper.updateById(withdrawLog); |
|
|
|
} |
|
|
|
//审核通过
|
|
|
|
withdrawLog.setStatus("1"); |
|
|
|
withdrawLog.setStatus("2"); |
|
|
|
withdrawLog.setUpdateTime(new Date()); |
|
|
|
int update = withdrawLogMapper.updateById(withdrawLog); |
|
|
|
//TODO
|
|
|
|
//此处审核通过,调用支付宝企业付款到用户处方法
|
|
|
|
if(withdrawLog.getChannel().equals(WithdrawConstant.ALIPAY)){ |
|
|
|
//根据用户id查询其支付宝账户及其真实名称
|
|
|
|
CreatorAccountVo creatorAccount = creatorAccountService.queryListByType(withdrawLog.getCreatorId(), "1"); |
|
|
|
if(creatorAccount != null){ |
|
|
|
try{ |
|
|
|
AlipayFundTransUniTransferResponse body = this.trans(withdrawLog.getOrderId(), creatorAccount.getAliPayNo(), creatorAccount.getName(), withdrawLog.getAmt()); |
|
|
|
if(!body.getCode().equals(WithdrawConstant.SUCCESS)){ |
|
|
|
//提现失败
|
|
|
|
withdrawLog.setStatus("3"); |
|
|
|
withdrawLog.setReason(body.getSubMsg()); |
|
|
|
log.error("艺术家【{}】订单【{}】提现失败!原因为:【{}】",withdrawLog.getCreatorId(),withdrawLog.getOrderId(),body.getSubMsg()); |
|
|
|
return withdrawLogMapper.updateById(withdrawLog); |
|
|
|
} |
|
|
|
//扣除账户余额
|
|
|
|
Creator creator = creatorMapper.selectById(withdrawLog.getCreatorId()); |
|
|
|
BigDecimal amt = creator.getAmt().subtract(withdrawLog.getAmt()); |
|
|
|
creator.setAmt(amt); |
|
|
|
creatorMapper.updateById(creator); |
|
|
|
//TODO 分布式锁
|
|
|
|
}catch (AlipayApiException e){ |
|
|
|
log.error("艺术家【{}】订单【{}】提现异常!原因为:【{}】",withdrawLog.getCreatorId(),withdrawLog.getOrderId(),e); |
|
|
|
withdrawLog.setStatus("1"); |
|
|
|
withdrawLog.setReason(e.getMessage()); |
|
|
|
withdrawLogMapper.updateById(withdrawLog); |
|
|
|
throw new ServiceException(e.getMessage()); |
|
|
|
} |
|
|
|
return update; |
|
|
|
}else{ |
|
|
|
throw new ServiceException("该艺术家未绑定支付宝账户!请手动设置!"); |
|
|
|
} |
|
|
|
}else if(withdrawLog.getChannel().equals(WithdrawConstant.BANK)){ |
|
|
|
//TODO 转账到银行卡待开发
|
|
|
|
} |
|
|
|
log.info("艺术家【{}】订单【{}】提现【{}】元成功!",withdrawLog.getCreatorId(),withdrawLog.getOrderId(),withdrawLog.getAmt()); |
|
|
|
return withdrawLogMapper.updateById(withdrawLog); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
|