2024-03-01 17:31:26 +08:00
|
|
|
package com.ai.da.controller;
|
|
|
|
|
|
|
|
|
|
import com.ai.da.common.response.Response;
|
|
|
|
|
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;
|
|
|
|
|
import io.swagger.annotations.Api;
|
|
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
import javax.servlet.ServletException;
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
|
|
|
|
@RestController
|
|
|
|
|
@Api(tags = "PayPalCheckout接口")
|
|
|
|
|
@RequestMapping("/api/paypal")
|
|
|
|
|
public class PayPalCheckoutController {
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
private PayPalCheckoutService payPalCheckoutService;
|
|
|
|
|
|
|
|
|
|
@ApiOperation(value = "创建订单")
|
2024-03-06 20:56:22 +08:00
|
|
|
@PostMapping(value = "/trade/{amount}")
|
2025-01-07 11:07:49 +08:00
|
|
|
public Response<HashMap<String, String>> createOrder(@PathVariable Integer amount, @RequestParam String returnUrl, HttpServletRequest request) throws SerializeException {
|
|
|
|
|
HashMap<String, String> approvalUrl = payPalCheckoutService.createOrder(amount, returnUrl, request);
|
2024-03-01 17:31:26 +08:00
|
|
|
return Response.success(approvalUrl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ApiOperation(value = "ipn异步回调")
|
|
|
|
|
@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");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ApiOperation(value = "查询指定订单")
|
|
|
|
|
@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
|
|
|
/** 不提供退款接口 */
|
2024-03-01 17:31:26 +08:00
|
|
|
@ApiOperation("申请退款")
|
|
|
|
|
@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.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ApiOperation("执行扣款")
|
|
|
|
|
@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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|