package com.doumee.api.web.mall; import com.doumee.api.web.ApiController; import com.doumee.core.utils.Constants; import com.doumee.core.utils.ID; import com.doumee.core.wx.WxMiniConfig; import com.doumee.dao.business.model.ActivitySign; import com.doumee.dao.business.model.Fund; import com.doumee.dao.business.model.Goodsorder; import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse; import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; import java.util.Date; import java.util.Objects; /** * 支付回调 * * @Author : Rk * @create 2023/3/24 16:57 */ @Slf4j @RestController @CrossOrigin public class PaymentCallback extends ApiController { @PostMapping("/web/api/wxPayNotify") public String wxPay_notify(@RequestBody String xmlResult) { String wxId = ID.nextGUID(); log.info("支付回调信息("+wxId+") = > " + xmlResult); if (StringUtils.isEmpty(xmlResult)){ return null; } try { WxPayOrderNotifyResult result = WxMiniConfig.wxPayService.parseOrderNotifyResult(xmlResult); //自定义订单号 String outTradeNo = result.getOutTradeNo(); //微信订单号 String paymentNo = result.getTransactionId(); if (Constants.SUCCESS_STR.equals(result.getReturnCode())) { // 支付成功 switch (result.getAttach()) { //活动参与支付 case "ActivitySign": { ActivitySign activitySign = activitySignService.findById(Integer.valueOf(outTradeNo)); if(Objects.isNull(activitySign)){ return WxPayNotifyResponse.fail( "支付回调信息("+ wxId + ") = > 未查询到支付对象信息!"); } if(activitySign.getStatus().equals(Constants.ONE)){ return WxPayNotifyResponse.success("处理成功!"); } activitySign.setPayStatus(Constants.ONE); activitySign.setPayDate(new Date()); activitySign.setPayOrderId(paymentNo); activitySign.setStatus(Constants.ONE); activitySignService.updateById(activitySign); break; } case "terraceMall": { Goodsorder DBGoodsOrder = new Goodsorder(); DBGoodsOrder.setCode(Long.valueOf(outTradeNo)); Goodsorder goodsOrder = goodsorderService.findOne(DBGoodsOrder); if(Objects.isNull(goodsOrder)){ return WxPayNotifyResponse.fail( "支付回调信息("+ wxId + ") = > 未查询到支付对象信息!"); } if(goodsOrder.getStatus().equals(Constants.ONE)){ return WxPayNotifyResponse.success("处理成功!"); } goodsOrder.setPayStatus(Constants.ONE); goodsOrder.setPayDate(new Date()); goodsOrder.setPayOrderId(paymentNo); goodsOrder.setStatus(Constants.OrderStatus.PAY_DONE.getKey()); goodsOrder.setPayMethod(Constants.ZERO); goodsorderService.updateById(goodsOrder); //生成 咖啡计划订单明细表 if(goodsOrder.getType().equals(Constants.TWO)){ planorderDetailService.createPlanOrderDetail(goodsOrder); } Fund fund = new Fund(); fund.setOrderCode(goodsOrder.getPayOrderId()); fund.setCreator(goodsOrder.getMemberId()); fund.setCreateDate(new Date()); fund.setIsdeleted(Constants.ZERO); fund.setRemark(goodsOrder.getCode().toString()); fund.setMemberId(goodsOrder.getMemberId()); fund.setTitle("订单支付"); fund.setContent("订单支付"); fund.setObjId(goodsOrder.getId()); fund.setObjType(Constants.ONE); fund.setType(Constants.ZERO); fund.setNum(goodsOrder.getPrice()); fundService.create(fund); break; } case "shopGoods": { Goodsorder DBGoodsOrder = new Goodsorder(); DBGoodsOrder.setCode(Long.valueOf(outTradeNo)); Goodsorder goodsOrder = goodsorderService.findOne(DBGoodsOrder); if(Objects.isNull(goodsOrder)){ return WxPayNotifyResponse.fail( "支付回调信息("+ wxId + ") = > 未查询到支付对象信息!"); } if(goodsOrder.getStatus().equals(Constants.ONE)){ return WxPayNotifyResponse.success("处理成功!"); } goodsOrder.setPayStatus(Constants.ONE); goodsOrder.setPayDate(new Date()); goodsOrder.setPayOrderId(paymentNo); goodsOrder.setStatus(Constants.equalsInteger(goodsOrder.getReceiveType(),Constants.ZERO)?Constants.OrderStatus.PAY_DONE.getKey():Constants.OrderStatus.WAIT_RECEIVE.getKey()); goodsOrder.setPayMethod(Constants.ZERO); goodsorderService.updateById(goodsOrder); Fund fund = new Fund(); fund.setOrderCode(goodsOrder.getPayOrderId()); fund.setCreator(goodsOrder.getMemberId()); fund.setCreateDate(new Date()); fund.setIsdeleted(Constants.ZERO); fund.setRemark(goodsOrder.getCode().toString()); fund.setMemberId(goodsOrder.getMemberId()); fund.setTitle("订单支付"); fund.setContent("订单支付"); fund.setObjId(goodsOrder.getId()); fund.setObjType(Constants.ONE); fund.setType(Constants.ZERO); fund.setNum(goodsOrder.getPrice()); fundService.create(fund); break; } } return WxPayNotifyResponse.success("处理成功!"); } return WxPayNotifyResponse.fail(result.getReturnMsg()); } catch (Exception e) { e.printStackTrace(); log.error("微信回调结果异常,异常原因{}", e.getLocalizedMessage()); return WxPayNotifyResponse.fail(e.getMessage()); } } }