支付宝提现
服务端请求测试使用 公钥证书方式,接口规范支付宝平台文档API:链接: alipay.
jar引入
<!--支付宝jar包-->
<!-- https://mvnrepository.com/artifact/com.alipay.sdk/alipay-sdk-java -->
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>4.13.45.ALL</version>
</dependency>
控制层
/**
* 钱包金额提现
* @param withdrawalVO
* @return
*/
@RequiresPermissions("client")
@PostMapping("/cashAdvance")
@RequiredLicense(value = "license")
public ResultVO cashAdvance(@RequestBody WithdrawalVO withdrawalVO){
log.info("钱包金额提现 提现金额 = {} ",withdrawalVO);
try {
return aliPayAppSpendService.cashAdvance(withdrawalVO);
}catch (Exception e){
log.info("提现失败 {}", e);
return ReResult.result(1,"提现失败",null);
}
}
service
提现金额申请提现后冻结金额,成功后扣除冻结金额,失败返还,返回处理中则定时任务轮询单笔转账查询。
/**
* 钱包金额提现
*
* @param withdrawalVO
* @return
*/
@Transactional(rollbackFor = Exception.class)
public ResultVO cashAdvance(WithdrawalVO withdrawalVO) throws Exception {
Integer userId = JWTUtil.autoGetUserId();
//校验参数
ResultVO resData = checkWithdrawal(withdrawalVO);
if (resData.getStatus() == 1) {
return resData;
}
//获取当前登陆用户的提现支付宝账号及真实姓名
QueryWrapper<CashAccount> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("user_id", userId);
queryWrapper.eq("channel", 1);
List<CashAccount> cashAccountList = cashAccountMapper.selectList(queryWrapper);
if (CollectionUtils.isEmpty(cashAccountList)) {
return new ResultVO(1, "您尚未绑定支付宝账户,无法提现。");
}
//判断今日是否已提现
QueryWrapper<WithdrwalRecord> withdrwalWrapper = new QueryWrapper<>();
withdrwalWrapper.eq("user_id", userId);
withdrwalWrapper.apply("DATE_FORMAT(now(),'%Y-%m-%d') = DATE_FORMAT(create_time,'%Y-%m-%d')");
List<WithdrwalRecord> wthdrwalRecordList = withdrwalRecordMapper.selectList(withdrwalWrapper);
if(!CollectionUtils.isEmpty(wthdrwalRecordList)){
return new ResultVO(1, "今日已提现,请明日再操作");
}
//判断提现金额
Parameter parameter = parameterMapper.queryParameterByCode("withdrawal_amount");
BigDecimal amount = new BigDecimal(parameter.getValue());
BigDecimal withdrawalAmount = new BigDecimal(withdrawalVO.getWithdrawalAmount());
if (withdrawalAmount.compareTo(amount) < 0) {
return new ResultVO(1, "请输入大于"+amount+"的金额。");
}
UserVitalVO userVitalVO=new UserVitalVO();
userVitalVO.setUserId(userId);
UserVitalVO userVital=userMapper.getUserWalletBalance(userVitalVO);
if(userVital==null){
return new ResultVO(1, "用户不存在");
}
if(StringUtils.isEmpty(userVital.getBalance()) || new BigDecimal(userVital.getBalance().toString()).compareTo(new BigDecimal(withdrawalVO.getWithdrawalAmount()))<0){
return new ResultVO(1, "用户可提现金额不足");
}
String account = cashAccountList.get(0).getAccount();
String trueName = cashAccountList.get(0).getName();
/*获取转账需要的参数*/
//生产唯一订单号
String orderNum = PayAliUtil.createWithdrawalOrderNum(JWTUtil.autoGetUsername());
//提现金额
String ammountStr = withdrawalVO.getWithdrawalAmount();
BigDecimal received= null;
WithdrwalRecord withdrwalRecord= null;
try {
//冻结金额
UserVitalVO balance=new UserVitalVO();
balance.setUserId(userId);
BigDecimal sBalance=userVital.getBalance().subtract(new BigDecimal(ammountStr));
balance.setBalance(sBalance);
if(userVital.getFrozenAmount()==null){
userVital.setFrozenAmount(new BigDecimal(0.00));
}
BigDecimal frozenAmount=userVital.getFrozenAmount().add(new BigDecimal(ammountStr));
balance.setFrozenAmount(frozenAmount);
log.info("Balance= {} ammountStr= {} sBalance= {}",userVital.getBalance(),ammountStr,sBalance);
userMapper.updateUserWalletBalance(balance);
userMapper.updateWalletFrozenAmount(balance);
//手续费计算
Parameter recharge = parameterMapper.queryParameterByCode("service_charge");
String cashBackRate = recharge.getValue();
BigDecimal rate=new BigDecimal(cashBackRate).divide(new BigDecimal(100));
BigDecimal serviceCharge=new BigDecimal(ammountStr).multiply(rate);
received = new BigDecimal(ammountStr).subtract(serviceCharge).setScale(2,BigDecimal.ROUND_DOWN);
log.info("serviceCharge= {}",received);
serviceCharge=new BigDecimal(ammountStr).subtract(received);
log.info