package com.dsh.activity.controller; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.dsh.activity.entity.THuiminAgreement; import com.dsh.activity.entity.THuiminAgreementSetting; import com.dsh.activity.entity.THuiminCard; import com.dsh.activity.entity.TPayHuimin; import com.dsh.activity.feignclient.other.SiteClient; import com.dsh.activity.feignclient.other.StoreClient; import com.dsh.activity.feignclient.other.model.Store; import com.dsh.activity.model.response.HuiminAgreementVO; import com.dsh.activity.model.response.HuiminCardVO; import com.dsh.activity.service.*; import com.dsh.activity.util.PayMoneyUtil; import com.dsh.activity.util.ResultUtil; import com.dsh.activity.util.TokenUtil; import com.dsh.activity.util.UUIDUtil; import com.dsh.activity.util.wx.WxV3PayConfig; import com.wechat.pay.contrib.apache.httpclient.util.AesUtil; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.BufferedReader; import java.io.IOException; import java.io.PrintWriter; import java.nio.charset.StandardCharsets; import java.text.SimpleDateFormat; import java.time.LocalDateTime; import java.time.ZoneId; import java.util.*; import java.util.stream.Collectors; /** * 玩湃惠民卡回调 * * @author zhibing.pu * @date 2023/6/24 11:27 */ @RestController @RequestMapping("/base/huimin/callBack") public class HuiminCallBack { @Autowired private ICouponService couponService; @Autowired private HuiminAgreementService huiminAgreementService; @Autowired private HuiminAgreementSettingService huiminAgreementSettingService; @Resource private HuiminCardService huiminCardService; @Resource private PayHuiminService payHuiminService; @Resource private SiteClient siteClient; @Resource private StoreClient storeClient; @Resource private PayMoneyUtil payMoneyUtil; @Autowired private TokenUtil tokenUtil; private String smid = "2088330203191220";//平台支付宝商户号 /** * 玩湃惠民卡支付宝支付回调接口 */ @PostMapping("/aliPayHuiminCallback") public void aliPayHuiminCallback(HttpServletRequest request, HttpServletResponse response) { try { System.err.println("支付宝回调"); Map map = payMoneyUtil.alipayCallback(request); if (null != map) { String out_trade_no = map.get("out_trade_no"); String transaction_id = map.get("trade_no"); TPayHuimin one = payHuiminService.getOne(new LambdaQueryWrapper().eq(TPayHuimin::getCode, out_trade_no)); if (one != null) { if (one.getStatus() == 2) { PrintWriter out = response.getWriter(); out.write("success"); out.flush(); out.close(); } else if (one.getStatus() == 1) { one.setOrderNumber(transaction_id); one.setStatus(2); one.setPaymentTime(new Date()); payHuiminService.updateById(one); PrintWriter out = response.getWriter(); out.write("success"); out.flush(); out.close(); } else { PrintWriter out = response.getWriter(); out.write("success"); out.flush(); out.close(); } } } } catch (Exception e) { e.printStackTrace(); } } /** * 玩湃惠民卡微信支付回调接口 */ @PostMapping("/weixinPayHuiminCallback") public void weixinPayHuiminCallback(HttpServletRequest request, HttpServletResponse response) { try { System.err.println("微信回调"); Map map = payMoneyUtil.weixinpayCallback(request); if (null != map) { String out_trade_no = map.get("out_trade_no"); String transaction_id = map.get("transaction_id"); String result = map.get("result"); TPayHuimin one = payHuiminService.getOne(new LambdaQueryWrapper().eq(TPayHuimin::getCode, out_trade_no)); if (one != null) { if (one.getStatus() == 2) { PrintWriter out = response.getWriter(); out.write(result); out.flush(); out.close(); } else if (one.getStatus() == 1) { one.setOrderNumber(transaction_id); one.setStatus(2); one.setPaymentTime(new Date()); payHuiminService.updateById(one); PrintWriter out = response.getWriter(); out.write(result); out.flush(); out.close(); } else { PrintWriter out = response.getWriter(); out.write("SUCCESS"); out.flush(); out.close(); } } } } catch (Exception e) { e.printStackTrace(); } } @PostMapping("/weixinPayHuiminCallback1") public void weixinPayHuiminCallback1(HttpServletRequest request, HttpServletResponse response) { try { System.err.println("微信回调"); System.err.println("请求" + request); BufferedReader reader = request.getReader(); String string1 = reader.toString(); System.err.println("请求reader" + string1); StringBuilder requestBody = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { requestBody.append(line); } System.err.println("全部请求体" + requestBody); JSONObject jsonObject = JSONObject.parseObject(requestBody.toString()); JSONObject resource = jsonObject.getJSONObject("resource"); AesUtil aesUtil = new AesUtil(WxV3PayConfig.apiV3Key.getBytes(StandardCharsets.UTF_8)); String decryptedData = aesUtil.decryptToString(resource.getString("associated_data").getBytes(StandardCharsets.UTF_8), resource.getString("nonce").getBytes(StandardCharsets.UTF_8), resource.getString("ciphertext")); System.err.println("微信解密的字符串信息" + decryptedData); JSONObject jsonInfo = (JSONObject) JSONObject.parse(decryptedData); String out_trade_no = jsonInfo.getString("out_trade_no"); String transaction_id = jsonInfo.getString("transaction_id"); String trade_state = jsonInfo.getString("trade_state"); if (trade_state.equals("SUCCESS")) { TPayHuimin one = payHuiminService.getOne(new LambdaQueryWrapper().eq(TPayHuimin::getCode, out_trade_no)); if (one != null) { if (one.getStatus() == 2) { PrintWriter out = response.getWriter(); out.write("SUCCESS"); out.flush(); out.close(); } else if (one.getStatus() == 1) { one.setOrderNumber(transaction_id); one.setStatus(2); one.setPaymentTime(new Date()); payHuiminService.updateById(one); PrintWriter out = response.getWriter(); out.write("SUCCESS"); out.flush(); out.close(); } else { PrintWriter out = response.getWriter(); out.write("SUCCESS"); out.flush(); out.close(); } } } } catch (Exception e) { e.printStackTrace(); } } /** * 玩湃惠民卡微信退款回调 * * @param request * @param response */ @ResponseBody @PostMapping("/wxRefundHuiminCallback") public void wxRefundHuiminCallback(HttpServletRequest request, HttpServletResponse response) { Map map = payMoneyUtil.wxRefundCallback(request); if (null != map) { String refund_id = map.get("refund_id"); String out_refund_no = map.get("out_refund_no"); String result = map.get("result"); TPayHuimin one = payHuiminService.getOne(new LambdaQueryWrapper() .eq(TPayHuimin::getCode, out_refund_no)); one.setRefundStatus(2); one.setRefundTime(new Date()); one.setStatus(3); one.setRefundNumber(refund_id); payHuiminService.updateById(one); PrintWriter out = null; try { out = response.getWriter(); } catch (IOException e) { throw new RuntimeException(e); } out.println(result); out.flush(); out.close(); } } }