AutopayOrderController.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package com.fdkankan.pay.controller;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.fdkankan.pay.common.ResultData;
  4. import com.fdkankan.pay.entity.AutopayOrder;
  5. import com.fdkankan.pay.entity.Order;
  6. import com.fdkankan.pay.entity.PaypalConfig;
  7. import com.fdkankan.pay.entity.PaypalWebhookLog;
  8. import com.fdkankan.pay.service.IAutopayOrderService;
  9. import com.fdkankan.pay.service.IOrderService;
  10. import com.fdkankan.pay.service.IPaypalConfigService;
  11. import com.fdkankan.pay.service.IPaypalWebhookLogService;
  12. import com.fdkankan.pay.util.CacheUtil;
  13. import com.fdkankan.pay.util.paypal.restApi.RestApiPaypalService;
  14. import com.fdkankan.rabbitmq.util.RabbitMqProducer;
  15. import lombok.extern.slf4j.Slf4j;
  16. import org.apache.commons.lang.StringUtils;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.web.bind.annotation.*;
  19. import java.util.HashMap;
  20. /**
  21. * <p>
  22. * 前端控制器
  23. * </p>
  24. *
  25. * @author
  26. * @since 2023-09-26
  27. */
  28. @RestController
  29. @RequestMapping("/service/pay/paypal")
  30. @Slf4j
  31. public class AutopayOrderController {
  32. @Autowired
  33. IAutopayOrderService autopayOrderService;
  34. @Autowired
  35. IPaypalWebhookLogService paypalWebhookLogService;
  36. @Autowired
  37. RabbitMqProducer rabbitMqProducer;
  38. @Autowired
  39. RestApiPaypalService restApiPaypalService;
  40. @Autowired
  41. IPaypalConfigService paypalConfigService;
  42. @Autowired
  43. IOrderService orderService;
  44. @PostMapping("/webhook")
  45. public ResultData webhook(@RequestBody JSONObject webhookObj){
  46. log.info("webhook:{}",webhookObj);
  47. String event_type = webhookObj.getString("event_type");
  48. PaypalWebhookLog log = paypalWebhookLogService.saveLog(event_type, webhookObj);
  49. JSONObject resource = webhookObj.getJSONObject("resource");
  50. AutopayOrder autopayOrder = null;
  51. String subscriptionId = null;
  52. String state =resource.getString("status");
  53. switch (event_type){
  54. case "PAYMENT.SALE.COMPLETED" : //每日扣款
  55. subscriptionId = resource.getString("billing_agreement_id");
  56. break;
  57. case "CATALOG.PRODUCT.CREATED" : //创建商品
  58. break;
  59. case "BILLING.PLAN.CREATED" : //创建计划
  60. break;
  61. case "BILLING.PLAN.ACTIVATED" : //计划激活
  62. break;
  63. case "BILLING.SUBSCRIPTION.CREATED" : //创建订阅
  64. break;
  65. case "BILLING.SUBSCRIPTION.CANCELLED" : //取消订阅
  66. subscriptionId = resource.getString("id"); //订阅id
  67. break;
  68. case "BILLING.SUBSCRIPTION.EXPIRED" : //订阅过期
  69. break;
  70. case "BILLING.SUBSCRIPTION.SUSPENDED" : //订阅暂停
  71. break;
  72. default:
  73. AutopayOrderController.log.info("webhook-default-event:{}",event_type);
  74. break;
  75. }
  76. autopayOrder = autopayOrderService.getBySubscriptionId(subscriptionId);
  77. if(autopayOrder == null){
  78. AutopayOrderController.log.info("webhook-error:{},订阅Id:{}","订单不存在",subscriptionId);
  79. return ResultData.ok();
  80. }
  81. String orderSn = autopayOrder.getOrderSn();
  82. //通知官网支付状态状态
  83. log.setStatus(1);
  84. paypalWebhookLogService.updateById(log);
  85. HashMap<String,String >map = new HashMap<>();
  86. map.put("orderSn",orderSn);
  87. map.put("eventType",event_type);
  88. map.put("state",state);
  89. rabbitMqProducer.sendByWorkQueue(CacheUtil.autoPaypalQueue,map);
  90. return ResultData.ok();
  91. }
  92. @GetMapping("/cancel/{orderSn}")
  93. public ResultData cancel(@PathVariable String orderSn){
  94. AutopayOrder autopayOrder = autopayOrderService.getByOrderSn(orderSn);
  95. if(autopayOrder != null){
  96. Order order = orderService.getByOrderSn(orderSn);
  97. if(order != null){
  98. PaypalConfig paypalConfig = paypalConfigService.getByServeId(order.getServeId());
  99. if(paypalConfig != null){
  100. restApiPaypalService.cancelSubscriptions(paypalConfig,autopayOrder.getSubscriptionId());
  101. }
  102. }
  103. }
  104. return ResultData.ok();
  105. }
  106. }