Merge remote-tracking branch 'origin/dev/dev_xp' into dev/dev
This commit is contained in:
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