Merge remote-tracking branch 'origin/dev/dev_xp' into dev/dev
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
package com.ai.da.common.config;
|
||||
|
||||
import com.alipay.api.*;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
@Configuration
|
||||
//加载配置文件
|
||||
@PropertySource("classpath:alipay-sandbox.properties")
|
||||
public class AlipayClientConfig {
|
||||
|
||||
@Resource
|
||||
private Environment config;
|
||||
|
||||
@Bean
|
||||
public AlipayClient alipayClient() throws AlipayApiException {
|
||||
|
||||
AlipayConfig alipayConfig = new AlipayConfig();
|
||||
|
||||
//设置网关地址
|
||||
alipayConfig.setServerUrl(config.getProperty("alipay.gateway-url"));
|
||||
//设置应用Id
|
||||
alipayConfig.setAppId(config.getProperty("alipay.app-id"));
|
||||
//设置应用私钥
|
||||
alipayConfig.setPrivateKey(config.getProperty("alipay.merchant-private-key"));
|
||||
//设置请求格式,固定值json
|
||||
alipayConfig.setFormat(AlipayConstants.FORMAT_JSON);
|
||||
//设置字符集
|
||||
alipayConfig.setCharset(AlipayConstants.CHARSET_UTF8);
|
||||
//设置支付宝公钥
|
||||
alipayConfig.setAlipayPublicKey(config.getProperty("alipay.alipay-public-key"));
|
||||
//设置签名类型
|
||||
alipayConfig.setSignType(AlipayConstants.SIGN_TYPE_RSA2);
|
||||
//构造client
|
||||
AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
|
||||
|
||||
return alipayClient;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.ai.da.common.enums;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
@AllArgsConstructor
|
||||
@Getter
|
||||
public enum AliPayTradeStateEnum {
|
||||
|
||||
/**
|
||||
* 支付成功
|
||||
*/
|
||||
SUCCESS("TRADE_SUCCESS"),
|
||||
|
||||
/**
|
||||
* 未支付
|
||||
*/
|
||||
NOTPAY("WAIT_BUYER_PAY"),
|
||||
|
||||
/**
|
||||
* 已关闭
|
||||
*/
|
||||
CLOSED("TRADE_CLOSED"),
|
||||
|
||||
/**
|
||||
* 退款成功
|
||||
*/
|
||||
REFUND_SUCCESS("REFUND_SUCCESS"),
|
||||
|
||||
/**
|
||||
* 退款失败
|
||||
*/
|
||||
REFUND_ERROR("REFUND_ERROR");
|
||||
|
||||
/**
|
||||
* 类型
|
||||
*/
|
||||
private final String type;
|
||||
}
|
||||
49
src/main/java/com/ai/da/common/enums/OrderStatusEnum.java
Normal file
49
src/main/java/com/ai/da/common/enums/OrderStatusEnum.java
Normal file
@@ -0,0 +1,49 @@
|
||||
package com.ai.da.common.enums;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
@AllArgsConstructor
|
||||
@Getter
|
||||
public enum OrderStatusEnum {
|
||||
/**
|
||||
* 未支付
|
||||
*/
|
||||
NOT_PAY("未支付"),
|
||||
|
||||
|
||||
/**
|
||||
* 支付成功
|
||||
*/
|
||||
SUCCESS("支付成功"),
|
||||
|
||||
/**
|
||||
* 已关闭
|
||||
*/
|
||||
CLOSED("超时已关闭"),
|
||||
|
||||
/**
|
||||
* 已取消
|
||||
*/
|
||||
CANCEL("用户已取消"),
|
||||
|
||||
/**
|
||||
* 退款中
|
||||
*/
|
||||
REFUND_PROCESSING("退款中"),
|
||||
|
||||
/**
|
||||
* 已退款
|
||||
*/
|
||||
REFUND_SUCCESS("已退款"),
|
||||
|
||||
/**
|
||||
* 退款异常
|
||||
*/
|
||||
REFUND_ABNORMAL("退款异常");
|
||||
|
||||
/**
|
||||
* 类型
|
||||
*/
|
||||
private final String type;
|
||||
}
|
||||
24
src/main/java/com/ai/da/common/enums/PayTypeEnum.java
Normal file
24
src/main/java/com/ai/da/common/enums/PayTypeEnum.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package com.ai.da.common.enums;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
@AllArgsConstructor
|
||||
@Getter
|
||||
public enum PayTypeEnum {
|
||||
/**
|
||||
* 微信
|
||||
*/
|
||||
WXPAY("微信"),
|
||||
|
||||
|
||||
/**
|
||||
* 支付宝
|
||||
*/
|
||||
ALIPAY("支付宝");
|
||||
|
||||
/**
|
||||
* 类型
|
||||
*/
|
||||
private final String type;
|
||||
}
|
||||
@@ -94,7 +94,7 @@ public class AuthenticationFilter extends OncePerRequestFilter {
|
||||
if (StrUtil.isBlank(jwtToken)) {
|
||||
throw new RuntimeException("请传入token!");
|
||||
}
|
||||
if(jwtToken.equals("Bearer-eyJhbGciOiJIUzUxMiJ9.eyJqdGkiOiIyIiwic3ViIjoie1wiaWRcIjoyLFwidXNlcm5hbWVcIjpcImxpcnNcIn0iLCJpYXQiOjE2NjU3NDEwODcsImlzcyI6IkRXSiIsImF1dGhvcml0aWVzIjoiW10iLCJleHAiOjE2NzQzODEwODd9.ShM9R_NNFD7oo1OvxrEgg7PFeWinOuAKkuInUCMQupp66s64Hhv8tN0Wwr83nIN4rHPqtn95wmd4msWcvaFYJA")){
|
||||
if(jwtToken.equals("Bearer Bearer-eyJhbGciOiJIUzUxMiJ9.eyJqdGkiOiIyIiwic3ViIjoie1wiaWRcIjoyLFwidXNlcm5hbWVcIjpcImxpcnNcIn0iLCJpYXQiOjE2NjU3NDEwODcsImlzcyI6IkRXSiIsImF1dGhvcml0aWVzIjoiW10iLCJleHAiOjE2NzQzODEwODd9.ShM9R_NNFD7oo1OvxrEgg7PFeWinOuAKkuInUCMQupp66s64Hhv8tN0Wwr83nIN4rHPqtn95wmd4msWcvaFYJA")){
|
||||
//写死 暂时放行
|
||||
return;
|
||||
}
|
||||
|
||||
42
src/main/java/com/ai/da/common/task/AliPayTask.java
Normal file
42
src/main/java/com/ai/da/common/task/AliPayTask.java
Normal file
@@ -0,0 +1,42 @@
|
||||
package com.ai.da.common.task;
|
||||
|
||||
import com.ai.da.mapper.entity.OrderInfo;
|
||||
import com.ai.da.common.enums.PayTypeEnum;
|
||||
import com.ai.da.service.AliPayService;
|
||||
import com.ai.da.service.OrderInfoService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class AliPayTask {
|
||||
|
||||
@Resource
|
||||
private OrderInfoService orderInfoService;
|
||||
|
||||
@Resource
|
||||
private AliPayService aliPayService;
|
||||
|
||||
/**
|
||||
* 从第0秒开始每隔30秒执行1次,查询创建超过5分钟,并且未支付的订单
|
||||
*/
|
||||
@Scheduled(cron = "0/30 * * * * ?")
|
||||
public void orderConfirm(){
|
||||
|
||||
log.info("orderConfirm 被执行......");
|
||||
|
||||
List<OrderInfo> orderInfoList = orderInfoService.getNoPayOrderByDuration(1, PayTypeEnum.ALIPAY.getType());
|
||||
|
||||
for (OrderInfo orderInfo : orderInfoList) {
|
||||
String orderNo = orderInfo.getOrderNo();
|
||||
log.warn("超时订单 ===> {}", orderNo);
|
||||
|
||||
//核实订单状态:调用支付宝查单接口
|
||||
aliPayService.checkOrderStatus(orderNo);
|
||||
}
|
||||
}
|
||||
}
|
||||
46
src/main/java/com/ai/da/common/utils/OrderNoUtils.java
Normal file
46
src/main/java/com/ai/da/common/utils/OrderNoUtils.java
Normal file
@@ -0,0 +1,46 @@
|
||||
package com.ai.da.common.utils;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* 订单号工具类
|
||||
*
|
||||
* @author qy
|
||||
* @since 1.0
|
||||
*/
|
||||
public class OrderNoUtils {
|
||||
|
||||
/**
|
||||
* 获取订单编号
|
||||
* @return
|
||||
*/
|
||||
public static String getOrderNo() {
|
||||
return "ORDER_" + getNo();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取退款单编号
|
||||
* @return
|
||||
*/
|
||||
public static String getRefundNo() {
|
||||
return "REFUND_" + getNo();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取编号
|
||||
* @return
|
||||
*/
|
||||
public static String getNo() {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
|
||||
String newDate = sdf.format(new Date());
|
||||
String result = "";
|
||||
Random random = new Random();
|
||||
for (int i = 0; i < 3; i++) {
|
||||
result += random.nextInt(10);
|
||||
}
|
||||
return newDate + result;
|
||||
}
|
||||
|
||||
}
|
||||
206
src/main/java/com/ai/da/controller/AliPayController.java
Normal file
206
src/main/java/com/ai/da/controller/AliPayController.java
Normal file
@@ -0,0 +1,206 @@
|
||||
package com.ai.da.controller;
|
||||
|
||||
import com.ai.da.common.response.Response;
|
||||
import com.ai.da.mapper.entity.OrderInfo;
|
||||
import com.ai.da.service.AliPayService;
|
||||
import com.ai.da.service.OrderInfoService;
|
||||
import com.alipay.api.AlipayApiException;
|
||||
import com.alipay.api.AlipayConstants;
|
||||
import com.alipay.api.internal.util.AlipaySignature;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Map;
|
||||
|
||||
@CrossOrigin
|
||||
@RestController
|
||||
@RequestMapping("/api/ali-pay")
|
||||
@Api(tags = "网站支付宝支付")
|
||||
@Slf4j
|
||||
public class AliPayController {
|
||||
|
||||
@Resource
|
||||
private AliPayService aliPayService;
|
||||
|
||||
@Resource
|
||||
private Environment config;
|
||||
|
||||
@Resource
|
||||
private OrderInfoService orderInfoService;
|
||||
|
||||
@ApiOperation("统一收单下单并支付页面接口的调用")
|
||||
@PostMapping("/trade/page/pay/{productId}")
|
||||
public Response<String> tradePagePay(@PathVariable Long productId){
|
||||
|
||||
log.info("统一收单下单并支付页面接口的调用");
|
||||
//支付宝开放平台接受 request 请求对象后
|
||||
// 会为开发者生成一个html 形式的 form表单,包含自动提交的脚本
|
||||
String formStr = aliPayService.tradeCreate(productId);
|
||||
//我们将form表单字符串返回给前端程序,之后前端将会调用自动提交脚本,进行表单的提交
|
||||
//此时,表单会自动提交到action属性所指向的支付宝开放平台中,从而为用户展示一个支付页面
|
||||
return Response.success(formStr);
|
||||
}
|
||||
|
||||
@ApiOperation("支付通知")
|
||||
@PostMapping("/trade/notify")
|
||||
public String tradeNotify(@RequestParam Map<String, String> params){
|
||||
|
||||
log.info("支付通知正在执行");
|
||||
log.info("通知参数 ===> {}", params);
|
||||
|
||||
String result = "failure";
|
||||
|
||||
try {
|
||||
//异步通知验签
|
||||
boolean signVerified = AlipaySignature.rsaCheckV1(
|
||||
params,
|
||||
config.getProperty("alipay.alipay-public-key"),
|
||||
AlipayConstants.CHARSET_UTF8,
|
||||
AlipayConstants.SIGN_TYPE_RSA2); //调用SDK验证签名
|
||||
|
||||
if(!signVerified){
|
||||
//验签失败则记录异常日志,并在response中返回failure.
|
||||
log.error("支付成功异步通知验签失败!");
|
||||
return result;
|
||||
}
|
||||
|
||||
// 验签成功后
|
||||
log.info("支付成功异步通知验签成功!");
|
||||
|
||||
//按照支付结果异步通知中的描述,对支付结果中的业务内容进行二次校验,
|
||||
//1 商户需要验证该通知数据中的 out_trade_no 是否为商户系统中创建的订单号
|
||||
String outTradeNo = params.get("out_trade_no");
|
||||
OrderInfo order = orderInfoService.getOrderByOrderNo(outTradeNo);
|
||||
if(order == null){
|
||||
log.error("订单不存在");
|
||||
return result;
|
||||
}
|
||||
|
||||
//2 判断 total_amount 是否确实为该订单的实际金额(即商户订单创建时的金额)
|
||||
String totalAmount = params.get("total_amount");
|
||||
int totalAmountInt = new BigDecimal(totalAmount).multiply(new BigDecimal("100")).intValue();
|
||||
int totalFeeInt = order.getTotalFee().intValue();
|
||||
if(totalAmountInt != totalFeeInt){
|
||||
log.error("金额校验失败");
|
||||
return result;
|
||||
}
|
||||
|
||||
//3 校验通知中的 seller_id(或者 seller_email) 是否为 out_trade_no 这笔单据的对应的操作方
|
||||
String sellerId = params.get("seller_id");
|
||||
String sellerIdProperty = config.getProperty("alipay.seller-id");
|
||||
if(!sellerId.equals(sellerIdProperty)){
|
||||
log.error("商家pid校验失败");
|
||||
return result;
|
||||
}
|
||||
|
||||
//4 验证 app_id 是否为该商户本身
|
||||
String appId = params.get("app_id");
|
||||
String appIdProperty = config.getProperty("alipay.app-id");
|
||||
if(!appId.equals(appIdProperty)){
|
||||
log.error("appid校验失败");
|
||||
return result;
|
||||
}
|
||||
|
||||
//在支付宝的业务通知中,只有交易通知状态为 TRADE_SUCCESS时,
|
||||
// 支付宝才会认定为买家付款成功。
|
||||
String tradeStatus = params.get("trade_status");
|
||||
if(!"TRADE_SUCCESS".equals(tradeStatus)){
|
||||
log.error("支付未成功");
|
||||
return result;
|
||||
}
|
||||
|
||||
//处理业务 修改订单状态 记录支付日志
|
||||
aliPayService.processOrder(params);
|
||||
|
||||
//校验成功后在response中返回success并继续商户自身业务处理,校验失败返回failure
|
||||
result = "success";
|
||||
} catch (AlipayApiException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户取消订单
|
||||
* @param orderNo
|
||||
* @return
|
||||
*/
|
||||
@ApiOperation("用户取消订单")
|
||||
@PostMapping("/trade/close/{orderNo}")
|
||||
public Response<String> cancel(@PathVariable String orderNo){
|
||||
|
||||
log.info("取消订单");
|
||||
aliPayService.cancelOrder(orderNo);
|
||||
return Response.success("订单已取消");
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询订单
|
||||
* @param orderNo
|
||||
* @return
|
||||
*/
|
||||
@ApiOperation("查询订单:测试订单状态用")
|
||||
@GetMapping("/trade/query/{orderNo}")
|
||||
public Response<String> queryOrder(@PathVariable String orderNo) {
|
||||
|
||||
log.info("查询订单");
|
||||
|
||||
String result = aliPayService.queryOrder(orderNo);
|
||||
return Response.success(result);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 申请退款
|
||||
* @param orderNo
|
||||
* @param reason
|
||||
* @return
|
||||
*/
|
||||
@ApiOperation("申请退款")
|
||||
@PostMapping("/trade/refund/{orderNo}/{reason}")
|
||||
public Response<String> refunds(@PathVariable String orderNo, @PathVariable String reason){
|
||||
|
||||
log.info("申请退款");
|
||||
aliPayService.refund(orderNo, reason);
|
||||
return Response.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询退款
|
||||
* @param orderNo
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
@ApiOperation("查询退款:测试用")
|
||||
@GetMapping("/trade/fastpay/refund/{orderNo}")
|
||||
public Response<String> queryRefund(@PathVariable String orderNo) throws Exception {
|
||||
|
||||
log.info("查询退款");
|
||||
|
||||
String result = aliPayService.queryRefund(orderNo);
|
||||
return Response.success(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据账单类型和日期获取账单url地址
|
||||
*
|
||||
* @param billDate
|
||||
* @param type
|
||||
* @return
|
||||
*/
|
||||
@ApiOperation("获取账单url")
|
||||
@GetMapping("/bill/downloadurl/query/{billDate}/{type}")
|
||||
public Response<String> queryTradeBill(
|
||||
@PathVariable String billDate,
|
||||
@PathVariable String type) {
|
||||
log.info("获取账单url");
|
||||
String downloadUrl = aliPayService.queryBill(billDate, type);
|
||||
return Response.success(downloadUrl);
|
||||
}
|
||||
|
||||
}
|
||||
50
src/main/java/com/ai/da/controller/OrderInfoController.java
Normal file
50
src/main/java/com/ai/da/controller/OrderInfoController.java
Normal file
@@ -0,0 +1,50 @@
|
||||
package com.ai.da.controller;
|
||||
|
||||
import com.ai.da.common.enums.OrderStatusEnum;
|
||||
import com.ai.da.common.response.Response;
|
||||
import com.ai.da.mapper.entity.OrderInfo;
|
||||
import com.ai.da.service.OrderInfoService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
@CrossOrigin //开放前端的跨域访问
|
||||
@Api(tags = "商品订单管理")
|
||||
@RestController
|
||||
@RequestMapping("/api/order-info")
|
||||
public class OrderInfoController {
|
||||
|
||||
@Resource
|
||||
private OrderInfoService orderInfoService;
|
||||
|
||||
@ApiOperation("订单列表")
|
||||
@GetMapping("/list")
|
||||
public Response<List<OrderInfo>> list(){
|
||||
|
||||
List<OrderInfo> list = orderInfoService.listOrderByCreateTimeDesc();
|
||||
return Response.success(list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询本地订单状态
|
||||
* @param orderNo
|
||||
* @return
|
||||
*/
|
||||
@ApiOperation("查询本地订单状态")
|
||||
@GetMapping("/query-order-status/{orderNo}")
|
||||
public Response<String> queryOrderStatus(@PathVariable String orderNo){
|
||||
|
||||
String orderStatus = orderInfoService.getOrderStatus(orderNo);
|
||||
if(OrderStatusEnum.SUCCESS.getType().equals(orderStatus)){
|
||||
return Response.success("支付成功"); //支付成功
|
||||
}
|
||||
|
||||
return Response.success(101,"支付中......");
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
42
src/main/java/com/ai/da/controller/ProductController.java
Normal file
42
src/main/java/com/ai/da/controller/ProductController.java
Normal file
@@ -0,0 +1,42 @@
|
||||
package com.ai.da.controller;
|
||||
|
||||
|
||||
import com.ai.da.common.response.Response;
|
||||
import com.ai.da.mapper.entity.Product;
|
||||
import com.ai.da.service.ProductService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@CrossOrigin //开放前端的跨域访问
|
||||
@Api(tags = "商品管理")
|
||||
@RestController
|
||||
@RequestMapping("/api/product")
|
||||
public class ProductController {
|
||||
|
||||
@Resource
|
||||
private ProductService productService;
|
||||
|
||||
@ApiOperation("测试接口")
|
||||
@GetMapping("/test")
|
||||
public Response<String> test(){
|
||||
|
||||
return Response.success("now" + new Date());
|
||||
}
|
||||
|
||||
@ApiOperation("商品列表")
|
||||
@GetMapping("/list")
|
||||
public Response<List<Product>> list(){
|
||||
|
||||
List<Product> list = productService.list();
|
||||
return Response.success(list);
|
||||
}
|
||||
|
||||
}
|
||||
8
src/main/java/com/ai/da/mapper/OrderInfoMapper.java
Normal file
8
src/main/java/com/ai/da/mapper/OrderInfoMapper.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package com.ai.da.mapper;
|
||||
|
||||
import com.ai.da.mapper.entity.OrderInfo;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
|
||||
public interface OrderInfoMapper extends BaseMapper<OrderInfo> {
|
||||
|
||||
}
|
||||
7
src/main/java/com/ai/da/mapper/PaymentInfoMapper.java
Normal file
7
src/main/java/com/ai/da/mapper/PaymentInfoMapper.java
Normal file
@@ -0,0 +1,7 @@
|
||||
package com.ai.da.mapper;
|
||||
|
||||
import com.ai.da.mapper.entity.PaymentInfo;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
|
||||
public interface PaymentInfoMapper extends BaseMapper<PaymentInfo> {
|
||||
}
|
||||
8
src/main/java/com/ai/da/mapper/ProductMapper.java
Normal file
8
src/main/java/com/ai/da/mapper/ProductMapper.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package com.ai.da.mapper;
|
||||
|
||||
import com.ai.da.mapper.entity.Product;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
|
||||
public interface ProductMapper extends BaseMapper<Product> {
|
||||
|
||||
}
|
||||
9
src/main/java/com/ai/da/mapper/RefundInfoMapper.java
Normal file
9
src/main/java/com/ai/da/mapper/RefundInfoMapper.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package com.ai.da.mapper;
|
||||
|
||||
|
||||
import com.ai.da.mapper.entity.RefundInfo;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
|
||||
public interface RefundInfoMapper extends BaseMapper<RefundInfo> {
|
||||
|
||||
}
|
||||
19
src/main/java/com/ai/da/mapper/entity/BaseEntity.java
Normal file
19
src/main/java/com/ai/da/mapper/entity/BaseEntity.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package com.ai.da.mapper.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@Data
|
||||
public class BaseEntity {
|
||||
|
||||
//定义主键策略:跟随数据库的主键自增
|
||||
@TableId(value = "id", type = IdType.AUTO)
|
||||
private String id; //主键
|
||||
|
||||
private Date createTime;//创建时间
|
||||
|
||||
private Date updateTime;//更新时间
|
||||
}
|
||||
25
src/main/java/com/ai/da/mapper/entity/OrderInfo.java
Normal file
25
src/main/java/com/ai/da/mapper/entity/OrderInfo.java
Normal file
@@ -0,0 +1,25 @@
|
||||
package com.ai.da.mapper.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@TableName("t_order_info")
|
||||
public class OrderInfo extends BaseEntity{
|
||||
|
||||
private String title;//订单标题
|
||||
|
||||
private String orderNo;//商户订单编号
|
||||
|
||||
private Long userId;//用户id
|
||||
|
||||
private Long productId;//支付产品id
|
||||
|
||||
private Integer totalFee;//订单金额(分)
|
||||
|
||||
private String codeUrl;//订单二维码连接
|
||||
|
||||
private String orderStatus;//订单状态
|
||||
|
||||
private String paymentType;//支付方式
|
||||
}
|
||||
23
src/main/java/com/ai/da/mapper/entity/PaymentInfo.java
Normal file
23
src/main/java/com/ai/da/mapper/entity/PaymentInfo.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package com.ai.da.mapper.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@TableName("t_payment_info")
|
||||
public class PaymentInfo extends BaseEntity{
|
||||
|
||||
private String orderNo;//商品订单编号
|
||||
|
||||
private String transactionId;//支付系统交易编号
|
||||
|
||||
private String paymentType;//支付类型
|
||||
|
||||
private String tradeType;//交易类型
|
||||
|
||||
private String tradeState;//交易状态
|
||||
|
||||
private Integer payerTotal;//支付金额(分)
|
||||
|
||||
private String content;//通知参数
|
||||
}
|
||||
13
src/main/java/com/ai/da/mapper/entity/Product.java
Normal file
13
src/main/java/com/ai/da/mapper/entity/Product.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package com.ai.da.mapper.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@TableName("t_product")
|
||||
public class Product extends BaseEntity{
|
||||
|
||||
private String title; //商品名称
|
||||
|
||||
private Integer price; //价格(分)
|
||||
}
|
||||
27
src/main/java/com/ai/da/mapper/entity/RefundInfo.java
Normal file
27
src/main/java/com/ai/da/mapper/entity/RefundInfo.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package com.ai.da.mapper.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@TableName("t_refund_info")
|
||||
public class RefundInfo extends BaseEntity{
|
||||
|
||||
private String orderNo;//商品订单编号
|
||||
|
||||
private String refundNo;//退款单编号
|
||||
|
||||
private String refundId;//支付系统退款单号
|
||||
|
||||
private Integer totalFee;//原订单金额(分)
|
||||
|
||||
private Integer refund;//退款金额(分)
|
||||
|
||||
private String reason;//退款原因
|
||||
|
||||
private String refundStatus;//退款单状态
|
||||
|
||||
private String contentReturn;//申请退款返回参数
|
||||
|
||||
private String contentNotify;//退款结果通知参数
|
||||
}
|
||||
22
src/main/java/com/ai/da/service/AliPayService.java
Normal file
22
src/main/java/com/ai/da/service/AliPayService.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package com.ai.da.service;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public interface AliPayService {
|
||||
String tradeCreate(Long productId);
|
||||
|
||||
void processOrder(Map<String, String> params);
|
||||
|
||||
void cancelOrder(String orderNo);
|
||||
|
||||
String queryOrder(String orderNo);
|
||||
|
||||
void checkOrderStatus(String orderNo);
|
||||
|
||||
void refund(String orderNo, String reason);
|
||||
|
||||
String queryRefund(String orderNo);
|
||||
|
||||
String queryBill(String billDate, String type);
|
||||
|
||||
}
|
||||
25
src/main/java/com/ai/da/service/OrderInfoService.java
Normal file
25
src/main/java/com/ai/da/service/OrderInfoService.java
Normal file
@@ -0,0 +1,25 @@
|
||||
package com.ai.da.service;
|
||||
|
||||
|
||||
import com.ai.da.common.enums.OrderStatusEnum;
|
||||
import com.ai.da.mapper.entity.OrderInfo;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface OrderInfoService extends IService<OrderInfo> {
|
||||
|
||||
OrderInfo createOrderByProductId(Long productId, String paymentType);
|
||||
|
||||
void saveCodeUrl(String orderNo, String codeUrl);
|
||||
|
||||
List<OrderInfo> listOrderByCreateTimeDesc();
|
||||
|
||||
void updateStatusByOrderNo(String orderNo, OrderStatusEnum orderStatus);
|
||||
|
||||
String getOrderStatus(String orderNo);
|
||||
|
||||
List<OrderInfo> getNoPayOrderByDuration(int minutes, String paymentType);
|
||||
|
||||
OrderInfo getOrderByOrderNo(String orderNo);
|
||||
}
|
||||
10
src/main/java/com/ai/da/service/PaymentInfoService.java
Normal file
10
src/main/java/com/ai/da/service/PaymentInfoService.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package com.ai.da.service;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public interface PaymentInfoService {
|
||||
|
||||
void createPaymentInfo(String plainText);
|
||||
|
||||
void createPaymentInfoForAliPay(Map<String, String> params);
|
||||
}
|
||||
8
src/main/java/com/ai/da/service/ProductService.java
Normal file
8
src/main/java/com/ai/da/service/ProductService.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package com.ai.da.service;
|
||||
|
||||
import com.ai.da.mapper.entity.Product;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
public interface ProductService extends IService<Product> {
|
||||
|
||||
}
|
||||
20
src/main/java/com/ai/da/service/RefundInfoService.java
Normal file
20
src/main/java/com/ai/da/service/RefundInfoService.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package com.ai.da.service;
|
||||
|
||||
|
||||
import com.ai.da.mapper.entity.RefundInfo;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface RefundInfoService extends IService<RefundInfo> {
|
||||
|
||||
RefundInfo createRefundByOrderNo(String orderNo, String reason);
|
||||
|
||||
void updateRefund(String content);
|
||||
|
||||
List<RefundInfo> getNoRefundOrderByDuration(int minutes);
|
||||
|
||||
RefundInfo createRefundByOrderNoForAliPay(String orderNo, String reason);
|
||||
|
||||
void updateRefundForAliPay(String refundNo, String content, String refundStatus);
|
||||
}
|
||||
394
src/main/java/com/ai/da/service/impl/AliPayServiceImpl.java
Normal file
394
src/main/java/com/ai/da/service/impl/AliPayServiceImpl.java
Normal file
@@ -0,0 +1,394 @@
|
||||
package com.ai.da.service.impl;
|
||||
|
||||
import com.ai.da.common.enums.AliPayTradeStateEnum;
|
||||
import com.ai.da.common.enums.OrderStatusEnum;
|
||||
import com.ai.da.common.enums.PayTypeEnum;
|
||||
import com.ai.da.mapper.entity.OrderInfo;
|
||||
import com.ai.da.mapper.entity.RefundInfo;
|
||||
import com.ai.da.service.AliPayService;
|
||||
import com.ai.da.service.OrderInfoService;
|
||||
import com.ai.da.service.PaymentInfoService;
|
||||
import com.ai.da.service.RefundInfoService;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alipay.api.AlipayApiException;
|
||||
import com.alipay.api.AlipayClient;
|
||||
import com.alipay.api.request.*;
|
||||
import com.alipay.api.response.*;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.internal.LinkedTreeMap;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class AliPayServiceImpl implements AliPayService {
|
||||
|
||||
@Resource
|
||||
private OrderInfoService orderInfoService;
|
||||
|
||||
@Resource
|
||||
private AlipayClient alipayClient;
|
||||
|
||||
@Resource
|
||||
private Environment config;
|
||||
|
||||
@Resource
|
||||
private PaymentInfoService paymentInfoService;
|
||||
|
||||
@Resource
|
||||
private RefundInfoService refundsInfoService;
|
||||
|
||||
private final ReentrantLock lock = new ReentrantLock();
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public String tradeCreate(Long productId) {
|
||||
|
||||
try {
|
||||
//生成订单
|
||||
log.info("生成订单");
|
||||
OrderInfo orderInfo = orderInfoService.createOrderByProductId(productId, PayTypeEnum.ALIPAY.getType());
|
||||
|
||||
//调用支付宝接口
|
||||
AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
|
||||
//配置需要的公共请求参数
|
||||
//支付完成后,支付宝向谷粒学院发起异步通知的地址
|
||||
request.setNotifyUrl(config.getProperty("alipay.notify-url"));
|
||||
//支付完成后,我们想让页面跳转回谷粒学院的页面,配置returnUrl
|
||||
request.setReturnUrl(config.getProperty("alipay.return-url"));
|
||||
|
||||
//组装当前业务方法的请求参数
|
||||
JSONObject bizContent = new JSONObject();
|
||||
bizContent.put("out_trade_no", orderInfo.getOrderNo());
|
||||
BigDecimal total = new BigDecimal(orderInfo.getTotalFee().toString()).divide(new BigDecimal("100"));
|
||||
bizContent.put("total_amount", total);
|
||||
bizContent.put("subject", orderInfo.getTitle());
|
||||
bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");
|
||||
|
||||
request.setBizContent(bizContent.toString());
|
||||
|
||||
//执行请求,调用支付宝接口
|
||||
AlipayTradePagePayResponse response = alipayClient.pageExecute(request);
|
||||
|
||||
if(response.isSuccess()){
|
||||
log.info("调用成功,返回结果 ===> " + response.getBody());
|
||||
return response.getBody();
|
||||
} else {
|
||||
log.info("调用失败,返回码 ===> " + response.getCode() + ", 返回描述 ===> " + response.getMsg());
|
||||
throw new RuntimeException("创建支付交易失败");
|
||||
}
|
||||
} catch (AlipayApiException e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException("创建支付交易失败");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理订单
|
||||
* @param params
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public void processOrder(Map<String, String> params) {
|
||||
|
||||
log.info("处理订单");
|
||||
|
||||
//获取订单号
|
||||
String orderNo = params.get("out_trade_no");
|
||||
|
||||
/*在对业务数据进行状态检查和处理之前,
|
||||
要采用数据锁进行并发控制,
|
||||
以避免函数重入造成的数据混乱*/
|
||||
//尝试获取锁:
|
||||
// 成功获取则立即返回true,获取失败则立即返回false。不必一直等待锁的释放
|
||||
if(lock.tryLock()) {
|
||||
try {
|
||||
|
||||
//处理重复通知
|
||||
//接口调用的幂等性:无论接口被调用多少次,以下业务执行一次
|
||||
String orderStatus = orderInfoService.getOrderStatus(orderNo);
|
||||
if (!OrderStatusEnum.NOT_PAY.getType().equals(orderStatus)) {
|
||||
return;
|
||||
}
|
||||
|
||||
//更新订单状态
|
||||
orderInfoService.updateStatusByOrderNo(orderNo, OrderStatusEnum.SUCCESS);
|
||||
|
||||
//记录支付日志
|
||||
paymentInfoService.createPaymentInfoForAliPay(params);
|
||||
|
||||
} finally {
|
||||
//要主动释放锁
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户取消订单
|
||||
* @param orderNo
|
||||
*/
|
||||
@Override
|
||||
public void cancelOrder(String orderNo) {
|
||||
|
||||
//调用支付宝提供的统一收单交易关闭接口
|
||||
this.closeOrder(orderNo);
|
||||
|
||||
//更新用户订单状态
|
||||
orderInfoService.updateStatusByOrderNo(orderNo, OrderStatusEnum.CANCEL);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询订单
|
||||
* @param orderNo
|
||||
* @return 返回订单查询结果,如果返回null则表示支付宝端尚未创建订单
|
||||
*/
|
||||
@Override
|
||||
public String queryOrder(String orderNo) {
|
||||
|
||||
try {
|
||||
log.info("查单接口调用 ===> {}", orderNo);
|
||||
|
||||
AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
|
||||
JSONObject bizContent = new JSONObject();
|
||||
bizContent.put("out_trade_no", orderNo);
|
||||
request.setBizContent(bizContent.toString());
|
||||
|
||||
AlipayTradeQueryResponse response = alipayClient.execute(request);
|
||||
if(response.isSuccess()){
|
||||
log.info("调用成功,返回结果 ===> " + response.getBody());
|
||||
return response.getBody();
|
||||
} else {
|
||||
log.info("调用失败,返回码 ===> " + response.getCode() + ", 返回描述 ===> " + response.getMsg());
|
||||
//throw new RuntimeException("查单接口的调用失败");
|
||||
return null;//订单不存在
|
||||
}
|
||||
|
||||
} catch (AlipayApiException e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException("查单接口的调用失败");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据订单号调用支付宝查单接口,核实订单状态
|
||||
* 如果订单未创建,则更新商户端订单状态
|
||||
* 如果订单未支付,则调用关单接口关闭订单,并更新商户端订单状态
|
||||
* 如果订单已支付,则更新商户端订单状态,并记录支付日志
|
||||
* @param orderNo
|
||||
*/
|
||||
@Override
|
||||
public void checkOrderStatus(String orderNo) {
|
||||
|
||||
log.warn("根据订单号核实订单状态 ===> {}", orderNo);
|
||||
|
||||
String result = this.queryOrder(orderNo);
|
||||
|
||||
//订单未创建
|
||||
if(result == null){
|
||||
log.warn("核实订单未创建 ===> {}", orderNo);
|
||||
//更新本地订单状态
|
||||
orderInfoService.updateStatusByOrderNo(orderNo, OrderStatusEnum.CLOSED);
|
||||
}
|
||||
|
||||
//解析查单响应结果
|
||||
Gson gson = new Gson();
|
||||
HashMap<String, LinkedTreeMap> resultMap = gson.fromJson(result, HashMap.class);
|
||||
LinkedTreeMap alipayTradeQueryResponse = resultMap.get("alipay_trade_query_response");
|
||||
|
||||
String tradeStatus = (String)alipayTradeQueryResponse.get("trade_status");
|
||||
if(AliPayTradeStateEnum.NOTPAY.getType().equals(tradeStatus)){
|
||||
log.warn("核实订单未支付 ===> {}", orderNo);
|
||||
|
||||
//如果订单未支付,则调用关单接口关闭订单
|
||||
this.closeOrder(orderNo);
|
||||
|
||||
// 并更新商户端订单状态
|
||||
orderInfoService.updateStatusByOrderNo(orderNo, OrderStatusEnum.CLOSED);
|
||||
}
|
||||
|
||||
if(AliPayTradeStateEnum.SUCCESS.getType().equals(tradeStatus)){
|
||||
log.warn("核实订单已支付 ===> {}", orderNo);
|
||||
|
||||
//如果订单已支付,则更新商户端订单状态
|
||||
orderInfoService.updateStatusByOrderNo(orderNo, OrderStatusEnum.SUCCESS);
|
||||
|
||||
//并记录支付日志
|
||||
paymentInfoService.createPaymentInfoForAliPay(alipayTradeQueryResponse);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 关单接口的调用
|
||||
* @param orderNo 订单号
|
||||
*/
|
||||
private void closeOrder(String orderNo) {
|
||||
|
||||
try {
|
||||
log.info("关单接口的调用,订单号 ===> {}", orderNo);
|
||||
|
||||
AlipayTradeCloseRequest request = new AlipayTradeCloseRequest();
|
||||
JSONObject bizContent = new JSONObject();
|
||||
bizContent.put("out_trade_no", orderNo);
|
||||
request.setBizContent(bizContent.toString());
|
||||
AlipayTradeCloseResponse response = alipayClient.execute(request);
|
||||
|
||||
if(response.isSuccess()){
|
||||
log.info("调用成功,返回结果 ===> " + response.getBody());
|
||||
} else {
|
||||
log.info("调用失败,返回码 ===> " + response.getCode() + ", 返回描述 ===> " + response.getMsg());
|
||||
//throw new RuntimeException("关单接口的调用失败");
|
||||
}
|
||||
|
||||
} catch (AlipayApiException e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException("关单接口的调用失败");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 退款
|
||||
* @param orderNo
|
||||
* @param reason
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public void refund(String orderNo, String reason) {
|
||||
|
||||
try {
|
||||
log.info("调用退款API");
|
||||
|
||||
//创建退款单
|
||||
RefundInfo refundInfo = refundsInfoService.createRefundByOrderNoForAliPay(orderNo, reason);
|
||||
|
||||
//调用统一收单交易退款接口
|
||||
AlipayTradeRefundRequest request = new AlipayTradeRefundRequest ();
|
||||
|
||||
//组装当前业务方法的请求参数
|
||||
JSONObject bizContent = new JSONObject();
|
||||
bizContent.put("out_trade_no", orderNo);//订单编号
|
||||
BigDecimal refund = new BigDecimal(refundInfo.getRefund().toString()).divide(new BigDecimal("100"));
|
||||
//BigDecimal refund = new BigDecimal("2").divide(new BigDecimal("100"));
|
||||
bizContent.put("refund_amount", refund);//退款金额:不能大于支付金额
|
||||
bizContent.put("refund_reason", reason);//退款原因(可选)
|
||||
|
||||
request.setBizContent(bizContent.toString());
|
||||
|
||||
//执行请求,调用支付宝接口
|
||||
AlipayTradeRefundResponse response = alipayClient.execute(request);
|
||||
|
||||
if(response.isSuccess()){
|
||||
log.info("调用成功,返回结果 ===> " + response.getBody());
|
||||
|
||||
//更新订单状态
|
||||
orderInfoService.updateStatusByOrderNo(orderNo, OrderStatusEnum.REFUND_SUCCESS);
|
||||
|
||||
//更新退款单
|
||||
refundsInfoService.updateRefundForAliPay(
|
||||
refundInfo.getRefundNo(),
|
||||
response.getBody(),
|
||||
AliPayTradeStateEnum.REFUND_SUCCESS.getType()); //退款成功
|
||||
|
||||
} else {
|
||||
log.info("调用失败,返回码 ===> " + response.getCode() + ", 返回描述 ===> " + response.getMsg());
|
||||
|
||||
//更新订单状态
|
||||
orderInfoService.updateStatusByOrderNo(orderNo, OrderStatusEnum.REFUND_ABNORMAL);
|
||||
|
||||
//更新退款单
|
||||
refundsInfoService.updateRefundForAliPay(
|
||||
refundInfo.getRefundNo(),
|
||||
response.getBody(),
|
||||
AliPayTradeStateEnum.REFUND_ERROR.getType()); //退款失败
|
||||
}
|
||||
|
||||
|
||||
} catch (AlipayApiException e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException("创建退款申请失败");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询退款
|
||||
* @param orderNo
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public String queryRefund(String orderNo) {
|
||||
|
||||
try {
|
||||
log.info("查询退款接口调用 ===> {}", orderNo);
|
||||
|
||||
AlipayTradeFastpayRefundQueryRequest request = new AlipayTradeFastpayRefundQueryRequest();
|
||||
JSONObject bizContent = new JSONObject();
|
||||
bizContent.put("out_trade_no", orderNo);
|
||||
bizContent.put("out_request_no", orderNo);
|
||||
request.setBizContent(bizContent.toString());
|
||||
|
||||
AlipayTradeFastpayRefundQueryResponse response = alipayClient.execute(request);
|
||||
if(response.isSuccess()){
|
||||
log.info("调用成功,返回结果 ===> " + response.getBody());
|
||||
return response.getBody();
|
||||
} else {
|
||||
log.info("调用失败,返回码 ===> " + response.getCode() + ", 返回描述 ===> " + response.getMsg());
|
||||
//throw new RuntimeException("查单接口的调用失败");
|
||||
return null;//订单不存在
|
||||
}
|
||||
|
||||
} catch (AlipayApiException e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException("查单接口的调用失败");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 申请账单
|
||||
* @param billDate
|
||||
* @param type
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public String queryBill(String billDate, String type) {
|
||||
|
||||
try {
|
||||
|
||||
AlipayDataDataserviceBillDownloadurlQueryRequest request = new AlipayDataDataserviceBillDownloadurlQueryRequest();
|
||||
JSONObject bizContent = new JSONObject();
|
||||
bizContent.put("bill_type", type);
|
||||
bizContent.put("bill_date", billDate);
|
||||
request.setBizContent(bizContent.toString());
|
||||
AlipayDataDataserviceBillDownloadurlQueryResponse response = alipayClient.execute(request);
|
||||
|
||||
if(response.isSuccess()){
|
||||
log.info("调用成功,返回结果 ===> " + response.getBody());
|
||||
|
||||
//获取账单下载地址
|
||||
Gson gson = new Gson();
|
||||
HashMap<String, LinkedTreeMap> resultMap = gson.fromJson(response.getBody(), HashMap.class);
|
||||
LinkedTreeMap billDownloadurlResponse = resultMap.get("alipay_data_dataservice_bill_downloadurl_query_response");
|
||||
String billDownloadUrl = (String)billDownloadurlResponse.get("bill_download_url");
|
||||
|
||||
return billDownloadUrl;
|
||||
} else {
|
||||
log.info("调用失败,返回码 ===> " + response.getCode() + ", 返回描述 ===> " + response.getMsg());
|
||||
throw new RuntimeException("申请账单失败");
|
||||
}
|
||||
|
||||
} catch (AlipayApiException e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException("申请账单失败");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
172
src/main/java/com/ai/da/service/impl/OrderInfoServiceImpl.java
Normal file
172
src/main/java/com/ai/da/service/impl/OrderInfoServiceImpl.java
Normal file
@@ -0,0 +1,172 @@
|
||||
package com.ai.da.service.impl;
|
||||
|
||||
|
||||
import com.ai.da.common.enums.OrderStatusEnum;
|
||||
import com.ai.da.common.utils.OrderNoUtils;
|
||||
import com.ai.da.mapper.OrderInfoMapper;
|
||||
import com.ai.da.mapper.ProductMapper;
|
||||
import com.ai.da.mapper.entity.OrderInfo;
|
||||
import com.ai.da.mapper.entity.Product;
|
||||
import com.ai.da.service.OrderInfoService;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements OrderInfoService {
|
||||
|
||||
@Resource
|
||||
private ProductMapper productMapper;
|
||||
|
||||
/*@Resource
|
||||
private OrderInfoMapper orderInfoMapper;*/
|
||||
|
||||
@Override
|
||||
public OrderInfo createOrderByProductId(Long productId, String paymentType) {
|
||||
|
||||
//查找已存在但未支付的订单
|
||||
OrderInfo orderInfo = this.getNoPayOrderByProductId(productId, paymentType);
|
||||
if( orderInfo != null){
|
||||
return orderInfo;
|
||||
}
|
||||
|
||||
//获取商品信息
|
||||
Product product = productMapper.selectById(productId);
|
||||
|
||||
//生成订单
|
||||
orderInfo = new OrderInfo();
|
||||
orderInfo.setTitle(product.getTitle());
|
||||
orderInfo.setOrderNo(OrderNoUtils.getOrderNo()); //订单号
|
||||
orderInfo.setProductId(productId);
|
||||
orderInfo.setTotalFee(product.getPrice()); //分
|
||||
orderInfo.setOrderStatus(OrderStatusEnum.NOT_PAY.getType()); //未支付
|
||||
orderInfo.setPaymentType(paymentType);
|
||||
baseMapper.insert(orderInfo);
|
||||
|
||||
return orderInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 存储订单二维码
|
||||
* @param orderNo
|
||||
* @param codeUrl
|
||||
*/
|
||||
@Override
|
||||
public void saveCodeUrl(String orderNo, String codeUrl) {
|
||||
|
||||
QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("order_no", orderNo);
|
||||
|
||||
OrderInfo orderInfo = new OrderInfo();
|
||||
orderInfo.setCodeUrl(codeUrl);
|
||||
|
||||
baseMapper.update(orderInfo, queryWrapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询订单列表,并倒序查询
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public List<OrderInfo> listOrderByCreateTimeDesc() {
|
||||
|
||||
QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<OrderInfo>().orderByDesc("create_time");
|
||||
return baseMapper.selectList(queryWrapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据订单号更新订单状态
|
||||
* @param orderNo
|
||||
* @param orderStatus
|
||||
*/
|
||||
@Override
|
||||
public void updateStatusByOrderNo(String orderNo, OrderStatusEnum orderStatus) {
|
||||
|
||||
log.info("更新订单状态 ===> {}", orderStatus.getType());
|
||||
|
||||
QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("order_no", orderNo);
|
||||
|
||||
OrderInfo orderInfo = new OrderInfo();
|
||||
orderInfo.setOrderStatus(orderStatus.getType());
|
||||
|
||||
baseMapper.update(orderInfo, queryWrapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据订单号获取订单状态
|
||||
* @param orderNo
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public String getOrderStatus(String orderNo) {
|
||||
|
||||
QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("order_no", orderNo);
|
||||
OrderInfo orderInfo = baseMapper.selectOne(queryWrapper);
|
||||
if(orderInfo == null){
|
||||
return null;
|
||||
}
|
||||
return orderInfo.getOrderStatus();
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询创建超过minutes分钟并且未支付的订单
|
||||
* @param minutes
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public List<OrderInfo> getNoPayOrderByDuration(int minutes, String paymentType) {
|
||||
|
||||
Instant instant = Instant.now().minus(Duration.ofMinutes(minutes));
|
||||
|
||||
QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("order_status", OrderStatusEnum.NOT_PAY.getType());
|
||||
queryWrapper.le("create_time", instant);
|
||||
queryWrapper.eq("payment_type", paymentType);
|
||||
|
||||
List<OrderInfo> orderInfoList = baseMapper.selectList(queryWrapper);
|
||||
|
||||
return orderInfoList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据订单号获取订单
|
||||
* @param orderNo
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public OrderInfo getOrderByOrderNo(String orderNo) {
|
||||
|
||||
QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("order_no", orderNo);
|
||||
OrderInfo orderInfo = baseMapper.selectOne(queryWrapper);
|
||||
|
||||
return orderInfo;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 根据商品id查询未支付订单
|
||||
* 防止重复创建订单对象
|
||||
* @param productId
|
||||
* @return
|
||||
*/
|
||||
private OrderInfo getNoPayOrderByProductId(Long productId, String paymentType) {
|
||||
|
||||
QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("product_id", productId);
|
||||
queryWrapper.eq("order_status", OrderStatusEnum.NOT_PAY.getType());
|
||||
queryWrapper.eq("payment_type", paymentType);
|
||||
// queryWrapper.eq("user_id", userId);
|
||||
OrderInfo orderInfo = baseMapper.selectOne(queryWrapper);
|
||||
return orderInfo;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
package com.ai.da.service.impl;
|
||||
|
||||
import com.ai.da.common.enums.PayTypeEnum;
|
||||
import com.ai.da.mapper.PaymentInfoMapper;
|
||||
import com.ai.da.mapper.entity.PaymentInfo;
|
||||
import com.ai.da.service.PaymentInfoService;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.google.gson.Gson;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo> implements PaymentInfoService {
|
||||
|
||||
/**
|
||||
* 记录支付日志:微信支付
|
||||
* @param plainText
|
||||
*/
|
||||
@Override
|
||||
public void createPaymentInfo(String plainText) {
|
||||
|
||||
log.info("记录支付日志");
|
||||
|
||||
Gson gson = new Gson();
|
||||
HashMap plainTextMap = gson.fromJson(plainText, HashMap.class);
|
||||
|
||||
//订单号
|
||||
String orderNo = (String)plainTextMap.get("out_trade_no");
|
||||
//业务编号
|
||||
String transactionId = (String)plainTextMap.get("transaction_id");
|
||||
//支付类型
|
||||
String tradeType = (String)plainTextMap.get("trade_type");
|
||||
//交易状态
|
||||
String tradeState = (String)plainTextMap.get("trade_state");
|
||||
//用户实际支付金额
|
||||
Map<String, Object> amount = (Map)plainTextMap.get("amount");
|
||||
Integer payerTotal = ((Double) amount.get("payer_total")).intValue();
|
||||
|
||||
PaymentInfo paymentInfo = new PaymentInfo();
|
||||
paymentInfo.setOrderNo(orderNo);
|
||||
paymentInfo.setPaymentType(PayTypeEnum.WXPAY.getType());
|
||||
paymentInfo.setTransactionId(transactionId);
|
||||
paymentInfo.setTradeType(tradeType);
|
||||
paymentInfo.setTradeState(tradeState);
|
||||
paymentInfo.setPayerTotal(payerTotal);
|
||||
paymentInfo.setContent(plainText);
|
||||
|
||||
baseMapper.insert(paymentInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录支付日志:支付宝
|
||||
* @param params
|
||||
*/
|
||||
@Override
|
||||
public void createPaymentInfoForAliPay(Map<String, String> params) {
|
||||
|
||||
log.info("记录支付日志");
|
||||
|
||||
//获取订单号
|
||||
String orderNo = params.get("out_trade_no");
|
||||
//业务编号
|
||||
String transactionId = params.get("trade_no");
|
||||
//交易状态
|
||||
String tradeStatus = params.get("trade_status");
|
||||
//交易金额
|
||||
String totalAmount = params.get("total_amount");
|
||||
int totalAmountInt = new BigDecimal(totalAmount).multiply(new BigDecimal("100")).intValue();
|
||||
|
||||
|
||||
PaymentInfo paymentInfo = new PaymentInfo();
|
||||
paymentInfo.setOrderNo(orderNo);
|
||||
paymentInfo.setPaymentType(PayTypeEnum.ALIPAY.getType());
|
||||
paymentInfo.setTransactionId(transactionId);
|
||||
paymentInfo.setTradeType("电脑网站支付");
|
||||
paymentInfo.setTradeState(tradeStatus);
|
||||
paymentInfo.setPayerTotal(totalAmountInt);
|
||||
|
||||
Gson gson = new Gson();
|
||||
String json = gson.toJson(params, HashMap.class);
|
||||
paymentInfo.setContent(json);
|
||||
|
||||
baseMapper.insert(paymentInfo);
|
||||
}
|
||||
}
|
||||
12
src/main/java/com/ai/da/service/impl/ProductServiceImpl.java
Normal file
12
src/main/java/com/ai/da/service/impl/ProductServiceImpl.java
Normal file
@@ -0,0 +1,12 @@
|
||||
package com.ai.da.service.impl;
|
||||
|
||||
import com.ai.da.mapper.ProductMapper;
|
||||
import com.ai.da.mapper.entity.Product;
|
||||
import com.ai.da.service.ProductService;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {
|
||||
|
||||
}
|
||||
156
src/main/java/com/ai/da/service/impl/RefundInfoServiceImpl.java
Normal file
156
src/main/java/com/ai/da/service/impl/RefundInfoServiceImpl.java
Normal file
@@ -0,0 +1,156 @@
|
||||
package com.ai.da.service.impl;
|
||||
|
||||
import com.ai.da.common.utils.OrderNoUtils;
|
||||
import com.ai.da.mapper.RefundInfoMapper;
|
||||
import com.ai.da.mapper.entity.OrderInfo;
|
||||
import com.ai.da.mapper.entity.RefundInfo;
|
||||
import com.ai.da.service.OrderInfoService;
|
||||
import com.ai.da.service.RefundInfoService;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.google.gson.Gson;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
public class RefundInfoServiceImpl extends ServiceImpl<RefundInfoMapper, RefundInfo> implements RefundInfoService {
|
||||
|
||||
@Resource
|
||||
private OrderInfoService orderInfoService;
|
||||
|
||||
/**
|
||||
* 根据订单号创建退款订单
|
||||
* @param orderNo
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public RefundInfo createRefundByOrderNo(String orderNo, String reason) {
|
||||
|
||||
//根据订单号获取订单信息
|
||||
OrderInfo orderInfo = orderInfoService.getOrderByOrderNo(orderNo);
|
||||
|
||||
//根据订单号生成退款订单
|
||||
RefundInfo refundInfo = new RefundInfo();
|
||||
refundInfo.setOrderNo(orderNo);//订单编号
|
||||
refundInfo.setRefundNo(OrderNoUtils.getRefundNo());//退款单编号
|
||||
refundInfo.setTotalFee(orderInfo.getTotalFee());//原订单金额(分)
|
||||
refundInfo.setRefund(orderInfo.getTotalFee());//退款金额(分)
|
||||
refundInfo.setReason(reason);//退款原因
|
||||
|
||||
//保存退款订单
|
||||
baseMapper.insert(refundInfo);
|
||||
|
||||
return refundInfo;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 记录退款记录
|
||||
* @param content
|
||||
*/
|
||||
@Override
|
||||
public void updateRefund(String content) {
|
||||
|
||||
//将json字符串转换成Map
|
||||
Gson gson = new Gson();
|
||||
Map<String, String> resultMap = gson.fromJson(content, HashMap.class);
|
||||
|
||||
//根据退款单编号修改退款单
|
||||
QueryWrapper<RefundInfo> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("refund_no", resultMap.get("out_refund_no"));
|
||||
|
||||
//设置要修改的字段
|
||||
RefundInfo refundInfo = new RefundInfo();
|
||||
|
||||
refundInfo.setRefundId(resultMap.get("refund_id"));//微信支付退款单号
|
||||
|
||||
//查询退款和申请退款中的返回参数
|
||||
if(resultMap.get("status") != null){
|
||||
refundInfo.setRefundStatus(resultMap.get("status"));//退款状态
|
||||
refundInfo.setContentReturn(content);//将全部响应结果存入数据库的content字段
|
||||
}
|
||||
//退款回调中的回调参数
|
||||
if(resultMap.get("refund_status") != null){
|
||||
refundInfo.setRefundStatus(resultMap.get("refund_status"));//退款状态
|
||||
refundInfo.setContentNotify(content);//将全部响应结果存入数据库的content字段
|
||||
}
|
||||
|
||||
//更新退款单
|
||||
baseMapper.update(refundInfo, queryWrapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* 找出申请退款超过minutes分钟并且未成功的退款单
|
||||
* @param minutes
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public List<RefundInfo> getNoRefundOrderByDuration(int minutes) {
|
||||
|
||||
//minutes分钟之前的时间
|
||||
Instant instant = Instant.now().minus(Duration.ofMinutes(minutes));
|
||||
|
||||
QueryWrapper<RefundInfo> queryWrapper = new QueryWrapper<>();
|
||||
// queryWrapper.eq("refund_status", WxRefundStatus.PROCESSING.getType());
|
||||
queryWrapper.le("create_time", instant);
|
||||
List<RefundInfo> refundInfoList = baseMapper.selectList(queryWrapper);
|
||||
return refundInfoList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据订单号创建退款订单
|
||||
* @param orderNo
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public RefundInfo createRefundByOrderNoForAliPay(String orderNo, String reason) {
|
||||
|
||||
//根据订单号获取订单信息
|
||||
OrderInfo orderInfo = orderInfoService.getOrderByOrderNo(orderNo);
|
||||
|
||||
//根据订单号生成退款订单
|
||||
RefundInfo refundInfo = new RefundInfo();
|
||||
refundInfo.setOrderNo(orderNo);//订单编号
|
||||
refundInfo.setRefundNo(OrderNoUtils.getRefundNo());//退款单编号
|
||||
|
||||
refundInfo.setTotalFee(orderInfo.getTotalFee());//原订单金额(分)
|
||||
refundInfo.setRefund(orderInfo.getTotalFee());//退款金额(分)
|
||||
refundInfo.setReason(reason);//退款原因
|
||||
|
||||
//保存退款订单
|
||||
baseMapper.insert(refundInfo);
|
||||
|
||||
return refundInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新退款记录
|
||||
* @param refundNo
|
||||
* @param content
|
||||
* @param refundStatus
|
||||
*/
|
||||
@Override
|
||||
public void updateRefundForAliPay(String refundNo, String content, String refundStatus) {
|
||||
|
||||
//根据退款单编号修改退款单
|
||||
QueryWrapper<RefundInfo> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("refund_no", refundNo);
|
||||
|
||||
//设置要修改的字段
|
||||
RefundInfo refundInfo = new RefundInfo();
|
||||
refundInfo.setRefundStatus(refundStatus);//退款状态
|
||||
refundInfo.setContentReturn(content);//将全部响应结果存入数据库的content字段
|
||||
|
||||
//更新退款单
|
||||
baseMapper.update(refundInfo, queryWrapper);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user