2023-06-06 16:18:25 +08:00
|
|
|
|
package com.jsowell.adapay.service;
|
|
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
2023-06-07 15:10:35 +08:00
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
|
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
|
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-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-07 16:04:10 +08:00
|
|
|
|
import com.jsowell.adapay.dto.AdapayMemberInfoDTO;
|
2023-06-13 11:28:15 +08:00
|
|
|
|
import com.jsowell.adapay.vo.AdapayAccountBalanceVO;
|
2023-06-08 15:00:30 +08:00
|
|
|
|
import com.jsowell.adapay.vo.AdapayMemberInfoVO;
|
2023-06-14 14:48:58 +08:00
|
|
|
|
import com.jsowell.adapay.vo.AdapaySettleAccountVO;
|
2023-06-09 08:18:42 +08:00
|
|
|
|
import com.jsowell.common.exception.BusinessException;
|
2023-06-07 16:04:10 +08:00
|
|
|
|
import com.jsowell.common.util.StringUtils;
|
2023-06-06 16:18:25 +08:00
|
|
|
|
import com.jsowell.common.util.id.IdUtils;
|
2023-06-07 15:10:35 +08:00
|
|
|
|
import com.jsowell.pile.domain.AdapayMemberInfo;
|
2023-06-12 15:59:10 +08:00
|
|
|
|
import com.jsowell.pile.domain.AdapaySettleAccount;
|
2023-06-07 15:10:35 +08:00
|
|
|
|
import com.jsowell.pile.service.AdapayMemberInfoService;
|
2023-06-12 15:59:10 +08:00
|
|
|
|
import com.jsowell.pile.service.IAdapaySettleAccountService;
|
2023-06-06 16:18:25 +08:00
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
2023-06-08 15:00:30 +08:00
|
|
|
|
import org.springframework.beans.BeanUtils;
|
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-13 11:28:15 +08:00
|
|
|
|
import java.math.BigDecimal;
|
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-07 15:10:35 +08:00
|
|
|
|
@Autowired
|
|
|
|
|
|
private AdapayMemberInfoService adapayMemberInfoService;
|
2023-06-07 15:13:45 +08:00
|
|
|
|
|
2023-06-12 15:59:10 +08:00
|
|
|
|
@Autowired
|
|
|
|
|
|
private IAdapaySettleAccountService adapaySettleAccountService;
|
2023-06-06 16:18:25 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
2023-06-09 09:44:20 +08:00
|
|
|
|
* 创建汇付会员
|
|
|
|
|
|
* @param dto
|
|
|
|
|
|
* @throws Exception
|
2023-06-06 16:18:25 +08:00
|
|
|
|
*/
|
2023-06-07 16:04:10 +08:00
|
|
|
|
public void createMember(AdapayMemberInfoDTO dto) throws Exception {
|
2023-06-09 15:34:35 +08:00
|
|
|
|
AdapayMemberInfo adapayMemberInfo = adapayMemberInfoService.selectByMerchantId(dto.getMerchantId());
|
|
|
|
|
|
if (adapayMemberInfo != null) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2023-06-07 16:04:10 +08:00
|
|
|
|
log.info("=======execute CreateMember begin=======");
|
|
|
|
|
|
Map<String, Object> memberParams = Maps.newHashMap();
|
|
|
|
|
|
memberParams.put("member_id", "AM" + IdUtils.getMemberId());
|
|
|
|
|
|
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 08:18:42 +08:00
|
|
|
|
// 创建成功保存到数据库
|
|
|
|
|
|
JSONObject jsonObject = JSON.parseObject(String.valueOf(member));
|
|
|
|
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
|
|
|
|
mapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
|
2023-06-09 15:34:35 +08:00
|
|
|
|
adapayMemberInfo = mapper.readValue(jsonObject.toJSONString(), AdapayMemberInfo.class);
|
2023-06-09 08:18:42 +08:00
|
|
|
|
adapayMemberInfo.setMerchantId(dto.getMerchantId());
|
|
|
|
|
|
adapayMemberInfoService.insert(adapayMemberInfo);
|
2023-06-09 09:44:20 +08:00
|
|
|
|
|
|
|
|
|
|
dto.setAdapayMemberId(adapayMemberInfo.getMemberId());
|
|
|
|
|
|
// 创建结算账户
|
|
|
|
|
|
createSettleAccount(dto);
|
2023-06-07 16:04:10 +08:00
|
|
|
|
}
|
2023-06-08 15:00:30 +08:00
|
|
|
|
|
2023-06-09 09:44:20 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 查询汇付会员信息
|
|
|
|
|
|
* @param merchantId
|
|
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
2023-06-08 15:00:30 +08:00
|
|
|
|
public AdapayMemberInfoVO selectAdapayMember(String merchantId) {
|
|
|
|
|
|
AdapayMemberInfo adapayMemberInfo = adapayMemberInfoService.selectByMerchantId(merchantId);
|
|
|
|
|
|
if (adapayMemberInfo == null) {
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
AdapayMemberInfoVO resultVO = new AdapayMemberInfoVO();
|
|
|
|
|
|
BeanUtils.copyProperties(adapayMemberInfo, resultVO);
|
|
|
|
|
|
return resultVO;
|
|
|
|
|
|
}
|
2023-06-09 09:44:20 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 创建结算账户
|
|
|
|
|
|
*/
|
|
|
|
|
|
public void createSettleAccount(AdapayMemberInfoDTO dto) throws BaseAdaPayException {
|
|
|
|
|
|
Map<String, Object> accountInfo = Maps.newHashMap();
|
|
|
|
|
|
// 银行卡号
|
|
|
|
|
|
accountInfo.put("card_id", dto.getCardId());
|
|
|
|
|
|
// 银行卡对应的户名
|
|
|
|
|
|
accountInfo.put("card_name", dto.getCardName());
|
|
|
|
|
|
// 证件号,银行账户类型为对私时,必填
|
|
|
|
|
|
accountInfo.put("cert_id", dto.getCertId());
|
|
|
|
|
|
// 证件类型,仅支持:00-身份证,银行账户类型为对私时,必填
|
|
|
|
|
|
accountInfo.put("cert_type", "00");
|
|
|
|
|
|
// 手机号
|
|
|
|
|
|
accountInfo.put("tel_no", dto.getTelNo());
|
|
|
|
|
|
// 银行编码,详见附录 银行代码,银行账户类型对公时,必填
|
|
|
|
|
|
accountInfo.put("bank_code", dto.getBankCode());
|
|
|
|
|
|
// 银行账户类型:1-对公;2-对私
|
|
|
|
|
|
accountInfo.put("bank_acct_type", dto.getBankAcctType());
|
|
|
|
|
|
// 银行账户开户银行所在省份编码 (省市编码),银行账户类型为对公时,必填
|
|
|
|
|
|
accountInfo.put("prov_code", dto.getAreaCode());
|
|
|
|
|
|
// 银行账户开户银行所在地区编码(省市编码),银行账户类型为对公时,必填
|
|
|
|
|
|
accountInfo.put("area_code", dto.getAreaCode());
|
|
|
|
|
|
|
|
|
|
|
|
Map<String, Object> settleCountParams = Maps.newHashMap();
|
|
|
|
|
|
settleCountParams.put("member_id", dto.getAdapayMemberId());
|
|
|
|
|
|
settleCountParams.put("app_id", ADAPAY_APP_ID);
|
|
|
|
|
|
// channel String Y 目前仅支持:bank_account(银行卡)
|
|
|
|
|
|
settleCountParams.put("channel", "bank_account");
|
|
|
|
|
|
settleCountParams.put("account_info", accountInfo);
|
|
|
|
|
|
Map<String, Object> settleCount = SettleAccount.create(settleCountParams);
|
|
|
|
|
|
log.info("创建汇付结算账户result:{}", settleCount);
|
2023-06-12 15:59:10 +08:00
|
|
|
|
|
2023-06-13 11:33:13 +08:00
|
|
|
|
if (settleCount != null && "succeeded".equals(settleCount.get("status"))) {
|
2023-06-13 11:28:15 +08:00
|
|
|
|
AdapaySettleAccount adapaySettleAccount = new AdapaySettleAccount();
|
|
|
|
|
|
adapaySettleAccount.setSettleAccountId((String) settleCount.get("id"));
|
|
|
|
|
|
adapaySettleAccount.setMerchantId(dto.getMerchantId());
|
|
|
|
|
|
adapaySettleAccount.setAdapayMemberId(dto.getAdapayMemberId());
|
|
|
|
|
|
adapaySettleAccount.setCardId(dto.getCardId());
|
|
|
|
|
|
adapaySettleAccount.setCardName(dto.getCardName());
|
|
|
|
|
|
adapaySettleAccount.setCertId(dto.getCertId());
|
|
|
|
|
|
adapaySettleAccount.setCertType("00");
|
|
|
|
|
|
adapaySettleAccount.setTelNo(dto.getTelNo());
|
|
|
|
|
|
adapaySettleAccount.setBankCode(dto.getBankCode());
|
|
|
|
|
|
adapaySettleAccount.setBankAcctType(dto.getBankAcctType());
|
|
|
|
|
|
adapaySettleAccount.setProvCode(dto.getProvCode());
|
|
|
|
|
|
adapaySettleAccount.setAreaCode(dto.getAreaCode());
|
|
|
|
|
|
adapaySettleAccountService.insertAdapaySettleAccount(adapaySettleAccount);
|
|
|
|
|
|
}
|
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,用来查询余额
|
|
|
|
|
|
AdapaySettleAccount adapaySettleAccount = adapaySettleAccountService.selectByMerchantId(merchantId);
|
|
|
|
|
|
if (adapaySettleAccount == null) {
|
|
|
|
|
|
return vo;
|
|
|
|
|
|
}
|
|
|
|
|
|
String settle_account_id = adapaySettleAccount.getAdapayMemberId();
|
|
|
|
|
|
String member_id = adapaySettleAccount.getAdapayMemberId();
|
|
|
|
|
|
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);
|
2023-06-13 11:33:13 +08:00
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
public AdapaySettleAccountVO selectSettleAccount(String merchantId) {
|
|
|
|
|
|
AdapaySettleAccountVO vo = new AdapaySettleAccountVO();
|
|
|
|
|
|
// 通过merchantId 查询出汇付会员id 和 结算账户id,用来查询余额
|
|
|
|
|
|
AdapaySettleAccount adapaySettleAccount = adapaySettleAccountService.selectByMerchantId(merchantId);
|
|
|
|
|
|
if (adapaySettleAccount == null) {
|
|
|
|
|
|
return vo;
|
|
|
|
|
|
}
|
|
|
|
|
|
BeanUtils.copyProperties(adapaySettleAccount, vo);
|
|
|
|
|
|
return vo;
|
|
|
|
|
|
}
|
2023-06-06 16:18:25 +08:00
|
|
|
|
}
|