AutopayOrderController.java 5.4 KB

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