81 lines
2.8 KiB
Java
81 lines
2.8 KiB
Java
|
|
package com.ai.da.controller;
|
||
|
|
|
||
|
|
import com.ai.da.common.response.Response;
|
||
|
|
import com.ai.da.service.CallBackService;
|
||
|
|
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;
|
||
|
|
|
||
|
|
@Resource
|
||
|
|
private CallBackService callBackService;
|
||
|
|
|
||
|
|
@ApiOperation(value = "创建订单")
|
||
|
|
@PostMapping(value = "/trade/{productId}")
|
||
|
|
public Response<HashMap<String, String>> createOrder(@PathVariable Long productId,@RequestParam String returnUrl) throws SerializeException {
|
||
|
|
HashMap<String, String> approvalUrl = payPalCheckoutService.createOrder(productId,returnUrl);
|
||
|
|
return Response.success(approvalUrl);
|
||
|
|
}
|
||
|
|
|
||
|
|
@ApiOperation(value = "ipn异步回调")
|
||
|
|
@PostMapping(value = "/ipn/back")
|
||
|
|
public Response<String> callback(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||
|
|
Boolean result = callBackService.doGet(request, response);
|
||
|
|
if (result){
|
||
|
|
return Response.success();
|
||
|
|
}else {
|
||
|
|
return Response.fail(500,"failure");
|
||
|
|
}
|
||
|
|
// return payPalCheckoutService.callback(RequestToMapUtil.getParameterMap(request));
|
||
|
|
}
|
||
|
|
|
||
|
|
@ApiOperation(value = "查询指定订单")
|
||
|
|
@PostMapping(value = "/trade/query/{orderNo}")
|
||
|
|
public Response<String> queryOrder(@PathVariable String orderNo) throws SerializeException {
|
||
|
|
String s = payPalCheckoutService.queryOrder(orderNo);
|
||
|
|
return Response.success(s);
|
||
|
|
}
|
||
|
|
|
||
|
|
@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);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
}
|
||
|
|
|