2024-07-15 13:42:18 +08:00
|
|
|
package com.ai.da.controller;
|
|
|
|
|
|
|
|
|
|
import com.ai.da.common.response.Response;
|
2025-02-21 17:16:44 +08:00
|
|
|
import com.ai.da.common.utils.DateUtil;
|
|
|
|
|
import com.ai.da.common.utils.RedisUtil;
|
2025-02-19 14:14:20 +08:00
|
|
|
import com.ai.da.common.utils.SendEmailUtil;
|
2025-04-15 10:22:45 +08:00
|
|
|
import com.ai.da.mapper.primary.entity.ProductCoupons;
|
|
|
|
|
import com.ai.da.model.dto.CreateCouponDTO;
|
2024-11-18 16:20:25 +08:00
|
|
|
import com.ai.da.model.dto.ProductPurchaseDTO;
|
2025-04-15 10:22:45 +08:00
|
|
|
import com.ai.da.model.dto.QueryCouponsPageDTO;
|
|
|
|
|
import com.ai.da.model.vo.CheckCouponsVO;
|
2024-07-15 13:42:18 +08:00
|
|
|
import com.ai.da.service.StripeService;
|
2025-04-15 10:22:45 +08:00
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
2024-07-16 16:59:58 +08:00
|
|
|
import com.paypal.http.HttpResponse;
|
|
|
|
|
import com.paypal.payments.Refund;
|
2024-11-18 16:20:25 +08:00
|
|
|
import com.stripe.exception.StripeException;
|
2024-07-15 13:42:18 +08:00
|
|
|
import io.swagger.annotations.Api;
|
|
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
2025-02-19 14:14:20 +08:00
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
2024-07-15 13:42:18 +08:00
|
|
|
import org.springframework.web.bind.annotation.*;
|
2024-11-28 10:43:06 +08:00
|
|
|
import springfox.documentation.annotations.ApiIgnore;
|
2024-07-15 13:42:18 +08:00
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
import javax.servlet.ServletException;
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
2024-11-18 16:20:25 +08:00
|
|
|
import javax.servlet.http.HttpServletResponse;
|
2024-11-28 10:43:06 +08:00
|
|
|
import javax.validation.Valid;
|
2024-07-15 13:42:18 +08:00
|
|
|
import java.io.IOException;
|
2025-02-21 17:16:44 +08:00
|
|
|
import java.util.Date;
|
2024-11-28 10:43:06 +08:00
|
|
|
import java.util.List;
|
2024-07-15 13:42:18 +08:00
|
|
|
|
|
|
|
|
@Api(tags = "Stripe模块")
|
|
|
|
|
@Slf4j
|
|
|
|
|
@RestController
|
|
|
|
|
@RequestMapping("/api/stripe")
|
2024-11-28 10:43:06 +08:00
|
|
|
@ApiIgnore
|
2024-07-15 13:42:18 +08:00
|
|
|
public class StripeController {
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
private StripeService stripeService;
|
2025-02-21 17:16:44 +08:00
|
|
|
@Resource
|
|
|
|
|
private RedisUtil redisUtil;
|
2024-07-15 13:42:18 +08:00
|
|
|
|
|
|
|
|
@ApiOperation("创建支付链接")
|
2024-11-18 16:20:25 +08:00
|
|
|
@PostMapping("/createOrder")
|
2025-01-07 11:07:49 +08:00
|
|
|
public Response<String> pay(@Valid @RequestBody ProductPurchaseDTO productPurchaseDTO, HttpServletRequest request) {
|
|
|
|
|
return Response.success(stripeService.pay(productPurchaseDTO, request));
|
2024-07-15 13:42:18 +08:00
|
|
|
}
|
|
|
|
|
|
2025-02-19 14:14:20 +08:00
|
|
|
@Value("${stripe.webhook.fail.reminder}")
|
|
|
|
|
private String webhookReminderFlag;
|
|
|
|
|
|
2024-07-15 13:42:18 +08:00
|
|
|
@ApiOperation("支付通知")
|
|
|
|
|
@PostMapping("/trade/notify")
|
2024-11-18 16:20:25 +08:00
|
|
|
public void callback(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
2025-02-19 14:14:20 +08:00
|
|
|
try{
|
|
|
|
|
Boolean result = stripeService.notify(request);
|
|
|
|
|
if (result){
|
|
|
|
|
response.setStatus(HttpServletResponse.SC_OK);
|
|
|
|
|
}else {
|
|
|
|
|
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
|
|
|
|
}
|
|
|
|
|
}catch (Exception e){
|
|
|
|
|
log.error("Stripe Controller层异常捕捉, {}", e.getMessage());
|
|
|
|
|
e.printStackTrace();
|
2025-02-21 17:16:44 +08:00
|
|
|
String key_1 = RedisUtil.STRIPE_EXCEPTION_LOG + DateUtil.dateToStr(new Date(), DateUtil.YYYY_MM_DD_HH);
|
|
|
|
|
String key_2 = key_1 + ":" + DateUtil.dateToStr(new Date(), DateUtil.YYYY_MM_DD_hh_mm_ss);
|
|
|
|
|
String stackTrace = stripeService.getStackTrace(e, 10);
|
|
|
|
|
redisUtil.addToString(key_2, stackTrace);
|
|
|
|
|
Long size = redisUtil.getSize(key_1);
|
2025-02-19 14:14:20 +08:00
|
|
|
// 给我发送邮件
|
2025-02-21 17:16:44 +08:00
|
|
|
if (webhookReminderFlag.equals("1") && size == 3){
|
2025-02-19 14:14:20 +08:00
|
|
|
SendEmailUtil.commonExceptionReminder("Stripe Webhook 回调", new String[]{"xupei3360@163.com"});
|
|
|
|
|
}
|
2025-02-21 17:16:44 +08:00
|
|
|
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
2024-07-15 13:42:18 +08:00
|
|
|
}
|
|
|
|
|
}
|
2024-07-16 16:59:58 +08:00
|
|
|
|
|
|
|
|
@ApiOperation("申请退款")
|
2024-11-28 10:43:06 +08:00
|
|
|
@GetMapping("/trade/refund/{orderNo}/{reason}")
|
2024-07-16 16:59:58 +08:00
|
|
|
public Response<HttpResponse<Refund>> refund(@PathVariable String orderNo, @PathVariable String reason) throws IOException {
|
|
|
|
|
String response = stripeService.refund(null,orderNo,reason);
|
|
|
|
|
if (response.equals("退款成功")){
|
|
|
|
|
return Response.success();
|
|
|
|
|
}else {
|
|
|
|
|
return Response.fail("Request for refund failed.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-18 16:20:25 +08:00
|
|
|
@ApiOperation("获取订阅")
|
2024-11-28 10:43:06 +08:00
|
|
|
@GetMapping("/getSubscription")
|
|
|
|
|
public Response<List<String>> getSubscription(@RequestParam String name, @RequestParam String email) {
|
2024-11-18 16:20:25 +08:00
|
|
|
try {
|
2024-11-28 10:43:06 +08:00
|
|
|
return Response.success(stripeService.getSubscriptionIds(name, email));
|
2024-11-18 16:20:25 +08:00
|
|
|
} catch (StripeException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ApiOperation("取消订阅")
|
2024-11-28 10:43:06 +08:00
|
|
|
@GetMapping("/cancelSubscription")
|
2024-12-18 11:53:41 +08:00
|
|
|
public Response<String> cancelSubscription(@RequestParam String subscriptionId, @RequestParam(required = false) String reason) {
|
2024-12-16 10:26:02 +08:00
|
|
|
stripeService.cancelSubscription(subscriptionId, reason);
|
2024-11-18 16:20:25 +08:00
|
|
|
return Response.success("success");
|
|
|
|
|
}
|
2025-04-15 10:22:45 +08:00
|
|
|
|
|
|
|
|
@ApiOperation("创建推广码")
|
|
|
|
|
@PostMapping("/createCoupon")
|
|
|
|
|
public Response<String> createCoupon(@Valid @RequestBody CreateCouponDTO createCouponDTO){
|
|
|
|
|
return Response.success(stripeService.createCoupon(createCouponDTO));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ApiOperation("检查推广码")
|
|
|
|
|
@GetMapping("/checkCoupon")
|
|
|
|
|
public Response<CheckCouponsVO> checkCoupon(@RequestParam String promotionCode, @RequestParam Long price){
|
|
|
|
|
return Response.success(stripeService.checkProductCoupon(promotionCode, price));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ApiOperation("获取所有推广码")
|
|
|
|
|
@PostMapping("/getAllCoupons")
|
|
|
|
|
public Response<IPage<ProductCoupons>> getAllCoupons(@RequestBody QueryCouponsPageDTO queryCouponsPageDTO){
|
|
|
|
|
return Response.success(stripeService.getAllCoupons(queryCouponsPageDTO));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ApiOperation("检索优惠券")
|
|
|
|
|
@GetMapping("/retrieveCoupon")
|
|
|
|
|
public Response<String> retrieveCoupon(@RequestParam String couponId){
|
|
|
|
|
return Response.success(stripeService.retrieveCoupon(couponId));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ApiOperation("检索推广码")
|
|
|
|
|
@GetMapping("/retrievePromotionCode")
|
|
|
|
|
public Response<String> retrievePromotionCode(@RequestParam String retrievePromotionCode){
|
|
|
|
|
return Response.success(stripeService.retrievePromotionCode(retrievePromotionCode));
|
|
|
|
|
}
|
2025-01-07 11:07:49 +08:00
|
|
|
/*@ApiOperation("临时 取消订阅")
|
2024-11-28 10:43:06 +08:00
|
|
|
@GetMapping("/cancelSubscriptionTemp")
|
|
|
|
|
public Response<String> cancelSubscriptionTemp(@RequestParam String subscriptionId) {
|
|
|
|
|
stripeService.cancelSubscriptionTemp(subscriptionId);
|
|
|
|
|
return Response.success("success");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ApiOperation("创建订阅 临时")
|
|
|
|
|
@GetMapping("/createSubscriptionTemp")
|
|
|
|
|
public Response<String> createSubscriptionTemp(@RequestParam String name, @RequestParam String email) {
|
|
|
|
|
return Response.success(stripeService.createSubscriptionTemp(name, email));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ApiOperation("修改用户默认支付方式 临时")
|
|
|
|
|
@GetMapping("/changeCustomerPayment")
|
|
|
|
|
public Response<String> changeCustomerPayment(@RequestParam String name, @RequestParam String email) {
|
|
|
|
|
return Response.success(stripeService.changeCustomerPayment(name, email));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ApiOperation("临时 发送续订失败邮件")
|
|
|
|
|
@GetMapping("/sendRenewalFailEmail")
|
|
|
|
|
public Response<Boolean> sendRenewalFailEmail(@RequestParam String invoiceId, @RequestParam String subscriptionId, @RequestParam String orderNo) {
|
|
|
|
|
return Response.success(stripeService.sendRenewalFailEmail(invoiceId, subscriptionId,orderNo));
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-09 16:53:29 +08:00
|
|
|
@ApiOperation("临时 查询指定用户绑定的付款方式")
|
|
|
|
|
@GetMapping("/getCustomerPaymentMethod")
|
2024-12-18 11:53:41 +08:00
|
|
|
public Response<List<Map<String,String>>> getCustomerPaymentMethod(@RequestParam String name, @RequestParam String email) {
|
2024-12-09 16:53:29 +08:00
|
|
|
return Response.success(stripeService.getCustomerPaymentMethod(name, email));
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-18 11:53:41 +08:00
|
|
|
@ApiOperation("临时 解绑指定用户绑定的所有付款方式")
|
|
|
|
|
@GetMapping("/detachCustomerAllPaymentMethod")
|
|
|
|
|
public Response<String> detachCustomerAllPaymentMethod(@RequestParam String name, @RequestParam String email) {
|
|
|
|
|
return Response.success(stripeService.detachCustomerAllPaymentMethod(name, email));
|
2025-01-07 11:07:49 +08:00
|
|
|
}*/
|
2024-12-18 11:53:41 +08:00
|
|
|
|
2025-01-06 14:42:08 +08:00
|
|
|
|
2024-07-15 13:42:18 +08:00
|
|
|
}
|