Files
jsowell-charger-web/jsowell-pile/src/main/java/com/jsowell/adapay/service/AdapayMemberService.java

397 lines
18 KiB
Java
Raw Normal View History

2023-06-06 16:18:25 +08:00
package com.jsowell.adapay.service;
2023-06-28 08:54:55 +08:00
import com.alibaba.fastjson2.JSON;
import com.google.common.collect.Lists;
2023-06-06 16:18:25 +08:00
import com.google.common.collect.Maps;
2023-06-09 09:44:20 +08:00
import com.huifu.adapay.core.exception.BaseAdaPayException;
2023-06-20 11:39:24 +08:00
import com.huifu.adapay.model.CorpMember;
2023-06-28 10:33:50 +08:00
import com.huifu.adapay.model.Drawcash;
2023-06-06 16:18:25 +08:00
import com.huifu.adapay.model.Member;
2023-06-09 09:44:20 +08:00
import com.huifu.adapay.model.SettleAccount;
2023-06-20 16:24:04 +08:00
import com.jsowell.adapay.dto.CreateSettleAccountDTO;
2023-06-17 16:33:01 +08:00
import com.jsowell.adapay.dto.UpdateAccountConfigDTO;
2023-06-28 10:33:50 +08:00
import com.jsowell.adapay.dto.WithdrawDTO;
2023-06-15 14:06:33 +08:00
import com.jsowell.adapay.response.QueryMemberResponse;
2023-06-13 11:28:15 +08:00
import com.jsowell.adapay.vo.AdapayAccountBalanceVO;
import com.jsowell.adapay.vo.AdapayMemberInfoVO;
2023-06-14 14:48:58 +08:00
import com.jsowell.adapay.vo.AdapaySettleAccountVO;
2023-06-20 16:24:04 +08:00
import com.jsowell.common.constant.Constants;
2023-06-09 08:18:42 +08:00
import com.jsowell.common.exception.BusinessException;
2023-06-28 10:33:50 +08:00
import com.jsowell.common.util.AdapayUtil;
2023-06-07 16:04:10 +08:00
import com.jsowell.common.util.StringUtils;
2023-06-29 15:06:31 +08:00
import com.jsowell.common.util.ZipUtil;
2023-06-06 16:18:25 +08:00
import com.jsowell.common.util.id.IdUtils;
2023-06-15 14:26:53 +08:00
import com.jsowell.pile.domain.AdapayMemberAccount;
import com.jsowell.pile.service.IAdapayMemberAccountService;
2023-06-06 16:18:25 +08:00
import lombok.extern.slf4j.Slf4j;
2023-06-07 15:10:35 +08:00
import org.springframework.beans.factory.annotation.Autowired;
2023-06-07 16:04:10 +08:00
import org.springframework.beans.factory.annotation.Value;
2023-06-06 16:18:25 +08:00
import org.springframework.stereotype.Service;
2023-06-14 16:23:16 +08:00
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
2023-06-06 16:18:25 +08:00
2023-06-20 11:39:24 +08:00
import java.io.File;
2023-06-20 15:28:52 +08:00
import java.io.IOException;
2023-06-13 11:28:15 +08:00
import java.math.BigDecimal;
2023-07-01 11:46:17 +08:00
import java.util.List;
2023-06-06 16:18:25 +08:00
import java.util.Map;
@Slf4j
@Service
public class AdapayMemberService {
2023-06-07 15:10:35 +08:00
2023-06-07 16:04:10 +08:00
@Value("${adapay.appId}")
private String ADAPAY_APP_ID;
2023-06-15 14:26:53 +08:00
@Autowired
private IAdapayMemberAccountService adapayMemberAccountService;
2023-06-20 16:24:04 +08:00
public void createSettleAccount(CreateSettleAccountDTO dto) throws Exception {
String bankAcctType = dto.getBankAcctType();
if (StringUtils.equals(bankAcctType, Constants.ONE)) {
createCorpMember(dto);
2023-06-29 15:06:31 +08:00
} else if (StringUtils.equals(bankAcctType, Constants.TWO)) {
createMember(dto);
2023-06-20 16:24:04 +08:00
}
}
2023-06-06 16:18:25 +08:00
/**
2023-06-09 09:44:20 +08:00
* 创建汇付会员
2023-06-17 16:33:01 +08:00
*
2023-06-09 09:44:20 +08:00
* @param dto
* @throws Exception
2023-06-06 16:18:25 +08:00
*/
2023-06-14 16:23:16 +08:00
@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
2023-06-20 16:24:04 +08:00
public void createMember(CreateSettleAccountDTO dto) throws Exception {
2023-06-15 14:26:53 +08:00
AdapayMemberAccount adapayMemberAccount = adapayMemberAccountService.selectByMerchantId(dto.getMerchantId());
if (adapayMemberAccount != null) {
2023-06-28 10:33:50 +08:00
log.error("通过merchantId:{}, 没有查询到结算账户配置", dto.getMerchantId());
2023-06-09 15:34:35 +08:00
return;
}
2023-06-07 16:04:10 +08:00
log.info("=======execute CreateMember begin=======");
Map<String, Object> memberParams = Maps.newHashMap();
2023-06-29 16:16:25 +08:00
String adapayMemberId = Constants.ADAPAY_MEMBER_PREFIX + IdUtils.getMemberId();
2023-06-15 14:26:53 +08:00
memberParams.put("member_id", adapayMemberId);
2023-06-07 16:04:10 +08:00
memberParams.put("app_id", ADAPAY_APP_ID);
memberParams.put("location", dto.getLocation());
memberParams.put("email", dto.getEmail());
memberParams.put("gender", dto.getGender());
memberParams.put("nickname", dto.getNickname());
log.info("创建用户,请求参数:" + JSON.toJSONString(memberParams));
Map<String, Object> member = Member.create(memberParams);
log.info("创建用户,返回参数:" + JSON.toJSONString(member));
log.info("=======execute CreateMember end=======");
2023-06-09 09:44:20 +08:00
if (member == null || StringUtils.equals((String) member.get("status"), "failed")) {
2023-06-09 08:18:42 +08:00
String errorMsg = member == null ? "创建汇付用户失败" : (String) member.get("error_msg");
throw new BusinessException("00500001", errorMsg);
2023-06-07 16:04:10 +08:00
}
2023-06-09 09:44:20 +08:00
// 创建结算账户
2023-06-14 16:23:16 +08:00
Map<String, Object> accountInfo = Maps.newHashMap();
// 银行卡号
accountInfo.put("card_id", dto.getCardId());
// 银行卡对应的户名
accountInfo.put("card_name", dto.getCardName());
// 证件号,银行账户类型为对私时,必填
2023-06-16 15:42:04 +08:00
if (StringUtils.isNotBlank(dto.getCertId())) {
accountInfo.put("cert_id", dto.getCertId());
}
2023-06-14 16:23:16 +08:00
// 证件类型仅支持00-身份证,银行账户类型为对私时,必填
accountInfo.put("cert_type", "00");
// 手机号
accountInfo.put("tel_no", dto.getTelNo());
// 银行编码,详见附录 银行代码,银行账户类型对公时,必填
2023-06-16 15:42:04 +08:00
if (StringUtils.isNotBlank(dto.getBankCode())) {
accountInfo.put("bank_code", dto.getBankCode());
}
2023-06-14 16:23:16 +08:00
// 银行账户类型1-对公2-对私
accountInfo.put("bank_acct_type", dto.getBankAcctType());
// 银行账户开户银行所在省份编码 (省市编码),银行账户类型为对公时,必填
2023-06-16 15:34:33 +08:00
if (StringUtils.isNotBlank(dto.getProvCode())) {
accountInfo.put("prov_code", dto.getProvCode());
}
2023-06-14 16:23:16 +08:00
// 银行账户开户银行所在地区编码(省市编码),银行账户类型为对公时,必填
2023-06-16 15:34:33 +08:00
if (StringUtils.isNotBlank(dto.getAreaCode())) {
accountInfo.put("area_code", dto.getAreaCode());
}
2023-06-14 16:23:16 +08:00
Map<String, Object> settleCountParams = Maps.newHashMap();
2023-06-15 14:26:53 +08:00
settleCountParams.put("member_id", adapayMemberId);
2023-06-14 16:23:16 +08:00
settleCountParams.put("app_id", ADAPAY_APP_ID);
// channel String Y 目前仅支持bank_account银行卡
settleCountParams.put("channel", "bank_account");
settleCountParams.put("account_info", accountInfo);
2023-06-16 15:34:33 +08:00
log.info("创建汇付结算账户param:{}", settleCountParams);
2023-06-14 16:23:16 +08:00
Map<String, Object> settleCount = SettleAccount.create(settleCountParams);
log.info("创建汇付结算账户result:{}", settleCount);
if (settleCount == null || StringUtils.equals((String) settleCount.get("status"), "failed")) {
2023-06-14 16:29:51 +08:00
String errorMsg = settleCount == null ? "创建汇付结算账户失败" : (String) settleCount.get("error_msg");
2023-06-14 16:23:16 +08:00
throw new BusinessException("00500001", errorMsg);
}
2023-06-15 14:26:53 +08:00
String settleAccountId = (String) settleCount.get("id");
// 保存到数据库
adapayMemberAccount = new AdapayMemberAccount();
adapayMemberAccount.setMerchantId(dto.getMerchantId());
adapayMemberAccount.setAdapayMemberId(adapayMemberId);
adapayMemberAccount.setSettleAccountId(settleAccountId);
2023-07-05 14:49:47 +08:00
adapayMemberAccount.setStatus(Constants.ONE);
2023-06-15 14:26:53 +08:00
adapayMemberAccountService.insertAdapayMemberAccount(adapayMemberAccount);
2023-06-07 16:04:10 +08:00
}
2023-06-08 15:00:30 +08:00
2023-06-09 09:44:20 +08:00
/**
* 查询汇付会员信息
2023-06-17 16:33:01 +08:00
*
2023-06-09 09:44:20 +08:00
* @param merchantId
* @return
*/
public Map<String, Object> selectAdapayMember(String merchantId) throws BaseAdaPayException {
Map<String, Object> map = Maps.newHashMap();
AdapayMemberAccount adapayMemberAccount = adapayMemberAccountService.selectByMerchantId(merchantId);
if (adapayMemberAccount == null) {
2023-06-28 10:33:50 +08:00
log.error("通过merchantId:{}, 没有查询到结算账户配置", merchantId);
return null;
}
2023-06-14 16:56:25 +08:00
String adapayMemberId = adapayMemberAccount.getAdapayMemberId();
2023-06-29 16:16:25 +08:00
String bankAcctType;
2023-07-01 11:46:17 +08:00
AdapayMemberInfoVO adapayMemberInfoVO = null;
List<AdapaySettleAccountVO> list = null;
2023-06-29 16:16:25 +08:00
if (StringUtils.startsWith(adapayMemberId, Constants.ADAPAY_MEMBER_PREFIX)) {
bankAcctType = Constants.ONE;
// 查询个人用户
2023-07-01 11:46:17 +08:00
adapayMemberInfoVO = queryAdapayMemberInfo(adapayMemberId);
2023-06-29 16:16:25 +08:00
if (adapayMemberInfoVO != null) {
adapayMemberInfoVO.setMerchantId(merchantId);
}
AdapaySettleAccountVO adapaySettleAccountVO = queryAdapaySettleAccount(adapayMemberId, adapayMemberAccount.getSettleAccountId());
if (adapaySettleAccountVO != null) {
adapaySettleAccountVO.setMerchantId(merchantId);
}
map.put("adapayMember", adapayMemberInfoVO);
2023-07-01 11:46:17 +08:00
list = Lists.newArrayList(adapaySettleAccountVO);
map.put("settleAccountList", list);
2023-06-29 16:16:25 +08:00
} else {
bankAcctType = Constants.TWO;
// 查询企业用户
queryCorpAdapayMemberInfo(adapayMemberId);
}
2023-06-29 16:16:25 +08:00
map.put("bankAcctType", bankAcctType);
2023-07-01 11:46:17 +08:00
map.put("adapayMember", adapayMemberInfoVO);
map.put("settleAccountList", list);
return map;
2023-06-08 15:00:30 +08:00
}
2023-06-09 09:44:20 +08:00
2023-06-15 14:06:33 +08:00
2023-06-09 09:44:20 +08:00
/**
2023-06-15 14:06:33 +08:00
* 查询汇付会员信息
2023-06-09 09:44:20 +08:00
*/
public AdapayMemberInfoVO queryAdapayMemberInfo(String adapayMemberId) throws BaseAdaPayException {
if (StringUtils.isBlank(adapayMemberId)) {
return null;
}
2023-06-15 14:06:33 +08:00
Map<String, Object> memberParams = Maps.newHashMap();
memberParams.put("member_id", adapayMemberId);
memberParams.put("app_id", ADAPAY_APP_ID);
log.info("查询用户,请求参数:{}", JSON.toJSONString(memberParams));
Map<String, Object> member = Member.query(memberParams);
log.info("查询用户,返回参数:{}", JSON.toJSONString(member));
if (member == null || member.isEmpty() || !"succeeded".equals(member.get("status"))) {
return null;
}
2023-06-15 14:06:33 +08:00
QueryMemberResponse queryMemberResponse = JSON.parseObject(JSON.toJSONString(member), QueryMemberResponse.class);
AdapayMemberInfoVO resultVO = AdapayMemberInfoVO.builder()
.nickname(queryMemberResponse.getNickname())
.gender(queryMemberResponse.getGender())
.email(queryMemberResponse.getEmail())
.location(queryMemberResponse.getLocation())
.build();
return resultVO;
2023-06-15 14:06:33 +08:00
}
2023-06-09 09:44:20 +08:00
2023-06-29 16:16:25 +08:00
/**
* 查询企业用户信息
*/
public AdapayMemberInfoVO queryCorpAdapayMemberInfo(String adapayMemberId) throws BaseAdaPayException {
Map<String, Object> memberParams = Maps.newHashMap();
memberParams.put("member_id", adapayMemberId);
memberParams.put("app_id", ADAPAY_APP_ID);
Map<String, Object> member = CorpMember.query(memberParams);
if (member == null || member.isEmpty() || !"succeeded".equals(member.get("status"))) {
return null;
}
return null;
}
2023-06-15 14:06:33 +08:00
/**
* 查询汇付结算账户信息
*/
public AdapaySettleAccountVO queryAdapaySettleAccount(String adapayMemberId, String settleAccountId) throws BaseAdaPayException {
if (StringUtils.isBlank(adapayMemberId) || StringUtils.isBlank(settleAccountId)) {
return null;
}
2023-06-09 09:44:20 +08:00
Map<String, Object> settleCountParams = Maps.newHashMap();
2023-06-15 14:06:33 +08:00
settleCountParams.put("settle_account_id", settleAccountId);
settleCountParams.put("member_id", adapayMemberId);
2023-06-09 09:44:20 +08:00
settleCountParams.put("app_id", ADAPAY_APP_ID);
2023-06-15 14:06:33 +08:00
log.info("查询汇付结算账户信息param:{}", JSON.toJSONString(settleCountParams));
Map<String, Object> settleAccount = SettleAccount.query(settleCountParams);
2023-06-15 14:06:33 +08:00
log.info("查询汇付结算账户信息result:{}", JSON.toJSONString(settleAccount));
if (settleAccount == null || settleAccount.isEmpty() || !"succeeded".equals(settleAccount.get("status"))) {
return null;
}
QueryMemberResponse.SettleAccount settleAccountInfo = JSON.parseObject(JSON.toJSONString(settleAccount), QueryMemberResponse.SettleAccount.class);
QueryMemberResponse.AccountInfo accountInfo = settleAccountInfo.getAccount_info();
AdapaySettleAccountVO resultVO = AdapaySettleAccountVO.builder()
.adapayMemberId(settleAccountInfo.getMember_id())
.settleAccountId(settleAccountInfo.getId())
.cardId(accountInfo.getCard_id())
.cardName(accountInfo.getCard_name())
.certId(accountInfo.getCert_id())
.certType(accountInfo.getCert_type())
.telNo(accountInfo.getTel_no())
.bankName(accountInfo.getBank_name())
.bankCode(accountInfo.getBank_code())
.bankAcctType(accountInfo.getBank_acct_type())
.provCode(accountInfo.getProv_code())
.areaCode(accountInfo.getArea_code())
.build();
return resultVO;
2023-06-09 09:44:20 +08:00
}
2023-06-13 09:20:24 +08:00
/**
* 查询汇付会员账户余额
*/
2023-06-13 11:28:15 +08:00
public AdapayAccountBalanceVO queryAdapayAccountBalance(String merchantId) throws BaseAdaPayException {
AdapayAccountBalanceVO vo = AdapayAccountBalanceVO.builder().build();
// 通过merchantId 查询出汇付会员id 和 结算账户id用来查询余额
2023-06-15 14:40:11 +08:00
AdapayMemberAccount adapayMemberAccount = adapayMemberAccountService.selectByMerchantId(merchantId);
if (adapayMemberAccount == null) {
2023-06-28 10:33:50 +08:00
log.error("通过merchantId:{}, 没有查询到结算账户配置", merchantId);
2023-06-13 11:28:15 +08:00
return vo;
}
2023-06-15 16:42:33 +08:00
String settle_account_id = adapayMemberAccount.getSettleAccountId();
2023-06-15 14:40:11 +08:00
String member_id = adapayMemberAccount.getAdapayMemberId();
2023-06-13 11:28:15 +08:00
Map<String, Object> queryParams = Maps.newHashMap();
queryParams.put("settle_account_id", settle_account_id);
queryParams.put("member_id", member_id);
queryParams.put("app_id", ADAPAY_APP_ID);
Map<String, Object> settleCount = SettleAccount.balance(queryParams);
if (settleCount == null || settleCount.isEmpty() || !"succeeded".equals(settleCount.get("status"))) {
2023-06-13 11:28:15 +08:00
return vo;
}
vo.setFrzBalance(new BigDecimal((String) settleCount.get("frz_balance")));
vo.setAcctBalance(new BigDecimal((String) settleCount.get("acct_balance")));
vo.setAvlBalance(new BigDecimal((String) settleCount.get("avl_balance")));
vo.setLastAvlBalance(new BigDecimal((String) settleCount.get("last_avl_balance")));
return vo;
2023-06-13 09:20:24 +08:00
}
2023-06-14 14:48:58 +08:00
2023-06-20 10:09:21 +08:00
/**
* 更新结算账户设置
2023-06-20 11:39:24 +08:00
*
2023-06-20 10:09:21 +08:00
* @param dto
* @throws BaseAdaPayException
*/
2023-06-17 16:33:01 +08:00
public void updateSettleAccountConfig(UpdateAccountConfigDTO dto) throws BaseAdaPayException {
// 通过merchantId 查询出汇付会员id 和 结算账户id用来查询余额
AdapayMemberAccount adapayMemberAccount = adapayMemberAccountService.selectByMerchantId(dto.getMerchantId());
if (adapayMemberAccount == null) {
2023-06-28 10:33:50 +08:00
log.error("通过merchantId:{}, 没有查询到结算账户配置", dto.getMerchantId());
2023-06-17 16:33:01 +08:00
return;
}
// 修改账户配置
Map<String, Object> params = Maps.newHashMap();
params.put("app_id", ADAPAY_APP_ID);
params.put("member_id", adapayMemberAccount.getAdapayMemberId());
params.put("settle_account_id", adapayMemberAccount.getSettleAccountId());
if (StringUtils.isNotBlank(dto.getMinAmt())) {
params.put("min_amt", dto.getMinAmt());
}
if (StringUtils.isNotBlank(dto.getRemainedAmt())) {
params.put("remained_amt", dto.getRemainedAmt());
}
if (StringUtils.isNotBlank(dto.getChannelRemark())) {
params.put("channel_remark", dto.getChannelRemark());
}
Map<String, Object> settleCount = SettleAccount.update(params);
}
2023-06-20 10:09:21 +08:00
/**
* 创建企业用户
*/
2023-06-20 16:24:04 +08:00
public void createCorpMember(CreateSettleAccountDTO dto) throws BaseAdaPayException, IOException {
2023-06-20 11:39:24 +08:00
Map<String, Object> memberParams = Maps.newHashMap();
2023-06-29 16:16:25 +08:00
String adapayMemberId = Constants.ADAPAY_CORP_MEMBER_PREFIX + IdUtils.getMemberId();
2023-06-20 11:39:24 +08:00
memberParams.put("member_id", adapayMemberId);
memberParams.put("app_id", ADAPAY_APP_ID);
memberParams.put("order_no", "jsdk_order" + System.currentTimeMillis());
memberParams.put("social_credit_code_expires", dto.getSocialCreditCodeExpires());
memberParams.put("business_scope", dto.getBusinessScope());
memberParams.put("name", dto.getBusinessName());
memberParams.put("prov_code", dto.getProvCode());
memberParams.put("area_code", dto.getAreaCode());
memberParams.put("social_credit_code", dto.getSocialCreditCode());
memberParams.put("legal_person", dto.getLegalPerson());
memberParams.put("legal_cert_id", dto.getLegalCertId());
memberParams.put("legal_cert_id_expires", dto.getLegalCertIdExpires());
memberParams.put("legal_mp", dto.getLegalMp());
memberParams.put("address", dto.getAddress());
memberParams.put("zip_code", dto.getZipCode());
memberParams.put("telphone", dto.getTelphone());
memberParams.put("email", dto.getEmail());
memberParams.put("bank_code", dto.getBankCode());
memberParams.put("bank_acct_type", dto.getBankAcctType());
memberParams.put("card_no", dto.getCardNo());
memberParams.put("card_name", dto.getCardName());
2023-06-29 15:06:31 +08:00
File file = ZipUtil.createZipFileFromImages(dto.getImgList());
2023-06-20 11:39:24 +08:00
log.info("创建企业账户param:{}", memberParams);
Map<String, Object> member = CorpMember.create(memberParams, file);
log.info("创建企业账户result:{}", member);
2023-06-20 13:37:58 +08:00
// 保存到数据库
AdapayMemberAccount adapayMemberAccount = new AdapayMemberAccount();
adapayMemberAccount.setMerchantId(dto.getMerchantId());
adapayMemberAccount.setAdapayMemberId(adapayMemberId);
2023-07-05 14:49:47 +08:00
adapayMemberAccount.setStatus(Constants.ZERO);
2023-06-20 13:37:58 +08:00
adapayMemberAccountService.insertAdapayMemberAccount(adapayMemberAccount);
2023-06-20 10:09:21 +08:00
}
2023-06-20 16:24:04 +08:00
2023-06-28 10:33:50 +08:00
public void withdraw(WithdrawDTO dto) throws BaseAdaPayException {
// 通过merchantId 查询出汇付会员id 和 结算账户id用来查询余额
AdapayMemberAccount adapayMemberAccount = adapayMemberAccountService.selectByMerchantId(dto.getMerchantId());
if (adapayMemberAccount == null) {
log.error("通过merchantId:{}, 没有查询到结算账户配置", dto.getMerchantId());
return;
}
Map<String, Object> settleCountParams = Maps.newHashMap();
settleCountParams.put("order_no", "jsdk_payment_" + System.currentTimeMillis());
settleCountParams.put("cash_amt", AdapayUtil.formatAmount(dto.getCashAmt()));
settleCountParams.put("member_id", adapayMemberAccount.getAdapayMemberId());
settleCountParams.put("app_id", ADAPAY_APP_ID);
settleCountParams.put("settle_account_id", adapayMemberAccount.getSettleAccountId());
settleCountParams.put("cash_type", "T1");
settleCountParams.put("notify_url", "");
log.info("取现接口,请求参数:" + JSON.toJSONString(settleCountParams));
Map<String, Object> settleCount = Drawcash.create(settleCountParams);
log.info("取现接口返回参数" + JSON.toJSONString(settleCount));
}
2023-06-06 16:18:25 +08:00
}