Commit 15d767f6 by zhiwj

代码结构调整

parent 3d13d105
......@@ -10,18 +10,20 @@ public enum BuyTypeEnum {
/**
* 短信套餐包购买
*/
BUY_SHORT_MESSAGE_PACKAGE(2, "短信套餐包购买"),
BUY_SHORT_MESSAGE_PACKAGE(2, "短信套餐包购买", "BUY_SHORT_MESSAGE_PACKAGE"),
/**
* 商户余额充值
*/
BALANCE_RECHARGE(1, "商户余额充值");
BALANCE_RECHARGE(1, "商户余额充值", "BALANCE_RECHARGE");
private int code;
private String message;
private String type;
private BuyTypeEnum(int code, String message) {
private BuyTypeEnum(int code, String message, String type) {
this.code = code;
this.message = message;
this.type = type;
}
public static String getMessageByCode(Integer code) {
......@@ -36,19 +38,27 @@ public enum BuyTypeEnum {
return "未知";
}
public int getCode() {
return code;
public static String getTypeByCode(Integer code) {
if (code == null) {
return "未知";
}
for (BuyTypeEnum typeEnum : values()) {
if (code.intValue() == typeEnum.getCode()) {
return typeEnum.getType();
}
}
return "未知";
}
public void setCode(int code) {
this.code = code;
public int getCode() {
return code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
public String getType() {
return type;
}
}
......@@ -93,4 +93,23 @@ public interface Constants {
String NATIONCODESIGN = "+86";
String ERP = "erp";
/**
* 支付锁
*/
String PAY_LOCK = "enterprise:biilingcenterpay";
/**
* 余额支付锁
*/
String PAY_BALANCE_LOCK = "enterprise:biilingcenterbalancepay";
/**
* 退款锁
*/
String REFUND_LOCK = "enterprise:refund";
/**
* 支付回调
*/
String PAY_BACK_LOCK = "enterprise:biilingcentercallback";
}
......@@ -31,6 +31,10 @@ public class ApprovalCallBackDTO implements Serializable {
@NotNull(message = "审批时间不能为空, 格式为yyyyMMdd", groups = ApprovalValid.class)
private String timeEnd;
private String prepayId;
/**
* 支付类型
*/
private Integer payType;
private String onlineSerialNumber;
......@@ -138,6 +142,14 @@ public class ApprovalCallBackDTO implements Serializable {
this.totalFee = totalFee;
}
public Integer getPayType() {
return payType;
}
public void setPayType(Integer payType) {
this.payType = payType;
}
@Override
public String toString() {
return "ApprovalCallBackDTO{" +
......
......@@ -329,7 +329,7 @@ public class AuditLogApiServiceImpl implements AuditLogApiService {
if (AuditTypeEnum.HAOBAN.getCode().equals(auditLogDTO.getAuditType())) {
position = StringUtils.isBlank(auditLogDTO.getPosition()) ? "" : auditLogDTO.getPosition();
} else {
UserDTO userDTO = userApiService.getUserById(auditLogDTO.getApplyUserId()).getResult();
UserDTO userDTO = userApiService.getUserInfoById(auditLogDTO.getApplyUserId()).getResult();
LoginUserTypeEnum userTypeEnum = LoginUserTypeEnum.getByCode(userDTO.getLoginType());
position = userTypeEnum != null ? userTypeEnum.getMessage() : "";
}
......
......@@ -238,44 +238,21 @@ public class BillingAccountApiServiceImpl implements BillingAccountApiService {
@Override
public ServiceResponse<Integer> deductAccount(double totalFee, Integer enterpriseId) {
if (isNotInWhiteList(enterpriseId)) {
TabBillingAccount billingAccount = billingAccountService.getByEnterpriseId(enterpriseId);
Double accountBalance = billingAccount.getAccountBalance();
if (accountBalance >= 0) {
int i = billingAccountService.deductAccount(totalFee, enterpriseId);
LOGGER.info("扣费是否成功:{}", i > 0);
if (i > 0) {
TabBillingAccount account = this.billingAccountService.getByEnterpriseId(enterpriseId);
if (account.getAccountBalance() < 0) {
Object cache = RedisUtil.getCache("message:" + enterpriseId);
if (cache == null) {
String content = "您的达摩账户已欠费, 为避免您的业务受到影响,请及时<a href='" + config.getHost() + "cost-center/billing-center/recharge'>充值</a>";
this.pushMessageApiService.pushMessage(enterpriseId, "系统消息", "商户欠费", content);
sendSms(enterpriseId, "GICJFZX012");
// 待办事项
sendTotoItem(enterpriseId, "您的达摩账户已欠费");
RedisUtil.setCache("message:" + enterpriseId, 1, 1L, TimeUnit.DAYS);
}
} else if (account.getAccountBalance() < ACCOUNT_NOTICE_LINE) {
Object cache = RedisUtil.getCache("insufficientAccountBalanceMessage:" + enterpriseId);
if (cache == null) {
String content = "您的达摩账户余额已不足1000元,若账户欠费将中断部分正常业务,请及时<a href='" + config.getHost() + "cost-center/billing-center/recharge'>充值</a>";
this.pushMessageApiService.pushMessage(enterpriseId, "系统消息", "商户欠费", content);
sendSms(enterpriseId, "GICJFZX001");
// 待办事项
sendTotoItem(enterpriseId, "您的达摩账户余额已不足1000元");
RedisUtil.setCache("insufficientAccountBalanceMessage:" + enterpriseId, 1, 7L, TimeUnit.DAYS);
}
}
}
return ServiceResponse.success(i);
} else {
return ServiceResponse.failure(ErrorCode.OPERATION_FAILED.getCode(), "余额不足");
}
} else {
// 在白名单中
if (isInWhiteList(enterpriseId)) {
int i = billingAccountService.deductAccount(totalFee, enterpriseId);
return ServiceResponse.success(i);
}
// 不在白名单中
TabBillingAccount billingAccount = billingAccountService.getByEnterpriseId(enterpriseId);
Double accountBalance = billingAccount.getAccountBalance();
if (accountBalance < 0) {
return ServiceResponse.failure(ErrorCode.OPERATION_FAILED.getCode(), "余额不足");
}
int i = billingAccountService.deductAccount(totalFee, enterpriseId);
LOGGER.info("扣费是否成功:{}", i > 0);
return ServiceResponse.success(i);
}
private void sendSms(Integer enterpriseId, String sceneId) {
......@@ -300,14 +277,21 @@ public class BillingAccountApiServiceImpl implements BillingAccountApiService {
Integer enterpriseId = enterpriseDTO.getEnterpriseId();
if (isNotInWhiteList(enterpriseId)) {
TabBillingAccount account = this.billingAccountService.getByEnterpriseId(enterpriseId);
if (account.getAccountBalance() < ACCOUNT_NOTICE_LINE) {
String[] smsArr = new String[]{enterpriseDTO.getEnterpriseName(), String.valueOf(-account.getAccountBalance())};
ServiceResponse<UserDTO> userResult = userApiService.getUserByEnterpriseId(enterpriseId);
UserDTO userDTO = userResult.getResult();
ServiceResponse<Void> smsSendResult = smsSendApiService.sendPlatformSms(account.getAccountBalance() < 0 ? "GICJFZX012" :"GICJFZX001",
enterpriseId, userDTO.getPhoneAreaCode(), userDTO.getPhoneNumber(), smsArr);
if (!smsSendResult.isSuccess()) {
LOGGER.warn(smsSendResult.getMessage());
if (account.getAccountBalance() < 0) {
String content = "您的达摩账户已欠费, 为避免您的业务受到影响,请及时<a href='" + config.getHost() + "cost-center/billing-center/recharge'>充值</a>";
this.pushMessageApiService.pushMessage(enterpriseId, "系统消息", "商户欠费", content);
sendSms(enterpriseId, "GICJFZX012");
// 待办事项
sendTotoItem(enterpriseId, "您的达摩账户已欠费");
} else if (account.getAccountBalance() < ACCOUNT_NOTICE_LINE) {
Object cache = RedisUtil.getCache("insufficientAccountBalanceMessage:" + enterpriseId);
if (cache == null) {
String content = "您的达摩账户余额已不足1000元,若账户欠费将中断部分正常业务,请及时<a href='" + config.getHost() + "cost-center/billing-center/recharge'>充值</a>";
this.pushMessageApiService.pushMessage(enterpriseId, "系统消息", "商户欠费", content);
sendSms(enterpriseId, "GICJFZX001");
// 待办事项
sendTotoItem(enterpriseId, "您的达摩账户余额已不足1000元");
RedisUtil.setCache("insufficientAccountBalanceMessage:" + enterpriseId, 1, 7L, TimeUnit.DAYS);
}
}
}
......@@ -320,6 +304,11 @@ public class BillingAccountApiServiceImpl implements BillingAccountApiService {
return this.enterpriseWhiteListService.getEnterpriseWhiteList(enterpriseId) == null;
}
private boolean isInWhiteList(Integer enterpriseId) {
// 判断商户在不在白名单中
return this.enterpriseWhiteListService.getEnterpriseWhiteList(enterpriseId) != null;
}
private void sendTotoItem(Integer enterpriseId, String content) {
TodoItemDTO todoItemDTO = new TodoItemDTO();
UserDTO userDTO = userApiService.getUserByEnterpriseId(enterpriseId).getResult();
......
package com.gic.enterprise.service.outer.impl;
import java.math.BigDecimal;
import java.util.Date;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import com.alibaba.fastjson.JSON;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.commons.util.EntityUtil;
import com.gic.enterprise.constant.BillingPayResultEnum;
import com.gic.enterprise.constant.Constants;
import com.gic.enterprise.constant.PayTypeEnum;
import com.gic.enterprise.dto.*;
import com.gic.enterprise.entity.TabBillingAccount;
......@@ -20,7 +12,16 @@ import com.gic.enterprise.entity.TabBillingRefund;
import com.gic.enterprise.error.ErrorCode;
import com.gic.enterprise.service.*;
import com.gic.enterprise.utils.CreateRandomUtils;
import com.gic.redis.data.util.RedisUtil;
import com.gic.store.utils.valid.ValidUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import java.math.BigDecimal;
import java.util.Date;
/**
*
......@@ -96,27 +97,35 @@ public class BillingBalancePayApiServiceImpl implements BillingBalancePayApiServ
@Override
public ServiceResponse<BalancePayReturnDTO> savePayAndConfirmPay(OutPayDTO outPayDTO) {
ServiceResponse paramResult = ValidUtil.allCheckValidate(outPayDTO, OutPayDTO.ConfirmPay.class, OutPayDTO.SavePrePay.class);
if (!paramResult.isSuccess()) {
return paramResult;
}
outPayDTO.setPayType(PayTypeEnum.BALANCE_PAY.getCode());
ServiceResponse<BillingPayInfoDTO> serviceResponse = billingPayInfoApiService.savePrePayInfo(outPayDTO);
if (!serviceResponse.isSuccess()) {
return ServiceResponse.failure(serviceResponse.getCode(), serviceResponse.getMessage());
}
ServiceResponse<BillingPayInfoDTO> confirmPayResponse = billingPayInfoApiService.confirmPay(outPayDTO);
if (confirmPayResponse.isSuccess()) {
BillingPayInfoDTO payInfoDTO = confirmPayResponse.getResult();
BalancePayReturnDTO balancePayReturnDTO = new BalancePayReturnDTO();
balancePayReturnDTO.setPayInfoSerialNumber(payInfoDTO.getSerialNumber());
balancePayReturnDTO.setOrderSerialNumber(payInfoDTO.getOrderSerialNumber());
balancePayReturnDTO.setResultCode(payInfoDTO.getResultCode());
balancePayReturnDTO.setTotalFeePaid(payInfoDTO.getTotalFeePaid());
balancePayReturnDTO.setEnterpriseId(payInfoDTO.getEnterpriseId());
return ServiceResponse.success(balancePayReturnDTO);
} else {
return ServiceResponse.failure(confirmPayResponse.getCode(), confirmPayResponse.getMessage());
try {
RedisUtil.lock(Constants.PAY_BALANCE_LOCK + outPayDTO.getEnterpriseId(), 5L);
ServiceResponse paramResult = ValidUtil.allCheckValidate(outPayDTO, OutPayDTO.ConfirmPay.class, OutPayDTO.SavePrePay.class);
if (!paramResult.isSuccess()) {
return paramResult;
}
outPayDTO.setPayType(PayTypeEnum.BALANCE_PAY.getCode());
ServiceResponse<BillingPayInfoDTO> serviceResponse = billingPayInfoApiService.savePrePayInfo(outPayDTO);
if (!serviceResponse.isSuccess()) {
return ServiceResponse.failure(serviceResponse.getCode(), serviceResponse.getMessage());
}
ServiceResponse<BillingPayInfoDTO> confirmPayResponse = billingPayInfoApiService.confirmPay(outPayDTO);
if (confirmPayResponse.isSuccess()) {
BillingPayInfoDTO payInfoDTO = confirmPayResponse.getResult();
BalancePayReturnDTO balancePayReturnDTO = new BalancePayReturnDTO();
balancePayReturnDTO.setPayInfoSerialNumber(payInfoDTO.getSerialNumber());
balancePayReturnDTO.setOrderSerialNumber(payInfoDTO.getOrderSerialNumber());
balancePayReturnDTO.setResultCode(payInfoDTO.getResultCode());
balancePayReturnDTO.setTotalFeePaid(payInfoDTO.getTotalFeePaid());
balancePayReturnDTO.setEnterpriseId(payInfoDTO.getEnterpriseId());
return ServiceResponse.success(balancePayReturnDTO);
} else {
return ServiceResponse.failure(confirmPayResponse.getCode(), confirmPayResponse.getMessage());
}
} catch (Exception e) {
logger.info("余额支付回调错误", e);
throw new RuntimeException(e);
} finally {
RedisUtil.unlock(Constants.PAY_BALANCE_LOCK);
}
}
......@@ -150,60 +159,70 @@ public class BillingBalancePayApiServiceImpl implements BillingBalancePayApiServ
@Override
public ServiceResponse<RefundReturnDTO> refund(RefundDTO refundDTO) {
ServiceResponse paramResult = ValidUtil.allCheckValidate(refundDTO, RefundDTO.RefundValid.class);
if (!paramResult.isSuccess()) {
return paramResult;
}
ServiceResponse<BillingPayInfoDTO> payService = this.billingPayInfoApiService.getPayInfo(refundDTO.getEnterpriseId(), refundDTO.getOrderSerialNumber(), refundDTO.getRequestCode());
if (!payService.isSuccess()) {
return ServiceResponse.failure(payService.getCode(), payService.getMessage());
}
BillingPayInfoDTO payInfo = payService.getResult();
if (!BillingPayResultEnum.SUCCESS.getCode().equals(payInfo.getResultCode())) {
return ServiceResponse.failure(ErrorCode.NOTEXISTS.getCode(), "退款失败, 订单号不存在");
}
// 退款单号是否重复
TabBillingRefund refund = billingRefundService.getRefund(refundDTO.getEnterpriseId(), refundDTO.getRefundSerialNumber(), refundDTO.getRequestCode());
if (refund != null) {
return ServiceResponse.failure(ErrorCode.PARAMETER_ERROR.getCode(), "退款失败, 退款单号重复");
}
// 每笔退款金额(部分)超出订单总金额
Double refundFee = refundDTO.getRefundFee();
if (refundFee > payInfo.getTotalFeePaid()) {
return ServiceResponse.failure(ErrorCode.PARAMETER_ERROR.getCode(), "退款失败, 退款金额超出总金额");
}
// 每笔退款金额累计超出订单总金额
Double oldTotalFee = billingRefundService.getTotalRefundFee(refundDTO.getEnterpriseId(), refundDTO.getOrderSerialNumber(), refundDTO.getRequestCode());
if (oldTotalFee == null) {
oldTotalFee = 0d;
}
double olderFee = new BigDecimal(Double.toString(refundFee)).add(new BigDecimal(Double.toString(oldTotalFee))).doubleValue();
if (olderFee > refundFee) {
return ServiceResponse.failure(ErrorCode.PARAMETER_ERROR.getCode(), "退款失败, 退款金额超出总金额");
}
// 保存退款记录
Date date = new Date();
String serialNumber = CreateRandomUtils.createSerialNumber();
refundDTO.setRefundPayNumber(serialNumber);
refundDTO.setTimeEnd(date);
Integer line = billingRefundService.save(refundDTO);
if (line == 0) {
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
logger.info("保存退款记录失败:{}", JSON.toJSONString(refundDTO));
return ServiceResponse.failure(ErrorCode.UNKNOWN_ERROR.getCode(), "保存退款记录失败");
}
// 退款
int i = billingAccountService.updateAccountAfterRechargeOrRefund(olderFee, refundDTO.getEnterpriseId());
if (i == 0) {
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
logger.info("退款失败:{}", JSON.toJSONString(refundDTO));
return ServiceResponse.failure(ErrorCode.UNKNOWN_ERROR.getCode(), "退款失败");
try {
RedisUtil.lock(Constants.REFUND_LOCK + refundDTO.getEnterpriseId(), 5L);
ServiceResponse paramResult = ValidUtil.allCheckValidate(refundDTO, RefundDTO.RefundValid.class);
if (!paramResult.isSuccess()) {
return paramResult;
}
ServiceResponse<BillingPayInfoDTO> payService =
this.billingPayInfoApiService.getPayInfo(refundDTO.getEnterpriseId(), refundDTO.getOrderSerialNumber(), refundDTO.getRequestCode());
if (!payService.isSuccess()) {
return ServiceResponse.failure(payService.getCode(), payService.getMessage());
}
BillingPayInfoDTO payInfo = payService.getResult();
if (!BillingPayResultEnum.SUCCESS.getCode().equals(payInfo.getResultCode())) {
return ServiceResponse.failure(ErrorCode.NOTEXISTS.getCode(), "退款失败, 订单号不存在");
}
// 退款单号是否重复
TabBillingRefund refund = billingRefundService.getRefund(refundDTO.getEnterpriseId(), refundDTO.getRefundSerialNumber(), refundDTO.getRequestCode());
if (refund != null) {
return ServiceResponse.failure(ErrorCode.PARAMETER_ERROR.getCode(), "退款失败, 退款单号重复");
}
// 每笔退款金额(部分)超出订单总金额
Double refundFee = refundDTO.getRefundFee();
if (refundFee > payInfo.getTotalFeePaid()) {
return ServiceResponse.failure(ErrorCode.PARAMETER_ERROR.getCode(), "退款失败, 退款金额超出总金额");
}
// 每笔退款金额累计超出订单总金额
Double oldTotalFee = billingRefundService.getTotalRefundFee(refundDTO.getEnterpriseId(), refundDTO.getOrderSerialNumber(), refundDTO.getRequestCode());
if (oldTotalFee == null) {
oldTotalFee = 0d;
}
double olderFee = new BigDecimal(Double.toString(refundFee)).add(new BigDecimal(Double.toString(oldTotalFee))).doubleValue();
if (olderFee > refundFee) {
return ServiceResponse.failure(ErrorCode.PARAMETER_ERROR.getCode(), "退款失败, 退款金额超出总金额");
}
// 保存退款记录
Date date = new Date();
String serialNumber = CreateRandomUtils.createSerialNumber();
refundDTO.setRefundPayNumber(serialNumber);
refundDTO.setTimeEnd(date);
Integer line = billingRefundService.save(refundDTO);
if (line == 0) {
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
logger.info("保存退款记录失败:{}", JSON.toJSONString(refundDTO));
return ServiceResponse.failure(ErrorCode.UNKNOWN_ERROR.getCode(), "保存退款记录失败");
}
// 退款
int i = billingAccountService.updateAccountAfterRechargeOrRefund(olderFee, refundDTO.getEnterpriseId());
if (i == 0) {
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
logger.info("退款失败:{}", JSON.toJSONString(refundDTO));
return ServiceResponse.failure(ErrorCode.UNKNOWN_ERROR.getCode(), "退款失败");
}
// 返回应用市场
RefundReturnDTO refundReturnDTO = EntityUtil.changeEntityByOrika(RefundReturnDTO.class, refundDTO);
refundReturnDTO.setRefundPaySerialNumber(serialNumber);
refundReturnDTO.setDeductionTime(date);
return ServiceResponse.success(refundReturnDTO);
} catch (Exception e) {
logger.info("银行转账回调错误", e);
throw new RuntimeException(e);
} finally {
RedisUtil.unlock(Constants.REFUND_LOCK);
}
// 返回应用市场
RefundReturnDTO refundReturnDTO = EntityUtil.changeEntityByOrika(RefundReturnDTO.class, refundDTO);
refundReturnDTO.setRefundPaySerialNumber(serialNumber);
refundReturnDTO.setDeductionTime(date);
return ServiceResponse.success(refundReturnDTO);
}
@Override
......
......@@ -59,7 +59,6 @@ public class BillingPayInfoApiServiceImpl implements BillingPayInfoApiService {
@Autowired
public BillingPayInfoApiServiceImpl(Map<String, PayStrategy> strategyMap) {
this.strategyMap.clear();
this.strategyMap.putAll(strategyMap);
}
......@@ -126,7 +125,10 @@ public class BillingPayInfoApiServiceImpl implements BillingPayInfoApiService {
approvalCallBackDTO.setPrepayId(resData.getPrepay_id());
approvalCallBackDTO.setResultCode(StringUtils.equalsIgnoreCase(resData.getResult_code(), "SUCCESS") ? 1 : 0);
approvalCallBackDTO.setTotalFeePaid(new BigDecimal(resData.getTotal_fee()).divide(new BigDecimal("100"), 5, RoundingMode.HALF_UP).doubleValue());
approvalCallBackDTO.setTotalFee(approvalCallBackDTO.getTotalFeePaid());
approvalCallBackDTO.setOnlineSerialNumber(resData.getTransaction_id());
approvalCallBackDTO.setAuditStatus(BillingAuditStatusEnum.PASS.getCode());
approvalCallBackDTO.setPayType(PayTypeEnum.WE_CHAT_PAY.getCode());
return strategyMap.get(PayTypeEnum.WE_CHAT_PAY.getCode() + "").callBack(approvalCallBackDTO);
}
......@@ -145,6 +147,7 @@ public class BillingPayInfoApiServiceImpl implements BillingPayInfoApiService {
// 银行回调取计划金额
approvalCallBackDTO.setTotalFee(jsonObject.getDouble("plannedAmount"));
approvalCallBackDTO.setTotalFeePaid(jsonObject.getDouble("accountAmount"));
approvalCallBackDTO.setPayType(PayTypeEnum.OFFLINE_PAY.getCode());
ServiceResponse<Void> validParam = ValidParamsUtils.allCheckValidate(approvalCallBackDTO, ApprovalCallBackDTO.ApprovalValid.class);
if (!validParam.isSuccess()) {
return validParam;
......
......@@ -147,11 +147,11 @@ public class InternationalSmsRecordApiServiceImpl implements InternationalSmsRec
*/
private void setUserName(InternationalSmsRecordDTO smsRecordDTO) {
if (smsRecordDTO.getUserId() != null) {
ServiceResponse<UserDTO> userResp = userApiService.getUserById(smsRecordDTO.getUserId());
ServiceResponse<UserDTO> userResp = userApiService.getUserInfoById(smsRecordDTO.getUserId());
if (userResp.isSuccess()) {
UserDTO userDTO = userResp.getResult();
if (userDTO != null) {
smsRecordDTO.setUserName(smsRecordDTO.getUserName());
smsRecordDTO.setUserName(userDTO.getUserName());
}
} else {
logger.info("userId:{}, UserApiService.getUserById 调用失败:{}", smsRecordDTO.getUserId(), userResp.getMessage());
......
......@@ -111,10 +111,28 @@ public class SmsRecordApiServiceImpl implements SmsRecordApiService {
logger.info("短信验证不通过:{}", paramResult.getMessage());
return paramResult;
}
// 判断是扣套餐包还是扣余额
RAtomicLong smsCount = billingEnterpriseSmsPackageService.getTotalSmsInPackageByEnterpriseId(smsRecordDTO.getEnterpriseId());
smsRecordDTO.setCacheStatus(0);
smsRecordDTO.setDatabaseStatus(0);
// 缓存扣费 0套餐包 4余额
Integer fee = accountStandardService.queryUnitFee(smsRecordDTO.getEnterpriseId(), AccountStandardEnum.SMS.getCode());
int deductType = deduct(smsCount, smsRecordDTO.getEnterpriseId(), fee);
smsRecordDTO.setPayType(deductType);
smsRecordDTO.setFee((double)fee/1000);
Long smsRecordId = billingSmsRecordService.saveSmsRecord(smsRecordDTO);
smsRecordDTO.setSmsRecordId(smsRecordId);
billingSmsRecordService.updateCacheStatus(smsRecordDTO.getSmsRecordId());
smsRecordDTO.setCacheStatus(1);
// 队列
GicMQClient client = GICMQClientUtil.getClientInstance();
try {
String json = JSON.toJSONString(smsRecordDTO);
client.sendMessage("smsMq", json);
client.sendMessage("smsDbMq", JSON.toJSONString(smsRecordDTO));
} catch (Exception e) {
logger.warn("调用mq失败", e);
}
......@@ -126,7 +144,7 @@ public class SmsRecordApiServiceImpl implements SmsRecordApiService {
@Override
public ServiceResponse<Void> run(String arg) {
// 缓存中计费
/*// 缓存中计费
GicMQClient client = GICMQClientUtil.getClientInstance();
try {
logger.info("国内短信缓存扣费:{}", arg);
......@@ -154,17 +172,21 @@ public class SmsRecordApiServiceImpl implements SmsRecordApiService {
} catch (Exception e) {
logger.warn("调用mq失败", e);
}
*/
return ServiceResponse.success();
}
private int deduct(RAtomicLong rSmsCount, Integer enterpriseId, Integer fee) {
long smsCount = rSmsCount.get();
if (smsCount > 0) {
// 套餐包计费
/*
套餐包计费
这里不能改成 decrementAndGet自减, 否则会出现减到0的情况
*/
boolean smsCountDeduct = rSmsCount.compareAndSet(smsCount, smsCount - 1);
// rSmsCount.decrementAndGet();
if (!smsCountDeduct) {
// 设置失败 重新计算
// todo 这里不知道要不要加个延时之类的
return deduct(rSmsCount, enterpriseId, fee);
} else {
return 0;
......
......@@ -36,6 +36,7 @@ public class AliPayStrategy implements PayStrategy {
@Override
public ServiceResponse<Void> callBack(ApprovalCallBackDTO resData) {
return ServiceResponse.failure(ErrorCode.UNKNOWN_ERROR.getCode(), ErrorCode.UNKNOWN_ERROR.getMsg());
}
}
......@@ -10,18 +10,17 @@ import com.gic.commons.util.GlobalInfo;
import com.gic.enterprise.constant.*;
import com.gic.enterprise.dto.ApprovalCallBackDTO;
import com.gic.enterprise.dto.BillingPayInfoDTO;
import com.gic.enterprise.dto.EnterpriseDTO;
import com.gic.enterprise.dto.OutPayDTO;
import com.gic.enterprise.entity.TabBillingOrder;
import com.gic.enterprise.entity.TabBillingPayInfo;
import com.gic.enterprise.entity.TabBillingRechargeRecord;
import com.gic.enterprise.entity.TabEnterprise;
import com.gic.enterprise.error.ErrorCode;
import com.gic.enterprise.service.*;
import com.gic.enterprise.service.outer.impl.payComplete.PayCompleteHandler;
import com.gic.enterprise.utils.CreateRandomUtils;
import com.gic.finance.dto.TransferAccountsApprovalDTO;
import com.gic.finance.service.TransferAccountsApprovalApiService;
import com.gic.marketing.process.api.service.sms.SmsSendApiService;
import com.gic.redis.data.util.RedisUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
......@@ -31,6 +30,8 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
*
......@@ -58,78 +59,98 @@ public class OfflineStrategy implements PayStrategy {
@Autowired
private EnterpriseApiService enterpriseApiService;
private Map<String, PayCompleteHandler> payCompleteHandlerMap = new ConcurrentHashMap<>();
@Autowired
public OfflineStrategy(Map<String, PayCompleteHandler> payCompleteHandlerMap) {
this.payCompleteHandlerMap.putAll(payCompleteHandlerMap);
}
@Override
@Transactional(rollbackFor = Exception.class)
public ServiceResponse<BillingPayInfoDTO> savePrePayInfo(OutPayDTO outPayDTO) {
// 判断该商户下是否有相同的订单号
TabBillingPayInfo payInfo = billingPayInfoService.getByOrderSerialNumber(outPayDTO.getOrderSerialNumber(), PayTypeEnum.OFFLINE_PAY.getCode());
if (payInfo != null) {
return ServiceResponse.success(EntityUtil.changeEntityByJSON(BillingPayInfoDTO.class, payInfo));
}
BillingPayInfoDTO payInfoDTO = new BillingPayInfoDTO();
payInfoDTO.setEnterpriseId(outPayDTO.getEnterpriseId());
payInfoDTO.setSerialNumber(CreateRandomUtils.createSerialNumber());
payInfoDTO.setPayType(PayTypeEnum.OFFLINE_PAY.getCode());
payInfoDTO.setTotalFeePaid(outPayDTO.getTotalFeePaid());
payInfoDTO.setTimeStart(outPayDTO.getTimeStart());
payInfoDTO.setResultCode(BillingPayResultEnum.WAIT.getCode());
payInfoDTO.setInvoiceStatus(GlobalInfo.DATA_STATUS_DELETE);
payInfoDTO.setDeleteFlag(0);
payInfoDTO.setCreateTime(new Date());
payInfoDTO.setBuyType(outPayDTO.getBuyType());
payInfoDTO.setOrderSerialNumber(outPayDTO.getOrderSerialNumber());
payInfoDTO.setRequestCode(outPayDTO.getRequestCode());
payInfoDTO.setProductDescription(outPayDTO.getProductDescription());
Integer line = billingPayInfoService.savePayInfo(payInfoDTO);
if (line == 0) {
logger.info("线下支付交易保存失败, {}", JSON.toJSONString(payInfoDTO));
// 回滚
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
return ServiceResponse.failure(ErrorCode.OPERATION_FAILED.getCode(), "线下支付交易保存失败");
try {
RedisUtil.lock(Constants.PAY_LOCK + outPayDTO.getEnterpriseId(), 5L);
// 判断该商户下是否有相同的订单号
TabBillingPayInfo payInfo = billingPayInfoService.getByOrderSerialNumber(outPayDTO.getOrderSerialNumber(), PayTypeEnum.OFFLINE_PAY.getCode());
if (payInfo != null) {
return ServiceResponse.success(EntityUtil.changeEntityByJSON(BillingPayInfoDTO.class, payInfo));
}
BillingPayInfoDTO payInfoDTO = new BillingPayInfoDTO();
payInfoDTO.setEnterpriseId(outPayDTO.getEnterpriseId());
payInfoDTO.setSerialNumber(CreateRandomUtils.createSerialNumber());
payInfoDTO.setPayType(PayTypeEnum.OFFLINE_PAY.getCode());
payInfoDTO.setTotalFeePaid(outPayDTO.getTotalFeePaid());
payInfoDTO.setTimeStart(outPayDTO.getTimeStart());
payInfoDTO.setResultCode(BillingPayResultEnum.WAIT.getCode());
payInfoDTO.setInvoiceStatus(GlobalInfo.DATA_STATUS_DELETE);
payInfoDTO.setDeleteFlag(0);
payInfoDTO.setCreateTime(new Date());
payInfoDTO.setBuyType(outPayDTO.getBuyType());
payInfoDTO.setOrderSerialNumber(outPayDTO.getOrderSerialNumber());
payInfoDTO.setRequestCode(outPayDTO.getRequestCode());
payInfoDTO.setProductDescription(outPayDTO.getProductDescription());
Integer line = billingPayInfoService.savePayInfo(payInfoDTO);
if (line == 0) {
logger.info("线下支付交易保存失败, {}", JSON.toJSONString(payInfoDTO));
// 回滚
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
return ServiceResponse.failure(ErrorCode.OPERATION_FAILED.getCode(), "线下支付交易保存失败");
}
return ServiceResponse.success(payInfoDTO);
} catch (Exception e) {
logger.info("预支付错误", e);
throw new RuntimeException(e);
} finally {
RedisUtil.unlock(Constants.PAY_LOCK);
}
return ServiceResponse.success(payInfoDTO);
}
@Override
public ServiceResponse<BillingPayInfoDTO> confirmPay(OutPayDTO outPayDTO) {
TabBillingPayInfo payInfo = billingPayInfoService.getByOrderSerialNumber(outPayDTO.getEnterpriseId(), outPayDTO.getRequestCode() ,outPayDTO.getOrderSerialNumber());
BillingPayInfoDTO billingPayInfoDTO = new BillingPayInfoDTO();
if (payInfo == null) {
String serialNumber = CreateRandomUtils.createSerialNumber();
billingPayInfoDTO.setEnterpriseId(outPayDTO.getEnterpriseId());
billingPayInfoDTO.setSerialNumber(serialNumber);
billingPayInfoDTO.setOrderSerialNumber(outPayDTO.getOrderSerialNumber());
billingPayInfoDTO.setInvoiceStatus(GlobalInfo.DATA_STATUS_DELETE);
billingPayInfoDTO.setPayType(PayTypeEnum.OFFLINE_PAY.getCode());
billingPayInfoDTO.setTimeStart(DateUtil.formatString(new Date(), DateUtil.FORMAT_DATETIME_14));
billingPayInfoDTO.setDeleteFlag(0);
billingPayInfoDTO.setCreateTime(new Date());
billingPayInfoDTO.setBuyType(outPayDTO.getBuyType());
billingPayInfoDTO.setAuthCode(outPayDTO.getAuthCode());
Integer payInfoId = billingPayInfoService.savePayInfo(billingPayInfoDTO);
billingPayInfoDTO.setPayInfoId(payInfoId);
} else {
billingPayInfoDTO = EntityUtil.changeEntityByJSON(BillingPayInfoDTO.class, payInfo);
// 更新交易信息
ApprovalCallBackDTO dto = new ApprovalCallBackDTO();
dto.setBuyType(payInfo.getBuyType());
dto.setEnterpriseId(payInfo.getEnterpriseId());
dto.setPaySerialNumber(payInfo.getSerialNumber());
dto.setResultCode(BillingPayResultEnum.SUCCESS.getCode());
Integer line = billingPayInfoService.updatePayInfoBySerialNumber(dto);
if (line == 0) {
logger.info("更新支付信息失败, {}", JSON.toJSONString(dto));
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
return ServiceResponse.failure(ErrorCode.OPERATION_FAILED.getCode(), "更新支付信息失败");
try {
RedisUtil.lock(Constants.PAY_LOCK + outPayDTO.getEnterpriseId(), 5L);
TabBillingPayInfo payInfo = billingPayInfoService.getByOrderSerialNumber(outPayDTO.getEnterpriseId(), outPayDTO.getRequestCode(), outPayDTO.getOrderSerialNumber());
BillingPayInfoDTO billingPayInfoDTO = new BillingPayInfoDTO();
if (payInfo == null) {
String serialNumber = CreateRandomUtils.createSerialNumber();
billingPayInfoDTO.setEnterpriseId(outPayDTO.getEnterpriseId());
billingPayInfoDTO.setSerialNumber(serialNumber);
billingPayInfoDTO.setOrderSerialNumber(outPayDTO.getOrderSerialNumber());
billingPayInfoDTO.setInvoiceStatus(GlobalInfo.DATA_STATUS_DELETE);
billingPayInfoDTO.setPayType(PayTypeEnum.OFFLINE_PAY.getCode());
billingPayInfoDTO.setTimeStart(DateUtil.formatString(new Date(), DateUtil.FORMAT_DATETIME_14));
billingPayInfoDTO.setDeleteFlag(0);
billingPayInfoDTO.setCreateTime(new Date());
billingPayInfoDTO.setBuyType(outPayDTO.getBuyType());
billingPayInfoDTO.setAuthCode(outPayDTO.getAuthCode());
Integer payInfoId = billingPayInfoService.savePayInfo(billingPayInfoDTO);
billingPayInfoDTO.setPayInfoId(payInfoId);
} else {
billingPayInfoDTO = EntityUtil.changeEntityByJSON(BillingPayInfoDTO.class, payInfo);
// 更新交易信息
ApprovalCallBackDTO dto = new ApprovalCallBackDTO();
dto.setBuyType(payInfo.getBuyType());
dto.setEnterpriseId(payInfo.getEnterpriseId());
dto.setPaySerialNumber(payInfo.getSerialNumber());
dto.setResultCode(BillingPayResultEnum.SUCCESS.getCode());
Integer line = billingPayInfoService.updatePayInfoBySerialNumber(dto);
if (line == 0) {
logger.info("更新支付信息失败, {}", JSON.toJSONString(dto));
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
return ServiceResponse.failure(ErrorCode.OPERATION_FAILED.getCode(), "更新支付信息失败");
}
}
billingPayInfoService.delOtherPayInfoExceptId(billingPayInfoDTO.getPayInfoId(), billingPayInfoDTO.getEnterpriseId(), billingPayInfoDTO.getOrderSerialNumber());
// 发起审批
initiateApproval(outPayDTO);
return ServiceResponse.success(billingPayInfoDTO);
} catch (Exception e) {
logger.info("确认支付错误", e);
throw new RuntimeException(e);
} finally {
RedisUtil.unlock(Constants.PAY_LOCK);
}
billingPayInfoService.delOtherPayInfoExceptId(billingPayInfoDTO.getPayInfoId(), billingPayInfoDTO.getEnterpriseId(), billingPayInfoDTO.getOrderSerialNumber());
// 发起审批
initiateApproval(outPayDTO);
return ServiceResponse.success(billingPayInfoDTO);
}
@Override
......@@ -141,76 +162,30 @@ public class OfflineStrategy implements PayStrategy {
@Override
@Transactional(rollbackFor = Exception.class)
public ServiceResponse<Void> callBack(ApprovalCallBackDTO resData) {
logger.info("银行线下支付回调结果:{}", JSON.toJSONString(resData));
String orderSerialNumber = resData.getOrderSerialNumber();
TabBillingPayInfo billingPayInfo = billingPayInfoService.getByOrderSerialNumber(orderSerialNumber, PayTypeEnum.OFFLINE_PAY.getCode());
resData.setPaySerialNumber(billingPayInfo.getSerialNumber());
if (StringUtils.isBlank(billingPayInfo.getTimeEnd())) {
// 写入支付结果
Integer i = billingPayInfoService.updatePayInfoBySerialNumber(resData);
switch (resData.getBuyType()) {
case 1:
// 商户余额充值
billingRechargeRecordService.updateBySerialNumber(billingPayInfo.getEnterpriseId(), resData.getOrderSerialNumber(), resData.getTotalFee(), resData.getTotalFeePaid(), resData.getAuditStatus(), resData.getTimeEnd(), null);
sendRechargeSuccessSms(resData, billingPayInfo.getEnterpriseId());
sendApprovalSms(resData, "GICJFZX007");
break;
case 2:
// 短信套餐包购买
this.billingOrderService.updateBySerialNumber(resData.getOrderSerialNumber(), resData.getTotalFeePaid(), resData.getAuditStatus(), resData.getTimeEnd());
sendOrderApprovalSms(resData, "GICJFZX008");
break;
default:
break;
}
}else {
logger.info("银行转账重复回调 orderSerialNumber:{}", resData.getOrderSerialNumber());
}
return ServiceResponse.success();
}
private void sendRechargeSuccessSms(ApprovalCallBackDTO resData, Integer enterpriseId) {
UserDTO userDTO = userApiService.getUserByEnterpriseId(enterpriseId).getResult();
sendSms(resData, userDTO, "GICJFZX005");
}
private void sendApprovalSms(ApprovalCallBackDTO resData, String code) {
TabBillingRechargeRecord rechargeRecord = billingRechargeRecordService.getRechargeRecordBySerialNumber(resData.getOrderSerialNumber());
Integer initiator = rechargeRecord.getInitiator();
UserDTO userDTO = userApiService.getUserById(initiator).getResult();
sendSms(resData, userDTO, code);
}
private void sendOrderApprovalSms(ApprovalCallBackDTO resData, String code) {
TabBillingOrder order = billingOrderService.getOrderBySerialNumber(resData.getOrderSerialNumber());
Integer initiator = order.getInitiatorUser();
if (initiator != null) {
UserDTO userDTO = userApiService.getUserById(initiator).getResult();
sendSms(resData, userDTO, code);
}
}
private void sendSms(ApprovalCallBackDTO resData, UserDTO userDTO, String code) {
try {
if (StringUtils.isBlank(userDTO.getPhoneAreaCode())) {
userDTO.setPhoneAreaCode("+86");
}
EnterpriseDTO enterpriseDTO = enterpriseApiService.getEnterpriseById(userDTO.getEnterpriseId()).getResult();
String[] smsArr = new String[]{enterpriseDTO.getEnterpriseName(), resData.getOrderSerialNumber(), BillingAuditStatusEnum.PASS.getCode().equals(resData.getAuditStatus()) ? "成功" : "失败"};
ServiceResponse<Void> smsSendResult = smsSendApiService.sendPlatformSms(code,
userDTO.getEnterpriseId(), userDTO.getPhoneAreaCode(), userDTO.getPhoneNumber(), smsArr);
if (!smsSendResult.isSuccess()) {
logger.warn(smsSendResult.getMessage());
RedisUtil.lock(Constants.PAY_BACK_LOCK + resData.getEnterpriseId(), 5L);
logger.info("银行线下支付回调结果:{}", JSON.toJSONString(resData));
String orderSerialNumber = resData.getOrderSerialNumber();
TabBillingPayInfo billingPayInfo = billingPayInfoService.getByOrderSerialNumber(orderSerialNumber, PayTypeEnum.OFFLINE_PAY.getCode());
resData.setPaySerialNumber(billingPayInfo.getSerialNumber());
// 防止重复推信息
if (StringUtils.isNotBlank(billingPayInfo.getTimeEnd())) {
logger.info("银行转账重复回调 orderSerialNumber:{}", resData.getOrderSerialNumber());
return ServiceResponse.success();
}
// 写入支付结果
Integer i = billingPayInfoService.updatePayInfoBySerialNumber(resData);
payCompleteHandlerMap.get(BuyTypeEnum.getTypeByCode(resData.getBuyType())).payComplete(resData, billingPayInfo);
return ServiceResponse.success();
} catch (Exception e) {
logger.info("发送短信失败", e);
logger.info("银行转账回调错误", e);
throw new RuntimeException(e);
} finally {
RedisUtil.unlock(Constants.PAY_BACK_LOCK);
}
}
private void initiateApproval(OutPayDTO outPayDTO) {
TransferAccountsApprovalDTO approvalDTO = new TransferAccountsApprovalDTO();
approvalDTO.setOrderNumber(outPayDTO.getOrderSerialNumber());
......
......@@ -3,24 +3,26 @@ package com.gic.enterprise.service.outer.impl.pay;
import com.alibaba.fastjson.JSON;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.auth.dto.UserDTO;
import com.gic.auth.service.UserApiService;
import com.gic.commons.util.DateUtil;
import com.gic.commons.util.EntityUtil;
import com.gic.commons.util.GlobalInfo;
import com.gic.enterprise.constant.BillingAuditStatusEnum;
import com.gic.enterprise.constant.BuyTypeEnum;
import com.gic.enterprise.constant.Constants;
import com.gic.enterprise.constant.PayTypeEnum;
import com.gic.enterprise.dto.ApprovalCallBackDTO;
import com.gic.enterprise.dto.BillingPayInfoDTO;
import com.gic.enterprise.dto.EnterpriseDTO;
import com.gic.enterprise.dto.OutPayDTO;
import com.gic.enterprise.entity.TabBillingPayInfo;
import com.gic.enterprise.entity.TabBillingRechargeRecord;
import com.gic.enterprise.error.ErrorCode;
import com.gic.enterprise.service.*;
import com.gic.enterprise.service.BillingPayInfoService;
import com.gic.enterprise.service.EnterpriseApiService;
import com.gic.enterprise.service.outer.impl.payComplete.PayCompleteHandler;
import com.gic.enterprise.utils.CreateRandomUtils;
import com.gic.log.api.dto.SystemSetLogDTO;
import com.gic.log.api.service.LogApiService;
import com.gic.marketing.process.api.service.sms.SmsSendApiService;
import com.gic.redis.data.util.RedisUtil;
import com.gic.thirdparty.api.dto.ScanPayReqDataDTO;
import com.gic.thirdparty.api.service.Pay4WXService;
import org.apache.commons.lang3.StringUtils;
......@@ -31,6 +33,8 @@ import org.springframework.stereotype.Component;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
*
......@@ -47,44 +51,50 @@ public class WxPayStrategy implements PayStrategy {
@Autowired
private BillingPayInfoService billingPayInfoService;
@Autowired
private BillingRechargeRecordService billingRechargeRecordService;
@Autowired
private BillingOrderService billingOrderService;
@Autowired
private PushMessageApiService pushMessageApiService;
@Autowired
private UserApiService userApiService;
@Autowired
private EnterpriseApiService enterpriseApiService;
@Autowired
SmsSendApiService smsSendApiService;
@Autowired
private LogApiService logApiService;
private Map<String, PayCompleteHandler> payCompleteHandlerMap = new ConcurrentHashMap<>();
@Autowired
public WxPayStrategy(Map<String, PayCompleteHandler> payCompleteHandlerMap) {
this.payCompleteHandlerMap.putAll(payCompleteHandlerMap);
}
@Override
public ServiceResponse<BillingPayInfoDTO> savePrePayInfo(OutPayDTO outPayDTO) {
if (StringUtils.isNotBlank(outPayDTO.getOrderSerialNumber())) {
TabBillingPayInfo payInfo = billingPayInfoService.getByOrderSerialNumber(outPayDTO.getOrderSerialNumber(), outPayDTO.getPayType());
if (payInfo != null) {
BillingPayInfoDTO payInfoDTO = EntityUtil.changeEntityByOrika(BillingPayInfoDTO.class, payInfo);
return ServiceResponse.success(payInfoDTO);
try {
RedisUtil.lock(Constants.PAY_BACK_LOCK + outPayDTO.getEnterpriseId(), 5L);
if (StringUtils.isNotBlank(outPayDTO.getOrderSerialNumber())) {
TabBillingPayInfo payInfo = billingPayInfoService.getByOrderSerialNumber(outPayDTO.getOrderSerialNumber(), outPayDTO.getPayType());
if (payInfo != null) {
BillingPayInfoDTO payInfoDTO = EntityUtil.changeEntityByOrika(BillingPayInfoDTO.class, payInfo);
return ServiceResponse.success(payInfoDTO);
}
}
}
String serialNumber = CreateRandomUtils.createSerialNumber();
BillingPayInfoDTO billingPayInfoDTO = saveWxPrePay(outPayDTO.getEnterpriseId(), serialNumber, outPayDTO.getTotalFee(), outPayDTO.getBuyType(), outPayDTO.getOrderSerialNumber());
// 微信支付
if (billingPayInfoDTO != null) {
return ServiceResponse.success(billingPayInfoDTO);
} else {
return ServiceResponse.failure(ErrorCode.SYSTEM_ERROR.getCode(), "调用微信支付失败");
String serialNumber = CreateRandomUtils.createSerialNumber();
BillingPayInfoDTO billingPayInfoDTO = saveWxPrePay(outPayDTO.getEnterpriseId(), serialNumber, outPayDTO.getTotalFee(), outPayDTO.getBuyType(), outPayDTO.getOrderSerialNumber());
// 微信支付
if (billingPayInfoDTO != null) {
return ServiceResponse.success(billingPayInfoDTO);
} else {
return ServiceResponse.failure(ErrorCode.SYSTEM_ERROR.getCode(), "调用微信支付失败");
}
} catch (Exception e) {
logger.info("预支付错误", e);
throw new RuntimeException(e);
} finally {
RedisUtil.unlock(Constants.PAY_BACK_LOCK);
}
}
@Override
public ServiceResponse<BillingPayInfoDTO> confirmPay(OutPayDTO outPayDTO) {
logger.info("线下转账没有该方法 confirmPay");
logger.info("微信没有该方法 confirmPay");
return ServiceResponse.success();
}
......@@ -115,40 +125,29 @@ public class WxPayStrategy implements PayStrategy {
@Override
public ServiceResponse<Void> callBack(ApprovalCallBackDTO resData) {
logger.info("微信支付回调结果:{}", JSON.toJSONString(resData));
// 写入支付结果
TabBillingPayInfo billingPayInfo = billingPayInfoService.getBySerialNumber(resData.getPaySerialNumber());
if (StringUtils.isBlank(billingPayInfo.getTimeEnd())) {
Integer i = billingPayInfoService.updatePayInfoBySerialNumber(resData);
switch (billingPayInfo.getBuyType()) {
case 1:
// 商户余额充值
billingRechargeRecordService.updateBySerialNumber(resData.getEnterpriseId(), billingPayInfo.getOrderSerialNumber(), resData.getTotalFeePaid(), resData.getTotalFeePaid(), BillingAuditStatusEnum.PASS.getCode(), resData.getTimeEnd(), resData.getOnlineSerialNumber());
this.pushMessageApiService.pushMessage(resData.getEnterpriseId(), "系统通知", "余额充值", "您的商户成功充值"+resData.getTotalFeePaid()+"元!感谢您的支持和使用!");
sendRechargeSuccessSms(resData, billingPayInfo.getEnterpriseId());
TabBillingRechargeRecord rechargeRecord = billingRechargeRecordService.getRechargeRecordBySerialNumber(billingPayInfo.getOrderSerialNumber());
UserDTO userDTO = userApiService.getUserById(rechargeRecord.getInitiatorUser()).getResult();
createLog("充值", "账户余额", userDTO);
break;
case 2:
// 短信套餐包购买
this.billingOrderService.updateBySerialNumber(billingPayInfo.getOrderSerialNumber(), resData.getTotalFeePaid(), BillingAuditStatusEnum.PASS.getCode(), resData.getTimeEnd());
TabBillingRechargeRecord tabBillingRechargeRecord = billingRechargeRecordService.getRechargeRecordBySerialNumber(billingPayInfo.getOrderSerialNumber());
UserDTO user = userApiService.getUserById(tabBillingRechargeRecord.getInitiatorUser()).getResult();
createLog("支付", "订单内容", user);
break;
default:
break;
try {
RedisUtil.lock(Constants.PAY_BACK_LOCK + resData.getEnterpriseId(), 5L);
logger.info("微信支付回调结果:{}", JSON.toJSONString(resData));
// 写入支付结果
TabBillingPayInfo billingPayInfo = billingPayInfoService.getBySerialNumber(resData.getPaySerialNumber());
// 防止微信重复推信息
if (StringUtils.isNotBlank(billingPayInfo.getTimeEnd())) {
logger.info("微信重复回调 paySerialNumber:{}", resData.getPaySerialNumber());
return ServiceResponse.success();
}
Integer i = billingPayInfoService.updatePayInfoBySerialNumber(resData);
resData.setOrderSerialNumber(billingPayInfo.getOrderSerialNumber());
payCompleteHandlerMap.get(BuyTypeEnum.getTypeByCode(resData.getBuyType())).payComplete(resData, billingPayInfo);
// 删除其他的支付方式
billingPayInfoService.delOtherPayInfoExceptId(billingPayInfo.getPayInfoId(), billingPayInfo.getEnterpriseId(), billingPayInfo.getOrderSerialNumber());
} else {
logger.info("微信重复回调 paySerialNumber:{}", resData.getPaySerialNumber());
return ServiceResponse.success();
} catch (Exception e) {
logger.info("微信回调错误", e);
throw new RuntimeException(e);
} finally {
RedisUtil.unlock(Constants.PAY_BACK_LOCK);
}
return ServiceResponse.success();
}
private BillingPayInfoDTO saveWxPrePay(Integer enterpriseId, String serialNumber, Double totalFee, Integer buyType, String orderSerialNumber) {
......@@ -187,33 +186,6 @@ public class WxPayStrategy implements PayStrategy {
}
}
private void sendRechargeSuccessSms(ApprovalCallBackDTO resData, Integer enterpriseId) {
UserDTO userDTO = userApiService.getUserByEnterpriseId(enterpriseId).getResult();
sendSms(resData, userDTO, "GICJFZX005");
}
private void sendSms(ApprovalCallBackDTO resData, UserDTO userDTO, String code) {
try {
if (StringUtils.isBlank(userDTO.getPhoneAreaCode())) {
userDTO.setPhoneAreaCode("+86");
}
EnterpriseDTO enterpriseDTO = enterpriseApiService.getEnterpriseById(userDTO.getEnterpriseId()).getResult();
String[] smsArr = new String[]{enterpriseDTO.getEnterpriseName(), resData.getOrderSerialNumber(), BillingAuditStatusEnum.PASS.getCode().equals(resData.getAuditStatus()) ? "成功" : "失败"};
ServiceResponse<Void> smsSendResult = smsSendApiService.sendPlatformSms(code,
userDTO.getEnterpriseId(), userDTO.getPhoneAreaCode(), userDTO.getPhoneNumber(), smsArr);
if (!smsSendResult.isSuccess()) {
logger.warn(smsSendResult.getMessage());
}
} catch (Exception e) {
logger.info("发送短信失败", e);
}
}
public void createLog(String content, String operationObject, UserDTO userDTO) {
doLog(content, operationObject, userDTO);
}
private void doLog(String content, String operationObject, UserDTO userDTO) {
final SystemSetLogDTO logDTO = new SystemSetLogDTO();
//操作时间
......
package com.gic.enterprise.service.outer.impl.payComplete;
import com.gic.enterprise.dto.ApprovalCallBackDTO;
import com.gic.enterprise.entity.TabBillingPayInfo;
/**
*
* @Description:
* @author zhiwj
* @date 2020-11-24 15:39
*/
public interface PayCompleteHandler {
/**
* 购买套餐包或余额充值以后的统一处理
* @Title: payComplete
* @Description:
* @author zhiwj
* @param callBackDTO
* @param billingPayInfo
* @return void
* @throws
*/
void payComplete(ApprovalCallBackDTO callBackDTO, TabBillingPayInfo billingPayInfo);
}
package com.gic.enterprise.service.outer.impl.payComplete;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.auth.dto.UserDTO;
import com.gic.auth.service.UserApiService;
import com.gic.enterprise.constant.BillingAuditStatusEnum;
import com.gic.enterprise.constant.PayTypeEnum;
import com.gic.enterprise.dto.ApprovalCallBackDTO;
import com.gic.enterprise.dto.EnterpriseDTO;
import com.gic.enterprise.entity.TabBillingPayInfo;
import com.gic.enterprise.entity.TabBillingRechargeRecord;
import com.gic.enterprise.service.BillingRechargeRecordService;
import com.gic.enterprise.service.EnterpriseApiService;
import com.gic.enterprise.service.PushMessageApiService;
import com.gic.log.api.dto.SystemSetLogDTO;
import com.gic.log.api.service.LogApiService;
import com.gic.marketing.process.api.service.sms.SmsSendApiService;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
* 余额充值支付完成
* @Description:
* @author zhiwj
* @date 2020-11-24 15:57
*/
@Component("BALANCE_RECHARGE")
public class RechargeHandler implements PayCompleteHandler {
private Logger logger = LogManager.getLogger(RechargeHandler.class);
@Autowired
private BillingRechargeRecordService billingRechargeRecordService;
@Autowired
private UserApiService userApiService;
@Autowired
private PushMessageApiService pushMessageApiService;
@Autowired
private EnterpriseApiService enterpriseApiService;
@Autowired
private SmsSendApiService smsSendApiService;
@Autowired
private LogApiService logApiService;
@Override
public void payComplete(ApprovalCallBackDTO resData, TabBillingPayInfo billingPayInfo) {
// 通知
pushMessageApiService.pushMessage(resData.getEnterpriseId(), "系统通知", "余额充值", "您的商户成功充值"+resData.getTotalFeePaid()+"元!感谢您的支持和使用!");
// 充值成功短信
sendRechargeSuccessSms(resData, billingPayInfo.getEnterpriseId());
// 日志
TabBillingRechargeRecord rechargeRecord = billingRechargeRecordService.getRechargeRecordBySerialNumber(billingPayInfo.getOrderSerialNumber());
UserDTO userDTO = userApiService.getUserInfoById(rechargeRecord.getInitiatorUser()).getResult();
doLog("充值", "账户余额", userDTO);
// 审核成功短信
if (PayTypeEnum.OFFLINE_PAY.getCode() == resData.getPayType()) {
sendApprovalSms(resData, "GICJFZX008");
}
billingRechargeRecordService.updateBySerialNumber(resData.getEnterpriseId(), resData.getOrderSerialNumber(),
resData.getTotalFee(), resData.getTotalFeePaid(), resData.getAuditStatus(), resData.getTimeEnd(), resData.getOnlineSerialNumber());
}
private void sendApprovalSms(ApprovalCallBackDTO resData, String code) {
TabBillingRechargeRecord rechargeRecord = billingRechargeRecordService.getRechargeRecordBySerialNumber(resData.getOrderSerialNumber());
Integer initiator = rechargeRecord.getInitiator();
UserDTO userDTO = userApiService.getUserInfoById(initiator).getResult();
sendSms(resData, userDTO, code);
}
private void sendRechargeSuccessSms(ApprovalCallBackDTO resData, Integer enterpriseId) {
UserDTO userDTO = userApiService.getUserByEnterpriseId(enterpriseId).getResult();
sendSms(resData, userDTO, "GICJFZX005");
}
private void sendSms(ApprovalCallBackDTO resData, UserDTO userDTO, String code) {
try {
if (StringUtils.isBlank(userDTO.getPhoneAreaCode())) {
userDTO.setPhoneAreaCode("+86");
}
EnterpriseDTO enterpriseDTO = enterpriseApiService.getEnterpriseById(userDTO.getEnterpriseId()).getResult();
String[] smsArr = new String[]{enterpriseDTO.getEnterpriseName(), resData.getOrderSerialNumber(), BillingAuditStatusEnum.PASS.getCode().equals(resData.getAuditStatus()) ? "成功" : "失败"};
ServiceResponse<Void> smsSendResult = smsSendApiService.sendPlatformSms(code,
userDTO.getEnterpriseId(), userDTO.getPhoneAreaCode(), userDTO.getPhoneNumber(), smsArr);
if (!smsSendResult.isSuccess()) {
logger.warn(smsSendResult.getMessage());
}
} catch (Exception e) {
logger.info("发送短信失败", e);
}
}
private void doLog(String content, String operationObject, UserDTO userDTO) {
final SystemSetLogDTO logDTO = new SystemSetLogDTO();
//操作时间
logDTO.setLogTime(new Date());
logDTO.setCreateTime(new Date());
//操作人
logDTO.setUserId(Long.valueOf(userDTO.getUserId()));
logDTO.setUserName(userDTO.getUserName());
//操作内容
logDTO.setContent(content);
//为了search查询,暂时用这个字段存
logDTO.setInterfaceName(content);
////操作对象
logDTO.setBusinessName(operationObject);
//操作模块
logDTO.setRelationId(45000L);
logDTO.setProject("企业-计费中心");
// }
//账号(手机号)
logDTO.setRemark1(userDTO.getPhoneAreaCode());
logDTO.setRemark2(userDTO.getPhoneNumber());
logDTO.setEnterpriseId(userDTO.getEnterpriseId().longValue());
EnterpriseDTO enterpriseDTO = enterpriseApiService.getEnterpriseById(userDTO.getEnterpriseId()).getResult();
logDTO.setEnterpriseName(enterpriseDTO.getEnterpriseName());
logApiService.saveSystemSetLog(logDTO);
}
}
package com.gic.enterprise.service.outer.impl.payComplete;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.auth.dto.UserDTO;
import com.gic.auth.service.UserApiService;
import com.gic.enterprise.constant.BillingAuditStatusEnum;
import com.gic.enterprise.dto.ApprovalCallBackDTO;
import com.gic.enterprise.dto.EnterpriseDTO;
import com.gic.enterprise.entity.TabBillingOrder;
import com.gic.enterprise.entity.TabBillingPayInfo;
import com.gic.enterprise.service.BillingOrderService;
import com.gic.enterprise.service.EnterpriseApiService;
import com.gic.log.api.dto.SystemSetLogDTO;
import com.gic.log.api.service.LogApiService;
import com.gic.marketing.process.api.service.sms.SmsSendApiService;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
*
* @Description:
* @author zhiwj
* @date 2020-11-24 16:01
*/
@Component("BUY_SHORT_MESSAGE_PACKAGE")
public class SmsPackageHandler implements PayCompleteHandler {
private Logger logger = LogManager.getLogger(SmsPackageHandler.class);
@Autowired
private BillingOrderService billingOrderService;
@Autowired
private UserApiService userApiService;
@Autowired
private EnterpriseApiService enterpriseApiService;
@Autowired
private SmsSendApiService smsSendApiService;
@Autowired
private LogApiService logApiService;
@Override
public void payComplete(ApprovalCallBackDTO resData, TabBillingPayInfo billingPayInfo) {
billingOrderService.updateBySerialNumber(resData.getOrderSerialNumber(), resData.getTotalFeePaid(), resData.getAuditStatus(), resData.getTimeEnd());
TabBillingOrder order = this.billingOrderService.getOrderBySerialNumber(resData.getOrderSerialNumber());
UserDTO user = userApiService.getUserInfoById(order.getInitiatorUser()).getResult();
doLog("支付", "订单内容", user);
sendOrderApprovalSms(resData, "GICJFZX006");
}
private void sendOrderApprovalSms(ApprovalCallBackDTO resData, String code) {
TabBillingOrder order = billingOrderService.getOrderBySerialNumber(resData.getOrderSerialNumber());
Integer initiator = order.getInitiatorUser();
if (initiator != null) {
UserDTO userDTO = userApiService.getUserInfoById(initiator).getResult();
sendSms(resData, userDTO, code);
}
}
private void sendSms(ApprovalCallBackDTO resData, UserDTO userDTO, String code) {
try {
if (StringUtils.isBlank(userDTO.getPhoneAreaCode())) {
userDTO.setPhoneAreaCode("+86");
}
EnterpriseDTO enterpriseDTO = enterpriseApiService.getEnterpriseById(userDTO.getEnterpriseId()).getResult();
String[] smsArr = new String[]{enterpriseDTO.getEnterpriseName(), resData.getOrderSerialNumber(), BillingAuditStatusEnum.PASS.getCode().equals(resData.getAuditStatus()) ? "成功" : "失败"};
ServiceResponse<Void> smsSendResult = smsSendApiService.sendPlatformSms(code,
userDTO.getEnterpriseId(), userDTO.getPhoneAreaCode(), userDTO.getPhoneNumber(), smsArr);
if (!smsSendResult.isSuccess()) {
logger.warn(smsSendResult.getMessage());
}
} catch (Exception e) {
logger.info("发送短信失败", e);
}
}
private void doLog(String content, String operationObject, UserDTO userDTO) {
final SystemSetLogDTO logDTO = new SystemSetLogDTO();
//操作时间
logDTO.setLogTime(new Date());
logDTO.setCreateTime(new Date());
//操作人
logDTO.setUserId(Long.valueOf(userDTO.getUserId()));
logDTO.setUserName(userDTO.getUserName());
//操作内容
logDTO.setContent(content);
//为了search查询,暂时用这个字段存
logDTO.setInterfaceName(content);
////操作对象
logDTO.setBusinessName(operationObject);
//操作模块
logDTO.setRelationId(45000L);
logDTO.setProject("企业-计费中心");
// }
//账号(手机号)
logDTO.setRemark1(userDTO.getPhoneAreaCode());
logDTO.setRemark2(userDTO.getPhoneNumber());
logDTO.setEnterpriseId(userDTO.getEnterpriseId().longValue());
EnterpriseDTO enterpriseDTO = enterpriseApiService.getEnterpriseById(userDTO.getEnterpriseId()).getResult();
logDTO.setEnterpriseName(enterpriseDTO.getEnterpriseName());
logApiService.saveSystemSetLog(logDTO);
}
}
......@@ -149,4 +149,5 @@
<dubbo:reference interface="com.gic.widget.screening.api.service.EsScreeningEnterpriseInitService" id="esScreeningEnterpriseInitService" timeout="6000" />
<dubbo:reference interface="com.gic.cloud.service.IndexDescApiService" id="indexDescApiService" timeout="6000" />
<dubbo:reference interface="com.gic.member.tag.api.service.SceneCrowdGroupApiService" id="sceneCrowdGroupApiService" timeout="6000" />
<dubbo:reference interface="com.gic.store.service.StoreWidgetApiService" id="storeWidgetApiService" timeout="6000" />
</beans>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment