Merge branch 'dev/dev_xp' into dev/dev

# Conflicts:
#	src/main/resources/paypal-sandbox.properties
This commit is contained in:
2024-04-11 11:45:03 +08:00
7 changed files with 119 additions and 25 deletions

View File

@@ -40,7 +40,12 @@ public enum OrderStatusEnum {
/**
* 退款异常
*/
REFUND_ABNORMAL("退款异常");
REFUND_ABNORMAL("退款异常"),
/**
* paypal订单状态为 APPROVED
*/
ORDER_PROCESSING("订单处理中");
/**
* 类型

View File

@@ -7,18 +7,24 @@ import lombok.Getter;
@AllArgsConstructor
public enum PayPalOrderStatusEnum {
// The order was created with the specified context.
// 订单已创建并具有指定的上下文。
CREATED("CREATED"),
// The order was saved and persisted. The order status continues to be in progress until a capture is made with final_capture = true for all purchase units within the order.
// 订单已保存并持久化。订单状态仍处于进行中,直到对订单中的所有购买单元进行了 final_capture = true 的捕获为止
SAVED("SAVED"),
// The customer approved the payment through the PayPal wallet or another form of guest or unbranded payment. For example, a card, bank account, or so on.
// 客户通过PayPal钱包或其他形式的游客或非品牌支付批准了付款。例如信用卡、银行账户等。
APPROVED("APPROVED"),
// All purchase units in the order are voided.
// 订单中的所有购买单元都已作废。
VOIDED("VOIDED"),
// The payment was authorized or the authorized payment was captured for the order.
// 订单的支付已被授权或已捕获授权的支付。
COMPLETED("COMPLETED"),
// The order requires an action from the payer (e.g. 3DS authentication). Redirect the payer to the "rel":"payer-action" HATEOAS link returned as part of the response prior to authorizing or capturing the order.
// 订单需要支付者执行某项操作例如3DS身份验证。在授权或捕获订单之前请将支付者重定向到响应中返回的"rel":"payer-action" HATEOAS链接。
PAYER_ACTION_REQUIRED("PAYER_ACTION_REQUIRED");
private final String status;
}

View File

@@ -24,10 +24,10 @@ public class AliPayTask {
/**
* 从第0秒开始每隔30秒执行1次查询创建超过5分钟并且未支付的订单
*/
@Scheduled(cron = "0/30 * * * * ?")
// @Scheduled(cron = "0/30 * * * * ?")
public void orderConfirm(){
log.info("orderConfirm 被执行......");
log.info("Alipay orderConfirm 被执行......");
List<OrderInfo> orderInfoList = orderInfoService.getNoPayOrderByDuration(5, PayTypeEnum.ALIPAY.getType());

View File

@@ -0,0 +1,42 @@
package com.ai.da.common.task;
import com.ai.da.common.enums.PayTypeEnum;
import com.ai.da.mapper.primary.entity.OrderInfo;
import com.ai.da.service.OrderInfoService;
import com.ai.da.service.PayPalCheckoutService;
import com.paypal.http.exceptions.SerializeException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Slf4j
@Component
public class PaypalTask {
@Resource
private OrderInfoService orderInfoService;
@Resource
private PayPalCheckoutService payPalCheckoutService;
@Scheduled(cron = "0/30 * * * * ?")
public void orderConfirm() throws SerializeException {
log.info("PayPal orderConfirm 被执行......");
List<OrderInfo> orderInfoList = orderInfoService.getNoPayOrderByDuration(5, PayTypeEnum.PAYPAL.getType());
for (OrderInfo orderInfo : orderInfoList) {
String orderNo = orderInfo.getOrderNo();
log.warn("超时订单 ===> {}", orderNo);
//核实订单状态:调用支付宝查单接口
payPalCheckoutService.checkOrderStatus(orderNo);
}
}
}

View File

@@ -45,8 +45,8 @@ public class PayPalCheckoutController {
@ApiOperation(value = "查询指定订单")
@PostMapping(value = "/trade/query/{orderNo}")
public Response<String> queryOrder(@PathVariable String orderNo) throws SerializeException {
String s = payPalCheckoutService.queryOrder(orderNo);
public Response<Order> queryOrder(@PathVariable String orderNo) throws SerializeException {
Order s = payPalCheckoutService.queryOrder(orderNo);
return Response.success(s);
}

View File

@@ -19,7 +19,7 @@ public interface PayPalCheckoutService {
Boolean doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException;
String queryOrder(String orderNo) throws SerializeException;
Order queryOrder(String orderNo) throws SerializeException;
Order captureOrder(String orderId) throws IOException;
@@ -28,5 +28,7 @@ public interface PayPalCheckoutService {
String getOAuth();
void processOrder(String orderId);
void checkOrderStatus(String orderNo) throws SerializeException;
}

View File

@@ -3,7 +3,6 @@ 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;
@@ -27,7 +26,6 @@ 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;
@@ -329,7 +327,7 @@ public class PayPalCheckoutServiceImpl implements PayPalCheckoutService {
* @return
* @throws SerializeException
*/
public String queryOrder(String orderNo) throws SerializeException {
public Order queryOrder(String orderNo) throws SerializeException {
OrdersGetRequest request = new OrdersGetRequest(orderNo);
HttpResponse<Order> response = null;
@@ -337,33 +335,31 @@ public class PayPalCheckoutServiceImpl implements PayPalCheckoutService {
response = payPalClient.client(mode, clientId, clientSecret).execute(request);
} catch (Exception e) {
log.error("paypal订单查询失败失败原因 ===> {}", e.getMessage());
return null;
}
System.out.println("Status Code: " + response.statusCode());
System.out.println("Status: " + response.result().status());
System.out.println("Order id: " + response.result().id());
log.debug("Status Code: " + response.statusCode() + "\tStatus: " + response.result().status() + "\tOrder 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());
log.debug("\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());
log.debug("\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());
log.debug("Links: \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;
log.info("Full response body: {}", json);
return response.result();
}
/**
@@ -598,6 +594,49 @@ public class PayPalCheckoutServiceImpl implements PayPalCheckoutService {
creditsService.buyCredits(orderInfo.getAccountId(), orderInfo.getTotalFee() / Integer.parseInt(CreditsEventsEnum.PRICE.getValue()));
}
}
@Override
public void checkOrderStatus(String orderNo) throws SerializeException {
log.warn("根据订单号核实订单状态 ===> {}", orderNo);
Order result = this.queryOrder(orderNo);
// 订单未创建 | 订单异常 | 订单创建过但未支付
if(result == null || PayPalOrderStatusEnum.CREATED.getStatus().equals(result.status())){
log.warn("核实订单未创建 ===> {}", orderNo);
//更新本地订单状态
orderInfoService.updateStatusByOrderNo(orderNo, OrderStatusEnum.TIMEOUT_CLOSED);
return ;
}
// 解析查单响应结果
String tradeStatus = result.status();
OrderInfo orderByOrderNo = orderInfoService.getOrderByOrderNo(orderNo);
// 支付了,但还没完成
if(PayPalOrderStatusEnum.APPROVED.getStatus().equals(tradeStatus)){
log.warn("核实订单未支付 ===> {}", orderNo);
// 更新商户端订单状态
orderInfoService.updateStatusByOrderNo(orderNo, OrderStatusEnum.ORDER_PROCESSING);
}
if(PayPalOrderStatusEnum.COMPLETED.getStatus().equals(tradeStatus)){
log.warn("核实订单已支付 ===> {}", orderNo);
//如果订单已支付,则更新商户端订单状态
orderInfoService.updateStatusByOrderNo(orderNo, OrderStatusEnum.SUCCESS);
//并记录支付日志
paymentInfoService.createPaymentInfoForPayPal(result);
// 添加积分变更记录
creditsService.insertToCreditsDetail(orderByOrderNo.getAccountId(),
CreditsEventsEnum.BUY_CREDITS.getName() + "--Paypal",
CreditsEventsEnum.BUY_CREDITS.getValue(),
"positive");
// 更新积分
creditsService.buyCredits(orderByOrderNo.getAccountId(),orderByOrderNo.getTotalFee() / Integer.parseInt(CreditsEventsEnum.PRICE.getValue()));
}
}
}