use of com.github.liuweijw.business.pay.domain.PayChannel in project fw-cloud-framework by liuweijw.
the class PayNotifyController method doNotifyResult.
public String doNotifyResult(HttpServletRequest request, HttpServletResponse response) {
String logPrefix = "【微信支付回调通知】";
log.info("====== 开始接收微信支付回调通知 ======");
try {
String xmlResult = IOUtils.toString(request.getInputStream(), request.getCharacterEncoding());
log.info("{}通知请求数据:reqStr={}", logPrefix, xmlResult);
if (StringHelper.isBlank(xmlResult)) {
return WxPayNotifyResponse.fail("FAIL");
}
WxPayServiceImpl wxPayService = new WxPayServiceImpl();
WxPayOrderNotifyResult result = WxPayOrderNotifyResult.fromXML(xmlResult);
// 验证业务数据是否正确
if (!PayConstant.RETURN_VALUE_SUCCESS.equalsIgnoreCase(result.getResultCode()) || !PayConstant.RETURN_VALUE_SUCCESS.equalsIgnoreCase(result.getResultCode())) {
log.error("returnCode={},resultCode={},errCode={},errCodeDes={}", result.getReturnCode(), result.getResultCode(), result.getErrCode(), result.getErrCodeDes());
return WxPayNotifyResponse.fail("notify data failed");
}
// 总金额
Integer total_fee = result.getTotalFee();
// 商户系统订单号
String out_trade_no = result.getOutTradeNo();
// 查询payOrder记录
String payOrderId = out_trade_no;
PayOrder payOrder = payOrderService.findPayOrderByOrderId(payOrderId);
if (null == payOrder) {
log.error("Can't found payOrder form db. payOrderId={}, ", payOrderId);
return WxPayNotifyResponse.fail("未查询到相应订单信息");
}
// 查询payChannel记录
String mchId = payOrder.getMch_id();
String channelId = payOrder.getChannelId();
PayChannel payChannel = payChannelService.findPayChannel(channelId, mchId);
if (null == payChannel) {
log.error("Can't found payChannel form db. mchId={} channelId={}, ", payOrderId, mchId, channelId);
return WxPayNotifyResponse.fail("未查询到订单相关渠道信息");
}
WxPayConfig wxPayConfig = WxPayUtil.getWxPayConfig(payChannel.getParam(), result.getTradeType(), wxPayProperties.getCertRootPath(), wxPayProperties.getNotifyUrl());
wxPayService.setConfig(wxPayConfig);
// 签名校验
result.checkResult(wxPayService, null, false);
// 核对金额
long wxPayAmt = new BigDecimal(total_fee).longValue();
long dbPayAmt = payOrder.getAmount().longValue();
if (dbPayAmt != wxPayAmt) {
log.error("db payOrder record payPrice not equals total_fee. total_fee={},payOrderId={}", total_fee, payOrderId);
return WxPayNotifyResponse.fail("支付金额不正确");
}
// 处理订单
// 0:订单生成,1:支付中,-1:支付失败,2:支付成功,3:业务处理完成,-2:订单过期
int payStatus = payOrder.getStatus();
if (payStatus == PayConstant.PAY_STATUS_COMPLETE) {
// 处理完成
log.info("====== 订单已经完成直接返回 ======");
return WxPayNotifyResponse.success("OK");
}
if (payStatus != PayConstant.PAY_STATUS_SUCCESS) {
boolean updatePayOrderRows = payOrderService.updatePayOrderStatus4Success(payOrder.getPayOrderId());
if (!updatePayOrderRows) {
log.error("{}更新支付状态失败,将payOrderId={},更新payStatus={}失败", logPrefix, payOrder.getPayOrderId(), PayConstant.PAY_STATUS_FAILED);
return WxPayNotifyResponse.fail("处理订单失败");
}
log.error("{}更新支付状态成功,将payOrderId={},更新payStatus={}成功", logPrefix, payOrder.getPayOrderId(), PayConstant.PAY_STATUS_SUCCESS);
payOrder.setStatus(PayConstant.PAY_STATUS_SUCCESS);
}
// 业务系统后端通知
this.notifyService.notifyPayOrder(payOrder);
log.info("====== 完成接收微信支付回调通知 ======");
return WxPayNotifyResponse.success("OK");
} catch (WxPayException e) {
// 出现业务错误
log.error("微信回调结果异常,异常原因" + e);
log.info("{}请求数据result_code=FAIL", logPrefix);
log.info("err_code:", e.getErrCode());
log.info("err_code_des:", e.getErrCodeDes());
return WxPayNotifyResponse.fail(e.getMessage());
} catch (Exception e) {
log.error("微信回调结果异常,异常原因" + e);
return WxPayNotifyResponse.fail(e.getMessage());
}
}
use of com.github.liuweijw.business.pay.domain.PayChannel in project fw-cloud-framework by liuweijw.
the class PaySendRedpackController method validateSendRedpackReqParams.
private R<Boolean> validateSendRedpackReqParams(PaySendRedpackReqBean sendRedpackReqBean, PaySendRedpack paySendRedpack, WxPaySendRedpackRequestBuilder wxPaySendRedpackRequestBuilder) {
if (null == sendRedpackReqBean)
return new R<Boolean>().data(false).failure("请求[/pay/order/sendRedpack]参数不能为空!");
// 是否是发放普通
boolean isCommonRedPack = sendRedpackReqBean.getRedPackType().intValue() == 0;
String preFix = isCommonRedPack ? "【发放普通红包】" : "【发放裂变红包】";
// 商户id
String mchId = sendRedpackReqBean.getMchId();
if (StringHelper.isBlank(mchId))
return new R<Boolean>().data(false).failure(preFix + "商户ID不存在!");
// 商户订单号
String mchOrderNo = sendRedpackReqBean.getMchOrderNo();
if (StringHelper.isBlank(mchOrderNo))
return new R<Boolean>().data(false).failure(preFix + "商户订单号不存在!");
// 渠道ID
String channelId = sendRedpackReqBean.getChannelId();
if (StringHelper.isBlank(channelId))
return new R<Boolean>().data(false).failure(preFix + "渠道ID不存在!");
// 红包发放总金额(单位分)
Integer amount = sendRedpackReqBean.getTotalAmount();
if (null == amount || amount <= 0)
return new R<Boolean>().data(false).failure(preFix + "红包发放总金额(单位分)不正确!");
if (amount < 100 || amount > 20000)
return new R<Boolean>().data(false).failure(preFix + "每个红包的平均金额必须在1.00元到200.00元之间!");
// 红包发放总人数
Integer totalNum = sendRedpackReqBean.getTotalNum();
if (null == totalNum || totalNum <= 0)
return new R<Boolean>().data(false).failure(preFix + "红包发放总人数设置不正确!");
// 红包祝福语
String wishing = sendRedpackReqBean.getWishing();
if (StringHelper.isBlank(wishing))
return new R<Boolean>().data(false).failure(preFix + "红包祝福语未设置!");
// ip
String ip = sendRedpackReqBean.getIp();
if (StringHelper.isBlank(ip))
return new R<Boolean>().data(false).failure(preFix + "请求IP地址不正确!");
// 活动名称
String actName = sendRedpackReqBean.getActName();
if (StringHelper.isBlank(actName))
return new R<Boolean>().data(false).failure(preFix + "活动名称未设置!");
// 备注
String remark = sendRedpackReqBean.getRemark();
if (StringHelper.isBlank(remark))
return new R<Boolean>().data(false).failure(preFix + "备注信息未设置!");
// 场景id
String sceneId = sendRedpackReqBean.getSceneId();
if (StringHelper.isBlank(sceneId) || !checkSceneId(sceneId))
return new R<Boolean>().data(false).failure(preFix + "场景id不正确!");
// 签名信息
String sign = sendRedpackReqBean.getSign();
if (StringHelper.isBlank(sign))
return new R<Boolean>().data(false).failure(preFix + "未签名!");
// 查询商户信息
PayMchInfo mchInfo = mchInfoService.findMchInfoByMchId(mchId);
if (null == mchInfo)
return new R<Boolean>().data(false).failure(preFix + "商户信息不存在!");
if (mchInfo.getStatu().intValue() != 1)
return new R<Boolean>().data(false).failure(preFix + "商户信息已失效!");
if (StringHelper.isBlank(mchInfo.getReqKey()))
return new R<Boolean>().data(false).failure(preFix + "商户请求私钥未设置!");
// 查询商户对应的支付渠道
PayChannel payChannel = payChannelService.findPayChannel(channelId, mchId);
if (null == payChannel)
return new R<Boolean>().data(false).failure(preFix + "商户渠道[channelId=" + channelId + ",mchId=" + mchId + "]信息不存在!");
if (payChannel.getStatu().intValue() != 1)
return new R<Boolean>().data(false).failure(preFix + "商户渠道[channelId=" + channelId + ",mchId=" + mchId + "]信息已经失效!");
WxPayConfig wxPayConfig = WxPayUtil.getWxPayConfig(payChannel.getParam(), null, wxPayProperties.getCertRootPath(), wxPayProperties.getNotifyUrl());
if (null == wxPayConfig)
return new R<Boolean>().data(false).failure(preFix + "商户渠道[channelId=" + channelId + ",mchId=" + mchId + "]信息param设置不正确!");
// 验证签名数据
boolean verifyFlag = PayUtil.verifyPaySign((JSONObject) JSON.toJSON(sendRedpackReqBean), mchInfo.getReqKey());
if (!verifyFlag)
return new R<Boolean>().data(false).failure(preFix + "验证签名失败!");
paySendRedpack.setSendOrderId(SequenceUtils.getInstance().generateBizSeqNo("SRP"));
paySendRedpack.setWxPayConfig(wxPayConfig);
paySendRedpack.setResKey(mchInfo.getResKey());
paySendRedpack.setRedPackType(isCommonRedPack ? 0 : 1);
paySendRedpack.setChannelId(channelId);
// 商户订单号
paySendRedpack.setMchOrderNo(mchOrderNo);
// 商户号
paySendRedpack.setMchId(wxPayConfig.getMchId());
paySendRedpack.setOpenId(sendRedpackReqBean.getOpenId());
// 付款金额
paySendRedpack.setTotalAmount(sendRedpackReqBean.getTotalAmount());
// 红包发放总人数
paySendRedpack.setTotalNum(sendRedpackReqBean.getTotalNum());
if (!isCommonRedPack) {
// 红包金额设置方式 裂变红包才进行设置
paySendRedpack.setAmtType("ALL_RAND");
} else {
paySendRedpack.setAmtType(null);
}
// 红包祝福语
paySendRedpack.setWishing(sendRedpackReqBean.getWishing());
paySendRedpack.setIp(ip);
// 活动名称
paySendRedpack.setActName(sendRedpackReqBean.getActName());
// 备注
paySendRedpack.setRemark(sendRedpackReqBean.getRemark());
// 场景id
paySendRedpack.setSceneId(sceneId);
if (!PublicHelper.isEmpty(sendRedpackReqBean.getRiskInfo()))
// 活动信息
paySendRedpack.setRiskInfo(sendRedpackReqBean.getRiskInfo());
if (!PublicHelper.isEmpty(sendRedpackReqBean.getConsumeMchId()))
// 资金授权商户号
paySendRedpack.setConsumeMchId(sendRedpackReqBean.getConsumeMchId());
wxPaySendRedpackRequestBuilder.mchBillNo(paySendRedpack.getMchOrderNo()).wxAppid(wxPayConfig.getAppId()).sendName(// 商户名称 来自商户数据库
mchInfo.getMchName()).reOpenid(paySendRedpack.getOpenId()).totalAmount(paySendRedpack.getTotalAmount()).totalNum(paySendRedpack.getTotalNum()).amtType(paySendRedpack.getAmtType()).wishing(paySendRedpack.getWishing()).clientIp(paySendRedpack.getIp()).actName(paySendRedpack.getActName()).remark(paySendRedpack.getRemark()).sceneId(paySendRedpack.getSceneId()).riskInfo(paySendRedpack.getRiskInfo()).consumeMchId(paySendRedpack.getConsumeMchId()).build();
return new R<Boolean>().data(true).success(preFix + "信息验证成功!");
}
use of com.github.liuweijw.business.pay.domain.PayChannel in project fw-cloud-framework by liuweijw.
the class PayNotifyController method notifyTest.
@RequestMapping("/notifyTest/{payOrderId}")
@ResponseBody
public String notifyTest(@PathVariable String payOrderId) {
String logPrefix = "【微信支付回调通知】";
log.info("====== 开始接收微信支付回调通知 ======");
PayOrder payOrder = payOrderService.findPayOrderByOrderId(payOrderId);
if (null == payOrder) {
log.error("Can't found payOrder form db. payOrderId={}, ", payOrderId);
return WxPayNotifyResponse.fail("未查询到相应订单信息");
}
// 查询payChannel记录
String mchId = payOrder.getMch_id();
String channelId = payOrder.getChannelId();
PayChannel payChannel = payChannelService.findPayChannel(channelId, mchId);
if (null == payChannel) {
log.error("Can't found payChannel form db. mchId={} channelId={}, ", payOrderId, mchId, channelId);
return WxPayNotifyResponse.fail("未查询到订单相关渠道信息");
}
// 处理订单
// 0:订单生成,1:支付中,-1:支付失败,2:支付成功,3:业务处理完成,-2:订单过期
int payStatus = payOrder.getStatus();
if (payStatus == PayConstant.PAY_STATUS_COMPLETE) {
// 处理完成
log.info("====== 订单已经完成直接返回 ======");
return WxPayNotifyResponse.success("OK");
}
if (payStatus != PayConstant.PAY_STATUS_SUCCESS) {
boolean updatePayOrderRows = payOrderService.updatePayOrderStatus4Success(payOrder.getPayOrderId());
if (!updatePayOrderRows) {
log.error("{}更新支付状态失败,将payOrderId={},更新payStatus={}失败", logPrefix, payOrder.getPayOrderId(), PayConstant.PAY_STATUS_SUCCESS);
return WxPayNotifyResponse.fail("处理订单失败");
}
log.error("{}更新支付状态成功,将payOrderId={},更新payStatus={}成功", logPrefix, payOrder.getPayOrderId(), PayConstant.PAY_STATUS_SUCCESS);
payOrder.setStatus(PayConstant.PAY_STATUS_SUCCESS);
}
// 业务系统后端通知
this.notifyService.notifyPayOrder(payOrder);
log.info("====== 完成接收微信支付回调通知 ======");
return WxPayNotifyResponse.success("OK");
}
use of com.github.liuweijw.business.pay.domain.PayChannel in project fw-cloud-framework by liuweijw.
the class PayAliNotifyController method notifyTest.
@RequestMapping("/notifyTest/{payOrderId}")
@ResponseBody
public String notifyTest(@PathVariable String payOrderId) {
String logPrefix = "【支付宝支付回调通知】";
log.info("====== 开始接收支付宝支付回调通知 ======");
PayOrder payOrder = payOrderService.findPayOrderByOrderId(payOrderId);
if (null == payOrder) {
log.error("Can't found payOrder form db. payOrderId={}, ", payOrderId);
return PayConstant.RETURN_ALIPAY_VALUE_FAIL;
}
// 查询payChannel记录
String mchId = payOrder.getMch_id();
String channelId = payOrder.getChannelId();
PayChannel payChannel = payChannelService.findPayChannel(channelId, mchId);
if (null == payChannel) {
log.error("Can't found payChannel form db. mchId={} channelId={}, ", payOrderId, mchId, channelId);
return PayConstant.RETURN_ALIPAY_VALUE_FAIL;
}
// 处理订单
// 0:订单生成,1:支付中,-1:支付失败,2:支付成功,3:业务处理完成,-2:订单过期
int payStatus = payOrder.getStatus();
if (payStatus == PayConstant.PAY_STATUS_COMPLETE) {
// 处理完成
log.info("====== 订单已经完成直接返回 ======");
return PayConstant.RETURN_ALIPAY_VALUE_SUCCESS;
}
if (payStatus != PayConstant.PAY_STATUS_SUCCESS) {
boolean updatePayOrderRows = payOrderService.updatePayOrderStatus4Success(payOrder.getPayOrderId());
if (!updatePayOrderRows) {
log.error("{}更新支付状态失败,将payOrderId={},更新失败", logPrefix, payOrder.getPayOrderId());
return PayConstant.RETURN_ALIPAY_VALUE_FAIL;
}
log.error("{}更新支付状态成功,将payOrderId={},更新payStatus={}成功", logPrefix, payOrder.getPayOrderId(), PayConstant.PAY_STATUS_SUCCESS);
payOrder.setStatus(PayConstant.PAY_STATUS_SUCCESS);
}
// 业务系统后端通知
this.notifyService.notifyPayOrder(payOrder);
log.info("====== 完成接收支付宝支付回调通知 ======");
return PayConstant.RETURN_ALIPAY_VALUE_SUCCESS;
}
use of com.github.liuweijw.business.pay.domain.PayChannel in project fw-cloud-framework by liuweijw.
the class PayAliNotifyController method doNotifyResult.
public String doNotifyResult(HttpServletRequest request, HttpServletResponse response) {
String logPrefix = "【支付宝支付回调通知】";
log.info("====== 开始接收支付宝支付回调通知 ======");
try {
// 获取支付宝POST过来反馈信息
Map<String, String> params = new HashMap<>();
Map<String, String[]> requestParams = request.getParameterMap();
for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext(); ) {
String name = iter.next();
String[] values = requestParams.get(name);
String valueStr = "";
for (int i = 0; i < values.length; i++) {
valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
}
// 乱码解决,这段代码在出现乱码时使用。如果mysign和sign不相等也可以使用这段代码转化
// valueStr = new String(valueStr.getBytes("ISO-8859-1"), "gbk");
params.put(name, valueStr);
}
log.info("{}通知请求数据:reqStr={}", logPrefix, params);
if (params.isEmpty()) {
log.error("{}请求参数为空", logPrefix);
return PayConstant.RETURN_ALIPAY_VALUE_FAIL;
}
// 商户订单号
String out_trade_no = params.get("out_trade_no");
// 支付金额
String total_amount = params.get("total_amount");
if (StringUtils.isEmpty(out_trade_no)) {
log.error("{}AliPay Notify parameter out_trade_no is empty. out_trade_no={}", logPrefix, out_trade_no);
return PayConstant.RETURN_ALIPAY_VALUE_FAIL;
}
if (StringUtils.isEmpty(total_amount)) {
log.error("{}AliPay Notify parameter total_amount is empty. total_fee={}", logPrefix, total_amount);
return PayConstant.RETURN_ALIPAY_VALUE_FAIL;
}
// 查询payOrder记录
String payOrderId = out_trade_no;
PayOrder payOrder = payOrderService.findPayOrderByOrderId(payOrderId);
if (null == payOrder) {
log.error("{}Can't found payOrder form db. payOrderId={}, ", logPrefix, payOrderId);
return PayConstant.RETURN_ALIPAY_VALUE_FAIL;
}
// 查询payChannel记录
String mchId = payOrder.getMch_id();
String channelId = payOrder.getChannelId();
PayChannel payChannel = payChannelService.findPayChannel(channelId, mchId);
if (null == payChannel) {
log.error("{}Can't found payChannel form db. mchId={} channelId={}, ", logPrefix, payOrderId, mchId, channelId);
return PayConstant.RETURN_ALIPAY_VALUE_FAIL;
}
boolean verify_result = false;
try {
verify_result = AlipaySignature.rsaCheckV1(params, AlipayUtil.init(payChannel.getParam()).getAlipayPublicKey(), AlipayConfig.CHARSET, "RSA2");
} catch (AlipayApiException e) {
log.error("{}AlipaySignature.rsaCheckV1 error:" + e, logPrefix);
}
// 验证签名
if (!verify_result)
return PayConstant.RETURN_ALIPAY_VALUE_FAIL;
// 核对金额
long aliPayAmt = new BigDecimal(total_amount).movePointRight(2).longValue();
long dbPayAmt = payOrder.getAmount().longValue();
if (dbPayAmt != aliPayAmt) {
log.error("{}db payOrder record payPrice not equals total_fee. total_fee={},payOrderId={}", logPrefix, aliPayAmt, payOrderId);
return PayConstant.RETURN_ALIPAY_VALUE_FAIL;
}
// 处理订单
// 0:订单生成,1:支付中,-1:支付失败,2:支付成功,3:业务处理完成,-2:订单过期
int payStatus = payOrder.getStatus();
if (payStatus == PayConstant.PAY_STATUS_COMPLETE) {
// 处理完成
log.info("====== 订单已经完成直接返回 ======");
return PayConstant.RETURN_ALIPAY_VALUE_SUCCESS;
}
if (payStatus != PayConstant.PAY_STATUS_SUCCESS) {
boolean updatePayOrderRows = payOrderService.updatePayOrderStatus4Success(payOrder.getPayOrderId());
if (!updatePayOrderRows) {
log.error("{}更新支付状态失败,将payOrderId={},更新payStatus={}失败", logPrefix, payOrder.getPayOrderId(), PayConstant.PAY_STATUS_FAILED);
return PayConstant.RETURN_ALIPAY_VALUE_FAIL;
}
log.error("{}更新支付状态成功,将payOrderId={},更新payStatus={}成功", logPrefix, payOrder.getPayOrderId(), PayConstant.PAY_STATUS_SUCCESS);
payOrder.setStatus(PayConstant.PAY_STATUS_SUCCESS);
}
// 业务系统后端通知
this.notifyService.notifyPayOrder(payOrder);
log.info("====== 完成接收支付宝支付回调通知 ======");
return PayConstant.RETURN_ALIPAY_VALUE_SUCCESS;
} catch (Exception e) {
log.error("支付宝回调结果异常,异常原因" + e);
return PayConstant.RETURN_ALIPAY_VALUE_FAIL;
}
}
Aggregations