微信后端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
5.7 KiB

package com.ynxbd.common.service;
import com.ynxbd.common.bean.enums.MerchantEnum;
import com.ynxbd.common.bean.pay.Order;
import com.ynxbd.common.bean.pay.PayOutCollect;
import com.ynxbd.common.config.OCHelper;
import com.ynxbd.common.dao.OutCollectPayDao;
import com.ynxbd.common.helper.common.CopyHelper;
import com.ynxbd.common.helper.common.JsonHelper;
import com.ynxbd.common.helper.http.OkHttpHelper;
import com.ynxbd.common.result.Result;
import com.ynxbd.common.result.ResultEnum;
import com.ynxbd.common.result.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import java.math.BigDecimal;
@Slf4j
public class OutCollectService {
/**
* [外采]扫收款码支付
*/
public Order ocPayMicro(Order order, String brCode, String cardNo) {
Order oResult = new Order();
PayOutCollect addInfo = CopyHelper.fatherToChild(order, PayOutCollect.class);
addInfo.setBarCode(brCode);
addInfo.setIdCardNo(cardNo);
addInfo.setPayState(0);
addInfo.setNoticeState(0);
addInfo.setPayWay("2");
boolean isResult = new OutCollectPayDao().insert(addInfo);
oResult.setSuccess(isResult);
return oResult;
}
/**
* [外采]支付
*
* @param totalFee
*/
public void ocPay(String outTradeNo, BigDecimal totalFee, String collectId, String openid, String mid, String name, String cardNo, String patientId, String remark) throws ServiceException {
log.info("collectId={}, mid={}, name={}, cardNo={}, patientId={}, remark={}", collectId, mid, name, cardNo, patientId, remark);
if (ObjectUtils.isEmpty(collectId) || ObjectUtils.isEmpty(mid) || ObjectUtils.isEmpty(name) || ObjectUtils.isEmpty(cardNo)) {
throw new ServiceException(ResultEnum.PARAM_IS_DEFECT);
}
PayOutCollect addInfo = new PayOutCollect();
addInfo.setOutTradeNo(outTradeNo);
addInfo.setTotalFee(totalFee);
addInfo.setMid(mid);
addInfo.setOpenid(openid);
addInfo.setCollectId(collectId);
// --------------------------
addInfo.setName(name);
addInfo.setIdCardNo(cardNo);
addInfo.setPatientId(patientId);
addInfo.setRemark(remark);
addInfo.setPayWay("3");
addInfo.setPayState(-1);
addInfo.setNoticeState(-1);
if (!new OutCollectPayDao().insert(addInfo)) {
log.error("[外采]信息预存失败 collectId={}", collectId);
}
}
/**
* [外采]回调通知
*/
private boolean callback(String bankTransNo, BigDecimal totalFee, String collectId) throws ServiceException {
String resp = OkHttpHelper.get(OCHelper.URL + "/api/Pay/CallBack", map -> {
map.put("collectId", collectId);
map.put("payAmount", totalFee);
map.put("bankTransNo", bankTransNo);
});
log. info("[外采]回调 resp={}", resp);
if (resp == null) {
throw new ServiceException("[外采]回调请求失败");
}
Result result = JsonHelper.parseObject(resp, Result.class);
if (result == null) {
throw new ServiceException("[外采]回调,数据转换失败");
}
Integer code = result.getCode();
if (code == null || code != 200) {
String tip = String.format("[外采]回调异常 code=%s, message=%s, code", code, result.getMessage());
throw new ServiceException(tip);
}
return true;
}
/**
* [外采]回调通知
*/
public void ocPayNotify(MerchantEnum merchantEnum, String openid, BigDecimal totalFee, String outTradeNo, String bankTransNo, String payInfo) throws ServiceException {
OutCollectPayDao ocPayDao = new OutCollectPayDao();
PayOutCollect ocPay = ocPayDao.selectByOutTradeNo(outTradeNo);
if (ocPay == null) {
throw new ServiceException(ResultEnum.DATA_NOT_FOUND,
String.format("【%s】[外采]数据库中未找到订单 outTradeNo={%s}, bankTransNo={%s}", merchantEnum.NAME, outTradeNo, bankTransNo));
}
if (totalFee == null || totalFee.compareTo(BigDecimal.ZERO) == 0) {
throw new ServiceException(ResultEnum.PAY_MONEY_IS_ZERO);
}
String collectId = ocPay.getCollectId();
if (collectId == null) {
throw new ServiceException(ResultEnum.PARAM_IS_DEFECT);
}
Integer payState = ocPay.getPayState();
Integer noticeState = ocPay.getNoticeState();
if (payState == null || noticeState == null || payState != -1 || noticeState != -1) { // 状态不明确
throw new ServiceException(ResultEnum.PAY_REPEAT,
String.format("{%s}[外采]订单已支付 outTradeNo={%s}, bankTransNo={%s}", merchantEnum.NAME, outTradeNo, bankTransNo));
}
// 更新商户支付状态
if (!ocPayDao.updateMerPaidByOutTradeNo(outTradeNo, bankTransNo)) {
log.info("{} [外采]更新订单失败 outTradeNo={}, bankTransNo={}", merchantEnum.NAME, outTradeNo, bankTransNo);
}
try {
if (!callback(bankTransNo, ocPay.getTotalFee(), collectId)) { // 失败
return;
}
} catch (ServiceException e) {
ocPayDao.updateNoticeErr(outTradeNo, 500, e.getMessage());
throw e;
}
if (!ocPayDao.updateNoticeOk(outTradeNo)) {
log.info("{} [外采]更新信息失败 outTradeNo={}, collectId={}", merchantEnum.NAME, outTradeNo, collectId);
}
}
}