package com.ruoyi.payment.controller;
|
|
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.ruoyi.common.core.domain.R;
|
import com.ruoyi.common.core.web.domain.AjaxResult;
|
import com.ruoyi.payment.api.vo.PaymentOrder;
|
import com.ruoyi.payment.api.vo.WxRefundNotifyResp;
|
import com.ruoyi.payment.wx.enums.RefundEnum;
|
import com.ruoyi.payment.api.model.WxPaymentRefundModel;
|
import com.ruoyi.payment.wx.resp.NotifyV3PayDecodeRespBody;
|
import com.ruoyi.payment.wx.utils.WxV3Pay;
|
import io.swagger.annotations.Api;
|
import io.swagger.annotations.ApiOperation;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.web.bind.annotation.*;
|
|
import javax.servlet.http.HttpServletRequest;
|
import java.io.IOException;
|
import java.math.BigDecimal;
|
import java.util.Map;
|
import java.util.Objects;
|
|
/**
|
* 微信相关接口
|
*/
|
@Slf4j
|
@RestController
|
@CrossOrigin
|
@RequestMapping("/wx/")
|
@Api(tags = {"微信支付相关接口"})
|
public class WxPayController {
|
@Autowired
|
private WxV3Pay wxV3Pay;
|
|
|
/**
|
* 按实际修改
|
*/
|
@PostMapping("orderPay")
|
@ApiOperation("订单支付")
|
public R<Map<String, Object>> orderPay(@RequestBody PaymentOrder paymentOrder) {
|
// 查询订单
|
// 0元订单不走支付
|
// 价格
|
Integer totalPrice = paymentOrder.getAmount().multiply(new BigDecimal(100)).intValue();
|
// 生成订单号
|
String orderNo = paymentOrder.getCode();
|
// 查询用户信息 用户openid
|
String openId = paymentOrder.getOpenId();
|
// 订单做修改
|
// 调用支付方法
|
Map<String, Object> result = wxV3Pay.jsApi(orderNo, totalPrice, openId, paymentOrder.getNotifyUrl(),paymentOrder.getDescription());
|
log.info("支付参数:{}", result);
|
return R.ok(result);
|
}
|
|
/**
|
* 微信v3支付-订单退款
|
*
|
* @return
|
*/
|
@ApiOperation("订单退款")
|
@PostMapping(value = "refund-order")
|
public AjaxResult<String> refundOrder() {
|
Map<String, Object> result = wxV3Pay.refund(new WxPaymentRefundModel());
|
log.info("退款结果:{}", result);
|
// 微信支付退款单号
|
String refund_id = result.get("refund_id").toString();
|
// 商户退款单号
|
String out_refund_no = result.get("out_refund_no").toString();
|
// 微信支付订单号
|
String transaction_id = result.get("transaction_id").toString();
|
// 商户订单号 tradeNo
|
String out_trade_no = result.get("out_trade_no").toString();
|
// 退款成功时间
|
String success_time = Objects.nonNull(result.get("success_time")) ? result.get("success_time").toString() : null;
|
// 退款状态 RefundEnum
|
String status = result.get("status").toString();
|
// TODO 退款业务处理
|
return AjaxResult.success();
|
}
|
/**
|
* 微信v3支付-订单退款 远程调用
|
*
|
* @return
|
*/
|
@ApiOperation("订单退款")
|
@PostMapping(value = "refundOrderR")
|
public R<String> refundOrderR(@RequestBody WxPaymentRefundModel model) {
|
Map<String, Object> result = wxV3Pay.refund(model);
|
log.info("退款结果:{}", result);
|
// 微信支付退款单号
|
String refund_id = result.get("refund_id").toString();
|
// 商户退款单号
|
String out_refund_no = result.get("out_refund_no").toString();
|
// 微信支付订单号
|
String transaction_id = result.get("transaction_id").toString();
|
// 商户订单号 tradeNo
|
String out_trade_no = result.get("out_trade_no").toString();
|
// 退款成功时间
|
String success_time = Objects.nonNull(result.get("success_time")) ? result.get("success_time").toString() : null;
|
// 退款状态 RefundEnum
|
String status = result.get("status").toString();
|
return R.ok(status);
|
}
|
/**
|
* 支付回调
|
*/
|
@PostMapping("pay/notify")
|
public R<Map<String, Object>> payNotify(HttpServletRequest request) throws Exception {
|
try {
|
Map<String, Object> params = wxV3Pay.verifyNotify(request, new TypeReference<Map<String, Object>>() {});
|
log.info("支付回调:{}", params);
|
String outRefundNo = (String) params.get("out_refund_no");
|
String out_trade_no = params.get("out_trade_no").toString();
|
String substring = outRefundNo.substring(0, 2);
|
switch (substring){
|
//购物订单
|
case "GW":
|
|
break;
|
}
|
|
return R.ok(params);
|
} catch (Exception e) {
|
log.error("支付回调异常:{}", e, e);
|
wxV3Pay.ack(false, e.getMessage());
|
return R.fail("回调异常");
|
}
|
|
}
|
|
/**
|
* 支付回调成功后
|
*/
|
@PostMapping("pay/ack")
|
public void ack(){
|
try {
|
wxV3Pay.ack();
|
} catch (IOException e) {
|
throw new RuntimeException(e);
|
}
|
}
|
|
|
|
|
/**
|
* 退款回调
|
*/
|
@PostMapping("refund/notify")
|
public R<WxRefundNotifyResp> refundNotify(HttpServletRequest request) throws IOException {
|
try {
|
Map<String, Object> params = wxV3Pay.verifyNotify(request, new TypeReference<Map<String, Object>>() {
|
});
|
// 商户订单号
|
String out_trade_no = params.get("out_trade_no").toString();
|
// 商户退款单号
|
String out_refund_no = params.get("out_refund_no").toString();
|
// 微信支付订单号
|
String transaction_id = params.get("transaction_id").toString();
|
// 微信支付退款单号
|
String refund_id = params.get("refund_id").toString();
|
// 退款状态
|
String tradeState = params.get("refund_status").toString();
|
// 退款成功时间
|
// 时间不对的话,可以调用 WxTimeUtils.toRfc3339Date(success_time)转换一下
|
String success_time = params.get("success_time").toString();
|
if (tradeState.equals(RefundEnum.SUCCESS.name())) {
|
WxRefundNotifyResp resp = new WxRefundNotifyResp();
|
resp.setOut_trade_no(out_trade_no);
|
resp.setOut_refund_no(out_refund_no);
|
resp.setTradeState(tradeState);
|
resp.setTransaction_id(transaction_id);
|
resp.setRefund_id(refund_id);
|
resp.setSuccess_time(success_time);
|
wxV3Pay.ack();
|
return R.ok(resp);
|
} else {
|
wxV3Pay.ack(false, "不是成功的退款状态");
|
}
|
} catch (Exception e) {
|
wxV3Pay.ack(false, e.getMessage());
|
}
|
return R.fail();
|
}
|
|
/**
|
* 查询订单信息
|
* @param orderId
|
* @return
|
*/
|
@PostMapping("query/queryOrderInfo")
|
public R<NotifyV3PayDecodeRespBody> queryOrderInfo(@RequestParam("orderId") String orderId){
|
NotifyV3PayDecodeRespBody query = wxV3Pay.query(orderId);
|
return R.ok(query);
|
}
|
|
|
/**
|
* 关闭订单
|
* @param outTradeNo
|
*/
|
@PostMapping("pay/close")
|
public void close(@RequestParam("outTradeNo") String outTradeNo){
|
wxV3Pay.close(outTradeNo);
|
}
|
}
|