06/06 16:21

This commit is contained in:
zyx 2023-06-06 16:21:49 +08:00
parent 69d3d47348
commit 6c16abed71
7 changed files with 126 additions and 13 deletions

View File

@ -17,6 +17,11 @@
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>4.35.79.ALL</version>
</dependency>
<!--邮箱验证的依赖-->
<dependency>
<groupId>org.apache.commons</groupId>

View File

@ -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";
}

View File

@ -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;
}
}

View File

@ -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<IPage<Orders>> page(@RequestParam Integer page,
@ResponseBody
public R<Page<Orders>> 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<Orders> ordersPage = new Page<>(page, pageSize);
LambdaQueryWrapper<Orders> 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<String> setStatus(@RequestBody Orders orders){
LambdaUpdateWrapper<Orders> 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<String> addOrder(@RequestBody Orders orders){
ordersService.addOrder(orders);
return R.success("下单成功");
@ResponseBody
public R<Orders> addOrder(@RequestBody Orders orders){
Orders order = ordersService.addOrder(orders);
return R.success(order);
}
//查看订单
@GetMapping("/userPage")
@ResponseBody
public R<Page<OrdersDTO>> page(@RequestParam Integer page,
@RequestParam Integer pageSize){
LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
@ -80,4 +85,15 @@ public class OrdersController {
Page<OrdersDTO> dtoPage = ordersService.getUserPage(page, pageSize, lqw);
return R.success(dtoPage);
}
//支付完成之后的回调方法处理订单状态等
@GetMapping("/afterPayOrder")
public String afterPayOrder(@RequestParam String out_trade_no){
LambdaUpdateWrapper<Orders> 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";
}
}

View File

@ -13,7 +13,7 @@ import cn.czyx007.mt.bean.Orders;
* @since 2023-05-29 11:08:45
*/
public interface OrdersService extends IService<Orders> {
void addOrder(Orders orders);
Orders addOrder(Orders orders);
Page<OrdersDTO> getUserPage(Integer page, Integer pageSize, LambdaQueryWrapper<Orders> lqw);
}

View File

@ -40,7 +40,7 @@ public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> 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<OrdersMapper, Orders> impleme
orderDetailService.saveBatch(orderDetailList);
//7.清空购物车数据
shoppingCartService.remove(lqw);
return order;
}
@Override

View File

@ -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});