AutopayOrderController.java 5.5 KB

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