From 6c16abed71f175d45c9a416e22a68c90b25ecf10 Mon Sep 17 00:00:00 2001 From: zyx <1029606625@qq.com> Date: Tue, 6 Jun 2023 16:21:49 +0800 Subject: [PATCH] 06/06 16:21 --- pom.xml | 5 ++ .../cn/czyx007/mt/config/AlipayConfig.java | 28 +++++++++ .../mt/controller/AlipayController.java | 63 +++++++++++++++++++ .../mt/controller/OrdersController.java | 30 ++++++--- .../cn/czyx007/mt/service/OrdersService.java | 2 +- .../mt/service/impl/OrdersServiceImpl.java | 3 +- .../static/front/page/add-order.html | 8 +-- 7 files changed, 126 insertions(+), 13 deletions(-) create mode 100644 src/main/java/cn/czyx007/mt/config/AlipayConfig.java create mode 100644 src/main/java/cn/czyx007/mt/controller/AlipayController.java diff --git a/pom.xml b/pom.xml index e7683a2..8948e50 100644 --- a/pom.xml +++ b/pom.xml @@ -17,6 +17,11 @@ 11 + + com.alipay.sdk + alipay-sdk-java + 4.35.79.ALL + org.apache.commons diff --git a/src/main/java/cn/czyx007/mt/config/AlipayConfig.java b/src/main/java/cn/czyx007/mt/config/AlipayConfig.java new file mode 100644 index 0000000..6a44532 --- /dev/null +++ b/src/main/java/cn/czyx007/mt/config/AlipayConfig.java @@ -0,0 +1,28 @@ +package cn.czyx007.mt.config; + +/** + * @author : 张宇轩 + * @createTime : 2023/6/6 - 9:24 + */ +public class AlipayConfig { + // 商户appid + public static String APPID = ""; + // 私钥 pkcs8格式的 + public static String RSA_PRIVATE_KEY = ""; + // 服务器异步通知页面路径 需http://或者https://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问 + public static String notify_url = "http://商户网关地址/alipay.trade.wap.pay-JAVA-UTF-8/notify_url.jsp"; + // 页面跳转同步通知页面路径 需http://或者https://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问 商户可以自定义同步跳转地址 + public static String return_url = "http://localhost:8080/order/afterPayOrder"; + // 请求网关地址 + public static String URL = "https://openapi-sandbox.dl.alipaydev.com/gateway.do"; + // 编码 + public static String CHARSET = "UTF-8"; + // 返回格式 + public static String FORMAT = "json"; + // 支付宝公钥 + public static String ALIPAY_PUBLIC_KEY = ""; + // 日志记录目录定义在 logFile 中 + public static String log_path = "/log"; + // RSA2 + public static String SIGNTYPE = "RSA2"; +} \ No newline at end of file diff --git a/src/main/java/cn/czyx007/mt/controller/AlipayController.java b/src/main/java/cn/czyx007/mt/controller/AlipayController.java new file mode 100644 index 0000000..f30e07d --- /dev/null +++ b/src/main/java/cn/czyx007/mt/controller/AlipayController.java @@ -0,0 +1,63 @@ +package cn.czyx007.mt.controller; + +import cn.czyx007.mt.config.AlipayConfig; +import com.alibaba.fastjson.JSONObject; +import com.alipay.api.AlipayApiException; +import com.alipay.api.AlipayClient; +import com.alipay.api.DefaultAlipayClient; +import com.alipay.api.request.AlipayTradeWapPayRequest; +import com.alipay.api.response.AlipayTradeWapPayResponse; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import java.math.BigDecimal; + +/** + * @author : 张宇轩 + * @createTime : 2023/6/6 - 9:30 + */ +@RestController +@RequestMapping("pay") +@Slf4j +public class AlipayController { + //实现对接支付宝沙箱支付 + @GetMapping("/orderPay") + public String orderPay(@RequestParam("orderNum") Long out_trade_no, + @RequestParam("amount") BigDecimal total_amount) throws AlipayApiException { + AlipayClient alipayClient = new DefaultAlipayClient( + AlipayConfig.URL, + AlipayConfig.APPID, + AlipayConfig.RSA_PRIVATE_KEY, + AlipayConfig.FORMAT, + AlipayConfig.CHARSET, + AlipayConfig.ALIPAY_PUBLIC_KEY, + AlipayConfig.SIGNTYPE); + AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest(); + //异步接收地址,仅支持http/https,公网可访问 + request.setNotifyUrl(AlipayConfig.notify_url); + //同步跳转地址,仅支持http/https + request.setReturnUrl(AlipayConfig.return_url); + /* *****必传参数***** */ + JSONObject bizContent = new JSONObject(); + //商户订单号,商家自定义,保持唯一性 + bizContent.put("out_trade_no", out_trade_no); + //支付金额,最小值0.01元 + bizContent.put("total_amount", total_amount); + //订单标题,不可使用特殊符号 + bizContent.put("subject", "美食元素订单支付"); + + /* ****可选参数***** */ + //手机网站支付默认传值FAST_INSTANT_TRADE_PAY + bizContent.put("product_code", "QUICK_WAP_WAY"); + + request.setBizContent(bizContent.toString()); + AlipayTradeWapPayResponse response = alipayClient.pageExecute(request); + + String result = response.getBody(); + log.info("result: {}", result); + return result; + } +} diff --git a/src/main/java/cn/czyx007/mt/controller/OrdersController.java b/src/main/java/cn/czyx007/mt/controller/OrdersController.java index 936df6d..d6ac66c 100644 --- a/src/main/java/cn/czyx007/mt/controller/OrdersController.java +++ b/src/main/java/cn/czyx007/mt/controller/OrdersController.java @@ -15,6 +15,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import org.springframework.beans.BeanUtils; import org.springframework.format.annotation.DateTimeFormat; import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.*; import org.springframework.beans.factory.annotation.Autowired; @@ -30,7 +31,7 @@ import java.util.stream.Collectors; * @author 张宇轩 * @since 2023-05-29 11:09:40 */ -@RestController +@Controller @RequestMapping("order") public class OrdersController { /** @@ -40,7 +41,8 @@ public class OrdersController { private OrdersService ordersService; @GetMapping("/page") - public R> page(@RequestParam Integer page, + @ResponseBody + public R> page(@RequestParam Integer page, @RequestParam Integer pageSize, @RequestParam(required = false) String number, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @@ -50,13 +52,14 @@ public class OrdersController { Page ordersPage = new Page<>(page, pageSize); LambdaQueryWrapper lqw = new LambdaQueryWrapper<>(); lqw.like(number!=null, Orders::getNumber, number) - .gt(beginTime!=null, Orders::getOrderTime, beginTime) - .lt(endTime!=null, Orders::getOrderTime, endTime); + .ge(beginTime!=null, Orders::getOrderTime, beginTime) + .le(endTime!=null, Orders::getOrderTime, endTime); ordersService.page(ordersPage, lqw); return R.success(ordersPage); } @PutMapping + @ResponseBody public R setStatus(@RequestBody Orders orders){ LambdaUpdateWrapper luw = new LambdaUpdateWrapper<>(); luw.eq(Orders::getId, orders.getId()).set(Orders::getStatus, orders.getStatus()); @@ -66,13 +69,15 @@ public class OrdersController { //下单操作 @PostMapping("/submit") - public R addOrder(@RequestBody Orders orders){ - ordersService.addOrder(orders); - return R.success("下单成功"); + @ResponseBody + public R addOrder(@RequestBody Orders orders){ + Orders order = ordersService.addOrder(orders); + return R.success(order); } //查看订单 @GetMapping("/userPage") + @ResponseBody public R> page(@RequestParam Integer page, @RequestParam Integer pageSize){ LambdaQueryWrapper lqw = new LambdaQueryWrapper<>(); @@ -80,4 +85,15 @@ public class OrdersController { Page dtoPage = ordersService.getUserPage(page, pageSize, lqw); return R.success(dtoPage); } + + //支付完成之后的回调方法,处理订单状态等 + @GetMapping("/afterPayOrder") + public String afterPayOrder(@RequestParam String out_trade_no){ + LambdaUpdateWrapper luw = new LambdaUpdateWrapper<>(); + luw.eq(Orders::getNumber, out_trade_no) + .set(Orders::getStatus, 2) + .set(Orders::getCheckoutTime, LocalDateTime.now()); + ordersService.update(luw); + return "redirect:/front/page/pay-success.html"; + } } diff --git a/src/main/java/cn/czyx007/mt/service/OrdersService.java b/src/main/java/cn/czyx007/mt/service/OrdersService.java index 42cc23f..99f070a 100644 --- a/src/main/java/cn/czyx007/mt/service/OrdersService.java +++ b/src/main/java/cn/czyx007/mt/service/OrdersService.java @@ -13,7 +13,7 @@ import cn.czyx007.mt.bean.Orders; * @since 2023-05-29 11:08:45 */ public interface OrdersService extends IService { - void addOrder(Orders orders); + Orders addOrder(Orders orders); Page getUserPage(Integer page, Integer pageSize, LambdaQueryWrapper lqw); } diff --git a/src/main/java/cn/czyx007/mt/service/impl/OrdersServiceImpl.java b/src/main/java/cn/czyx007/mt/service/impl/OrdersServiceImpl.java index 471b44e..3055fc3 100644 --- a/src/main/java/cn/czyx007/mt/service/impl/OrdersServiceImpl.java +++ b/src/main/java/cn/czyx007/mt/service/impl/OrdersServiceImpl.java @@ -40,7 +40,7 @@ public class OrdersServiceImpl extends ServiceImpl impleme private OrderDetailService orderDetailService; @Override - public void addOrder(Orders orders) { + public Orders addOrder(Orders orders) { //1.查询用户信息 User user = userService.getById(BaseContext.getCurrentId()); //2.查询地址信息 @@ -88,6 +88,7 @@ public class OrdersServiceImpl extends ServiceImpl impleme orderDetailService.saveBatch(orderDetailList); //7.清空购物车数据 shoppingCartService.remove(lqw); + return order; } @Override diff --git a/src/main/resources/static/front/page/add-order.html b/src/main/resources/static/front/page/add-order.html index cc130d8..140335e 100644 --- a/src/main/resources/static/front/page/add-order.html +++ b/src/main/resources/static/front/page/add-order.html @@ -195,10 +195,10 @@ if(res.code === 1){ window.requestAnimationFrame(()=>{ //调用后端控制器,实现支付调用 - // window.location. - // replace('/pay/orderPay?orderNum='+ - // res.data.number+'&amount='+res.data.amount) - window.location.replace('/front/page/pay-success.html') + window.location. + replace('/pay/orderPay?orderNum='+ + res.data.number+'&amount='+res.data.amount) + // window.location.replace('/front/page/pay-success.html') }) }else{ this.$notify({ type:'warning', message:res.msg});