From 18118188e370e45f6e1bd43e5b33fd4c4a156655 Mon Sep 17 00:00:00 2001
From: lin <sbla5888@163.com>
Date: Mon, 08 Apr 2024 16:01:44 +0800
Subject: [PATCH] 微信支付+获取用户的openid
---
src/main/java/org/springblade/modules/pay/controller/WxPayController.java | 139 +++++++++++++++++++++++++++-------------------
1 files changed, 82 insertions(+), 57 deletions(-)
diff --git a/src/main/java/org/springblade/modules/pay/controller/WxPayController.java b/src/main/java/org/springblade/modules/pay/controller/WxPayController.java
index 016e4c6..22df1f0 100644
--- a/src/main/java/org/springblade/modules/pay/controller/WxPayController.java
+++ b/src/main/java/org/springblade/modules/pay/controller/WxPayController.java
@@ -1,35 +1,32 @@
package org.springblade.modules.pay.controller;
import com.google.gson.Gson;
-import com.wechat.pay.contrib.apache.httpclient.auth.ScheduledUpdateCertificatesVerifier;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
-import lombok.AllArgsConstructor;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springblade.common.config.WxPayConfig;
+import lombok.extern.slf4j.Slf4j;
import org.springblade.common.utils.HttpUtils;
import org.springblade.common.utils.WechatPay2ValidatorForRequest;
-import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.tool.api.R;
import org.springblade.modules.pay.entity.WxPayInfo;
import org.springblade.modules.pay.service.IWxPayService;
import org.springframework.web.bind.annotation.*;
+import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
+@CrossOrigin //跨域
@RestController
-@AllArgsConstructor
-@RequestMapping("/wxpay")
-@Api(value = "微信支付接口", tags = "微信支付接口")
-public class WxPayController extends BladeController {
+@RequestMapping("/wxPay")
+@Api(tags = "网站微信支付APIv3")
+@Slf4j
+public class WxPayController {
- private static final Logger logger = LoggerFactory.getLogger(WxPayController.class);
-
+ @Resource
private IWxPayService wxPayService;
+
@PostMapping("/save")
public R save(@RequestBody WxPayInfo wxPayInfo) {
@@ -57,21 +54,22 @@
return R.data(wxPayService.getOpenId(code));
}
+
/**
* jsapi下单
*
- * @param productId
+ * @param properChargeRecordId
* @return
* @throws Exception
*/
@ApiOperation("调用jsapi统一下单API")
@GetMapping("/jsapiPay")
- public R jsapiPay(@RequestParam("productId") Long productId) throws Exception {
+ public R jsapiPay(@RequestParam("properChargeRecordId") Long properChargeRecordId) throws Exception {
- logger.info("发起支付请求 v3");
+ log.info("发起支付请求 v3");
//返回支付二维码连接和订单号
- Map<String, String> map = wxPayService.jsapiPay(productId);
+ Map<String, String> map = wxPayService.jsapiPay(properChargeRecordId);
return R.data(map);
}
@@ -83,55 +81,82 @@
@ApiOperation("支付通知")
@PostMapping("/native/notify")
public String nativeNotify(HttpServletRequest request, HttpServletResponse response) {
+ return wxPayService.nativeNotify(request, response);
+ }
- Gson gson = new Gson();
- Map<String, String> map = new HashMap<>();//应答对象
+ /**
+ * 用户取消订单
+ *
+ * @param orderNo
+ * @return
+ * @throws Exception
+ */
+ @ApiOperation("用户取消订单")
+ @PostMapping("/cancel/{orderNo}")
+ public R cancel(@PathVariable String orderNo) throws Exception {
- try {
+ log.info("取消订单");
- //处理通知参数
- String body = HttpUtils.readData(request);
- Map<String, Object> bodyMap = gson.fromJson(body, HashMap.class);
- String requestId = (String) bodyMap.get("id");
- logger.info("支付通知的id ===> {}", requestId);
- logger.info("支付通知的完整数据 ===> {}", body);
- //int a = 9 / 0;
- ScheduledUpdateCertificatesVerifier verifier = new WxPayConfig().getVerifier("111", "111", "111", "111");
- //签名的验证
- WechatPay2ValidatorForRequest wechatPay2ValidatorForRequest
- = new WechatPay2ValidatorForRequest(verifier, requestId, body);
- if (!wechatPay2ValidatorForRequest.validate(request)) {
- logger.error("通知验签失败");
- //失败应答
- response.setStatus(500);
- map.put("code", "ERROR");
- map.put("message", "通知验签失败");
- return gson.toJson(map);
- }
- logger.info("通知验签成功");
+ wxPayService.cancelOrder(orderNo);
+ return R.data("订单已取消");
+ }
- //处理订单
- wxPayService.processOrder(bodyMap);
+ /**
+ * 查询订单
+ *
+ * @param orderNo
+ * @return
+ * @throws Exception
+ */
+ @ApiOperation("查询订单")
+ @GetMapping("/query/{orderNo}")
+ public R queryOrder(@PathVariable String orderNo) throws Exception {
- //应答超时
- //模拟接收微信端的重复通知
- // TimeUnit.SECONDS.sleep(5);
+ log.info("查询订单");
- //成功应答
- response.setStatus(200);
- map.put("code", "SUCCESS");
- map.put("message", "成功");
- return gson.toJson(map);
+ String result = wxPayService.queryOrder(orderNo);
+ return R.data("result", result);
- } catch (Exception e) {
- e.printStackTrace();
- //失败应答
- response.setStatus(500);
- map.put("code", "ERROR");
- map.put("message", "失败");
- return gson.toJson(map);
- }
+ }
+
+ @ApiOperation("申请退款")
+ @PostMapping("/refunds/{orderNo}/{reason}")
+ public R refunds(@PathVariable String orderNo, @PathVariable String reason) throws Exception {
+
+ log.info("申请退款");
+ wxPayService.refund(orderNo, reason);
+ return R.success("退款申请成功");
+ }
+
+ /**
+ * 查询退款
+ *
+ * @param refundNo
+ * @return
+ * @throws Exception
+ */
+ @ApiOperation("查询退款")
+ @GetMapping("/query-refund/{refundNo}")
+ public R queryRefund(@PathVariable String refundNo) throws Exception {
+
+ log.info("查询退款");
+
+ String result = wxPayService.queryRefund(refundNo);
+ return R.data("result", result);
+ }
+
+
+ /**
+ * 退款结果通知
+ * 退款状态改变后,微信会把相关退款结果发送给商户。
+ */
+ @ApiOperation("退款结果通知")
+ @PostMapping("/refunds/notify")
+ public String refundsNotify(HttpServletRequest request, HttpServletResponse response) {
+
+ log.info("退款通知执行");
+ return wxPayService.refundsNotify(request, response);
}
--
Gitblit v1.9.3