| | |
| | | package com.yeshi.fanli.service.impl.money.extract;
|
| | |
|
| | | import java.math.BigDecimal;
|
| | | import java.util.Calendar;
|
| | | import java.util.Date;
|
| | | import java.util.List;
|
| | | import java.util.UUID;
|
| | |
|
| | | import javax.annotation.Resource;
|
| | |
|
| | | import org.springframework.stereotype.Service;
|
| | | import org.springframework.transaction.annotation.Transactional;
|
| | |
|
| | | import com.alipay.api.AlipayApiException;
|
| | | import com.alipay.api.AlipayClient;
|
| | | import com.alipay.api.DefaultAlipayClient;
|
| | | import com.alipay.api.request.AlipayFundTransToaccountTransferRequest;
|
| | | import com.alipay.api.response.AlipayFundTransToaccountTransferResponse;
|
| | | import com.yeshi.fanli.dao.mybatis.AlipayAccountValidNormalHistoryMapper;
|
| | | import com.yeshi.fanli.dao.mybatis.BindingAccountMapper;
|
| | | import com.yeshi.fanli.dao.mybatis.UserInfoMapper;
|
| | | import com.yeshi.fanli.dao.mybatis.money.UserMoneyDetailMapper;
|
| | | import com.yeshi.fanli.entity.bus.user.AlipayAccountValidNormalHistory;
|
| | | import com.yeshi.fanli.entity.bus.user.BindingAccount;
|
| | | import com.yeshi.fanli.entity.bus.user.UserAccountBindingHistory;
|
| | | import com.yeshi.fanli.entity.bus.user.UserInfo;
|
| | | import com.yeshi.fanli.entity.money.UserMoneyDebt;
|
| | | import com.yeshi.fanli.entity.money.UserMoneyDebt.UserMoneyDebtTypeEnum;
|
| | | import com.yeshi.fanli.entity.money.UserMoneyDetail;
|
| | | import com.yeshi.fanli.exception.money.UserMoneyDebtException;
|
| | | import com.yeshi.fanli.exception.money.UserMoneyDetailException;
|
| | | import com.yeshi.fanli.exception.user.AlipayAccountException;
|
| | | import com.yeshi.fanli.exception.user.AlipayTransferException;
|
| | | import com.yeshi.fanli.exception.user.BindingAccountException;
|
| | | import com.yeshi.fanli.log.LogHelper;
|
| | | import com.yeshi.fanli.service.inter.money.UserMoneyDebtService;
|
| | | import com.yeshi.fanli.service.inter.money.UserMoneyService;
|
| | | import com.yeshi.fanli.service.inter.money.extract.BindingAccountService;
|
| | | import com.yeshi.fanli.service.inter.money.msg.UserMoneyMsgNotificationService;
|
| | | import com.yeshi.fanli.service.inter.user.UserAccountBindingHistoryService;
|
| | | import com.yeshi.fanli.util.Constant;
|
| | | import com.yeshi.fanli.util.StringUtil;
|
| | | import com.yeshi.fanli.util.TimeUtil;
|
| | | import com.yeshi.fanli.util.factory.UserMoneyDetailFactory;
|
| | |
|
| | | import net.sf.json.JSONObject;
|
| | |
|
| | | @Service
|
| | | public class BindingAccountServiceImpl implements BindingAccountService {
|
| | | @Resource
|
| | | private BindingAccountMapper bindingAccountMapper;
|
| | | @Resource
|
| | | private AlipayAccountValidNormalHistoryMapper alipayAccountValidNormalHistoryMapper;
|
| | | @Resource
|
| | | private UserInfoMapper userInfoMapper;
|
| | | @Resource
|
| | | private UserAccountBindingHistoryService userAccountBindingHistoryService;
|
| | | @Resource
|
| | | private UserMoneyDetailMapper userMoneyDetailMapper;
|
| | | @Resource
|
| | | private UserMoneyService userMoneyService;
|
| | | @Resource
|
| | | private UserMoneyMsgNotificationService userMoneyMsgNotificationService;
|
| | | @Resource
|
| | | private UserMoneyDebtService userMoneyDebtService;
|
| | |
|
| | | public List<BindingAccount> getBindingAccountByUid(long uid) {
|
| | | return bindingAccountMapper.selectByUid(uid);
|
| | | }
|
| | |
|
| | | public void addBindingAccount(BindingAccount addAccount) throws BindingAccountException {
|
| | | BindingAccount account = bindingAccountMapper.selectByUidAndType(addAccount.getUserInfo().getId(),
|
| | | addAccount.getType());
|
| | |
|
| | | if (account == null) {
|
| | | bindingAccountMapper.insertSelective(addAccount);
|
| | | } else {
|
| | | throw new BindingAccountException(Constant.BA_EXIST);
|
| | | }
|
| | | }
|
| | |
|
| | | @Transactional
|
| | | public Integer deleteBindingAccount(BindingAccount account) {
|
| | | BindingAccount oldAccount = bindingAccountMapper.selectByUidAndType(account.getUserInfo().getId(),
|
| | | account.getType());
|
| | | if (oldAccount != null)
|
| | | bindingAccountMapper.deleteByPrimaryKey(oldAccount.getId());
|
| | | return 1;
|
| | | }
|
| | |
|
| | | public BindingAccount getBindingAccountByUidAndType(long uid, int type) {
|
| | | BindingAccount account = bindingAccountMapper.selectByUidAndType(uid, type);
|
| | | return account;
|
| | | }
|
| | |
|
| | | @Override
|
| | | public BindingAccount changeAlipayBinding(Long uid, String name, String account) {
|
| | | BindingAccount bindingAccount = getBindingAccountByUidAndType(uid, BindingAccount.TYPE_ALIPAY);
|
| | | if (bindingAccount == null)// 创建账号
|
| | | {
|
| | | bindingAccount = new BindingAccount();
|
| | | bindingAccount.setAccount(account);
|
| | | bindingAccount.setName(name);
|
| | | bindingAccount.setType(BindingAccount.TYPE_ALIPAY);
|
| | | bindingAccount.setUserInfo(new UserInfo(uid));
|
| | | bindingAccountMapper.insertSelective(bindingAccount);
|
| | | } else {
|
| | | bindingAccount.setName(name);
|
| | | bindingAccount.setAccount(account);
|
| | | bindingAccountMapper.updateByPrimaryKeySelective(bindingAccount);
|
| | | }
|
| | | return bindingAccount;
|
| | | }
|
| | |
|
| | | @Override
|
| | | public int deleteByPrimaryKey(Long id) {
|
| | | return bindingAccountMapper.deleteByPrimaryKey(id);
|
| | | }
|
| | |
|
| | | @Transactional(rollbackFor=Exception.class)
|
| | | @Override
|
| | | public void validAlipayAccount(Long uid, String account, String name)
|
| | | throws AlipayTransferException, AlipayApiException, AlipayAccountException {
|
| | | if (uid == null)
|
| | | throw new AlipayAccountException(AlipayAccountException.CODE_NO_PARAMS, "用户ID不能为空");
|
| | | if (StringUtil.isNullOrEmpty(account))
|
| | | throw new AlipayAccountException(AlipayAccountException.CODE_NO_PARAMS, "账号不能为空");
|
| | | if (StringUtil.isNullOrEmpty(name))
|
| | | throw new AlipayAccountException(AlipayAccountException.CODE_NO_PARAMS, "真实姓名不能为空");
|
| | | //
|
| | | List<BindingAccount> bindingAccountList = bindingAccountMapper.selectByAccount(account);
|
| | | if (bindingAccountList != null && bindingAccountList.size() > 0) {
|
| | | if (bindingAccountList.get(0).getUserInfo().getId().longValue() != uid)
|
| | | throw new AlipayAccountException(AlipayAccountException.CODE_ALREADY_BIND,
|
| | | "该支付宝账号已被其他账号绑定,请更换其他的支付宝账号来绑定");
|
| | | }
|
| | |
|
| | | // TODO 做频率验证-每月验证一次
|
| | | AlipayAccountValidNormalHistory latest = alipayAccountValidNormalHistoryMapper.selectLatestByUid(uid);
|
| | | if (latest != null) {
|
| | | Calendar caLatest = Calendar.getInstance();
|
| | | caLatest.setTimeInMillis(latest.getCreateTime().getTime());
|
| | | Calendar nowLatest = Calendar.getInstance();
|
| | | if (caLatest.get(Calendar.MONTH) == nowLatest.get(Calendar.MONTH))// 上次更改和现在是同一个月
|
| | | throw new AlipayAccountException(AlipayAccountException.CODE_TIMES_LIMIT, "每月只能更换绑定一次支付宝账号,请次月再试。");
|
| | | }
|
| | |
|
| | | UserInfo userInfo = userInfoMapper.selectByPrimaryKeyForUpdate(uid);
|
| | | if (userInfo.getMyHongBao().compareTo(new BigDecimal("0")) <= 0)
|
| | | throw new AlipayAccountException(AlipayAccountException.CODE_NO_MONEY, "你的账户无余额,无需绑定提现帐号。");
|
| | |
|
| | | // 需要转账验证
|
| | | BigDecimal money = new BigDecimal("0.1");
|
| | | transferAlipayWithVerify(account, name);
|
| | |
|
| | | // 转账成功
|
| | | // 插入转账成功表
|
| | | AlipayAccountValidNormalHistory history = new AlipayAccountValidNormalHistory();
|
| | | history.setAccount(account);
|
| | | history.setCreateTime(new Date());
|
| | | history.setName(name);
|
| | | history.setUid(uid);
|
| | | alipayAccountValidNormalHistoryMapper.insertSelective(history);
|
| | | UserMoneyDetail userMoneyDetail = null;
|
| | | // 新版资金
|
| | | try {
|
| | | userMoneyDetail = UserMoneyDetailFactory.createExtractAccountValid(history, money);
|
| | | } catch (UserMoneyDetailException e) {
|
| | | try {
|
| | | LogHelper.errorDetailInfo(e);
|
| | | } catch (Exception e1) {
|
| | | e1.printStackTrace();
|
| | | }
|
| | | }
|
| | |
|
| | | // 余额充足
|
| | | if (userInfo.getMyHongBao().compareTo(money) >= 0) {
|
| | | // 扣款
|
| | | userMoneyService.subUserMoney(uid, money, userMoneyDetail);
|
| | | userMoneyMsgNotificationService.alipayAccountValid(history);
|
| | | } else {// 余额不足,暂时不扣款,加入借贷关系
|
| | | UserMoneyDebt debt = new UserMoneyDebt();
|
| | | debt.setCreateTime(new Date());
|
| | | debt.setLeftMoney(money);
|
| | | debt.setOriginMoney(money);
|
| | | debt.setSourceId(history.getId());
|
| | | debt.setType(UserMoneyDebtTypeEnum.extractVerify);
|
| | | debt.setUid(uid);
|
| | | try {
|
| | | userMoneyDebtService.addUserMoneyDebt(debt);
|
| | | } catch (UserMoneyDebtException e) {
|
| | | LogHelper.errorDetailInfo(e, "验证ID:" + history.getId(), "");
|
| | | }
|
| | |
|
| | | }
|
| | |
|
| | | }
|
| | |
|
| | | private void transferAlipayWithVerify(String account, String name)
|
| | | throws AlipayTransferException, AlipayApiException {
|
| | | String privateKey = Constant.alipayConfig.getPrivateKey();
|
| | | AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",
|
| | | Constant.alipayConfig.getAppId(), privateKey, "json", "gbk", null, "RSA2");
|
| | | AlipayFundTransToaccountTransferRequest request = new AlipayFundTransToaccountTransferRequest();
|
| | | String uuid = UUID.randomUUID().toString().replace("-", "");
|
| | | String appName = Constant.systemCommonConfig.getProjectChineseName();
|
| | | JSONObject json = new JSONObject();
|
| | | json.put("out_biz_no", uuid);
|
| | | json.put("payee_type", "ALIPAY_LOGONID");
|
| | | json.put("payee_account", account);
|
| | | json.put("amount", "0.1");
|
| | | json.put("payer_show_name", appName + "支付宝验证");
|
| | | json.put("payee_real_name", name);
|
| | | json.put("remark", "来自" + appName + "的支付宝验证打款");
|
| | | request.setBizContent(json.toString());
|
| | | AlipayFundTransToaccountTransferResponse response = null;
|
| | | response = alipayClient.execute(request);
|
| | | // 成功转账
|
| | | if (response != null && response.isSuccess() && "10000".equals(response.getCode())) {
|
| | | return;
|
| | | } else// 转账失败
|
| | | {
|
| | | throw new AlipayTransferException(Integer.parseInt(response.getCode()), response.getSubCode(),
|
| | | response.getSubMsg());
|
| | | }
|
| | | }
|
| | |
|
| | | @Transactional(rollbackFor=Exception.class)
|
| | | @Override
|
| | | public BindingAccount changeAlipayBindingWithVerify(Long uid, String name, String account)
|
| | | throws AlipayTransferException, AlipayApiException, AlipayAccountException {
|
| | | BindingAccount bindingAccount = bindingAccountMapper.selectByUidAndType(uid, BindingAccount.TYPE_ALIPAY);
|
| | | if (bindingAccount != null) {
|
| | | // 验证是否7天内更换了手机号
|
| | | UserAccountBindingHistory history = userAccountBindingHistoryService.getLatestHistory(uid,
|
| | | UserAccountBindingHistory.TYPE_PHONE);
|
| | | // 手机号更换绑定的7天内不能提现
|
| | | if (history != null && !history.getFirst()
|
| | | && (System.currentTimeMillis() - history.getCreateTime().getTime()) < 1000 * 60 * 60 * 24 * 7L) {
|
| | | throw new AlipayAccountException(111, "修改手机号后,7天内无法更换提现账号");
|
| | | }
|
| | | }
|
| | |
|
| | | try {
|
| | | validAlipayAccount(uid, account, name);
|
| | | } catch (AlipayTransferException e1) {
|
| | | throw new AlipayTransferException(e1.getCode(), e1.getSubCode(), e1.getMsg());
|
| | | } catch (AlipayApiException e1) {
|
| | | throw new AlipayApiException(e1.getErrCode(), e1.getErrMsg());
|
| | | } catch (AlipayAccountException e1) {
|
| | | throw new AlipayAccountException(e1.getCode(), e1.getMsg());
|
| | | }
|
| | |
|
| | | if (bindingAccount == null)// 创建账号
|
| | | {
|
| | | bindingAccount = new BindingAccount();
|
| | | bindingAccount.setAccount(account);
|
| | | bindingAccount.setName(name);
|
| | | bindingAccount.setType(BindingAccount.TYPE_ALIPAY);
|
| | | bindingAccount.setUserInfo(new UserInfo(uid));
|
| | | bindingAccountMapper.insertSelective(bindingAccount);
|
| | | } else {
|
| | | BindingAccount updateBindingAccount = new BindingAccount();
|
| | | updateBindingAccount.setId(bindingAccount.getId());
|
| | | updateBindingAccount.setName(name);
|
| | | updateBindingAccount.setAccount(account);
|
| | | bindingAccountMapper.updateByPrimaryKeySelective(updateBindingAccount);
|
| | | bindingAccount.setName(updateBindingAccount.getName());
|
| | | bindingAccount.setAccount(updateBindingAccount.getAccount());
|
| | | }
|
| | |
|
| | | return bindingAccount;
|
| | | }
|
| | |
|
| | | @Override
|
| | | public boolean canVerifyAlipayAccount(Long uid) throws BindingAccountException {
|
| | | UserInfo userInfo = userInfoMapper.selectByPrimaryKeyForUpdate(uid);
|
| | | if (userInfo.getMyHongBao().compareTo(new BigDecimal("0")) <= 0) {
|
| | | BindingAccount bindingAccount = bindingAccountMapper.selectByUidAndType(uid, BindingAccount.TYPE_ALIPAY);
|
| | | if (bindingAccount != null)
|
| | | throw new BindingAccountException(1, "当前账户没有余额,无需修改,请有余额后修改。");
|
| | | else
|
| | | throw new BindingAccountException(1, "当前账户没有余额,请有余额后绑定。");
|
| | | }
|
| | | AlipayAccountValidNormalHistory latest = alipayAccountValidNormalHistoryMapper.selectLatestByUid(uid);
|
| | | if (latest != null) {
|
| | | if (TimeUtil.getGernalTime(System.currentTimeMillis(), "yyyy-MM")
|
| | | .equalsIgnoreCase(TimeUtil.getGernalTime(latest.getCreateTime().getTime(), "yyyy-MM")))// 上次更改和现在是同一年同一个月
|
| | | throw new BindingAccountException(2, "每月仅可修改1次提现账号,请下月再试吧。");
|
| | | }
|
| | | return true;
|
| | | }
|
| | |
|
| | | }
|
| | | package com.yeshi.fanli.service.impl.money.extract; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.util.Calendar; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | |
| | | import javax.annotation.Resource; |
| | | |
| | | import com.yeshi.fanli.entity.SystemEnum; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import com.alipay.api.AlipayApiException; |
| | | import com.yeshi.fanli.dao.mybatis.AlipayAccountValidNormalHistoryMapper; |
| | | import com.yeshi.fanli.dao.mybatis.BindingAccountMapper; |
| | | import com.yeshi.fanli.dao.mybatis.UserInfoMapper; |
| | | import com.yeshi.fanli.dao.mybatis.money.UserMoneyDetailMapper; |
| | | import com.yeshi.fanli.entity.bus.user.AlipayAccountValidNormalHistory; |
| | | import com.yeshi.fanli.entity.bus.user.BindingAccount; |
| | | import com.yeshi.fanli.entity.bus.user.UserAccountBindingHistory; |
| | | import com.yeshi.fanli.entity.bus.user.UserInfo; |
| | | import com.yeshi.fanli.entity.money.UserMoneyDebt; |
| | | import com.yeshi.fanli.entity.money.UserMoneyDebt.UserMoneyDebtTypeEnum; |
| | | import com.yeshi.fanli.entity.money.UserMoneyDetail; |
| | | import com.yeshi.fanli.exception.money.UserMoneyDebtException; |
| | | import com.yeshi.fanli.exception.money.UserMoneyDetailException; |
| | | import com.yeshi.fanli.exception.user.AlipayAccountException; |
| | | import com.yeshi.fanli.exception.user.AlipayTransferException; |
| | | import com.yeshi.fanli.exception.user.BindingAccountException; |
| | | import com.yeshi.fanli.log.LogHelper; |
| | | import com.yeshi.fanli.service.inter.money.UserMoneyDebtService; |
| | | import com.yeshi.fanli.service.inter.money.UserMoneyService; |
| | | import com.yeshi.fanli.service.inter.money.extract.BindingAccountService; |
| | | import com.yeshi.fanli.service.inter.money.msg.UserMoneyMsgNotificationService; |
| | | import com.yeshi.fanli.service.inter.user.UserAccountBindingHistoryService; |
| | | import com.yeshi.fanli.util.AlipayUtil; |
| | | import com.yeshi.fanli.util.Constant; |
| | | import com.yeshi.fanli.util.StringUtil; |
| | | import org.yeshi.utils.TimeUtil; |
| | | import com.yeshi.fanli.util.factory.UserMoneyDetailFactory; |
| | | |
| | | @Service |
| | | public class BindingAccountServiceImpl implements BindingAccountService { |
| | | @Resource |
| | | private BindingAccountMapper bindingAccountMapper; |
| | | @Resource |
| | | private AlipayAccountValidNormalHistoryMapper alipayAccountValidNormalHistoryMapper; |
| | | @Resource |
| | | private UserInfoMapper userInfoMapper; |
| | | @Resource |
| | | private UserAccountBindingHistoryService userAccountBindingHistoryService; |
| | | @Resource |
| | | private UserMoneyDetailMapper userMoneyDetailMapper; |
| | | @Resource |
| | | private UserMoneyService userMoneyService; |
| | | @Resource |
| | | private UserMoneyMsgNotificationService userMoneyMsgNotificationService; |
| | | @Resource |
| | | private UserMoneyDebtService userMoneyDebtService; |
| | | |
| | | public List<BindingAccount> getBindingAccountByUid(long uid) { |
| | | return bindingAccountMapper.selectByUid(uid); |
| | | } |
| | | |
| | | public void addBindingAccount(BindingAccount addAccount) throws BindingAccountException { |
| | | BindingAccount account = bindingAccountMapper.selectByUidAndType(addAccount.getUserInfo().getId(), |
| | | addAccount.getType()); |
| | | |
| | | if (account == null) { |
| | | bindingAccountMapper.insertSelective(addAccount); |
| | | } else { |
| | | throw new BindingAccountException(Constant.BA_EXIST); |
| | | } |
| | | } |
| | | |
| | | @Transactional |
| | | public Integer deleteBindingAccount(BindingAccount account) { |
| | | BindingAccount oldAccount = bindingAccountMapper.selectByUidAndType(account.getUserInfo().getId(), |
| | | account.getType()); |
| | | if (oldAccount != null) |
| | | bindingAccountMapper.deleteByPrimaryKey(oldAccount.getId()); |
| | | return 1; |
| | | } |
| | | |
| | | public BindingAccount getBindingAccountByUidAndType(long uid, int type) { |
| | | BindingAccount account = bindingAccountMapper.selectByUidAndType(uid, type); |
| | | return account; |
| | | } |
| | | |
| | | @Override |
| | | public BindingAccount changeAlipayBinding(Long uid, String name, String account) { |
| | | BindingAccount bindingAccount = getBindingAccountByUidAndType(uid, BindingAccount.TYPE_ALIPAY); |
| | | if (bindingAccount == null)// 创建账号 |
| | | { |
| | | bindingAccount = new BindingAccount(); |
| | | bindingAccount.setAccount(account); |
| | | bindingAccount.setName(name); |
| | | bindingAccount.setType(BindingAccount.TYPE_ALIPAY); |
| | | bindingAccount.setUserInfo(new UserInfo(uid)); |
| | | bindingAccountMapper.insertSelective(bindingAccount); |
| | | } else { |
| | | bindingAccount.setName(name); |
| | | bindingAccount.setAccount(account); |
| | | bindingAccountMapper.updateByPrimaryKeySelective(bindingAccount); |
| | | } |
| | | return bindingAccount; |
| | | } |
| | | |
| | | @Override |
| | | public int deleteByPrimaryKey(Long id) { |
| | | return bindingAccountMapper.deleteByPrimaryKey(id); |
| | | } |
| | | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | @Override |
| | | public void validAlipayAccount(Long uid, String account, String name) |
| | | throws AlipayTransferException, AlipayApiException, AlipayAccountException { |
| | | if (uid == null) |
| | | throw new AlipayAccountException(AlipayAccountException.CODE_NO_PARAMS, "用户ID不能为空"); |
| | | if (StringUtil.isNullOrEmpty(account)) |
| | | throw new AlipayAccountException(AlipayAccountException.CODE_NO_PARAMS, "账号不能为空"); |
| | | if (StringUtil.isNullOrEmpty(name)) |
| | | throw new AlipayAccountException(AlipayAccountException.CODE_NO_PARAMS, "真实姓名不能为空"); |
| | | |
| | | List<BindingAccount> bindingAccountList = bindingAccountMapper.selectByAccount(account); |
| | | |
| | | if (bindingAccountList != null && bindingAccountList.size() > 0) { |
| | | UserInfo user = userInfoMapper.selectByPrimaryKey(uid); |
| | | SystemEnum systemEnum = user.getSystem(); |
| | | |
| | | for (int i = 0; i < bindingAccountList.size(); i++) { |
| | | UserInfo u = userInfoMapper.selectByPrimaryKey(bindingAccountList.get(i).getUserInfo().getId()); |
| | | //同一系统下绑定账号不能重复 |
| | | if (u.getSystem() == systemEnum && bindingAccountList.get(i).getUserInfo().getId().longValue() != uid) { |
| | | throw new AlipayAccountException(AlipayAccountException.CODE_ALREADY_BIND, |
| | | "该支付宝账号已被其他账号绑定,请更换其他的支付宝账号来绑定"); |
| | | } |
| | | } |
| | | } |
| | | |
| | | AlipayAccountValidNormalHistory latest = alipayAccountValidNormalHistoryMapper.selectLatestByUid(uid); |
| | | if (latest != null) { |
| | | Calendar caLatest = Calendar.getInstance(); |
| | | caLatest.setTimeInMillis(latest.getCreateTime().getTime()); |
| | | Calendar nowLatest = Calendar.getInstance(); |
| | | if (caLatest.get(Calendar.MONTH) == nowLatest.get(Calendar.MONTH))// 上次更改和现在是同一个月 |
| | | throw new AlipayAccountException(AlipayAccountException.CODE_TIMES_LIMIT, "每月只能更换绑定一次支付宝账号,请次月再试。"); |
| | | } |
| | | |
| | | UserInfo userInfo = userInfoMapper.selectByPrimaryKeyForUpdate(uid); |
| | | if (userInfo.getMyHongBao().compareTo(new BigDecimal("0")) <= 0) |
| | | throw new AlipayAccountException(AlipayAccountException.CODE_NO_MONEY, "你的账户无余额,无需绑定提现帐号。"); |
| | | |
| | | // 需要转账验证 |
| | | BigDecimal money = new BigDecimal("0.1"); |
| | | transferAlipayWithVerify(account, name); |
| | | |
| | | // 转账成功 |
| | | // 插入转账成功表 |
| | | AlipayAccountValidNormalHistory history = new AlipayAccountValidNormalHistory(); |
| | | history.setAccount(account); |
| | | history.setCreateTime(new Date()); |
| | | history.setName(name); |
| | | history.setUid(uid); |
| | | alipayAccountValidNormalHistoryMapper.insertSelective(history); |
| | | UserMoneyDetail userMoneyDetail = null; |
| | | // 新版资金 |
| | | try { |
| | | userMoneyDetail = UserMoneyDetailFactory.createExtractAccountValid(history, money); |
| | | } catch (UserMoneyDetailException e) { |
| | | try { |
| | | LogHelper.errorDetailInfo(e); |
| | | } catch (Exception e1) { |
| | | e1.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | // 余额充足 |
| | | if (userInfo.getMyHongBao().compareTo(money) >= 0) { |
| | | // 扣款 |
| | | userMoneyService.subUserMoney(uid, money, userMoneyDetail); |
| | | userMoneyMsgNotificationService.alipayAccountValid(history, "1个月", latest != null); |
| | | } else {// 余额不足,暂时不扣款,加入借贷关系 |
| | | UserMoneyDebt debt = new UserMoneyDebt(); |
| | | debt.setCreateTime(new Date()); |
| | | debt.setLeftMoney(money); |
| | | debt.setOriginMoney(money); |
| | | debt.setSourceId(history.getId()); |
| | | debt.setType(UserMoneyDebtTypeEnum.extractVerify); |
| | | debt.setUid(uid); |
| | | try { |
| | | userMoneyDebtService.addUserMoneyDebt(debt); |
| | | } catch (UserMoneyDebtException e) { |
| | | LogHelper.errorDetailInfo(e, "验证ID:" + history.getId(), ""); |
| | | } |
| | | |
| | | } |
| | | |
| | | } |
| | | |
| | | private void transferAlipayWithVerify(String account, String name) |
| | | throws AlipayTransferException, AlipayApiException { |
| | | String outBizNo = StringUtil.Md5(account) + TimeUtil.getGernalTime(System.currentTimeMillis(), "yyyyMMdd"); |
| | | String appName = Constant.systemCommonConfig.getProjectChineseName(); |
| | | AlipayUtil.transfer(outBizNo, account, name, new BigDecimal("0.1"), appName + "支付宝验证", "来自" + appName + "的支付宝验证打款"); |
| | | } |
| | | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | @Override |
| | | public BindingAccount changeAlipayBindingWithVerify(Long uid, String name, String account) |
| | | throws AlipayTransferException, AlipayApiException, AlipayAccountException { |
| | | BindingAccount bindingAccount = bindingAccountMapper.selectByUidAndType(uid, BindingAccount.TYPE_ALIPAY); |
| | | if (bindingAccount != null) { |
| | | // 验证是否7天内更换了手机号 |
| | | UserAccountBindingHistory history = userAccountBindingHistoryService.getLatestHistory(uid, |
| | | UserAccountBindingHistory.TYPE_PHONE); |
| | | // 手机号更换绑定的7天内不能提现 |
| | | if (history != null && !history.getFirst() |
| | | && (System.currentTimeMillis() - history.getCreateTime().getTime()) < 1000 * 60 * 60 * 24 * 7L) { |
| | | throw new AlipayAccountException(111, "修改手机号后,7天内无法更换提现账号"); |
| | | } |
| | | } |
| | | |
| | | try { |
| | | validAlipayAccount(uid, account, name); |
| | | } catch (AlipayTransferException e1) { |
| | | throw new AlipayTransferException(e1.getCode(), e1.getSubCode(), e1.getMsg()); |
| | | } catch (AlipayApiException e1) { |
| | | throw new AlipayApiException(e1.getErrCode(), e1.getErrMsg()); |
| | | } catch (AlipayAccountException e1) { |
| | | throw new AlipayAccountException(e1.getCode(), e1.getMsg()); |
| | | } |
| | | |
| | | if (bindingAccount == null)// 创建账号 |
| | | { |
| | | bindingAccount = new BindingAccount(); |
| | | bindingAccount.setAccount(account); |
| | | bindingAccount.setName(name); |
| | | bindingAccount.setType(BindingAccount.TYPE_ALIPAY); |
| | | bindingAccount.setUserInfo(new UserInfo(uid)); |
| | | bindingAccountMapper.insertSelective(bindingAccount); |
| | | } else { |
| | | BindingAccount updateBindingAccount = new BindingAccount(); |
| | | updateBindingAccount.setId(bindingAccount.getId()); |
| | | updateBindingAccount.setName(name); |
| | | updateBindingAccount.setAccount(account); |
| | | bindingAccountMapper.updateByPrimaryKeySelective(updateBindingAccount); |
| | | bindingAccount.setName(updateBindingAccount.getName()); |
| | | bindingAccount.setAccount(updateBindingAccount.getAccount()); |
| | | } |
| | | |
| | | return bindingAccount; |
| | | } |
| | | |
| | | @Override |
| | | public boolean canVerifyAlipayAccount(Long uid) throws BindingAccountException { |
| | | UserInfo userInfo = userInfoMapper.selectByPrimaryKeyForUpdate(uid); |
| | | if (userInfo.getMyHongBao().compareTo(new BigDecimal("0")) <= 0) { |
| | | BindingAccount bindingAccount = bindingAccountMapper.selectByUidAndType(uid, BindingAccount.TYPE_ALIPAY); |
| | | if (bindingAccount != null) |
| | | throw new BindingAccountException(1, "当前账户没有余额,无需修改,请有余额后修改。"); |
| | | else |
| | | throw new BindingAccountException(1, "当前账户没有余额,请有余额后绑定。"); |
| | | } |
| | | AlipayAccountValidNormalHistory latest = alipayAccountValidNormalHistoryMapper.selectLatestByUid(uid); |
| | | if (latest != null) { |
| | | if (TimeUtil.getGernalTime(System.currentTimeMillis(), "yyyy-MM") |
| | | .equalsIgnoreCase(TimeUtil.getGernalTime(latest.getCreateTime().getTime(), "yyyy-MM")))// 上次更改和现在是同一年同一个月 |
| | | throw new BindingAccountException(2, "每月仅可修改1次提现账号,请下月再试吧。"); |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | } |