net.mingsoft.pay.biz.impl.PayLogBizImpl Maven / Gradle / Ivy
/**
* Copyright (c) 2012-present 铭软科技(mingsoft.net)
* 本软件及相关文档文件(以下简称“软件”)的版权归 铭软科技 所有
* 遵循 铭软科技《服务协议》中的《保密条款》
*/
package net.mingsoft.pay.biz.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeRefundModel;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest;
import com.github.binarywang.wxpay.bean.result.WxPayRefundResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import net.mingsoft.base.biz.impl.BaseBizImpl;
import net.mingsoft.base.dao.IBaseDao;
import net.mingsoft.basic.exception.BusinessException;
import net.mingsoft.mdiy.util.ConfigUtil;
import net.mingsoft.pay.action.web.WeixinPayAction;
import net.mingsoft.pay.bean.PayBean;
import net.mingsoft.pay.bean.PayLogBean;
import net.mingsoft.pay.bean.PayRefundBean;
import net.mingsoft.pay.biz.IPayLogBiz;
import net.mingsoft.pay.constant.Const;
import net.mingsoft.pay.constant.e.RefundType;
import net.mingsoft.pay.dao.IPayLogDao;
import net.mingsoft.pay.entity.PayLogEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* 交易记录管理持久化层
* @author 铭飞团队
* @version
* 版本号:100
* 创建日期:2018-2-9 13:32:03
* 历史修订:
*/
@Service("payLogBizImpl")
@Transactional
public class PayLogBizImpl extends BaseBizImpl implements IPayLogBiz {
@Autowired
private IPayLogDao payLogDao;
@Override
protected IBaseDao getDao() {
// TODO Auto-generated method stub
return payLogDao;
}
@Override
public boolean refund(String id) {
PayLogEntity logEntity = (PayLogEntity) getEntity(Integer.parseInt(id));
PayRefundBean payRefundBean = new PayRefundBean();
payRefundBean.setOrderNo(logEntity.getOrderNo());
payRefundBean.setPrice(logEntity.getLogMoney().toString());
payRefundBean.setRefundReason("系统退款");
payRefundBean.setTransactionId(logEntity.getLogTransactionId());
payRefundBean.setType(logEntity.getLogPayType());
return refund(payRefundBean);
}
@Override
public boolean refund(PayRefundBean payRefundBean) {
RefundType refundType = RefundType.getType(payRefundBean.getType());
switch (refundType) {
case ALI_PAY:
//支付宝退款
return alipayRefund(payRefundBean);
case WEI_XIN:
//微信退款
return weixinRefund(payRefundBean);
default:
throw new IllegalStateException("Unexpected value: " + refundType);
}
}
@Override
public List queryForPayLogBean(PayLogEntity log) {
return payLogDao.queryForPayLogBean(log);
}
private boolean weixinRefund(PayRefundBean payRefundBean) {
if (ObjectUtil.isNotNull(payRefundBean.getPrice()) && Double.parseDouble(payRefundBean.getPrice()) <= 0) {
throw new BusinessException("pay.no.price");
}
Map weixinPayConfig = ConfigUtil.getMap("WEIXIN_PAY_CONFIG_NAME");
if (CollUtil.isEmpty(weixinPayConfig)) {
throw new BusinessException("pay.config");
}
WxPayService wxService = WeixinPayAction.buildPayService(weixinPayConfig, null);
WxPayRefundRequest wprRequest = new WxPayRefundRequest();
//查询支付日志
PayLogEntity payLog = new PayLogEntity();
payLog.setOrderNo(payRefundBean.getOrderNo());
PayLogEntity newPayLog = (PayLogEntity) getEntity(payLog);
if (newPayLog == null) {
throw new BusinessException("order.no.exist");
}
//设置退款参数
wprRequest.setTransactionId(payRefundBean.getTransactionId());
wprRequest.setOutRefundNo(newPayLog.getOrderNo());
wprRequest.setOutTradeNo(newPayLog.getOrderNo());
wprRequest.setTotalFee((int) (newPayLog.getLogMoney() * 100));
wprRequest.setRefundDesc(payRefundBean.getRefundReason());
//计算退款金额
wprRequest.setRefundFee((int) ((Double.parseDouble(payRefundBean.getPrice()) * 100) - (ObjectUtil.isNotNull(payRefundBean.getServiceCharge()) ? Double.parseDouble(payRefundBean.getServiceCharge()) * 100 : 0)));
try {
WxPayRefundResult wprResult = wxService.refund(wprRequest);
if (wprResult.getResultCode().equalsIgnoreCase("SUCCESS")) {
//改变状态
if (StringUtils.equals(newPayLog.getLogStatus(), PayLogEntity.LogStatusEnum.PAY.toString())) {
newPayLog.setLogStatus(PayLogEntity.LogStatusEnum.REFUND.toString());
newPayLog.setUpdateDate(new Date());
updateEntity(newPayLog);
this.LOG.info("out_trade_no: " + wprResult.getOutTradeNo() + " refund SUCCESS!");
}
return true;
}
} catch (WxPayException e) {
e.printStackTrace();
}
return false;
}
private boolean alipayRefund(PayRefundBean payRefundBean) {
if (ObjectUtil.isNotNull(payRefundBean.getPrice()) && Double.parseDouble(payRefundBean.getPrice()) <= 0) {
throw new BusinessException("pay.no.price");
}
Map alipayConfig = ConfigUtil.getMap(Const.ALIPAY_PAY_CONFIG_NAME);
if (CollUtil.isEmpty(alipayConfig)) {
throw new BusinessException("pay.config");
}
//实例化客户端
AlipayClient alipayClient = new DefaultAlipayClient(net.mingsoft.pay.constant.Const.ALIPAY_GATEWAY_URL,
alipayConfig.get("payAppId"),
alipayConfig.get("payAppPrivateKey"), "json", "UTF-8",
alipayConfig.get("payAlipayPublicKey"), "RSA2");
//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
AlipayTradeRefundRequest payRequest = new AlipayTradeRefundRequest();
//SDK已经封装掉了公共参数,这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
AlipayTradeRefundModel model = new AlipayTradeRefundModel();
//需要配合前端传递参数
PayLogEntity payLog = new PayLogEntity();
payLog.setOrderNo(payRefundBean.getOrderNo());
PayLogEntity newPayLog = (PayLogEntity) getEntity(payLog);
if (newPayLog == null) {
throw new BusinessException("order.no.exist");
}
//退款金额
model.setRefundAmount((((Double.parseDouble(payRefundBean.getPrice()) * 100) - (ObjectUtil.isNotNull(payRefundBean.getServiceCharge()) ? Double.parseDouble(payRefundBean.getServiceCharge()) * 100 : 0) * 100) / 100) + "");
//订单号
model.setOutTradeNo(payRefundBean.getOrderNo());
model.setTradeNo(payRefundBean.getTransactionId());
model.setRefundReason(payRefundBean.getRefundReason());
payRequest.setBizModel(model);
try {
AlipayTradeRefundResponse alipayTradeAppPayResponse = alipayClient.execute(payRequest);
if (alipayTradeAppPayResponse.getMsg().equalsIgnoreCase("SUCCESS")) {
// 判断存在的订单是否已经支付成功,如果没有支付成功就进行支付操作
if (newPayLog.getLogStatus() == PayLogEntity.LogStatusEnum.PAY.toString()) {
newPayLog.setLogStatus(PayLogEntity.LogStatusEnum.REFUND.toString());
newPayLog.setUpdateDate(new Date());
updateEntity(newPayLog);
this.LOG.info("out_trade_no: " + alipayTradeAppPayResponse.getOutTradeNo() + " refund SUCCESS!");
}
return true;
}
} catch (AlipayApiException e) {
e.printStackTrace();
}
return false;
}
}