Merge remote-tracking branch 'origin/dev/dev' into dev/dev_shb
This commit is contained in:
@@ -128,4 +128,6 @@ public interface AccountService extends IService<Account> {
|
||||
void upgradeNotification();
|
||||
|
||||
void moveLibraryDate();
|
||||
|
||||
void updateCredits(Long accountId, String value);
|
||||
}
|
||||
|
||||
@@ -3,7 +3,9 @@ package com.ai.da.service;
|
||||
import java.util.Map;
|
||||
|
||||
public interface AliPayService {
|
||||
String tradeCreate(Long productId,String returnUrl);
|
||||
String tradeCreate(Integer amount,String returnUrl);
|
||||
|
||||
String tradeNotify(Map<String, String> params);
|
||||
|
||||
void processOrder(Map<String, String> params);
|
||||
|
||||
|
||||
12
src/main/java/com/ai/da/service/CallBackService.java
Normal file
12
src/main/java/com/ai/da/service/CallBackService.java
Normal file
@@ -0,0 +1,12 @@
|
||||
package com.ai.da.service;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
public interface CallBackService {
|
||||
|
||||
Boolean doGet(HttpServletRequest req, HttpServletResponse resp)
|
||||
throws ServletException, IOException;
|
||||
}
|
||||
29
src/main/java/com/ai/da/service/CreditsService.java
Normal file
29
src/main/java/com/ai/da/service/CreditsService.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package com.ai.da.service;
|
||||
|
||||
|
||||
import com.ai.da.common.enums.CreditsEventsEnum;
|
||||
import com.ai.da.common.response.PageBaseResponse;
|
||||
import com.ai.da.mapper.primary.entity.CreditsDetail;
|
||||
import com.ai.da.model.dto.QueryIncomeOrExpenditureDTO;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
public interface CreditsService extends IService<CreditsDetail> {
|
||||
|
||||
void initCredits();
|
||||
|
||||
Boolean buyCredits(Long accountId, Integer quantity);
|
||||
|
||||
void creditsIncrease(Long accountId, String event);
|
||||
|
||||
void creditsDecrease(Long accountId, String event);
|
||||
|
||||
String getCredits(Long accountId);
|
||||
|
||||
void creditsRefund(Long accountId, Integer quantity);
|
||||
|
||||
void insertToCreditsDetail(Long accountId, String changeEvent, String credits, String changeType);
|
||||
|
||||
PageBaseResponse<CreditsDetail> queryCreditsDetailsPage(QueryIncomeOrExpenditureDTO queryPageByTimeDTO);
|
||||
|
||||
Boolean checkCredits(Long accountId, CreditsEventsEnum event, Integer num);
|
||||
}
|
||||
@@ -2,14 +2,16 @@ package com.ai.da.service;
|
||||
|
||||
|
||||
import com.ai.da.common.enums.OrderStatusEnum;
|
||||
import com.ai.da.common.response.PageBaseResponse;
|
||||
import com.ai.da.mapper.primary.entity.OrderInfo;
|
||||
import com.ai.da.model.dto.QueryPageByTimeDTO;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface OrderInfoService extends IService<OrderInfo> {
|
||||
|
||||
OrderInfo createOrderByProductId(Long productId, String paymentType);
|
||||
OrderInfo createOrderByProductId(Integer productId, String paymentType);
|
||||
|
||||
void saveCodeUrl(String orderNo, String codeUrl);
|
||||
|
||||
@@ -22,4 +24,8 @@ public interface OrderInfoService extends IService<OrderInfo> {
|
||||
List<OrderInfo> getNoPayOrderByDuration(int minutes, String paymentType);
|
||||
|
||||
OrderInfo getOrderByOrderNo(String orderNo);
|
||||
|
||||
PageBaseResponse<OrderInfo> getOrderByPage(QueryPageByTimeDTO queryPageByTimeDTO);
|
||||
|
||||
void updateOrderNoById(Long id, String orderNo);
|
||||
}
|
||||
|
||||
32
src/main/java/com/ai/da/service/PayPalCheckoutService.java
Normal file
32
src/main/java/com/ai/da/service/PayPalCheckoutService.java
Normal file
@@ -0,0 +1,32 @@
|
||||
package com.ai.da.service;
|
||||
|
||||
import com.paypal.http.exceptions.SerializeException;
|
||||
import com.paypal.orders.Order;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public interface PayPalCheckoutService {
|
||||
|
||||
HashMap<String, String> createOrder(Integer amount,String returnUrl) throws SerializeException;
|
||||
|
||||
// String callback(@SuppressWarnings("rawtypes") Map map);
|
||||
|
||||
Boolean doPost(HttpServletRequest req, HttpServletResponse resp)
|
||||
throws ServletException, IOException;
|
||||
|
||||
String queryOrder(String orderNo) throws SerializeException;
|
||||
|
||||
Order captureOrder(String orderId) throws IOException;
|
||||
|
||||
Boolean refundOrder(String orderId, String reason) throws IOException;
|
||||
|
||||
String getOAuth();
|
||||
|
||||
void processOrder(String orderId);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.ai.da.service;
|
||||
|
||||
import com.paypal.orders.Order;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public interface PaymentInfoService {
|
||||
@@ -7,4 +9,6 @@ public interface PaymentInfoService {
|
||||
void createPaymentInfo(String plainText);
|
||||
|
||||
void createPaymentInfoForAliPay(Map<String, String> params);
|
||||
|
||||
void createPaymentInfoForPayPal(Order order);
|
||||
}
|
||||
|
||||
@@ -5,7 +5,9 @@ import org.springframework.stereotype.Service;
|
||||
@Service
|
||||
public interface RabbitMQService {
|
||||
|
||||
void publishMessage(String message);
|
||||
void publishMessageToGenerate(String message);
|
||||
|
||||
void publishMessageToSR(String message);
|
||||
|
||||
Integer getMessageCount(String queueUrl);
|
||||
}
|
||||
|
||||
@@ -17,4 +17,6 @@ public interface RefundInfoService extends IService<RefundInfo> {
|
||||
RefundInfo createRefundByOrderNoForAliPay(String orderNo, String reason);
|
||||
|
||||
void updateRefundForAliPay(String refundNo, String content, String refundStatus);
|
||||
|
||||
void updateRefundForPayPal(Long id, String refundId, String content, String refundStatus);
|
||||
}
|
||||
|
||||
22
src/main/java/com/ai/da/service/SuperResolutionService.java
Normal file
22
src/main/java/com/ai/da/service/SuperResolutionService.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package com.ai.da.service;
|
||||
|
||||
import com.ai.da.common.response.PageBaseResponse;
|
||||
import com.ai.da.mapper.primary.entity.TaskList;
|
||||
import com.ai.da.model.dto.QueryTaskHistoryDTO;
|
||||
import com.ai.da.model.dto.SuperResolutionDTO;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface SuperResolutionService extends IService<TaskList> {
|
||||
|
||||
List<String> prepareForSR(List<SuperResolutionDTO> superResolutionDTO);
|
||||
|
||||
void SR(SuperResolutionDTO superResolutionDTO) throws Exception;
|
||||
|
||||
void setSRResult(String taskId, String output, String status);
|
||||
|
||||
void updateSROutput(String taskId, String status, String output);
|
||||
|
||||
PageBaseResponse<TaskList> getTaskHistoryPage(QueryTaskHistoryDTO queryTaskHistoryDTO);
|
||||
}
|
||||
22
src/main/java/com/ai/da/service/TaskListService.java
Normal file
22
src/main/java/com/ai/da/service/TaskListService.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package com.ai.da.service;
|
||||
|
||||
import com.ai.da.common.response.PageBaseResponse;
|
||||
import com.ai.da.mapper.primary.entity.TaskList;
|
||||
import com.ai.da.model.dto.QueryTaskHistoryDTO;
|
||||
import com.ai.da.model.dto.SuperResolutionDTO;
|
||||
import com.ai.da.model.dto.TaskDTO;
|
||||
import com.ai.da.model.vo.TaskVO;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface TaskListService extends IService<TaskList> {
|
||||
|
||||
List<TaskDTO<SuperResolutionDTO>> getExecTask(List<String> taskIdList);
|
||||
|
||||
void addToTaskListRedis(TaskDTO<SuperResolutionDTO> taskDTO);
|
||||
|
||||
void updateTaskStatusOrOutputRedis(String taskId, String status, String output);
|
||||
|
||||
PageBaseResponse<TaskVO> getAllTask(QueryTaskHistoryDTO queryTaskHistoryDTO);
|
||||
}
|
||||
@@ -36,6 +36,7 @@ import org.springframework.util.Assert;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.math.BigDecimal;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
@@ -488,6 +489,8 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
|
||||
trialOrder.setStatus(0);
|
||||
trialOrderMapper.insert(trialOrder);
|
||||
SendEmailUtil.sendCustomEmail("1023316923@qq.com", null, trialOrder,1);
|
||||
SendEmailUtil.sendCustomEmail("calvinwong@aidlab.hk", null, trialOrder,1);
|
||||
SendEmailUtil.sendCustomEmail("kaicpang.pang@connect.polyu.hk", null, trialOrder,1);
|
||||
// 判断当前的试用订单是否自动批准
|
||||
if (AutoApproved.getStatus()) {
|
||||
// 改变试用订单状态,新增试用用户
|
||||
@@ -500,7 +503,7 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
|
||||
account.setIsTrial(1);
|
||||
account.setIsBeginner(1);
|
||||
account.setValidStartTime(System.currentTimeMillis());
|
||||
account.setValidEndTime(Instant.now().plus(3, ChronoUnit.DAYS).toEpochMilli());
|
||||
account.setValidEndTime(Instant.now().plus(5, ChronoUnit.DAYS).toEpochMilli());
|
||||
accountMapper.updateById(account);
|
||||
}else {
|
||||
account.setUserName(trialOrder.getUserName());
|
||||
@@ -508,7 +511,7 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
|
||||
account.setUserEmail(trialOrder.getEmail());
|
||||
account.setLanguage(Language.ENGLISH.name());
|
||||
account.setValidStartTime(System.currentTimeMillis());
|
||||
account.setValidEndTime(Instant.now().plus(3, ChronoUnit.DAYS).toEpochMilli());
|
||||
account.setValidEndTime(Instant.now().plus(5, ChronoUnit.DAYS).toEpochMilli());
|
||||
account.setCreateDate(new Date());
|
||||
account.setIsTrial(1);
|
||||
account.setIsBeginner(1);
|
||||
@@ -516,6 +519,8 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
|
||||
}
|
||||
// 发送邮件提醒用户试用用户已创建
|
||||
SendEmailUtil.sendCustomEmail("1023316923@qq.com", null, trialOrder,2);
|
||||
SendEmailUtil.sendCustomEmail("calvinwong@aidlab.hk", null, trialOrder,2);
|
||||
SendEmailUtil.sendCustomEmail("kaicpang.pang@connect.polyu.hk", null, trialOrder,2);
|
||||
SendEmailUtil.sendCustomEmail(account.getUserEmail(), null, trialOrder, 3);
|
||||
}
|
||||
return Boolean.TRUE;
|
||||
@@ -548,7 +553,7 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
|
||||
account.setIsTrial(1);
|
||||
account.setIsBeginner(1);
|
||||
account.setValidStartTime(System.currentTimeMillis());
|
||||
account.setValidEndTime(Instant.now().plus(3, ChronoUnit.DAYS).toEpochMilli());
|
||||
account.setValidEndTime(Instant.now().plus(5, ChronoUnit.DAYS).toEpochMilli());
|
||||
accountMapper.updateById(account);
|
||||
}else {
|
||||
account.setUserName(trialOrder.getUserName());
|
||||
@@ -556,7 +561,7 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
|
||||
account.setUserEmail(trialOrder.getEmail());
|
||||
account.setLanguage(Language.ENGLISH.name());
|
||||
account.setValidStartTime(System.currentTimeMillis());
|
||||
account.setValidEndTime(Instant.now().plus(3, ChronoUnit.DAYS).toEpochMilli());
|
||||
account.setValidEndTime(Instant.now().plus(5, ChronoUnit.DAYS).toEpochMilli());
|
||||
account.setCreateDate(new Date());
|
||||
account.setIsTrial(1);
|
||||
account.setIsBeginner(1);
|
||||
@@ -564,6 +569,8 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
|
||||
}
|
||||
// 发送邮件提醒用户试用用户已创建
|
||||
SendEmailUtil.sendCustomEmail("1023316923@qq.com", null, trialOrder,2);
|
||||
SendEmailUtil.sendCustomEmail("calvinwong@aidlab.hk", null, trialOrder,2);
|
||||
SendEmailUtil.sendCustomEmail("kaicpang.pang@connect.polyu.hk", null, trialOrder,2);
|
||||
SendEmailUtil.sendCustomEmail(account.getUserEmail(), null, trialOrder, 3);
|
||||
}
|
||||
return Boolean.TRUE;
|
||||
@@ -885,4 +892,11 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
|
||||
|
||||
// 未迁移过的进行迁移,注意模特数据迁移打点信息以及转换模特格式
|
||||
}
|
||||
|
||||
public void updateCredits(Long accountId, String value){
|
||||
Account account = new Account();
|
||||
account.setId(accountId);
|
||||
account.setCredits(new BigDecimal(value));
|
||||
accountMapper.updateById(account);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,18 @@
|
||||
package com.ai.da.service.impl;
|
||||
|
||||
import com.ai.da.common.config.exception.BusinessException;
|
||||
import com.ai.da.common.enums.AliPayTradeStateEnum;
|
||||
import com.ai.da.common.enums.CreditsEventsEnum;
|
||||
import com.ai.da.common.enums.OrderStatusEnum;
|
||||
import com.ai.da.common.enums.PayTypeEnum;
|
||||
import com.ai.da.mapper.primary.entity.OrderInfo;
|
||||
import com.ai.da.mapper.primary.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.ai.da.service.*;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alipay.api.AlipayApiException;
|
||||
import com.alipay.api.AlipayClient;
|
||||
import com.alipay.api.AlipayConstants;
|
||||
import com.alipay.api.internal.util.AlipaySignature;
|
||||
import com.alipay.api.request.*;
|
||||
import com.alipay.api.response.*;
|
||||
import com.google.gson.Gson;
|
||||
@@ -46,16 +47,19 @@ public class AliPayServiceImpl implements AliPayService {
|
||||
@Resource
|
||||
private RefundInfoService refundsInfoService;
|
||||
|
||||
@Resource
|
||||
private CreditsService creditsService;
|
||||
|
||||
private final ReentrantLock lock = new ReentrantLock();
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public String tradeCreate(Long productId, String returnUrl) {
|
||||
public String tradeCreate(Integer amount, String returnUrl) {
|
||||
|
||||
try {
|
||||
//生成订单
|
||||
log.info("生成订单");
|
||||
OrderInfo orderInfo = orderInfoService.createOrderByProductId(productId, PayTypeEnum.ALIPAY.getType());
|
||||
OrderInfo orderInfo = orderInfoService.createOrderByProductId(amount, PayTypeEnum.ALIPAY.getType());
|
||||
|
||||
//调用支付宝接口
|
||||
AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
|
||||
@@ -69,9 +73,10 @@ public class AliPayServiceImpl implements AliPayService {
|
||||
//组装当前业务方法的请求参数
|
||||
JSONObject bizContent = new JSONObject();
|
||||
bizContent.put("out_trade_no", orderInfo.getOrderNo());
|
||||
BigDecimal total = new BigDecimal(orderInfo.getTotalFee().toString()).divide(new BigDecimal("100"));
|
||||
BigDecimal total = new BigDecimal(orderInfo.getTotalFee().toString());
|
||||
bizContent.put("total_amount", total);
|
||||
bizContent.put("subject", orderInfo.getTitle());
|
||||
// bizContent.put("subject", orderInfo.getTitle());
|
||||
bizContent.put("subject", "积分购买");
|
||||
bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");
|
||||
|
||||
request.setBizContent(bizContent.toString());
|
||||
@@ -84,14 +89,93 @@ public class AliPayServiceImpl implements AliPayService {
|
||||
return response.getBody();
|
||||
} else {
|
||||
log.info("调用失败,返回码 ===> " + response.getCode() + ", 返回描述 ===> " + response.getMsg());
|
||||
throw new RuntimeException("创建支付交易失败");
|
||||
throw new BusinessException("Order creation failed");
|
||||
}
|
||||
} catch (AlipayApiException e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException("创建支付交易失败");
|
||||
throw new BusinessException("Order creation failed");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String tradeNotify(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).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;
|
||||
}
|
||||
|
||||
//处理业务 修改订单状态 记录支付日志
|
||||
processOrder(params);
|
||||
|
||||
//校验成功后在response中返回success并继续商户自身业务处理,校验失败返回failure
|
||||
result = "success";
|
||||
} catch (AlipayApiException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 处理订单
|
||||
* @param params
|
||||
@@ -104,6 +188,7 @@ public class AliPayServiceImpl implements AliPayService {
|
||||
|
||||
//获取订单号
|
||||
String orderNo = params.get("out_trade_no");
|
||||
String totalAmount = params.get("total_amount");
|
||||
|
||||
/*在对业务数据进行状态检查和处理之前,
|
||||
要采用数据锁进行并发控制,
|
||||
@@ -112,26 +197,31 @@ public class AliPayServiceImpl implements AliPayService {
|
||||
// 成功获取则立即返回true,获取失败则立即返回false。不必一直等待锁的释放
|
||||
if(lock.tryLock()) {
|
||||
try {
|
||||
|
||||
//处理重复通知
|
||||
//接口调用的幂等性:无论接口被调用多少次,以下业务执行一次
|
||||
String orderStatus = orderInfoService.getOrderStatus(orderNo);
|
||||
if (!OrderStatusEnum.NOT_PAY.getType().equals(orderStatus)) {
|
||||
// String orderStatus = orderInfoService.getOrderStatus(orderNo);
|
||||
OrderInfo orderByOrderNo = orderInfoService.getOrderByOrderNo(orderNo);
|
||||
String orderStatus = orderByOrderNo.getOrderStatus();
|
||||
// 当订单状态处于未支付或超时已关闭时,更新订单状态
|
||||
if (!OrderStatusEnum.NOT_PAY.getType().equals(orderStatus) || !OrderStatusEnum.TIMEOUT_CLOSED.getType().equals(orderStatus)) {
|
||||
return;
|
||||
}
|
||||
|
||||
//更新订单状态
|
||||
orderInfoService.updateStatusByOrderNo(orderNo, OrderStatusEnum.SUCCESS);
|
||||
|
||||
//记录支付日志
|
||||
paymentInfoService.createPaymentInfoForAliPay(params);
|
||||
|
||||
// 添加积分变更记录
|
||||
creditsService.insertToCreditsDetail(orderByOrderNo.getAccountId(),
|
||||
CreditsEventsEnum.BUY_CREDITS.getName() + "--Alipay",
|
||||
CreditsEventsEnum.BUY_CREDITS.getValue(),
|
||||
"positive");
|
||||
// 更新积分
|
||||
creditsService.buyCredits(orderByOrderNo.getAccountId(),Integer.parseInt(totalAmount) / Integer.parseInt(CreditsEventsEnum.PRICE.getValue()));
|
||||
} finally {
|
||||
//要主动释放锁
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -176,7 +266,7 @@ public class AliPayServiceImpl implements AliPayService {
|
||||
|
||||
} catch (AlipayApiException e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException("查单接口的调用失败");
|
||||
throw new BusinessException("查单接口的调用失败");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -198,7 +288,7 @@ public class AliPayServiceImpl implements AliPayService {
|
||||
if(result == null){
|
||||
log.warn("核实订单未创建 ===> {}", orderNo);
|
||||
//更新本地订单状态
|
||||
orderInfoService.updateStatusByOrderNo(orderNo, OrderStatusEnum.CLOSED);
|
||||
orderInfoService.updateStatusByOrderNo(orderNo, OrderStatusEnum.TIMEOUT_CLOSED);
|
||||
}
|
||||
|
||||
//解析查单响应结果
|
||||
@@ -207,24 +297,28 @@ public class AliPayServiceImpl implements AliPayService {
|
||||
LinkedTreeMap alipayTradeQueryResponse = resultMap.get("alipay_trade_query_response");
|
||||
|
||||
String tradeStatus = (String)alipayTradeQueryResponse.get("trade_status");
|
||||
OrderInfo orderByOrderNo = orderInfoService.getOrderByOrderNo(orderNo);
|
||||
if(AliPayTradeStateEnum.NOTPAY.getType().equals(tradeStatus)){
|
||||
log.warn("核实订单未支付 ===> {}", orderNo);
|
||||
|
||||
//如果订单未支付,则调用关单接口关闭订单
|
||||
this.closeOrder(orderNo);
|
||||
|
||||
// 并更新商户端订单状态
|
||||
orderInfoService.updateStatusByOrderNo(orderNo, OrderStatusEnum.CLOSED);
|
||||
orderInfoService.updateStatusByOrderNo(orderNo, OrderStatusEnum.TIMEOUT_CLOSED);
|
||||
}
|
||||
|
||||
if(AliPayTradeStateEnum.SUCCESS.getType().equals(tradeStatus)){
|
||||
log.warn("核实订单已支付 ===> {}", orderNo);
|
||||
|
||||
//如果订单已支付,则更新商户端订单状态
|
||||
orderInfoService.updateStatusByOrderNo(orderNo, OrderStatusEnum.SUCCESS);
|
||||
|
||||
//并记录支付日志
|
||||
paymentInfoService.createPaymentInfoForAliPay(alipayTradeQueryResponse);
|
||||
// 添加积分变更记录
|
||||
creditsService.insertToCreditsDetail(orderByOrderNo.getAccountId(),
|
||||
CreditsEventsEnum.BUY_CREDITS.getName() + "--Alipay",
|
||||
CreditsEventsEnum.BUY_CREDITS.getValue(),
|
||||
"positive");
|
||||
// 更新积分
|
||||
creditsService.buyCredits(orderByOrderNo.getAccountId(),orderByOrderNo.getTotalFee() / Integer.parseInt(CreditsEventsEnum.PRICE.getValue()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -252,8 +346,8 @@ public class AliPayServiceImpl implements AliPayService {
|
||||
}
|
||||
|
||||
} catch (AlipayApiException e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException("关单接口的调用失败");
|
||||
log.error("关单失败,原因 ===> {}",e.getMessage());
|
||||
throw new BusinessException("关单接口的调用失败");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -268,17 +362,14 @@ public class AliPayServiceImpl implements AliPayService {
|
||||
|
||||
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(refundInfo.getRefund().toString());
|
||||
//BigDecimal refund = new BigDecimal("2").divide(new BigDecimal("100"));
|
||||
bizContent.put("refund_amount", refund);//退款金额:不能大于支付金额
|
||||
bizContent.put("refund_reason", reason);//退款原因(可选)
|
||||
@@ -290,33 +381,30 @@ public class AliPayServiceImpl implements AliPayService {
|
||||
|
||||
if(response.isSuccess()){
|
||||
log.info("调用成功,返回结果 ===> " + response.getBody());
|
||||
|
||||
//更新订单状态
|
||||
orderInfoService.updateStatusByOrderNo(orderNo, OrderStatusEnum.REFUND_SUCCESS);
|
||||
|
||||
//更新退款单
|
||||
refundsInfoService.updateRefundForAliPay(
|
||||
refundInfo.getRefundNo(),
|
||||
response.getBody(),
|
||||
AliPayTradeStateEnum.REFUND_SUCCESS.getType()); //退款成功
|
||||
// 更新积分状态
|
||||
OrderInfo orderByOrderNo = orderInfoService.getOrderByOrderNo(orderNo);
|
||||
creditsService.creditsRefund(orderByOrderNo.getAccountId(), orderByOrderNo.getTotalFee() / Integer.parseInt(CreditsEventsEnum.PRICE.getValue()));
|
||||
|
||||
} 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("创建退款申请失败");
|
||||
throw new BusinessException("创建退款申请失败");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -349,7 +437,7 @@ public class AliPayServiceImpl implements AliPayService {
|
||||
|
||||
} catch (AlipayApiException e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException("查单接口的调用失败");
|
||||
throw new BusinessException("查单接口的调用失败");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -363,7 +451,6 @@ public class AliPayServiceImpl implements AliPayService {
|
||||
public String queryBill(String billDate, String type) {
|
||||
|
||||
try {
|
||||
|
||||
AlipayDataDataserviceBillDownloadurlQueryRequest request = new AlipayDataDataserviceBillDownloadurlQueryRequest();
|
||||
JSONObject bizContent = new JSONObject();
|
||||
bizContent.put("bill_type", type);
|
||||
@@ -383,13 +470,11 @@ public class AliPayServiceImpl implements AliPayService {
|
||||
return billDownloadUrl;
|
||||
} else {
|
||||
log.info("调用失败,返回码 ===> " + response.getCode() + ", 返回描述 ===> " + response.getMsg());
|
||||
throw new RuntimeException("申请账单失败");
|
||||
throw new BusinessException("申请账单失败");
|
||||
}
|
||||
|
||||
} catch (AlipayApiException e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException("申请账单失败");
|
||||
log.error("申请账单失败,原因 ===> {}",e.getMessage());
|
||||
throw new BusinessException("申请账单失败");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
159
src/main/java/com/ai/da/service/impl/CallBackServiceImpl.java
Normal file
159
src/main/java/com/ai/da/service/impl/CallBackServiceImpl.java
Normal file
@@ -0,0 +1,159 @@
|
||||
package com.ai.da.service.impl;
|
||||
|
||||
import com.ai.da.common.config.PayPalClient;
|
||||
import com.ai.da.common.constant.PayPalCheckoutConstant;
|
||||
import com.ai.da.common.utils.paypalRequest.WebhookVerifyRequest;
|
||||
import com.ai.da.service.CallBackService;
|
||||
import com.ai.da.service.PayPalCheckoutService;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.paypal.api.payments.Event;
|
||||
import com.paypal.base.Constants;
|
||||
import com.paypal.base.SDKUtil;
|
||||
import com.paypal.base.rest.APIContext;
|
||||
import com.paypal.base.rest.PayPalRESTException;
|
||||
import com.paypal.http.HttpResponse;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.SignatureException;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static com.ai.da.common.constant.PayPalCheckoutConstant.MODE;
|
||||
|
||||
// #Validate Webhook Sample
|
||||
//
|
||||
// This sample code demonstrates how to validate a webhook received on your
|
||||
// web server. This sample assumes that you use the java servlet, which returns
|
||||
// the HttpServletRequest object. However, you can modify this code to
|
||||
// your specific case.
|
||||
//
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class CallBackServiceImpl implements CallBackService {
|
||||
|
||||
@Value("${paypal.client-id}")
|
||||
private String clientId;
|
||||
|
||||
@Value("${paypal.client-secret}")
|
||||
private String clientSecret;
|
||||
|
||||
@Resource
|
||||
private PayPalClient payPalClient;
|
||||
|
||||
@Resource
|
||||
private PayPalCheckoutService payPalCheckoutService;
|
||||
|
||||
@Override
|
||||
public Boolean doGet(HttpServletRequest req, HttpServletResponse resp)
|
||||
throws ServletException, IOException {
|
||||
return doPost(req, resp);
|
||||
}
|
||||
|
||||
// ##Validate Webhook
|
||||
protected Boolean doPost(HttpServletRequest req, HttpServletResponse resp)
|
||||
throws ServletException, IOException {
|
||||
try {
|
||||
String body = getBody(req);
|
||||
Map webhookEvent = new ObjectMapper().readValue(body, Map.class);
|
||||
|
||||
HashMap<String, Object> webhookRequest = new HashMap<>();
|
||||
webhookRequest.put("auth_algo",SDKUtil.validateAndGet(getHeadersInfo(req), "PAYPAL-AUTH-ALGO"));
|
||||
webhookRequest.put("cert_url",SDKUtil.validateAndGet(getHeadersInfo(req), "PAYPAL-CERT-URL"));
|
||||
webhookRequest.put("transmission_id",SDKUtil.validateAndGet(getHeadersInfo(req), "PAYPAL-TRANSMISSION-ID"));
|
||||
webhookRequest.put("transmission_sig",SDKUtil.validateAndGet(getHeadersInfo(req), "PAYPAL-TRANSMISSION-SIG"));
|
||||
webhookRequest.put("transmission_time",SDKUtil.validateAndGet(getHeadersInfo(req), "PAYPAL-TRANSMISSION-TIME"));
|
||||
webhookRequest.put("webhook_id",PayPalCheckoutConstant.WEBHOOK_ID);
|
||||
webhookRequest.put("webhook_event",webhookEvent);
|
||||
|
||||
WebhookVerifyRequest webhookVerifyRequest = new WebhookVerifyRequest();
|
||||
webhookVerifyRequest.authorization(payPalCheckoutService.getOAuth());
|
||||
webhookVerifyRequest.requestBody(webhookRequest);
|
||||
// 验签
|
||||
HttpResponse<HashMap> verified = payPalClient.client(MODE, clientId, clientSecret).execute(webhookVerifyRequest);
|
||||
boolean verifyResult = verified.result().get("verification_status").toString().equals("SUCCESS");
|
||||
if (verifyResult){
|
||||
// ### Api Context
|
||||
APIContext apiContext = new APIContext(clientId, clientSecret, PayPalCheckoutConstant.MODE);
|
||||
|
||||
// Set the webhookId that you received when you created this webhook.
|
||||
apiContext.addConfiguration(Constants.PAYPAL_WEBHOOK_ID, PayPalCheckoutConstant.WEBHOOK_ID);
|
||||
Boolean result = Event.validateReceivedEvent(apiContext, getHeadersInfo(
|
||||
req), body);
|
||||
log.info("Webhook Validated: " + result);
|
||||
|
||||
if (result){
|
||||
// 处理订单数据
|
||||
LinkedHashMap<String,LinkedHashMap<String,String>> webhookEventMap = (LinkedHashMap<String,LinkedHashMap<String,String>>) webhookEvent;
|
||||
String orderId = webhookEventMap.get("resource").get("id");
|
||||
payPalCheckoutService.processOrder(orderId);
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
|
||||
}
|
||||
} catch (PayPalRESTException | InvalidKeyException | NoSuchAlgorithmException | SignatureException e) {
|
||||
log.error(e.getMessage());
|
||||
}
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
|
||||
// Simple helper method to help you extract the headers from HttpServletRequest object.
|
||||
private static Map<String, String> getHeadersInfo(HttpServletRequest request) {
|
||||
Map<String, String> map = new HashMap<String, String>();
|
||||
@SuppressWarnings("rawtypes")
|
||||
Enumeration headerNames = request.getHeaderNames();
|
||||
while (headerNames.hasMoreElements()) {
|
||||
String key = (String) headerNames.nextElement();
|
||||
String value = request.getHeader(key);
|
||||
map.put(key, value);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
// Simple helper method to fetch request data as a string from HttpServletRequest object.
|
||||
private static String getBody(HttpServletRequest request) throws IOException {
|
||||
String body;
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
BufferedReader bufferedReader = null;
|
||||
try {
|
||||
InputStream inputStream = request.getInputStream();
|
||||
if (inputStream != null) {
|
||||
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
|
||||
char[] charBuffer = new char[128];
|
||||
int bytesRead = -1;
|
||||
while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
|
||||
stringBuilder.append(charBuffer, 0, bytesRead);
|
||||
}
|
||||
} else {
|
||||
stringBuilder.append("");
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
throw ex;
|
||||
} finally {
|
||||
if (bufferedReader != null) {
|
||||
try {
|
||||
bufferedReader.close();
|
||||
} catch (IOException ex) {
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
}
|
||||
body = stringBuilder.toString();
|
||||
log.info("回调参数 ===> {}", body);
|
||||
return body;
|
||||
}
|
||||
}
|
||||
182
src/main/java/com/ai/da/service/impl/CreditsServiceImpl.java
Normal file
182
src/main/java/com/ai/da/service/impl/CreditsServiceImpl.java
Normal file
@@ -0,0 +1,182 @@
|
||||
package com.ai.da.service.impl;
|
||||
|
||||
import com.ai.da.common.config.exception.BusinessException;
|
||||
import com.ai.da.common.context.UserContext;
|
||||
import com.ai.da.common.enums.CreditsEventsEnum;
|
||||
import com.ai.da.common.response.PageBaseResponse;
|
||||
import com.ai.da.mapper.primary.AccountMapper;
|
||||
import com.ai.da.mapper.primary.CreditsDetailMapper;
|
||||
import com.ai.da.mapper.primary.entity.Account;
|
||||
import com.ai.da.mapper.primary.entity.CreditsDetail;
|
||||
import com.ai.da.model.dto.QueryIncomeOrExpenditureDTO;
|
||||
import com.ai.da.service.AccountService;
|
||||
import com.ai.da.service.CreditsService;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import io.netty.util.internal.StringUtil;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Objects;
|
||||
|
||||
@Service
|
||||
public class CreditsServiceImpl extends ServiceImpl<CreditsDetailMapper, CreditsDetail> implements CreditsService {
|
||||
|
||||
@Resource
|
||||
private AccountService accountService;
|
||||
|
||||
@Resource
|
||||
private AccountMapper accountMapper;
|
||||
|
||||
@Override
|
||||
public void initCredits() {
|
||||
accountService.updateCredits(UserContext.getUserHolder().getId(), CreditsEventsEnum.INIT.getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean buyCredits(Long accountId, Integer quantity) {
|
||||
BigDecimal existingCredits = accountMapper.selectById(accountId).getCredits();
|
||||
BigDecimal newCredits = new BigDecimal(CreditsEventsEnum.BUY_CREDITS.getValue()).multiply(new BigDecimal(quantity));
|
||||
BigDecimal added = existingCredits.add(newCredits);
|
||||
accountService.updateCredits(accountId, added.toString());
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void creditsIncrease(Long accountId, String creditsEvent) {
|
||||
CreditsEventsEnum event = null;
|
||||
|
||||
switch (creditsEvent) {
|
||||
case "Daily Check-In":
|
||||
event = CreditsEventsEnum.DAILY_CHECKIN;
|
||||
break;
|
||||
case "Social Media Sharing":
|
||||
event = CreditsEventsEnum.SOCIAL_MEDIA_SHARING;
|
||||
break;
|
||||
case "Other":
|
||||
event = CreditsEventsEnum.OTHER;
|
||||
break;
|
||||
case "Super Resolution":
|
||||
event = CreditsEventsEnum.SUPER_RESOLUTION;
|
||||
break;
|
||||
default:
|
||||
throw new BusinessException("UNKNOWN TYPE");
|
||||
}
|
||||
BigDecimal existingCredits = accountMapper.selectById(accountId).getCredits();
|
||||
BigDecimal add = new BigDecimal(event.getValue()).add(existingCredits);
|
||||
accountService.updateCredits(accountId, add.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void creditsDecrease(Long accountId, String creditsEvent) {
|
||||
CreditsEventsEnum event;
|
||||
|
||||
switch (creditsEvent) {
|
||||
case "Super Resolution":
|
||||
event = CreditsEventsEnum.SUPER_RESOLUTION;
|
||||
break;
|
||||
case "Other":
|
||||
event = CreditsEventsEnum.OTHER;
|
||||
break;
|
||||
default:
|
||||
log.error("UNKNOWN TYPE");
|
||||
throw new BusinessException("UNKNOWN TYPE");
|
||||
}
|
||||
BigDecimal existingCredits = accountMapper.selectById(accountId).getCredits();
|
||||
BigDecimal subtract = existingCredits.subtract(new BigDecimal(event.getValue()));
|
||||
accountService.updateCredits(accountId, subtract.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCredits(Long accountId) {
|
||||
Account account = accountMapper.selectById(accountId);
|
||||
return account.getCredits().toString();
|
||||
}
|
||||
|
||||
public void creditsRefund(Long accountId, Integer quantity) {
|
||||
BigDecimal existingCredits = accountMapper.selectById(accountId).getCredits();
|
||||
BigDecimal newCredits = new BigDecimal(CreditsEventsEnum.BUY_CREDITS.getValue()).multiply(new BigDecimal(quantity));
|
||||
BigDecimal subtracted = existingCredits.subtract(newCredits);
|
||||
accountService.updateCredits(accountId, subtracted.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* 向积分变更详细表添加记录
|
||||
*
|
||||
* @param changeEvent 导致积分变更的事件
|
||||
* @param credits 变更的积分
|
||||
* @param changeType 变更类型 : positive->增 negative->减
|
||||
*/
|
||||
@Override
|
||||
public void insertToCreditsDetail(Long accountId, String changeEvent, String credits, String changeType) {
|
||||
CreditsDetail creditsDetail = new CreditsDetail();
|
||||
Account account = accountMapper.selectById(accountId);
|
||||
// BigDecimal finalCredits;
|
||||
String changeCredits;
|
||||
if ("positive".equals(changeType)) {
|
||||
// finalCredits = account.getCredits().add(new BigDecimal(credits));
|
||||
changeCredits = "+" + credits;
|
||||
} else {
|
||||
// finalCredits = account.getCredits().subtract(new BigDecimal(credits));
|
||||
changeCredits = "-" + credits;
|
||||
}
|
||||
creditsDetail.setAccountId(accountId);
|
||||
creditsDetail.setChangeEvent(changeEvent);
|
||||
creditsDetail.setChangedCredits(changeCredits);
|
||||
// creditsDetail.setCredits(finalCredits);
|
||||
creditsDetail.setCredits(account.getCredits());
|
||||
creditsDetail.setCreateTime(LocalDateTime.now());
|
||||
|
||||
baseMapper.insert(creditsDetail);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageBaseResponse<CreditsDetail> queryCreditsDetailsPage(QueryIncomeOrExpenditureDTO queryPageByTimeDTO) {
|
||||
QueryWrapper<CreditsDetail> qw = new QueryWrapper<>();
|
||||
qw.eq("account_id", UserContext.getUserHolder().getId());
|
||||
|
||||
String startTime = queryPageByTimeDTO.getStartTime();
|
||||
String endTime = queryPageByTimeDTO.getEndTime();
|
||||
if (StringUtil.isNullOrEmpty(startTime)) {
|
||||
startTime = "2024-03-01 00:00:00";
|
||||
}
|
||||
if (StringUtil.isNullOrEmpty(endTime)) {
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||
endTime = now.format(dateTimeFormatter);
|
||||
}
|
||||
if (!Objects.isNull(queryPageByTimeDTO.getIsIncome())) {
|
||||
if (queryPageByTimeDTO.getIsIncome()) {
|
||||
qw.likeRight("changed_credits", "+");
|
||||
} else {
|
||||
qw.likeRight("changed_credits", "-");
|
||||
}
|
||||
}
|
||||
|
||||
qw.between("create_time", startTime, endTime);
|
||||
qw.orderByDesc("create_time");
|
||||
Page<CreditsDetail> pageInfo = new Page<>(queryPageByTimeDTO.getPage(), queryPageByTimeDTO.getSize());
|
||||
Page<CreditsDetail> orderInfo = baseMapper.selectPage(pageInfo, qw);
|
||||
if (CollectionUtils.isEmpty(orderInfo.getRecords())) {
|
||||
return PageBaseResponse.success(new Page<>());
|
||||
}
|
||||
|
||||
return PageBaseResponse.success(orderInfo);
|
||||
}
|
||||
|
||||
public Boolean checkCredits(Long accountId, CreditsEventsEnum event, Integer num) {
|
||||
String credits = getCredits(accountId);
|
||||
if (new BigDecimal(credits)
|
||||
.subtract(new BigDecimal(event.getValue())
|
||||
.multiply(new BigDecimal(num)))
|
||||
.compareTo(BigDecimal.ZERO) < 0) {
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
}
|
||||
@@ -68,13 +68,13 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
|
||||
@Resource
|
||||
private GenerateCancelMapper generateCancelMapper;
|
||||
|
||||
@Value("${redis.key.consumptionOrder}")
|
||||
@Value("${redis.key.orderForGenerate}")
|
||||
private String consumptionOrderKey;
|
||||
|
||||
@Value("${redis.key.cancelSet}")
|
||||
@Value("${redis.key.generateCancelSet}")
|
||||
private String cancelSetKey;
|
||||
|
||||
@Value("${redis.key.exceptionMap}")
|
||||
@Value("${redis.key.generateExceptionMap}")
|
||||
private String exceptionMapKey;
|
||||
|
||||
@Value("${redis.key.resultMap}")
|
||||
@@ -368,7 +368,7 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
|
||||
redisUtil.addToZSet(consumptionOrderKey, uuid, maxScore);
|
||||
|
||||
// 4、将消息发布到MQ消息队列
|
||||
rabbitMQService.publishMessage(jsonString);
|
||||
rabbitMQService.publishMessageToGenerate(jsonString);
|
||||
|
||||
// 5、返回唯一id
|
||||
return new PrepareForGenerateVO(uuid, 2 - trialsCount);
|
||||
|
||||
@@ -1,21 +1,30 @@
|
||||
package com.ai.da.service.impl;
|
||||
|
||||
|
||||
import com.ai.da.common.context.UserContext;
|
||||
import com.ai.da.common.enums.CreditsEventsEnum;
|
||||
import com.ai.da.common.enums.OrderStatusEnum;
|
||||
import com.ai.da.common.response.PageBaseResponse;
|
||||
import com.ai.da.common.utils.OrderNoUtils;
|
||||
import com.ai.da.mapper.primary.OrderInfoMapper;
|
||||
import com.ai.da.mapper.primary.ProductMapper;
|
||||
import com.ai.da.mapper.primary.entity.OrderInfo;
|
||||
import com.ai.da.mapper.primary.entity.Product;
|
||||
import com.ai.da.model.dto.QueryPageByTimeDTO;
|
||||
import com.ai.da.model.vo.AuthPrincipalVo;
|
||||
import com.ai.da.service.OrderInfoService;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import io.netty.util.internal.StringUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@@ -25,27 +34,27 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
|
||||
@Resource
|
||||
private ProductMapper productMapper;
|
||||
|
||||
/*@Resource
|
||||
private OrderInfoMapper orderInfoMapper;*/
|
||||
|
||||
@Override
|
||||
public OrderInfo createOrderByProductId(Long productId, String paymentType) {
|
||||
public OrderInfo createOrderByProductId(Integer amount, String paymentType) {
|
||||
|
||||
//查找已存在但未支付的订单
|
||||
OrderInfo orderInfo = this.getNoPayOrderByProductId(productId, paymentType);
|
||||
/*OrderInfo orderInfo = this.getNoPayOrderByProductId(amount, paymentType);
|
||||
if( orderInfo != null){
|
||||
return orderInfo;
|
||||
}
|
||||
}*/
|
||||
|
||||
//获取商品信息
|
||||
Product product = productMapper.selectById(productId);
|
||||
// Product product = productMapper.selectById(amount);
|
||||
AuthPrincipalVo userHolder = UserContext.getUserHolder();
|
||||
Long accountId = userHolder.getId();
|
||||
|
||||
//生成订单
|
||||
orderInfo = new OrderInfo();
|
||||
orderInfo.setTitle(product.getTitle());
|
||||
orderInfo.setOrderNo(OrderNoUtils.getOrderNo()); //订单号
|
||||
orderInfo.setProductId(productId);
|
||||
orderInfo.setTotalFee(product.getPrice()); //分
|
||||
OrderInfo orderInfo = new OrderInfo();
|
||||
orderInfo.setAccountId(accountId);
|
||||
orderInfo.setTitle("积分购买 X" + amount );
|
||||
orderInfo.setOrderNo(OrderNoUtils.getOrderNo()); //订单号 ??
|
||||
// orderInfo.setProductId(amount);
|
||||
orderInfo.setTotalFee(Integer.parseInt(CreditsEventsEnum.PRICE.getValue()) * amount); // 元 HKD
|
||||
orderInfo.setOrderStatus(OrderStatusEnum.NOT_PAY.getType()); //未支付
|
||||
orderInfo.setPaymentType(paymentType);
|
||||
baseMapper.insert(orderInfo);
|
||||
@@ -169,4 +178,38 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
|
||||
OrderInfo orderInfo = baseMapper.selectOne(queryWrapper);
|
||||
return orderInfo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageBaseResponse<OrderInfo> getOrderByPage(QueryPageByTimeDTO queryPageByTimeDTO){
|
||||
QueryWrapper<OrderInfo> qw = new QueryWrapper<>();
|
||||
qw.eq("account_id",UserContext.getUserHolder().getId());
|
||||
|
||||
String startTime = queryPageByTimeDTO.getStartTime();
|
||||
String endTime = queryPageByTimeDTO.getEndTime();
|
||||
if (StringUtil.isNullOrEmpty(startTime)){
|
||||
startTime = "2024-02-01 00:00:00";
|
||||
}
|
||||
if (StringUtil.isNullOrEmpty(endTime)){
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||
endTime = now.format(dateTimeFormatter);
|
||||
}
|
||||
qw.between("create_time", startTime, endTime);
|
||||
qw.orderByDesc("create_time");
|
||||
Page<OrderInfo> pageInfo = new Page<>(queryPageByTimeDTO.getPage(), queryPageByTimeDTO.getSize());
|
||||
Page<OrderInfo> orderInfo = baseMapper.selectPage(pageInfo, qw);
|
||||
if (CollectionUtils.isEmpty(orderInfo.getRecords())) {
|
||||
return PageBaseResponse.success(new Page<>());
|
||||
}
|
||||
|
||||
return PageBaseResponse.success(orderInfo);
|
||||
}
|
||||
|
||||
public void updateOrderNoById(Long id, String orderNo){
|
||||
OrderInfo orderInfo = new OrderInfo();
|
||||
orderInfo.setId(id);
|
||||
orderInfo.setOrderNo(orderNo);
|
||||
|
||||
baseMapper.updateById(orderInfo);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,597 @@
|
||||
package com.ai.da.service.impl;
|
||||
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import com.ai.da.common.config.PayPalClient;
|
||||
import com.ai.da.common.config.exception.BusinessException;
|
||||
import com.ai.da.common.constant.PayPalCheckoutConstant;
|
||||
import com.ai.da.common.enums.*;
|
||||
import com.ai.da.common.utils.RedisUtil;
|
||||
import com.ai.da.common.utils.paypalRequest.AuthenticationRequest;
|
||||
import com.ai.da.common.utils.paypalRequest.WebhookVerifyRequest;
|
||||
import com.ai.da.mapper.primary.entity.OrderInfo;
|
||||
import com.ai.da.mapper.primary.entity.RefundInfo;
|
||||
import com.ai.da.service.*;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.google.gson.Gson;
|
||||
import com.paypal.api.payments.Event;
|
||||
import com.paypal.base.Constants;
|
||||
import com.paypal.base.SDKUtil;
|
||||
import com.paypal.base.rest.APIContext;
|
||||
import com.paypal.base.rest.PayPalRESTException;
|
||||
import com.paypal.http.HttpResponse;
|
||||
import com.paypal.http.exceptions.SerializeException;
|
||||
import com.paypal.http.serializer.Json;
|
||||
import com.paypal.orders.*;
|
||||
import com.paypal.payments.CapturesGetRequest;
|
||||
import com.paypal.payments.CapturesRefundRequest;
|
||||
import com.paypal.payments.RefundRequest;
|
||||
import com.paypal.payments.RefundsGetRequest;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.json.JSONObject;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.SignatureException;
|
||||
import java.util.*;
|
||||
|
||||
import static com.ai.da.common.constant.PayPalCheckoutConstant.*;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
//@RefreshScope
|
||||
public class PayPalCheckoutServiceImpl implements PayPalCheckoutService {
|
||||
|
||||
@Value("${paypal.receiver.email}")
|
||||
private String receiverEmail;
|
||||
|
||||
@Value("${paypal.client-id}")
|
||||
private String clientId;
|
||||
|
||||
@Value("${paypal.client-secret}")
|
||||
private String clientSecret;
|
||||
|
||||
@Resource
|
||||
private PayPalClient payPalClient;
|
||||
@Resource
|
||||
private OrderInfoService orderInfoService;
|
||||
@Resource
|
||||
private PaymentInfoService paymentInfoService;
|
||||
@Resource
|
||||
private RefundInfoService refundsInfoService;
|
||||
@Resource
|
||||
private CreditsService creditsService;
|
||||
@Resource
|
||||
private RedisUtil redisUtil;
|
||||
|
||||
/**
|
||||
* 创建订单的方法
|
||||
*/
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public HashMap<String, String> createOrder(Integer amount, String returnUrl) throws SerializeException {
|
||||
// 生成订单
|
||||
log.info("生成订单");
|
||||
OrderInfo orderInfo = orderInfoService.createOrderByProductId(amount, PayTypeEnum.PAYPAL.getType());
|
||||
|
||||
OrdersCreateRequest request = new OrdersCreateRequest();
|
||||
request.header("prefer", "return=representation");
|
||||
request.requestBody(buildRequestBody(String.valueOf(orderInfo.getTotalFee()), returnUrl));
|
||||
HttpResponse<Order> response = null;
|
||||
try {
|
||||
response = payPalClient.client(MODE, clientId, clientSecret).execute(request);
|
||||
} catch (Exception e) {
|
||||
log.error("调用paypal订单创建失败,失败原因 ===> {}", e.getMessage());
|
||||
throw new BusinessException("Order creation failed");
|
||||
}
|
||||
|
||||
String approve = "";
|
||||
assert response != null;
|
||||
if (response.statusCode() == 201) {
|
||||
log.info("Status Code = {}, Status = {}, OrderID = {}, Intent = {}", response.statusCode(), response.result().status(), response.result().id(), response.result().checkoutPaymentIntent());
|
||||
for (LinkDescription link : response.result().links()) {
|
||||
log.info("Links-{}: {} \tCall Type: {}", link.rel(), link.href(), link.method());
|
||||
if (link.rel().equals("approve")) {
|
||||
approve = link.href();
|
||||
}
|
||||
}
|
||||
String totalAmount = response.result().purchaseUnits().get(0).amountWithBreakdown().currencyCode() + ":" + response.result().purchaseUnits().get(0).amountWithBreakdown().value();
|
||||
log.info("Total Amount: {}", totalAmount);
|
||||
String json = new JSONObject(new Json().serialize(response.result())).toString(4);
|
||||
log.info("createOrder response body: {}", json);
|
||||
}
|
||||
|
||||
String orderId = response.result().id();
|
||||
orderInfoService.updateOrderNoById(orderInfo.getId(), orderId);
|
||||
|
||||
HashMap<String, String> returnData = new HashMap<>();
|
||||
returnData.put("approve", approve);
|
||||
returnData.put("orderNo", orderId);
|
||||
|
||||
// 需要返回地址和订单id
|
||||
return returnData;
|
||||
}
|
||||
|
||||
// ##Validate Webhook
|
||||
public Boolean doPost(HttpServletRequest req, HttpServletResponse resp)
|
||||
throws ServletException, IOException {
|
||||
try {
|
||||
String body = getBody(req);
|
||||
Map webhookEvent = new ObjectMapper().readValue(body, Map.class);
|
||||
|
||||
HashMap<String, Object> webhookRequest = new HashMap<>();
|
||||
webhookRequest.put("auth_algo", SDKUtil.validateAndGet(getHeadersInfo(req), "PAYPAL-AUTH-ALGO"));
|
||||
webhookRequest.put("cert_url", SDKUtil.validateAndGet(getHeadersInfo(req), "PAYPAL-CERT-URL"));
|
||||
webhookRequest.put("transmission_id", SDKUtil.validateAndGet(getHeadersInfo(req), "PAYPAL-TRANSMISSION-ID"));
|
||||
webhookRequest.put("transmission_sig", SDKUtil.validateAndGet(getHeadersInfo(req), "PAYPAL-TRANSMISSION-SIG"));
|
||||
webhookRequest.put("transmission_time", SDKUtil.validateAndGet(getHeadersInfo(req), "PAYPAL-TRANSMISSION-TIME"));
|
||||
webhookRequest.put("webhook_id", PayPalCheckoutConstant.WEBHOOK_ID);
|
||||
webhookRequest.put("webhook_event", webhookEvent);
|
||||
|
||||
WebhookVerifyRequest webhookVerifyRequest = new WebhookVerifyRequest();
|
||||
webhookVerifyRequest.authorization(getOAuth());
|
||||
webhookVerifyRequest.requestBody(webhookRequest);
|
||||
// 验签
|
||||
HttpResponse<HashMap> verified = payPalClient.client(MODE, clientId, clientSecret).execute(webhookVerifyRequest);
|
||||
boolean verifyResult = verified.result().get("verification_status").toString().equals("SUCCESS");
|
||||
if (verifyResult) {
|
||||
// ### Api Context
|
||||
APIContext apiContext = new APIContext(clientId, clientSecret, PayPalCheckoutConstant.MODE);
|
||||
|
||||
// Set the webhookId that you received when you created this webhook.
|
||||
apiContext.addConfiguration(Constants.PAYPAL_WEBHOOK_ID, PayPalCheckoutConstant.WEBHOOK_ID);
|
||||
Boolean result = Event.validateReceivedEvent(apiContext, getHeadersInfo(
|
||||
req), body);
|
||||
log.info("Webhook Validated: " + result);
|
||||
|
||||
if (result) {
|
||||
// 处理订单数据
|
||||
LinkedHashMap<String, LinkedHashMap<String, String>> webhookEventMap = (LinkedHashMap<String, LinkedHashMap<String, String>>) webhookEvent;
|
||||
String orderId = webhookEventMap.get("resource").get("id");
|
||||
processOrder(orderId);
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
} else {
|
||||
log.error("Paypal 验签失败");
|
||||
}
|
||||
} catch (PayPalRESTException | InvalidKeyException | NoSuchAlgorithmException | SignatureException e) {
|
||||
log.error(e.getMessage());
|
||||
}
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
|
||||
// Simple helper method to help you extract the headers from HttpServletRequest object.
|
||||
private static Map<String, String> getHeadersInfo(HttpServletRequest request) {
|
||||
Map<String, String> map = new HashMap<String, String>();
|
||||
@SuppressWarnings("rawtypes")
|
||||
Enumeration headerNames = request.getHeaderNames();
|
||||
while (headerNames.hasMoreElements()) {
|
||||
String key = (String) headerNames.nextElement();
|
||||
String value = request.getHeader(key);
|
||||
map.put(key, value);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
// Simple helper method to fetch request data as a string from HttpServletRequest object.
|
||||
private static String getBody(HttpServletRequest request) throws IOException {
|
||||
String body;
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
BufferedReader bufferedReader = null;
|
||||
try {
|
||||
InputStream inputStream = request.getInputStream();
|
||||
if (inputStream != null) {
|
||||
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
|
||||
char[] charBuffer = new char[128];
|
||||
int bytesRead = -1;
|
||||
while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
|
||||
stringBuilder.append(charBuffer, 0, bytesRead);
|
||||
}
|
||||
} else {
|
||||
stringBuilder.append("");
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
throw ex;
|
||||
} finally {
|
||||
if (bufferedReader != null) {
|
||||
try {
|
||||
bufferedReader.close();
|
||||
} catch (IOException ex) {
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
}
|
||||
body = stringBuilder.toString();
|
||||
log.info("回调参数 ===> {}", body);
|
||||
return body;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成订单主体信息
|
||||
*/
|
||||
private OrderRequest buildRequestBody(String price, String returnUrl) {
|
||||
OrderRequest orderRequest = new OrderRequest();
|
||||
orderRequest.checkoutPaymentIntent(CAPTURE);
|
||||
|
||||
ApplicationContext applicationContext = new ApplicationContext()
|
||||
.brandName(BRANDNAME)
|
||||
.landingPage(LANDINGPAGE)
|
||||
.cancelUrl(returnUrl).returnUrl(returnUrl)
|
||||
.userAction(USERACTION)
|
||||
.shippingPreference(SHIPPINGPREFERENCE);
|
||||
orderRequest.applicationContext(applicationContext);
|
||||
|
||||
List<PurchaseUnitRequest> purchaseUnitRequests = new ArrayList<PurchaseUnitRequest>();
|
||||
|
||||
PurchaseUnitRequest purchaseUnitRequest = new PurchaseUnitRequest()
|
||||
.amountWithBreakdown(new AmountWithBreakdown()
|
||||
.amountBreakdown(new AmountBreakdown())
|
||||
.currencyCode(CurrencyCodesEnum.HONG_KONG_DOLLAR.getCode())
|
||||
.value(price));
|
||||
purchaseUnitRequests.add(purchaseUnitRequest);
|
||||
orderRequest.purchaseUnits(purchaseUnitRequests);
|
||||
return orderRequest;
|
||||
}
|
||||
|
||||
/*@Override
|
||||
public String callback(@SuppressWarnings("rawtypes") Map map) {
|
||||
|
||||
log.info("paypal支付通知正在执行");
|
||||
log.info("通知参数 ===> {}", map);
|
||||
|
||||
// log.info(map.toString());
|
||||
String outTradeNo = (String) map.get("invoice");
|
||||
String paymentStatus = (String) map.get("payment_status");
|
||||
String amount = (String) map.get("mc_gross");
|
||||
String currency = (String) map.get("mc_currency");
|
||||
String paymentId = (String) map.get("txn_id");
|
||||
String parentPaymentId = (String) map.get("parent_txn_id");
|
||||
log.info("商家订单号 = {}", outTradeNo);
|
||||
log.info("订单状态 = {}", paymentStatus);
|
||||
log.info("金额 = {}", amount);
|
||||
log.info("币种 = {}", currency);
|
||||
log.info("流水号 = {}", paymentId);
|
||||
log.info("父流水号 = {}", parentPaymentId);
|
||||
|
||||
if (!receiverEmail.equals(map.get("receiver_email"))) {
|
||||
log.info("FAIL = 商户id错误, outTradeNo = {}", outTradeNo);
|
||||
return "failure";
|
||||
}
|
||||
if ("Completed".equals(paymentStatus)) {
|
||||
//进行数据库操作
|
||||
//
|
||||
//
|
||||
log.info("支付成功,状态为=COMPLETED");
|
||||
return "success";
|
||||
}
|
||||
if ("Refunded".equals(paymentStatus)) {
|
||||
//进行数据库操作
|
||||
//
|
||||
//
|
||||
log.info("退款成功");
|
||||
return "success";
|
||||
}
|
||||
if ("Pending".equals(paymentStatus) && StringUtils.isEmpty(parentPaymentId)) {
|
||||
String pendingReason = String.valueOf(map.get("pending_reason"));
|
||||
//进行数据库操作
|
||||
//
|
||||
//
|
||||
log.info("订单支付成功,状态为=PENDING,产生此状态的原因是 {}", pendingReason);
|
||||
return "success";
|
||||
}
|
||||
if (StringUtils.isEmpty(parentPaymentId)) {
|
||||
if (PayPalCheckoutConstant.PAYMENT_STATUS_REVERSED.equals(paymentStatus)
|
||||
|| PayPalCheckoutConstant.PAYMENT_STATUS_CANCELED_REVERSAL.equals(paymentStatus)
|
||||
|| PayPalCheckoutConstant.PAYMENT_STATUS_DENIED.equals(paymentStatus)) {
|
||||
String reasonCode = String.valueOf(map.get("reason_code"));
|
||||
//进行数据库操作(状态修改)
|
||||
//
|
||||
//
|
||||
log.info("订单异常,请尽快查看处理,状态为={},产生此状态的原因是 {} ", paymentStatus, reasonCode);
|
||||
return PayPalCheckoutConstant.SUCCESS;
|
||||
}
|
||||
if (PayPalCheckoutConstant.PAYMENT_STATUS_EXPIRED.equals(paymentStatus)
|
||||
|| PayPalCheckoutConstant.PAYMENT_STATUS_CREATED.equals(paymentStatus)
|
||||
|| PayPalCheckoutConstant.PAYMENT_STATUS_FAILED.equals(paymentStatus)
|
||||
|| PayPalCheckoutConstant.PAYMENT_STATUS_PROCESSED.equals(paymentStatus)
|
||||
|| PayPalCheckoutConstant.PAYMENT_STATUS_VOIDED.equals(paymentStatus)) {
|
||||
//进行数据库操作(状态修改)
|
||||
//
|
||||
//
|
||||
log.info("其他订单状态,订单异常,请尽快查看处理, 状态={}", paymentStatus);
|
||||
return PayPalCheckoutConstant.SUCCESS;
|
||||
}
|
||||
}
|
||||
return "failure";
|
||||
}*/
|
||||
|
||||
/**
|
||||
* 查询订单信息
|
||||
*
|
||||
* @param orderNo
|
||||
* @return
|
||||
* @throws SerializeException
|
||||
*/
|
||||
public String queryOrder(String orderNo) throws SerializeException {
|
||||
OrdersGetRequest request = new OrdersGetRequest(orderNo);
|
||||
|
||||
HttpResponse<Order> response = null;
|
||||
try {
|
||||
response = payPalClient.client(MODE, clientId, clientSecret).execute(request);
|
||||
} catch (Exception e) {
|
||||
log.error("paypal订单查询失败,失败原因 ===> {}", e.getMessage());
|
||||
}
|
||||
System.out.println("Status Code: " + response.statusCode());
|
||||
System.out.println("Status: " + response.result().status());
|
||||
System.out.println("Order id: " + response.result().id());
|
||||
if (response.result().purchaseUnits().get(0).payments() != null) {
|
||||
List<Capture> captures = response.result().purchaseUnits().get(0).payments().captures();
|
||||
if (captures != null) {
|
||||
for (Capture capture : captures) {
|
||||
System.out.println("\t订单编号= " + capture.invoiceId() + "\tCapture Id= " + capture.id() + "\tCapture status= " + capture.status() + "\tCapture amount= " + capture.amount().currencyCode() + ":" + capture.amount().value());
|
||||
}
|
||||
}
|
||||
List<Refund> refunds = response.result().purchaseUnits().get(0).payments().refunds();
|
||||
if (refunds != null) {
|
||||
for (Refund refund : refunds) {
|
||||
System.out.println("\t售后编号= " + refund.invoiceId() + "\tRefund Id= " + refund.id() + "\tRefund status= " + refund.status() + "\tRefund amount= " + refund.amount().currencyCode() + ":" + refund.amount().value());
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.println("Links: ");
|
||||
for (com.paypal.orders.LinkDescription link : response.result().links()) {
|
||||
System.out.println("\t" + link.rel() + ": " + link.href() + "\tCall Type: " + link.method());
|
||||
}
|
||||
|
||||
System.out.println("Full response body:");
|
||||
String json = new JSONObject(new Json().serialize(response.result())).toString(4);
|
||||
System.out.println(json);
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户授权支付成功,进行扣款操作
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Order captureOrder(String orderId) {
|
||||
OrdersCaptureRequest request = new OrdersCaptureRequest(orderId);
|
||||
request.requestBody(new OrderRequest());
|
||||
PayPalClient payPalClient = new PayPalClient();
|
||||
HttpResponse<Order> response;
|
||||
|
||||
try {
|
||||
response = payPalClient.client(MODE, clientId, clientSecret).execute(request);
|
||||
} catch (Exception e) {
|
||||
log.error("调用paypal扣款失败,失败原因 ===> {}", e.getMessage());
|
||||
throw new BusinessException("Order deduction failed.");
|
||||
}
|
||||
log.info("Status Code = {}, Status = {}, OrderID = {}", response.statusCode(), response.result().status(), response.result().id());
|
||||
for (LinkDescription link : response.result().links()) {
|
||||
log.info("Links-{}: {} \tCall Type: {}", link.rel(), link.href(), link.method());
|
||||
}
|
||||
for (PurchaseUnit purchaseUnit : response.result().purchaseUnits()) {
|
||||
for (Capture capture : purchaseUnit.payments().captures()) {
|
||||
log.info("Capture id: {}", capture.id());
|
||||
log.info("status: {}", capture.status());
|
||||
log.info("invoice_id: {}", capture.invoiceId());
|
||||
if ("COMPLETED".equals(capture.status())) {
|
||||
//进行数据库操作,修改订单状态为已支付成功,尽快发货(配合回调和CapturesGet查询确定成功)
|
||||
log.info("支付成功,状态为=COMPLETED");
|
||||
}
|
||||
if ("PENDING".equals(capture.status())) {
|
||||
log.info("status_details: {}", capture.captureStatusDetails().reason());
|
||||
String reason = "PENDING";
|
||||
if (capture.captureStatusDetails() != null && capture.captureStatusDetails().reason() != null) {
|
||||
reason = capture.captureStatusDetails().reason();
|
||||
}
|
||||
//进行数据库操作,修改订单状态为已支付成功,但触发了人工审核,请审核通过后再发货(配合回调和CapturesGet查询确定成功)
|
||||
log.info("支付成功,状态为=PENDING : {}", reason);
|
||||
}
|
||||
}
|
||||
}
|
||||
Payer buyer = response.result().payer();
|
||||
log.info("Buyer Email Address: {}", buyer.email());
|
||||
log.info("Buyer Name: {} {}", buyer.name().givenName(), buyer.name().surname());
|
||||
// String jsonString = JSON.toJSONString(response.result());
|
||||
String json = null;
|
||||
try {
|
||||
json = new JSONObject(new Json().serialize(response.result())).toString(4);
|
||||
} catch (SerializeException e) {
|
||||
log.warn("response序列化出错,具体信息 ===> {}", e.getMessage());
|
||||
}
|
||||
log.info("captureOrder response body: {}", json);
|
||||
// return Convert.toStr(new Json().serialize(response.result()));
|
||||
return response.result();
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询扣款
|
||||
*/
|
||||
public String queryCapture(String orderNo) throws IOException {
|
||||
CapturesGetRequest request = new CapturesGetRequest("扣款id, CaptureOrder生成");
|
||||
|
||||
HttpResponse<com.paypal.payments.Capture> response = payPalClient.client(MODE, clientId, clientSecret).execute(request);
|
||||
System.out.println("Status Code: " + response.statusCode());
|
||||
System.out.println("Status: " + response.result().status());
|
||||
System.out.println("Capture ids: " + response.result().id());
|
||||
System.out.println("Links: ");
|
||||
for (com.paypal.payments.LinkDescription link : response.result().links()) {
|
||||
System.out.println("\t" + link.rel() + ": " + link.href() + "\tCall Type: " + link.method());
|
||||
}
|
||||
System.out.println("Full response body:");
|
||||
System.out.println(new JSONObject(new Json().serialize(response.result())).toString(4));
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 申请退款
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Boolean refundOrder(String orderId, String reason) throws IOException {
|
||||
|
||||
RefundInfo refundByOrderNo = refundsInfoService.createRefundByOrderNo(orderId, reason);
|
||||
|
||||
OrdersGetRequest ordersGetRequest = new OrdersGetRequest(orderId);
|
||||
PayPalClient payPalClient = new PayPalClient();
|
||||
HttpResponse<com.paypal.orders.Order> ordersGetResponse = null;
|
||||
ordersGetRequest.authorization("Bearer " + getOAuth());
|
||||
boolean result;
|
||||
try {
|
||||
ordersGetResponse = payPalClient.client(MODE, clientId, clientSecret).execute(ordersGetRequest);
|
||||
} catch (Exception e) {
|
||||
log.error("调用paypal订单查询失败,失败原因 ===> {}", e.getMessage());
|
||||
throw new BusinessException("Order query failed");
|
||||
}
|
||||
String captureId = ordersGetResponse.result().purchaseUnits().get(0).payments().captures().get(0).id();
|
||||
CapturesRefundRequest request = new CapturesRefundRequest(captureId);
|
||||
request.authorization("Bearer " + getOAuth());
|
||||
request.prefer("return=representation");
|
||||
|
||||
OrderInfo orderInfo = orderInfoService.getOrderByOrderNo(orderId);
|
||||
request.requestBody(buildRefundRequestBody(String.valueOf(orderInfo.getTotalFee()), reason));
|
||||
HttpResponse<com.paypal.payments.Refund> response = null;
|
||||
try {
|
||||
response = payPalClient.client(MODE, clientId, clientSecret).execute(request);
|
||||
} catch (IOException e) {
|
||||
log.error("调用paypal退款申请失败,失败原因 {}", e.getMessage());
|
||||
throw new BusinessException("Request for refund failed");
|
||||
}
|
||||
|
||||
log.info("Status Code = {}, Status = {}, RefundID = {}", response.statusCode(), response.result().status(), response.result().id());
|
||||
if ("COMPLETED".equals(response.result().status())) {
|
||||
//进行数据库操作,修改状态为已退款(配合回调和退款查询确定退款成功)
|
||||
|
||||
//更新订单状态
|
||||
orderInfoService.updateStatusByOrderNo(orderId, OrderStatusEnum.REFUND_SUCCESS);
|
||||
|
||||
refundsInfoService.updateRefundForPayPal(
|
||||
refundByOrderNo.getId(),
|
||||
response.result().id(),
|
||||
new Gson().toJson(response.result(), com.paypal.payments.Refund.class),
|
||||
AliPayTradeStateEnum.REFUND_SUCCESS.getType()); //退款成功
|
||||
|
||||
// 更新积分状态
|
||||
OrderInfo orderByOrderNo = orderInfoService.getOrderByOrderNo(orderId);
|
||||
creditsService.creditsRefund(orderByOrderNo.getAccountId(), orderByOrderNo.getTotalFee() / Integer.parseInt(CreditsEventsEnum.PRICE.getValue()));
|
||||
log.info("退款成功");
|
||||
result = Boolean.TRUE;
|
||||
} else {
|
||||
//更新订单状态
|
||||
orderInfoService.updateStatusByOrderNo(orderId, OrderStatusEnum.REFUND_ABNORMAL);
|
||||
|
||||
//更新退款单
|
||||
refundsInfoService.updateRefundForPayPal(
|
||||
refundByOrderNo.getId(),
|
||||
response.result().id(),
|
||||
new Gson().toJson(response.result(), com.paypal.payments.Refund.class),
|
||||
AliPayTradeStateEnum.REFUND_ERROR.getType()); //退款失败
|
||||
result = Boolean.FALSE;
|
||||
}
|
||||
for (com.paypal.payments.LinkDescription link : response.result().links()) {
|
||||
log.info("Links-{}: {} \tCall Type: {}", link.rel(), link.href(), link.method());
|
||||
}
|
||||
String json = new JSONObject(new Json().serialize(response.result())).toString(4);
|
||||
log.info("refundOrder response body: {}", json);
|
||||
return result;
|
||||
}
|
||||
|
||||
public RefundRequest buildRefundRequestBody(String price, String reason) {
|
||||
RefundRequest refundRequest = new RefundRequest();
|
||||
com.paypal.payments.Money money = new com.paypal.payments.Money();
|
||||
money.currencyCode(CurrencyCodesEnum.HONG_KONG_DOLLAR.getCode());
|
||||
money.value(price);
|
||||
refundRequest.amount(money);
|
||||
// refundRequest.invoiceId("P2020052514440001");
|
||||
refundRequest.noteToPayer(reason);
|
||||
try {
|
||||
log.info("refund order body : {}", Convert.toStr(new Json().serialize(refundRequest)));
|
||||
} catch (SerializeException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
return refundRequest;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询退款
|
||||
*/
|
||||
public String queryRefund(String orderNo) throws IOException {
|
||||
RefundsGetRequest request = new RefundsGetRequest("退款id RefundOrder生成");
|
||||
HttpResponse<com.paypal.payments.Refund> response = payPalClient.client(MODE, clientId, clientSecret).execute(request);
|
||||
System.out.println("Status Code: " + response.statusCode());
|
||||
System.out.println("Status: " + response.result().status());
|
||||
System.out.println("Refund Id: " + response.result().id());
|
||||
System.out.println("Links: ");
|
||||
for (com.paypal.payments.LinkDescription link : response.result().links()) {
|
||||
System.out.println("\t" + link.rel() + ": " + link.href() + "\tCall Type: " + link.method());
|
||||
}
|
||||
System.out.println("Full response body:");
|
||||
System.out.println(new JSONObject(new Json().serialize(response.result())).toString(4));
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getOAuth() {
|
||||
// 1、判断缓存区是否有该token
|
||||
Boolean hasKey = redisUtil.hasKey(PAYPAL_TOKEN_KEY);
|
||||
if (hasKey) {
|
||||
return redisUtil.getFromString(PAYPAL_TOKEN_KEY);
|
||||
// return "A21AAKnpozur9r9omqQ2ge5aXHBBdEERi5F8FIgNYOjhhO2N7rjmkz2irh2lScpBO3s3Cqukw3eZkpYZ4YWE7rIacjv7MHmow";
|
||||
}
|
||||
|
||||
// 2、无或者过期,重新获取token,返回
|
||||
AuthenticationRequest authenticationRequest = new AuthenticationRequest();
|
||||
authenticationRequest.authorization(clientId, clientSecret);
|
||||
try {
|
||||
HttpResponse<HashMap> authResult = payPalClient.client(MODE, clientId, clientSecret).execute(authenticationRequest);
|
||||
String accessToken = authResult.result().get("access_token").toString();
|
||||
long expiresIn = Long.parseLong(authResult.result().get("expires_in").toString());
|
||||
// 3、存redis
|
||||
redisUtil.addToString(PAYPAL_TOKEN_KEY, accessToken, expiresIn);
|
||||
return accessToken;
|
||||
} catch (IOException e) {
|
||||
log.error("获取paypal token失败,失败原因 ===> {}", e.getMessage());
|
||||
throw new BusinessException(e);
|
||||
}
|
||||
}
|
||||
|
||||
// 处理当前订单
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void processOrder(String orderId) {
|
||||
// 1、确定当前订单是否已经被扣款
|
||||
OrderInfo orderInfo = orderInfoService.getOrderByOrderNo(orderId);
|
||||
if (orderInfo.getOrderStatus().equals(OrderStatusEnum.SUCCESS.getType())) {
|
||||
// 直接返回
|
||||
return;
|
||||
}
|
||||
// 发起扣款请求
|
||||
Order capturedOrder = captureOrder(orderId);
|
||||
// 业务处理
|
||||
if (PayPalOrderStatusEnum.COMPLETED.getStatus().equals(capturedOrder.status())) {
|
||||
//更新订单状态
|
||||
orderInfoService.updateStatusByOrderNo(orderId, OrderStatusEnum.SUCCESS);
|
||||
//记录支付日志
|
||||
paymentInfoService.createPaymentInfoForPayPal(capturedOrder);
|
||||
// 添加积分变更记录
|
||||
creditsService.insertToCreditsDetail(orderInfo.getAccountId(),
|
||||
CreditsEventsEnum.BUY_CREDITS.getName() + "--PayPal",
|
||||
CreditsEventsEnum.BUY_CREDITS.getValue(),
|
||||
"positive");
|
||||
// 更新积分
|
||||
creditsService.buyCredits(orderInfo.getAccountId(), orderInfo.getTotalFee() / Integer.parseInt(CreditsEventsEnum.PRICE.getValue()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.ai.da.mapper.primary.entity.PaymentInfo;
|
||||
import com.ai.da.service.PaymentInfoService;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.google.gson.Gson;
|
||||
import com.paypal.orders.Order;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@@ -70,8 +71,8 @@ public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, Payme
|
||||
String tradeStatus = params.get("trade_status");
|
||||
//交易金额
|
||||
String totalAmount = params.get("total_amount");
|
||||
int totalAmountInt = new BigDecimal(totalAmount).multiply(new BigDecimal("100")).intValue();
|
||||
|
||||
// int totalAmountInt = new BigDecimal(totalAmount).multiply(new BigDecimal("100")).intValue();
|
||||
int totalAmountInt = new BigDecimal(totalAmount).intValue();
|
||||
|
||||
PaymentInfo paymentInfo = new PaymentInfo();
|
||||
paymentInfo.setOrderNo(orderNo);
|
||||
@@ -87,4 +88,27 @@ public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, Payme
|
||||
|
||||
baseMapper.insert(paymentInfo);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createPaymentInfoForPayPal(Order order) {
|
||||
|
||||
log.info("记录支付日志");
|
||||
|
||||
// int totalAmountInt = new BigDecimal(totalAmount).multiply(new BigDecimal("100")).intValue();
|
||||
int totalAmountInt = new BigDecimal(order.purchaseUnits().get(0).payments().captures().get(0).amount().value()).intValue();
|
||||
|
||||
PaymentInfo paymentInfo = new PaymentInfo();
|
||||
paymentInfo.setOrderNo(order.id());
|
||||
paymentInfo.setPaymentType(PayTypeEnum.PAYPAL.getType());
|
||||
paymentInfo.setTransactionId(order.id());
|
||||
paymentInfo.setTradeType("电脑网站支付");
|
||||
paymentInfo.setTradeState(order.status());
|
||||
paymentInfo.setPayerTotal(totalAmountInt);
|
||||
|
||||
Gson gson = new Gson();
|
||||
String json = gson.toJson(order, Order.class);
|
||||
paymentInfo.setContent(json);
|
||||
|
||||
baseMapper.insert(paymentInfo);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,10 +27,14 @@ public class RabbitMQServiceImpl implements RabbitMQService {
|
||||
private MQPublisher mqPublisher;
|
||||
|
||||
@Override
|
||||
public void publishMessage(String message) {
|
||||
public void publishMessageToGenerate(String message) {
|
||||
mqPublisher.sendGenerateMessage(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void publishMessageToSR(String message) {
|
||||
mqPublisher.sendSRMessage(message);
|
||||
}
|
||||
@Override
|
||||
public Integer getMessageCount(String queueUrl) {
|
||||
|
||||
|
||||
@@ -40,8 +40,8 @@ public class RefundInfoServiceImpl extends ServiceImpl<RefundInfoMapper, RefundI
|
||||
RefundInfo refundInfo = new RefundInfo();
|
||||
refundInfo.setOrderNo(orderNo);//订单编号
|
||||
refundInfo.setRefundNo(OrderNoUtils.getRefundNo());//退款单编号
|
||||
refundInfo.setTotalFee(orderInfo.getTotalFee());//原订单金额(分)
|
||||
refundInfo.setRefund(orderInfo.getTotalFee());//退款金额(分)
|
||||
refundInfo.setTotalFee(orderInfo.getTotalFee());//原订单金额(元)
|
||||
refundInfo.setRefund(orderInfo.getTotalFee());//退款金额(元)
|
||||
refundInfo.setReason(reason);//退款原因
|
||||
|
||||
//保存退款订单
|
||||
@@ -50,7 +50,6 @@ public class RefundInfoServiceImpl extends ServiceImpl<RefundInfoMapper, RefundI
|
||||
return refundInfo;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 记录退款记录
|
||||
* @param content
|
||||
@@ -68,7 +67,6 @@ public class RefundInfoServiceImpl extends ServiceImpl<RefundInfoMapper, RefundI
|
||||
|
||||
//设置要修改的字段
|
||||
RefundInfo refundInfo = new RefundInfo();
|
||||
|
||||
refundInfo.setRefundId(resultMap.get("refund_id"));//微信支付退款单号
|
||||
|
||||
//查询退款和申请退款中的返回参数
|
||||
@@ -120,8 +118,8 @@ public class RefundInfoServiceImpl extends ServiceImpl<RefundInfoMapper, RefundI
|
||||
refundInfo.setOrderNo(orderNo);//订单编号
|
||||
refundInfo.setRefundNo(OrderNoUtils.getRefundNo());//退款单编号
|
||||
|
||||
refundInfo.setTotalFee(orderInfo.getTotalFee());//原订单金额(分)
|
||||
refundInfo.setRefund(orderInfo.getTotalFee());//退款金额(分)
|
||||
refundInfo.setTotalFee(orderInfo.getTotalFee());//原订单金额(元)
|
||||
refundInfo.setRefund(orderInfo.getTotalFee());//退款金额(元)
|
||||
refundInfo.setReason(reason);//退款原因
|
||||
|
||||
//保存退款订单
|
||||
@@ -153,4 +151,23 @@ public class RefundInfoServiceImpl extends ServiceImpl<RefundInfoMapper, RefundI
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateRefundForPayPal(Long id, String refundId,String content, String refundStatus) {
|
||||
|
||||
//根据退款单编号修改退款单
|
||||
// QueryWrapper<RefundInfo> queryWrapper = new QueryWrapper<>();
|
||||
// queryWrapper.eq("order_no", orderNo);
|
||||
|
||||
//设置要修改的字段
|
||||
RefundInfo refundInfo = new RefundInfo();
|
||||
refundInfo.setId(id);
|
||||
refundInfo.setRefundNo(null);
|
||||
refundInfo.setRefundId(refundId);
|
||||
refundInfo.setRefundStatus(refundStatus);//退款状态
|
||||
refundInfo.setContentReturn(content);//将全部响应结果存入数据库的content字段
|
||||
|
||||
//更新退款单
|
||||
// baseMapper.update(refundInfo, queryWrapper);
|
||||
baseMapper.updateById(refundInfo);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,204 @@
|
||||
package com.ai.da.service.impl;
|
||||
|
||||
import com.ai.da.common.config.exception.BusinessException;
|
||||
import com.ai.da.common.context.UserContext;
|
||||
import com.ai.da.common.enums.CreditsEventsEnum;
|
||||
import com.ai.da.common.response.PageBaseResponse;
|
||||
import com.ai.da.common.utils.RedisUtil;
|
||||
import com.ai.da.mapper.primary.TaskListMapper;
|
||||
import com.ai.da.mapper.primary.entity.TaskList;
|
||||
import com.ai.da.model.dto.QueryTaskHistoryDTO;
|
||||
import com.ai.da.model.dto.SuperResolutionDTO;
|
||||
import com.ai.da.model.dto.TaskDTO;
|
||||
import com.ai.da.python.PythonService;
|
||||
import com.ai.da.service.CreditsService;
|
||||
import com.ai.da.service.RabbitMQService;
|
||||
import com.ai.da.service.SuperResolutionService;
|
||||
import com.ai.da.service.TaskListService;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import io.netty.util.internal.StringUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class SuperResolutionServiceImpl extends ServiceImpl<TaskListMapper, TaskList> implements SuperResolutionService {
|
||||
|
||||
public static final Integer creditsConsumption = 100;
|
||||
|
||||
@Resource
|
||||
private CreditsService creditsService;
|
||||
@Resource
|
||||
private RabbitMQService rabbitMQService;
|
||||
@Resource
|
||||
private PythonService pythonService;
|
||||
|
||||
@Resource
|
||||
private TaskListService taskListService;
|
||||
|
||||
@Resource
|
||||
private RedisUtil redisUtil;
|
||||
|
||||
@Value("${redis.key.orderForSR}")
|
||||
private String orderForSR;
|
||||
|
||||
@Value("${redis.key.resultMap}")
|
||||
private String resultMapKey;
|
||||
|
||||
@Value("${minio.bucketName.users}")
|
||||
private String usersBucket;
|
||||
|
||||
@Value("${minio.endpoint}")
|
||||
private String endpoint;
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public List<String> prepareForSR(List<SuperResolutionDTO> superResolutionDTOList) {
|
||||
|
||||
Long accountId = UserContext.getUserHolder().getId();
|
||||
// 1、判断用户当前积分是否够本次超分消耗
|
||||
|
||||
Boolean credits = creditsService.checkCredits(accountId, CreditsEventsEnum.SUPER_RESOLUTION, superResolutionDTOList.size());
|
||||
// todo 积分扣除待升级
|
||||
if (credits) {
|
||||
// 先扣除积分,后失败后再加上
|
||||
creditsService.creditsDecrease(accountId, CreditsEventsEnum.SUPER_RESOLUTION.getName());
|
||||
} else {
|
||||
throw new BusinessException("Not enough Credits");
|
||||
}
|
||||
|
||||
ArrayList<String> uuidList = new ArrayList<>();
|
||||
|
||||
for (SuperResolutionDTO superResolutionDTO : superResolutionDTOList) {
|
||||
// 2、生成唯一id 使用uuid
|
||||
String uuid = UUID.randomUUID().toString();
|
||||
int num = 1;
|
||||
// 判断已经正常生成结果的uuid或正在排队的uuid中是否有相同的id
|
||||
while ((redisUtil.isElementExistsInMap(resultMapKey, uuid) ||
|
||||
redisUtil.isElementExistsInZSet(orderForSR, uuid))
|
||||
&& num < 10) {
|
||||
uuid = UUID.randomUUID().toString();
|
||||
num++;
|
||||
}
|
||||
uuid += "-" + accountId;
|
||||
uuidList.add(uuid);
|
||||
|
||||
// 3、截取minio地址 (前提是该url是由minio地址转换来的)
|
||||
String inputString = superResolutionDTO.getImages();
|
||||
String replace = inputString.replace(endpoint + "/", "");
|
||||
String minioPath = replace.substring(0, replace.indexOf("?"));
|
||||
// log.info(minioPath);
|
||||
superResolutionDTO.setImages(minioPath);
|
||||
|
||||
superResolutionDTO.setUniqueId(uuid);
|
||||
String jsonString = JSON.toJSONString(superResolutionDTO);
|
||||
|
||||
// 4、将数据存到数据库
|
||||
TaskList taskList = new TaskList();
|
||||
taskList.setAccountId(accountId);
|
||||
taskList.setTaskType("SR");
|
||||
taskList.setInputUrl(superResolutionDTO.getImages());
|
||||
taskList.setScale(superResolutionDTO.getScale());
|
||||
taskList.setStatus("Waiting");
|
||||
taskList.setTaskId(superResolutionDTO.getUniqueId());
|
||||
taskList.setCreateTime(LocalDateTime.now());
|
||||
|
||||
baseMapper.insert(taskList);
|
||||
|
||||
// 5、加入任务列表 设置状态为 等待中
|
||||
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||
String name = superResolutionDTO.getImages();
|
||||
taskListService.addToTaskListRedis(new TaskDTO<>(uuid, "SR", name.substring(name.lastIndexOf("/") + 1), superResolutionDTO, "Waiting", LocalDateTime.now().format(dateTimeFormatter)));
|
||||
|
||||
// 6、将消息发布到MQ消息队列
|
||||
log.info("发送消息到SR_QUEUE,参数 :{}", jsonString);
|
||||
rabbitMQService.publishMessageToSR(jsonString);
|
||||
}
|
||||
|
||||
// 6、返回唯一id列表
|
||||
return uuidList;
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public void SR(SuperResolutionDTO superResolutionDTO) throws Exception {
|
||||
// 1、向模型发起请求
|
||||
pythonService.superResolution(superResolutionDTO);
|
||||
|
||||
// 2、更新状态
|
||||
updateSROutput(superResolutionDTO.getUniqueId(), "Executing", null);
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public void setSRResult(String taskId, String output, String status) {
|
||||
Long accountId = Long.parseLong(taskId.substring(taskId.lastIndexOf("-") + 1));
|
||||
// 获取结果后更新
|
||||
updateSROutput(taskId, status, output);
|
||||
|
||||
if ("success".equals(status)) {
|
||||
// 3、记录积分变更
|
||||
creditsService.insertToCreditsDetail(accountId,
|
||||
CreditsEventsEnum.SUPER_RESOLUTION.getName(),
|
||||
CreditsEventsEnum.SUPER_RESOLUTION.getValue(),
|
||||
"negative");
|
||||
} else {
|
||||
// 将之前扣除的积分返还
|
||||
creditsService.creditsIncrease(accountId, CreditsEventsEnum.SUPER_RESOLUTION.getName());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void updateSROutput(String taskId, String status, String output) {
|
||||
UpdateWrapper<TaskList> uw = new UpdateWrapper<>();
|
||||
uw.eq("task_id", taskId);
|
||||
|
||||
uw.set("output_url", output);
|
||||
uw.set("status", status);
|
||||
baseMapper.update(null, uw);
|
||||
|
||||
}
|
||||
|
||||
// 分页查询
|
||||
@Override
|
||||
public PageBaseResponse<TaskList> getTaskHistoryPage(QueryTaskHistoryDTO queryTaskHistoryDTO) {
|
||||
QueryWrapper<TaskList> qw = new QueryWrapper<>();
|
||||
qw.eq("account_id", UserContext.getUserHolder().getId());
|
||||
|
||||
String startTime = queryTaskHistoryDTO.getStartTime();
|
||||
String endTime = queryTaskHistoryDTO.getEndTime();
|
||||
if (StringUtil.isNullOrEmpty(startTime)) {
|
||||
startTime = "2024-03-01 00:00:00";
|
||||
}
|
||||
if (StringUtil.isNullOrEmpty(endTime)) {
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||
endTime = now.format(dateTimeFormatter);
|
||||
}
|
||||
|
||||
qw.between("create_time", startTime, endTime);
|
||||
qw.orderByDesc("create_time");
|
||||
Page<TaskList> pageInfo = new Page<>(queryTaskHistoryDTO.getPage(), queryTaskHistoryDTO.getSize());
|
||||
Page<TaskList> orderInfo = baseMapper.selectPage(pageInfo, qw);
|
||||
if (CollectionUtils.isEmpty(orderInfo.getRecords())) {
|
||||
return PageBaseResponse.success(new Page<>());
|
||||
}
|
||||
|
||||
return PageBaseResponse.success(orderInfo);
|
||||
}
|
||||
}
|
||||
123
src/main/java/com/ai/da/service/impl/TaskListServiceImpl.java
Normal file
123
src/main/java/com/ai/da/service/impl/TaskListServiceImpl.java
Normal file
@@ -0,0 +1,123 @@
|
||||
package com.ai.da.service.impl;
|
||||
|
||||
import com.ai.da.common.context.UserContext;
|
||||
import com.ai.da.common.response.PageBaseResponse;
|
||||
import com.ai.da.common.utils.MinioUtil;
|
||||
import com.ai.da.common.utils.RedisUtil;
|
||||
import com.ai.da.mapper.primary.TaskListMapper;
|
||||
import com.ai.da.mapper.primary.entity.TaskList;
|
||||
import com.ai.da.model.dto.QueryTaskHistoryDTO;
|
||||
import com.ai.da.model.dto.SuperResolutionDTO;
|
||||
import com.ai.da.model.dto.TaskDTO;
|
||||
import com.ai.da.model.vo.TaskVO;
|
||||
import com.ai.da.service.SuperResolutionService;
|
||||
import com.ai.da.service.TaskListService;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import io.netty.util.internal.StringUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.lang.reflect.Type;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.*;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class TaskListServiceImpl extends ServiceImpl<TaskListMapper, TaskList> implements TaskListService {
|
||||
|
||||
@Resource
|
||||
private RedisUtil redisUtil;
|
||||
|
||||
@Value("${redis.key.taskList}")
|
||||
private String taskListKey;
|
||||
|
||||
@Resource
|
||||
private MinioUtil minioUtil;
|
||||
|
||||
@Resource
|
||||
private SuperResolutionService superResolutionService;
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public List<TaskDTO<SuperResolutionDTO>> getExecTask(List<String> taskIdList) {
|
||||
Long id = UserContext.getUserHolder().getId();
|
||||
// Set<String> keys = redisUtil.getKeysFromString(taskListKey + ":" + id + ":*");
|
||||
ArrayList<TaskDTO<SuperResolutionDTO>> taskDTOS = new ArrayList<>();
|
||||
taskIdList.forEach(taskId -> {
|
||||
String taskDetail = redisUtil.getFromString(taskListKey + ":" + id + ":" + taskId);
|
||||
Gson gson = new Gson();
|
||||
Type type = new TypeToken<TaskDTO<SuperResolutionDTO>>() {
|
||||
}.getType();
|
||||
TaskDTO<SuperResolutionDTO> taskDTO = gson.fromJson(taskDetail, type);
|
||||
if (Objects.isNull(taskDTO)) {
|
||||
// 未执行成功的taskId在redis被删除,将该条数据在db中的状态改为失败
|
||||
superResolutionService.updateSROutput(taskId, "fail", null);
|
||||
taskDTOS.add(new TaskDTO<>());
|
||||
} else {
|
||||
SuperResolutionDTO inputParam = taskDTO.getInputParam();
|
||||
inputParam.setImages(minioUtil.getPresignedUrl(inputParam.getImages(), 24 * 60));
|
||||
taskDTO.setOutputImage(StringUtil.isNullOrEmpty(taskDTO.getOutputImage()) ? null : minioUtil.getPresignedUrl(taskDTO.getOutputImage(), 24 * 60));
|
||||
taskDTOS.add(taskDTO);
|
||||
}
|
||||
});
|
||||
return taskDTOS;
|
||||
}
|
||||
|
||||
public void addToTaskListRedis(TaskDTO<SuperResolutionDTO> taskDTO) {
|
||||
String key = taskListKey + ":" + UserContext.getUserHolder().getId() + ":" + taskDTO.getTaskId();
|
||||
redisUtil.addToString(key, new Gson().toJson(taskDTO), 24L * 60 * 60 * 3);
|
||||
}
|
||||
|
||||
// 3、更新任务状态
|
||||
@Override
|
||||
public void updateTaskStatusOrOutputRedis(String taskId, String status, String output) {
|
||||
String key = taskListKey + ":" + taskId.substring(taskId.lastIndexOf("-") + 1) + ":" + taskId;
|
||||
String taskDetail = redisUtil.getFromString(key);
|
||||
Gson gson = new Gson();
|
||||
Type type = new TypeToken<TaskDTO<SuperResolutionDTO>>() {
|
||||
}.getType();
|
||||
TaskDTO<SuperResolutionDTO> taskDTO = gson.fromJson(taskDetail, type);
|
||||
taskDTO.setStatus(status);
|
||||
if (!StringUtil.isNullOrEmpty(output)) {
|
||||
taskDTO.setOutputImage(output);
|
||||
}
|
||||
Long expire = redisUtil.getExpire(key);
|
||||
redisUtil.addToString(key, new Gson().toJson(taskDTO), expire);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageBaseResponse<TaskVO> getAllTask(QueryTaskHistoryDTO queryTaskHistoryDTO) {
|
||||
PageBaseResponse<TaskVO> response = new PageBaseResponse<>();
|
||||
ArrayList<TaskVO> taskLists = new ArrayList<>();
|
||||
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||
switch (queryTaskHistoryDTO.getType()) {
|
||||
case "SR":
|
||||
PageBaseResponse<TaskList> taskHistoryPage = superResolutionService.getTaskHistoryPage(queryTaskHistoryDTO);
|
||||
List<TaskList> srHistory = taskHistoryPage.getContent();
|
||||
srHistory.forEach(s -> {
|
||||
// 成功失败的都返回
|
||||
TaskVO task = new TaskVO();
|
||||
task.setImageName(s.getInputUrl().substring(s.getInputUrl().lastIndexOf("/") + 1));
|
||||
task.setInputImage(minioUtil.getPresignedUrl(s.getInputUrl(), 24 * 60));
|
||||
task.setOutputImage(StringUtil.isNullOrEmpty(s.getOutputUrl()) ? null : minioUtil.getPresignedUrl(s.getOutputUrl(), 24 * 60));
|
||||
task.setStatus(s.getStatus());
|
||||
task.setTaskId(s.getTaskId());
|
||||
task.setCreateDate(s.getCreateTime().format(dateTimeFormatter));
|
||||
task.setOtherInput("×" + s.getScale());
|
||||
taskLists.add(task);
|
||||
});
|
||||
BeanUtils.copyProperties(taskHistoryPage, response);
|
||||
response.setContent(taskLists);
|
||||
break;
|
||||
case "GENERATE":
|
||||
log.info("未知任务类型--GENERATE");
|
||||
}
|
||||
return response;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user