2024-03-01 17:31:26 +08:00
|
|
|
package com.ai.da.controller;
|
|
|
|
|
|
|
|
|
|
import com.ai.da.common.response.Response;
|
2025-01-07 14:31:43 +08:00
|
|
|
import com.ai.da.model.dto.ProductPurchaseDTO;
|
2024-03-01 17:31:26 +08:00
|
|
|
import com.ai.da.service.PayPalCheckoutService;
|
|
|
|
|
import com.paypal.http.HttpResponse;
|
|
|
|
|
import com.paypal.http.exceptions.SerializeException;
|
|
|
|
|
import com.paypal.orders.Order;
|
|
|
|
|
import com.paypal.payments.Refund;
|
2025-12-11 10:35:08 +08:00
|
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
|
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
2024-03-01 17:31:26 +08:00
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
2025-11-25 16:46:05 +08:00
|
|
|
import jakarta.annotation.Resource;
|
|
|
|
|
import jakarta.servlet.ServletException;
|
|
|
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
|
|
|
import jakarta.servlet.http.HttpServletResponse;
|
|
|
|
|
import jakarta.validation.Valid;
|
2024-03-01 17:31:26 +08:00
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
|
|
|
|
@RestController
|
2025-12-11 10:35:08 +08:00
|
|
|
@Tag(name = "PayPalCheckout接口")
|
2024-03-01 17:31:26 +08:00
|
|
|
@RequestMapping("/api/paypal")
|
|
|
|
|
public class PayPalCheckoutController {
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
private PayPalCheckoutService payPalCheckoutService;
|
|
|
|
|
|
2025-12-11 10:35:08 +08:00
|
|
|
@Operation(summary = "创建订单")
|
2025-01-07 14:31:43 +08:00
|
|
|
@PostMapping(value = "/trade")
|
|
|
|
|
public Response<HashMap<String, String>> createOrder(@Valid @RequestBody ProductPurchaseDTO productPurchaseDTO, HttpServletRequest request) throws SerializeException {
|
|
|
|
|
HashMap<String, String> approvalUrl = payPalCheckoutService.createOrder(productPurchaseDTO, request);
|
2024-03-01 17:31:26 +08:00
|
|
|
return Response.success(approvalUrl);
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-11 10:35:08 +08:00
|
|
|
@Operation(summary = "ipn异步回调")
|
2024-03-01 17:31:26 +08:00
|
|
|
@PostMapping(value = "/ipn/back")
|
|
|
|
|
public Response<String> callback(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
2024-03-15 15:38:56 +08:00
|
|
|
Boolean result = payPalCheckoutService.doPost(request, response);
|
2024-03-01 17:31:26 +08:00
|
|
|
if (result){
|
2024-03-06 20:56:22 +08:00
|
|
|
return Response.success(200,"success");
|
2024-03-01 17:31:26 +08:00
|
|
|
}else {
|
|
|
|
|
return Response.fail(500,"failure");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-11 10:35:08 +08:00
|
|
|
@Operation(summary = "查询指定订单")
|
2024-03-01 17:31:26 +08:00
|
|
|
@PostMapping(value = "/trade/query/{orderNo}")
|
2024-04-10 16:07:52 +08:00
|
|
|
public Response<Order> queryOrder(@PathVariable String orderNo) throws SerializeException {
|
|
|
|
|
Order s = payPalCheckoutService.queryOrder(orderNo);
|
2024-03-01 17:31:26 +08:00
|
|
|
return Response.success(s);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-15 15:38:56 +08:00
|
|
|
/** 不提供退款接口 */
|
2025-12-11 10:35:08 +08:00
|
|
|
@Operation(summary = "申请退款")
|
2024-03-01 17:31:26 +08:00
|
|
|
@PostMapping("/trade/refund/{orderNo}/{reason}")
|
|
|
|
|
public Response<HttpResponse<Refund>> refund(@PathVariable String orderNo, @PathVariable String reason) throws IOException {
|
|
|
|
|
Boolean response = payPalCheckoutService.refundOrder(orderNo,reason);
|
|
|
|
|
if (response){
|
|
|
|
|
return Response.success();
|
|
|
|
|
}else {
|
|
|
|
|
return Response.fail("Request for refund failed.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-11 10:35:08 +08:00
|
|
|
@Operation(summary = "执行扣款")
|
2024-03-01 17:31:26 +08:00
|
|
|
@PostMapping("/trade/capture/{orderNo}")
|
|
|
|
|
public Response<com.paypal.orders.Order> captureOrder(@PathVariable String orderNo) throws IOException {
|
|
|
|
|
Order response = payPalCheckoutService.captureOrder(orderNo);
|
|
|
|
|
return Response.success(response);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|