package com.cdzy.payment.controller; import com.alibaba.fastjson2.JSONObject; import com.cdzy.common.model.JsonResult; import com.cdzy.payment.model.dto.EbikePaymentDto; import com.cdzy.payment.model.dto.EbikeRefundDto; import com.cdzy.payment.model.dto.HandleNotifyResult; import com.cdzy.payment.service.WxPayService; import com.wechat.pay.java.service.payments.model.Transaction; import com.wechat.pay.java.service.refund.model.Refund; import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; /** * 用户订单微信支付 控制层。 * * @author dingchao * @since 2025-04-24 */ @Slf4j @RestController @RequestMapping("/wxPayment") public class EbikeWxPaymentController { @Resource private WxPayService wxPayService; /** * 微信支付下单 * * @param paymentDto 支付信息 * @return 下单成功返回true,否则返回false */ @PostMapping("/prepay") public JsonResult prepay(@RequestBody EbikePaymentDto paymentDto) { JSONObject r = wxPayService.prepay(paymentDto.getOrderId(), paymentDto.getDescription(), paymentDto.getGoodsTag(), paymentDto.getOpenId(), paymentDto.getAmount(), paymentDto.getDetail(), paymentDto.getClientIp()); return r == null?JsonResult.failed("下单失败"):JsonResult.success(r); } /** * 通过交易订单号查询支付订单 * * @param transactionId 微信支付订单号 * @return 支付订单信息 */ @GetMapping("/queryOrderById/{transactionId}") public JsonResult queryOrderById(@PathVariable String transactionId) { Transaction r = wxPayService.queryOrderById(transactionId); return r == null?JsonResult.failed(String.format("交易订单号{%s}查询支付订单失败", transactionId)):JsonResult.success(r); } /** * 通过商户(骑行)订单号查询支付订单 * * @param outTradeNo 商户(骑行)订单号 * @return 支付订单信息 */ @GetMapping("/queryOrderByOutTradeNo/{outTradeNo}") public JsonResult queryOrderByOutTradeNo(@PathVariable String outTradeNo) { Transaction r = wxPayService.queryOrderByOutTradeNo(outTradeNo); return r == null?JsonResult.failed(String.format("骑行订单号{%s}查询支付订单失败", outTradeNo)):JsonResult.success(r); } /** * 退款申请 * * @param refundDto 退款信息 * @return 退款成功返回true,否则返回false */ @PostMapping("/refund") public JsonResult refund(@RequestBody EbikeRefundDto refundDto) { String r = wxPayService.refund(refundDto.getPaymentId(), refundDto.getOrderId(), refundDto.getReason(), refundDto.getAmount()); return r == null?JsonResult.failed("退款失败"):JsonResult.success(r); } /** * 通过退款单号查询退款信息 * * @param outRefundNo 商户(骑行)退款单号 * @return 退款信息 */ @GetMapping("/refundQuery/{outRefundNo}") public JsonResult refundQuery(@PathVariable String outRefundNo) { Refund r = wxPayService.queryRefundByOutNo(outRefundNo); return r == null?JsonResult.failed(String.format("退款单号{%s}查询退款失败", outRefundNo)):JsonResult.success(r); } // ================通知回调接口=============== // TODO 通知接口不能鉴权 /** * 支付回调通知 * * @param request 支付回调请求 * @param response 支付回调响应 * @return 支付回调响应 */ @PostMapping("/pay/notify") public String payNotify(HttpServletRequest request, HttpServletResponse response) { HandleNotifyResult r = wxPayService.handlePayNotify(request); if(!r.isSuccess()) { response.setStatus(500); JSONObject jsonObject = new JSONObject(); jsonObject.put("code", "SYSTEM_ERROR"); jsonObject.put("message", r.getMessage()); return jsonObject.toJSONString(); } response.setStatus(200); JSONObject jsonObject = new JSONObject(); jsonObject.put("code", "SUCCESS"); jsonObject.put("message", "OK"); return jsonObject.toJSONString(); } /** * 退款回调通知 * * @param request 退款回调请求 * @param response 退款回调响应 * @return 退款回调响应 */ @PostMapping("/refund/notify") public String refundNotify(HttpServletRequest request, HttpServletResponse response) { HandleNotifyResult r = wxPayService.handleRefundNotify(request); if(!r.isSuccess()) { response.setStatus(500); JSONObject jsonObject = new JSONObject(); jsonObject.put("code", "SYSTEM_ERROR"); jsonObject.put("message", r.getMessage()); return jsonObject.toJSONString(); } response.setStatus(200); JSONObject jsonObject = new JSONObject(); jsonObject.put("code", "SUCCESS"); jsonObject.put("message", "OK"); return jsonObject.toJSONString(); } }